implement media upload for Character groups Quests (Issue #193)
This commit is contained in:
parent
b759506229
commit
616b31660d
6 changed files with 314 additions and 18 deletions
|
|
@ -89,6 +89,22 @@
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allowed mimetypes with sizes
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $mimetypes = array(
|
||||||
|
'charactergroupsquests' => array(
|
||||||
|
array(
|
||||||
|
'mimetype' => 'image/jpeg',
|
||||||
|
'size' => 1048576,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Miscellaneous settings
|
* Miscellaneous settings
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $models = array('seminaries', 'charactergroups', 'charactergroupsquests', 'media', 'questgroups');
|
public $models = array('seminaries', 'charactergroups', 'charactergroupsquests', 'media', 'questgroups', 'uploads');
|
||||||
/**
|
/**
|
||||||
* Required components
|
* Required components
|
||||||
*
|
*
|
||||||
|
|
@ -87,6 +87,12 @@
|
||||||
$questmedia = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
$questmedia = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get uploads
|
||||||
|
$uploads = $this->Charactergroupsquests->getMediaForQuest($quest['id']);
|
||||||
|
foreach($uploads as &$upload) {
|
||||||
|
$upload['upload'] = $this->Uploads->getSeminaryuploadById($upload['seminaryupload_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('seminary', $seminary);
|
$this->set('seminary', $seminary);
|
||||||
|
|
@ -95,6 +101,7 @@
|
||||||
$this->set('questgroup', $questgroup);
|
$this->set('questgroup', $questgroup);
|
||||||
$this->set('groups', $groups);
|
$this->set('groups', $groups);
|
||||||
$this->set('media', $questmedia);
|
$this->set('media', $questmedia);
|
||||||
|
$this->set('uploads', $uploads);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -127,45 +134,109 @@
|
||||||
// Get Character groups
|
// Get Character groups
|
||||||
$groups = $this->Charactergroups->getGroupsForGroupsgroup($groupsgroup['id']);
|
$groups = $this->Charactergroups->getGroupsForGroupsgroup($groupsgroup['id']);
|
||||||
|
|
||||||
// Set XPs of Character groups for this Character groups Quest
|
// Get allowed mimetypes
|
||||||
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('setxps')))
|
$mimetypes = \nre\configs\AppConfig::$mimetypes['charactergroupsquests'];
|
||||||
|
|
||||||
|
// Manage
|
||||||
|
$validation = array();
|
||||||
|
if($this->request->getRequestMethod() == 'POST')
|
||||||
{
|
{
|
||||||
$xps = $this->request->getPostParam('xps');
|
// Upload media
|
||||||
foreach($groups as &$group)
|
if(!is_null($this->request->getPostParam('setmedia')))
|
||||||
{
|
{
|
||||||
if(array_key_exists($group['url'], $xps) && $xps[$group['url']] != 'null')
|
$file = $_FILES['media'];
|
||||||
{
|
|
||||||
$xpsFactor = intval($xps[$group['url']]) / $quest['xps'];
|
// Check error
|
||||||
$this->Charactergroupsquests->setXPsOfGroupForQuest($quest['id'], $group['id'], $xpsFactor);
|
if($file['error'] !== 0 || empty($file['tmp_name'])) {
|
||||||
|
$validation = $this->Validation->addValidationResult($validation, 'media', 'error', $file['error']);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$this->Charactergroupsquests->deleteGroupForQuest($quest['id'], $group['id']);
|
// Check mimetype
|
||||||
|
$mediaMimetype = null;
|
||||||
|
foreach($mimetypes as &$mimetype) {
|
||||||
|
if($mimetype['mimetype'] == $file['type']) {
|
||||||
|
$mediaMimetype = $mimetype;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(is_null($mediaMimetype)) {
|
||||||
|
$validation = $this->Validation->addValidationResult($validation, 'media', 'mimetype', $file['type']);
|
||||||
|
}
|
||||||
|
elseif($file['size'] > $mediaMimetype['size']) {
|
||||||
|
$validation = $this->Validation->addValidationResult($validation, 'media', 'size', $mediaMimetype['size']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload media
|
||||||
|
if($validation === true)
|
||||||
|
{
|
||||||
|
// Create filename
|
||||||
|
$filename = sprintf(
|
||||||
|
'%s-%d-%s.%s',
|
||||||
|
'charactergroupsquest',
|
||||||
|
$quest['id'],
|
||||||
|
date('Ymd-His'),
|
||||||
|
mb_substr($file['name'], strrpos($file['name'], '.')+1)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Upload file
|
||||||
|
$this->Charactergroupsquests->uploadMediaForQuest($this->Auth->getUserId(), $seminary['id'], $quest['id'], $file, $filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set XPs of Character groups for this Character groups Quest
|
||||||
|
if(!is_null($this->request->getPostParam('setxps')))
|
||||||
|
{
|
||||||
|
$xps = $this->request->getPostParam('xps');
|
||||||
|
foreach($groups as &$group)
|
||||||
|
{
|
||||||
|
if(array_key_exists($group['url'], $xps) && $xps[$group['url']] != 'null')
|
||||||
|
{
|
||||||
|
$xpsFactor = intval($xps[$group['url']]) / $quest['xps'];
|
||||||
|
$this->Charactergroupsquests->setXPsOfGroupForQuest($quest['id'], $group['id'], $xpsFactor);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->Charactergroupsquests->deleteGroupForQuest($quest['id'], $group['id']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to Quest page
|
// Redirect to Quest page
|
||||||
$this->redirect($this->linker->link(array('quest', $seminary['url'], $groupsgroup['url'], $quest['url']), 1));
|
if($validation === true || empty($validation)) {
|
||||||
|
$this->redirect($this->linker->link(array('quest', $seminary['url'], $groupsgroup['url'], $quest['url']), 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get icon
|
||||||
|
$questmedia = null;
|
||||||
|
if(!is_null($quest['questsmedia_id'])) {
|
||||||
|
$questmedia = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get uploads
|
||||||
|
$uploads = $this->Charactergroupsquests->getMediaForQuest($quest['id']);
|
||||||
|
foreach($uploads as &$upload) {
|
||||||
|
$upload['upload'] = $this->Uploads->getSeminaryuploadById($upload['seminaryupload_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Set XPs for Groups
|
// Set XPs for Groups
|
||||||
foreach($groups as &$group) {
|
foreach($groups as &$group) {
|
||||||
$group['quest_group'] = $this->Charactergroupsquests->getXPsOfGroupForQuest($quest['id'], $group['id']);
|
$group['quest_group'] = $this->Charactergroupsquests->getXPsOfGroupForQuest($quest['id'], $group['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Media
|
|
||||||
$questmedia = null;
|
|
||||||
if(!is_null($quest['questsmedia_id'])) {
|
|
||||||
$questmedia = $this->Media->getSeminaryMediaById($quest['questsmedia_id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('seminary', $seminary);
|
$this->set('seminary', $seminary);
|
||||||
$this->set('groupsgroup', $groupsgroup);
|
$this->set('groupsgroup', $groupsgroup);
|
||||||
$this->set('quest', $quest);
|
$this->set('quest', $quest);
|
||||||
|
$this->set('uploads', $uploads);
|
||||||
|
$this->set('mimetypes', $mimetypes);
|
||||||
$this->set('questgroup', $questgroup);
|
$this->set('questgroup', $questgroup);
|
||||||
$this->set('groups', $groups);
|
$this->set('groups', $groups);
|
||||||
$this->set('media', $questmedia);
|
$this->set('media', $questmedia);
|
||||||
|
$this->set('validation', $validation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@
|
||||||
* @param string $seminaryUrl URL-title of Seminary
|
* @param string $seminaryUrl URL-title of Seminary
|
||||||
* @param string $uploadUrl URL-name of the upload
|
* @param string $uploadUrl URL-name of the upload
|
||||||
*/
|
*/
|
||||||
public function seminary($seminaryUrl, $uploadUrl)
|
public function seminary($seminaryUrl, $uploadUrl, $action=null)
|
||||||
{
|
{
|
||||||
// Get Seminary
|
// Get Seminary
|
||||||
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
|
||||||
|
|
@ -105,7 +105,18 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file
|
// Get file
|
||||||
$file = $this->getUploadFile($upload);
|
switch($action)
|
||||||
|
{
|
||||||
|
case null:
|
||||||
|
$file = $this->getUploadFile($upload);
|
||||||
|
break;
|
||||||
|
case 'thumbnail':
|
||||||
|
$file = $this->createThumbnail($upload);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException($action);
|
||||||
|
break;
|
||||||
|
}
|
||||||
if(is_null($file)) {
|
if(is_null($file)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -184,6 +195,82 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a thumbnail from an upload.
|
||||||
|
*
|
||||||
|
* @param array $upload Upload to create thumbnail for
|
||||||
|
* @return object Thumbnail for the upload (or null if thumbnail is cached)
|
||||||
|
*/
|
||||||
|
private function createThumbnail($upload)
|
||||||
|
{
|
||||||
|
// Set filename
|
||||||
|
$upload['filename'] = ROOT.DS.\nre\configs\AppConfig::$dirs['seminaryuploads'].DS.$upload['url'];
|
||||||
|
|
||||||
|
// Set content-type
|
||||||
|
$this->response->addHeader("Content-type: image/jpeg");
|
||||||
|
|
||||||
|
// Cache
|
||||||
|
if($this->setCacheHeaders($upload['filename'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set geometry
|
||||||
|
$width = 100;
|
||||||
|
$height = 100;
|
||||||
|
|
||||||
|
switch($upload['mimetype'])
|
||||||
|
{
|
||||||
|
case 'image/jpeg':
|
||||||
|
case 'image/png':
|
||||||
|
// Read image from cache
|
||||||
|
$tempFileName = ROOT.DS.\nre\configs\AppConfig::$dirs['temporary'].DS.$upload['url'].'-'.$width.'x'.$height;
|
||||||
|
if(file_exists($tempFileName))
|
||||||
|
{
|
||||||
|
// Check age of file
|
||||||
|
if(date('r', filemtime($tempFileName)+(60*60*24)) > date('r', time())) {
|
||||||
|
// Too old, delete
|
||||||
|
unlink($tempFileName);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Valid, read and return
|
||||||
|
return file_get_contents($tempFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ImageMagick
|
||||||
|
$im = new \Imagick($upload['filename']);
|
||||||
|
|
||||||
|
// Calculate new size
|
||||||
|
$geometry = $im->getImageGeometry();
|
||||||
|
if($geometry['width'] < $width) {
|
||||||
|
$width = $geometry['width'];
|
||||||
|
}
|
||||||
|
if($geometry['height'] < $height) {
|
||||||
|
$height = $geometry['width'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process
|
||||||
|
$im->thumbnailImage($width, $height, true);
|
||||||
|
$im->contrastImage(1);
|
||||||
|
$im->setImageFormat('jpeg');
|
||||||
|
|
||||||
|
// Save temporary file
|
||||||
|
$im->writeImage($tempFileName);
|
||||||
|
|
||||||
|
|
||||||
|
// Return resized image
|
||||||
|
return $im;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException('thumbnail');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $this->getUploadFile($upload);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine file information and set the HTTP-header for
|
* Determine file information and set the HTTP-header for
|
||||||
* caching accordingly.
|
* caching accordingly.
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,12 @@
|
||||||
*/
|
*/
|
||||||
class CharactergroupsquestsModel extends \hhu\z\Model
|
class CharactergroupsquestsModel extends \hhu\z\Model
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Required models
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $models = array('uploads');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -224,6 +230,57 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload a media for a Character groups Quest.
|
||||||
|
*
|
||||||
|
* @param int $userId ID of user that does the upload
|
||||||
|
* @param int $seminaryId ID of Seminary
|
||||||
|
* @param int $questId ID of Quest to upload media for
|
||||||
|
* @param array $file File-array of file to upload
|
||||||
|
* @param string $filename Filename for media
|
||||||
|
* @return boolean Whether upload succeeded or not
|
||||||
|
*/
|
||||||
|
public function uploadMediaForQuest($userId, $seminaryId, $questId, $file, $filename)
|
||||||
|
{
|
||||||
|
// Save file on harddrive
|
||||||
|
$uploadId = $this->Uploads->uploadSeminaryFile($userId, $seminaryId, $file['name'], $filename, $file['tmp_name'], $file['type']);
|
||||||
|
if($uploadId === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create database record
|
||||||
|
$this->db->query(
|
||||||
|
'INSERT INTO charactergroupsquests_seminaryuploads '.
|
||||||
|
'(seminaryupload_id, charactergroupsquest_id, created_user_id) '.
|
||||||
|
'VALUES '.
|
||||||
|
'(?, ?, ?) ',
|
||||||
|
'iii',
|
||||||
|
$uploadId, $questId, $uploadId
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get uploaded Medai for a Character groups Quest.
|
||||||
|
*
|
||||||
|
* @param int $questId ID of Quest to get media for
|
||||||
|
* @return array Seminary uploads
|
||||||
|
*/
|
||||||
|
public function getMediaForQuest($questId)
|
||||||
|
{
|
||||||
|
return $this->db->query(
|
||||||
|
'SELECT seminaryupload_id, created, created_user_id '.
|
||||||
|
'FROM charactergroupsquests_seminaryuploads '.
|
||||||
|
'WHERE charactergroupsquest_id = ?',
|
||||||
|
'i',
|
||||||
|
$questId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Character groups Quest.
|
* Create a new Character groups Quest.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,56 @@
|
||||||
<li><?php if($questgroup['entered']) : ?><a href="<?=$linker->link(array('questgroups','questgroup',$seminary['url'],$questgroup['url']))?>"><?=$questgroup['title']?></a><?php else : ?><?=$questgroup['title']?><?php endif ?></li>
|
<li><?php if($questgroup['entered']) : ?><a href="<?=$linker->link(array('questgroups','questgroup',$seminary['url'],$questgroup['url']))?>"><?=$questgroup['title']?></a><?php else : ?><?=$questgroup['title']?><?php endif ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<h1><?=_('Media')?></h1>
|
||||||
|
<?php if($validation !== true) : ?>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($validation as $field => &$settings) : ?>
|
||||||
|
<li>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($settings as $setting => $value) : ?>
|
||||||
|
<li>
|
||||||
|
<?php switch($field) {
|
||||||
|
case 'media':
|
||||||
|
switch($setting) {
|
||||||
|
case 'error': printf(_('Error during file upload: %s'), $value);
|
||||||
|
break;
|
||||||
|
case 'mimetype': printf(_('File has wrong type “%s”'), $value);
|
||||||
|
break;
|
||||||
|
case 'size': echo _('File exceeds size maximum');
|
||||||
|
break;
|
||||||
|
default: echo _('File invalid');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} ?>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
<?php endif ?>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($uploads as &$upload) : ?>
|
||||||
|
<li>
|
||||||
|
<a href="<?=$linker->link(array('uploads','seminary',$seminary['url'],$upload['upload']['url']))?>">
|
||||||
|
<img src="<?=$linker->link(array('uploads','seminary',$seminary['url'],$upload['upload']['url'],'thumbnail'))?>" />
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
<p><?=_('Allowed file types')?>:</p>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($mimetypes as &$mimetype) : ?>
|
||||||
|
<li><?=sprintf(_('%s-files'), strtoupper(explode('/',$mimetype['mimetype'])[1]))?> <?php if($mimetype['size'] > 0) : ?>(<?=_('max.')?> <?=round($mimetype['size']/(1024*1024),2)?> MiB)<?php endif ?></li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="media" /><br />
|
||||||
|
<input type="submit" name="setmedia" value="<?=_('save')?>" />
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h1><i class="fa fa-users fa-fw"></i><?=$groupsgroup['name']?></h1>
|
<h1><i class="fa fa-users fa-fw"></i><?=$groupsgroup['name']?></h1>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,21 @@
|
||||||
<li><?php if($questgroup['entered']) : ?><a href="<?=$linker->link(array('questgroups','questgroup',$seminary['url'],$questgroup['url']))?>"><?=$questgroup['title']?></a><?php else : ?><?=$questgroup['title']?><?php endif ?></li>
|
<li><?php if($questgroup['entered']) : ?><a href="<?=$linker->link(array('questgroups','questgroup',$seminary['url'],$questgroup['url']))?>"><?=$questgroup['title']?></a><?php else : ?><?=$questgroup['title']?><?php endif ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<?php if(count($uploads) > 0) : ?>
|
||||||
|
<section>
|
||||||
|
<h1><?=_('Media')?></h1>
|
||||||
|
<ul>
|
||||||
|
<?php foreach($uploads as &$upload) : ?>
|
||||||
|
<li>
|
||||||
|
<a href="<?=$linker->link(array('uploads','seminary',$seminary['url'],$upload['upload']['url']))?>">
|
||||||
|
<img src="<?=$linker->link(array('uploads','seminary',$seminary['url'],$upload['upload']['url'],'thumbnail'))?>" />
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
<?php endif ?>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<h1><i class="fa fa-envelope fa-fw"></i><?=_('Description')?></h1>
|
<h1><i class="fa fa-envelope fa-fw"></i><?=_('Description')?></h1>
|
||||||
<p><?=\hhu\z\Utils::t($quest['description'])?></p>
|
<p><?=\hhu\z\Utils::t($quest['description'])?></p>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue