diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 5d61389d..e06e0ec5 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -89,6 +89,10 @@ 'charactergroup' => array( 'width' => 80, 'height' => 80 + ), + 'charactergroupsquest' => array( + 'width' => 80, + 'height' => 80 ) ); @@ -100,14 +104,14 @@ * @var array */ public static $mimetypes = array( - 'images' => array( + 'icons' => array( array( 'mimetype' => 'image/jpeg', - 'size' => 1048576 + 'size' => 102400 ), array( 'mimetype' => 'image/png', - 'size' => 1048576 + 'size' => 204800 ) ), 'charactergroupsquests' => array( diff --git a/controllers/CharactergroupsController.inc b/controllers/CharactergroupsController.inc index d542cfd1..77c08856 100644 --- a/controllers/CharactergroupsController.inc +++ b/controllers/CharactergroupsController.inc @@ -470,7 +470,7 @@ $groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl); // Get allowed mimetypes - $mimetypes = \nre\configs\AppConfig::$mimetypes['images']; + $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; // Values $charactergroupname = ''; @@ -489,7 +489,7 @@ } $motto = $this->request->getPostParam('motto'); - // Upload icon + // Validate icon $icon = null; if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE) { @@ -601,7 +601,7 @@ } // Get allowed mimetypes - $mimetypes = \nre\configs\AppConfig::$mimetypes['images']; + $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; // Values $charactergroupname = $group['name']; @@ -620,7 +620,7 @@ } $motto = $this->request->getPostParam('motto'); - // Upload icon + // Validate icon $icon = null; if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE) { diff --git a/controllers/CharactergroupsquestsController.inc b/controllers/CharactergroupsquestsController.inc index a68b15f9..df13c244 100644 --- a/controllers/CharactergroupsquestsController.inc +++ b/controllers/CharactergroupsquestsController.inc @@ -81,12 +81,6 @@ // Get Character groups-groups $groups = $this->Charactergroups->getGroupsForQuest($quest['id']); - // Media - $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) { @@ -105,7 +99,6 @@ $this->set('quest', $quest); $this->set('questgroup', $questgroup); $this->set('groups', $groups); - $this->set('media', $questmedia); $this->set('uploads', $uploads); } @@ -269,6 +262,9 @@ // Get Questgroups $questgroups = $this->Questgroups->getQuestgroupsForSeminary($seminary['id']); + // Get allowed mimetypes + $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + // Values $title = ''; $xps = 0; @@ -294,6 +290,34 @@ $wonText = $this->request->getPostParam('wonText'); $lostText = $this->request->getPostParam('lostText'); + // Validate icon + $icon = null; + if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE) + { + $icon = $_FILES['icon']; + + // Check error + if($icon['error'] !== UPLOAD_ERR_OK) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'error', $icon['error']); + } + + // Check mimetype + $mediaMimetype = null; + $icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']); + foreach($mimetypes as &$mimetype) { + if($mimetype['mimetype'] == $icon['mimetype']) { + $mediaMimetype = $mimetype; + break; + } + } + if(is_null($mediaMimetype)) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'mimetype', $icon['mimetype']); + } + elseif($icon['size'] > $mediaMimetype['size']) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'size', $mediaMimetype['size']); + } + } + // Validate Questgroup $questgroupIndex = null; foreach($questgroups as $index => &$questgroup) @@ -321,9 +345,25 @@ $wonText, $lostText ); + $quest = $this->Charactergroupsquests->getQuestById($questId); + + // Upload icon + if(!is_null($icon)) + { + $mediaId = $this->Media->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + sprintf('charactergroupsquest-%s', $quest['url']), + '', + $icon['mimetype'], + $icon['tmp_name'] + ); + if($mediaId !== false) { + $this->Charactergroupsquests->setMediaForQuest($quest['id'], $mediaId); + } + } // Redirect to Quest page - $quest = $this->Charactergroupsquests->getQuestById($questId); $this->redirect($this->linker->link(array('quest', $seminary['url'], $groupsgroup['url'], $quest['url']), 1)); } } @@ -350,6 +390,7 @@ $this->set('rules', $rules); $this->set('wonText', $wonText); $this->set('lostText', $lostText); + $this->set('mimetypes', $mimetypes); $this->set('validation', $validation); $this->set('validationSettings', $validationSettings); } @@ -383,6 +424,9 @@ $questgroup['selected'] = ($questgroup['id'] == $quest['questgroups_id']); } + // Get allowed mimetypes + $mimetypes = \nre\configs\AppConfig::$mimetypes['icons']; + // Values $title = $quest['title']; $xps = $quest['xps']; @@ -408,6 +452,34 @@ $wonText = $this->request->getPostParam('wonText'); $lostText = $this->request->getPostParam('lostText'); + // Validate icon + $icon = null; + if(!empty($_FILES) && array_key_exists('icon', $_FILES) && $_FILES['icon']['error'] != UPLOAD_ERR_NO_FILE) + { + $icon = $_FILES['icon']; + + // Check error + if($icon['error'] !== UPLOAD_ERR_OK) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'error', $icon['error']); + } + + // Check mimetype + $mediaMimetype = null; + $icon['mimetype'] = \hhu\z\Utils::getMimetype($icon['tmp_name'], $icon['type']); + foreach($mimetypes as &$mimetype) { + if($mimetype['mimetype'] == $icon['mimetype']) { + $mediaMimetype = $mimetype; + break; + } + } + if(is_null($mediaMimetype)) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'mimetype', $icon['mimetype']); + } + elseif($icon['size'] > $mediaMimetype['size']) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'size', $mediaMimetype['size']); + } + } + // Validate Questgroup $questgroupIndex = null; foreach($questgroups as $index => &$questgroup) @@ -435,9 +507,25 @@ $wonText, $lostText ); + $quest = $this->Charactergroupsquests->getQuestById($quest['id']); + + // Upload icon + if(!is_null($icon)) + { + $mediaId = $this->Media->createQuestMedia( + $this->Auth->getUserId(), + $seminary['id'], + sprintf('charactergroupsquest-%s', $quest['url']), + '', + $icon['mimetype'], + $icon['tmp_name'] + ); + if($mediaId !== false) { + $this->Charactergroupsquests->setMediaForQuest($quest['id'], $mediaId); + } + } // Redirect to Quest page - $quest = $this->Charactergroupsquests->getQuestById($quest['id']); $this->redirect($this->linker->link(array('quest', $seminary['url'], $groupsgroup['url'], $quest['url']), 1)); } } @@ -465,6 +553,7 @@ $this->set('rules', $rules); $this->set('wonText', $wonText); $this->set('lostText', $lostText); + $this->set('mimetypes', $mimetypes); $this->set('validation', $validation); $this->set('validationSettings', $validationSettings); } diff --git a/controllers/MediaController.inc b/controllers/MediaController.inc index 834da87a..c7080eca 100644 --- a/controllers/MediaController.inc +++ b/controllers/MediaController.inc @@ -29,7 +29,9 @@ 'seminarymoodpic' => array('admin', 'moderator', 'user'), 'seminary' => array('admin', 'moderator', 'user'), 'avatar' => array('admin', 'moderator', 'user'), - 'achievement' => array('admin', 'moderator', 'user') + 'achievement' => array('admin', 'moderator', 'user'), + 'charactergroup' => array('admin', 'moderator', 'user'), + 'charactergroupsquest' => array('admin', 'moderator', 'user') ); /** * User seminary permissions @@ -37,15 +39,17 @@ * @var array */ public $seminaryPermissions = array( - 'seminary' => array('admin', 'moderator', 'user', 'guest'), - 'achievement' => array('admin', 'moderator', 'user', 'guest') + 'seminary' => array('admin', 'moderator', 'user', 'guest'), + 'achievement' => array('admin', 'moderator', 'user', 'guest'), + 'charactergroup' => array('admin', 'moderator', 'user', 'guest'), + 'charactergroupsquest' => array('admin', 'moderator', 'user', 'guest') ); /** * Required models * * @var array */ - public $models = array('seminaries', 'achievements', 'media', 'avatars', 'charactergroups'); + public $models = array('seminaries', 'achievements', 'media', 'avatars', 'charactergroups', 'charactergroupsquests'); @@ -318,6 +322,48 @@ } + /** + * Action: charactergroupsquest + * + * Display the icon for a Character groups Quest of a Seminary. + * + * @throws IdNotFoundException + * @param string $seminaryUrl URL-Title of a Seminary + * @param string $groupsgroupUrl URL-Title of a Character groups-group + * @param string $questUrl URL-Title of a Character groups Quest + */ + public function charactergroupsquest($seminaryUrl, $groupsgroupUrl, $questUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character groups-group + $groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl); + + // Get Character groups Quests + $quest = $this->Charactergroupsquests->getQuestByUrl($groupsgroup['id'], $questUrl); + + // Check media + if(is_null($quest['questsmedia_id'])) { + $this->redirect($this->linker->link(array('grafics','charactergroup.jpg'))); + } + + // Get media + $media = $this->Media->getSeminaryMediaById($quest['questsmedia_id']); + + // Get file + $file = $this->getMediaFile($media, 'charactergroupsquest'); + if(is_null($file)) { + return; + } + + + // Pass data to view + $this->set('media', $media); + $this->set('file', $file); + } + + /** @@ -425,6 +471,14 @@ \nre\configs\AppConfig::$media['charactergroup']['height'] ); break; + case 'charactergroupsquest': + $file = self::resizeImage( + $media['filename'], + $format, + \nre\configs\AppConfig::$media['charactergroupsquest']['width'], + \nre\configs\AppConfig::$media['charactergroupsquest']['height'] + ); + break; default: throw new ParamsNotValidException($action); break; diff --git a/models/CharactergroupsquestsModel.inc b/models/CharactergroupsquestsModel.inc index 5c9f1602..102254e5 100644 --- a/models/CharactergroupsquestsModel.inc +++ b/models/CharactergroupsquestsModel.inc @@ -230,6 +230,25 @@ } + /** + * Set the media for a Character groups Quest. + * + * @param int $questId ID of Quest to upload media for + * @param int $mediaId ID of Quests media + */ + public function setMediaForQuest($questId, $mediaId) + { + $this->db->query( + 'UPDATE charactergroupsquests '. + 'SET questsmedia_id = ? '. + 'WHERE id = ?', + 'ii', + $mediaId, + $questId + ); + } + + /** * Upload a media for a Character groups Quest. * diff --git a/models/MediaModel.inc b/models/MediaModel.inc index e9cce0db..08823bab 100644 --- a/models/MediaModel.inc +++ b/models/MediaModel.inc @@ -136,114 +136,30 @@ /** - * Create a new Questsmedia by creating a new Seminarymedia and - * adding it to the list of Questsmedia. - * TODO Currently only temporary for easier data import. + * Create a new Quests media by creating a new Seminarymedia and + * adding it to the list of Quests media. + * + * @param int $userId ID of user that does the upload + * @param int $seminaryId ID of Seminary + * @param string $filename Filename of uploading media + * @param string $description Description for media + * @param string $mimetype Mimetype of media + * @param string $tmpFilename Name of temporary uploaded file + * @return mixed ID of media record or false if upload failed */ public function createQuestMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename) - { - $uploadId = false; - $this->db->setAutocommit(false); - - try { - // Create database record - $this->db->query( - 'INSERT INTO seminarymedia '. - '(created_user_id, seminary_id, name, url, description, mimetype) '. - 'VALUES '. - '(?, ? ,? ,?, ?, ?)', - 'iissss', - $userId, - $seminaryId, - $filename, - \nre\core\Linker::createLinkParam($filename), - $description, - $mimetype - ); - $uploadId = $this->db->getInsertId(); - - $this->db->query( - 'INSERT INTO questsmedia '. - '(media_id, created_user_id) '. - 'VALUES '. - '(?, ?)', - 'ii', - $uploadId, - $userId - ); - - // Create filename - $filename = ROOT.DS.'seminarymedia'.DS.$uploadId; - if(!move_uploaded_file($tmpFilename, $filename)) - { - $this->db->rollback(); - $uploadId = false; - } - } - catch(\nre\exceptions\DatamodelException $e) { - $this->db->rollback(); - $this->db->setAutocommit(true); - } - - - $this->db->setAutocommit(true); - return $uploadId; - } - - - /** - * Create a new Character groups media by creating a new Seminarymedia and - * adding it to the list of media for Character groups. - * - * @param int $userId - * @param int $seminaryId - * @param string $filename - * @param string $description - * @param string $mimetype - * @param string $tmpFilename - * @return - */ - public function createCharactergroupMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename) { $mediaId = false; $this->db->setAutocommit(false); try { - // Check for existing database record - $data = $this->db->query( - 'SELECT id '. - 'FROM seminarymedia '. - 'WHERE url = ?', - 's', - \nre\core\Linker::createLinkParam($filename) - ); - if(!empty($data)) { - $mediaId = $data[0]['id']; - } + // Create Seminary media record + $mediaId = $this->createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype); - // Create database record - if($mediaId === false) - { - $this->db->query( - 'INSERT INTO seminarymedia '. - '(created_user_id, seminary_id, name, url, description, mimetype) '. - 'VALUES '. - '(?, ? ,? ,?, ?, ?)', - 'iissss', - $userId, - $seminaryId, - $filename, - \nre\core\Linker::createLinkParam($filename), - $description, - $mimetype - ); - $mediaId = $this->db->getInsertId(); - } - - // Add media to Character groups media + // Add media to Quests media $this->db->query( - 'INSERT INTO charactergroupsmedia '. - '(seminarymedia_id, created_user_id) '. + 'INSERT INTO questsmedia '. + '(media_id, created_user_id) '. 'VALUES '. '(?, ?) '. 'ON DUPLICATE KEY UPDATE '. @@ -255,7 +171,7 @@ ); // Create filename - $filename = ROOT.DS.'seminarymedia'.DS.$mediaId; + $filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminarymedia'].DS.$mediaId; if(!move_uploaded_file($tmpFilename, $filename)) { $this->db->rollback(); @@ -272,6 +188,106 @@ return $mediaId; } + + /** + * Create a new Character groups media by creating a new Seminarymedia and + * adding it to the list of media for Character groups. + * + * @param int $userId ID of user that does the upload + * @param int $seminaryId ID of Seminary + * @param string $filename Filename of uploading media + * @param string $description Description for media + * @param string $mimetype Mimetype of media + * @param string $tmpFilename Name of temporary uploaded file + * @return mixed ID of media record or false if upload failed + */ + public function createCharactergroupMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename) + { + $mediaId = false; + $this->db->setAutocommit(false); + + try { + // Create Seminary media record + $mediaId = $this->createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype); + + // Add media to Character groups media + $this->db->query( + 'INSERT INTO charactergroupsmedia '. + '(seminarymedia_id, created_user_id) '. + 'VALUES '. + '(?, ?) '. + 'ON DUPLICATE KEY UPDATE '. + 'created_user_id = ?', + 'iii', + $mediaId, + $userId, + $userId + ); + + // Create filename + $filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminarymedia'].DS.$mediaId; + if(!move_uploaded_file($tmpFilename, $filename)) + { + $this->db->rollback(); + $mediaId = false; + } + } + catch(\nre\exceptions\DatamodelException $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + } + + + $this->db->setAutocommit(true); + return $mediaId; + } + + + + + /** + * Create a new Seminary media. + * + * @param int $userId ID of user that does the upload + * @param int $seminaryId ID of Seminary + * @param string $filename Filename of uploading media + * @param string $description Description for media + * @param string $mimetype Mimetype of media + * @return mixed ID of media record or false if upload failed + */ + private function createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype) + { + // Check for existing database record + $data = $this->db->query( + 'SELECT id '. + 'FROM seminarymedia '. + 'WHERE url = ?', + 's', + \nre\core\Linker::createLinkParam($filename) + ); + if(!empty($data)) { + return $data[0]['id']; + } + + // Create database record + $this->db->query( + 'INSERT INTO seminarymedia '. + '(created_user_id, seminary_id, name, url, description, mimetype) '. + 'VALUES '. + '(?, ? ,? ,?, ?, ?)', + 'iissss', + $userId, + $seminaryId, + $filename, + \nre\core\Linker::createLinkParam($filename), + $description, + $mimetype + ); + + + return $this->db->getInsertId(); + } + } ?> diff --git a/views/binary/media/charactergroupsquest.tpl b/views/binary/media/charactergroupsquest.tpl new file mode 100644 index 00000000..0d6fb0df --- /dev/null +++ b/views/binary/media/charactergroupsquest.tpl @@ -0,0 +1 @@ + diff --git a/views/html/charactergroups/creategroup.tpl b/views/html/charactergroups/creategroup.tpl index 466e107f..35b37d74 100644 --- a/views/html/charactergroups/creategroup.tpl +++ b/views/html/charactergroups/creategroup.tpl @@ -18,6 +18,17 @@ $value) : ?>
  • $value) : ?>
  • $value) : ?>
  • getMessage(); - break; } ?>
  • @@ -51,7 +59,17 @@ -
    + +
    + + +

    :

    + +
    /> diff --git a/views/html/charactergroupsquests/edit.tpl b/views/html/charactergroupsquests/edit.tpl index 0e8b43a9..4ef2f8aa 100644 --- a/views/html/charactergroupsquests/edit.tpl +++ b/views/html/charactergroupsquests/edit.tpl @@ -18,6 +18,17 @@ $value) : ?>
  • getMessage(); - break; } ?>
  • @@ -51,7 +59,18 @@ - + +
    + +
    + +

    :

    +
      + +
    • 0) : ?>(  MiB)
    • + +
    +
    /> diff --git a/views/html/charactergroupsquests/quest.tpl b/views/html/charactergroupsquests/quest.tpl index 163d5ac6..aa620e35 100644 --- a/views/html/charactergroupsquests/quest.tpl +++ b/views/html/charactergroupsquests/quest.tpl @@ -18,11 +18,7 @@
    - - - - - +