diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 5e1c96fe..5d61389d 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -85,6 +85,10 @@ 'avatar' => array( 'width' => 500, 'height' => 500 + ), + 'charactergroup' => array( + 'width' => 80, + 'height' => 80 ) ); @@ -96,10 +100,20 @@ * @var array */ public static $mimetypes = array( + 'images' => array( + array( + 'mimetype' => 'image/jpeg', + 'size' => 1048576 + ), + array( + 'mimetype' => 'image/png', + 'size' => 1048576 + ) + ), 'charactergroupsquests' => array( array( 'mimetype' => 'image/jpeg', - 'size' => 1048576, + 'size' => 1048576 ) ) ); diff --git a/controllers/CharactergroupsController.inc b/controllers/CharactergroupsController.inc index fcd9357c..7adf3409 100644 --- a/controllers/CharactergroupsController.inc +++ b/controllers/CharactergroupsController.inc @@ -62,7 +62,7 @@ 'group' => array('admin', 'moderator', 'user'), 'managegroup' => array('admin', 'moderator'), 'creategroup' => array('admin', 'moderator'), - 'editgroup' => array('admin', 'moderator'), + 'editgroup' => array('admin', 'moderator', 'user'), 'deletegroup' => array('admin', 'moderator') ); @@ -545,6 +545,15 @@ // Get Character group $group = $this->Charactergroups->getGroupByUrl($groupsgroup['id'], $groupUrl); + $group['characters'] = $this->Characters->getCharactersForGroup($group['id']); + + // Check permission + if(count(array_intersect(array('admin', 'moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) == 0 && !in_array(\hhu\z\controllers\SeminaryController::$character['id'], array_map(function($c) { return $c['id']; }, $group['characters']))) { + throw new \nre\exceptions\AccessDeniedException(); + } + + // Get allowed mimetypes + $mimetypes = \nre\configs\AppConfig::$mimetypes['images']; // Values $charactergroupname = $group['name']; @@ -557,12 +566,57 @@ { // Get params and validate them $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); - $charactergroupname = $this->request->getPostParam('charactergroupname'); + $charactergroupname = (count(array_intersect(array('admin','moderator'), \hhu\z\controllers\SeminaryController::$character['characterroles'])) > 0) ? $this->request->getPostParam('charactergroupname') : $group['name']; if($this->Charactergroups->characterGroupNameExists($charactergroupname, $group['id'])) { $validation = $this->Validation->addValidationResult($validation, 'charactergroupname', 'exist', true); } $motto = $this->request->getPostParam('motto'); + // Upload icon + if(!empty($_FILES) && array_key_exists('icon', $_FILES)) + { + $file = $_FILES['icon']; + + // Check error + if($file['error'] !== 0 || empty($file['tmp_name'])) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'error', $file['error']); + } + + // Check mimetype + $mediaMimetype = null; + $file['mimetype'] = \hhu\z\Utils::getMimetype($file['tmp_name'], $file['type']); + foreach($mimetypes as &$mimetype) { + if($mimetype['mimetype'] == $file['mimetype']) { + $mediaMimetype = $mimetype; + break; + } + } + if(is_null($mediaMimetype)) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'mimetype', $file['mimetype']); + } + elseif($file['size'] > $mediaMimetype['size']) { + $validation = $this->Validation->addValidationResult($validation, 'icon', 'size', $mediaMimetype['size']); + } + + // Create filename + $filename = sprintf('charactergroup-%s', $group['url']); + + // Upload icon + if($validation === true || empty($valiadion)) { + $mediaId = $this->Media->createCharactergroupMedia( + $this->Auth->getUserId(), + $seminary['id'], + $filename, + '', + $file['mimetype'], + $file['tmp_name'] + ); + if($mediaId !== false) { + $this->Charactergroups->setMediaForGroup($group['id'], $mediaId); + } + } + } + // Edit group if($validation === true) { @@ -593,6 +647,7 @@ // Pass data to view $this->set('seminary', $seminary); $this->set('groupsgroup', $groupsgroup); + $this->set('group', $group); $this->set('charactergroupname', $charactergroupname); $this->set('motto', $motto); $this->set('validation', $validation); diff --git a/controllers/MediaController.inc b/controllers/MediaController.inc index ab297f75..834da87a 100644 --- a/controllers/MediaController.inc +++ b/controllers/MediaController.inc @@ -45,7 +45,7 @@ * * @var array */ - public $models = array('seminaries', 'achievements', 'media', 'avatars'); + public $models = array('seminaries', 'achievements', 'media', 'avatars', 'charactergroups'); @@ -276,6 +276,48 @@ } + /** + * Action: charactergroup + * + * Display the icon for a Character group 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 $groupUrl URL-Title of a Character group + */ + public function charactergroup($seminaryUrl, $groupsgroupUrl, $groupUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character groups-group + $groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl); + + // Get Character group + $group = $this->Charactergroups->getGroupByUrl($groupsgroup['id'], $groupUrl); + + // Check media + if(is_null($group['charactergroupsmedia_id'])) { + $this->redirect($this->linker->link(array('grafics','charactergroup.jpg'))); + } + + // Get media + $media = $this->Media->getSeminaryMediaById($group['charactergroupsmedia_id']); + + // Get file + $file = $this->getMediaFile($media, 'charactergroup'); + if(is_null($file)) { + return; + } + + + // Pass data to view + $this->set('media', $media); + $this->set('file', $file); + } + + /** @@ -375,6 +417,14 @@ \nre\configs\AppConfig::$media['avatar']['height'] ); break; + case 'charactergroup': + $file = self::resizeImage( + $media['filename'], + $format, + \nre\configs\AppConfig::$media['charactergroup']['width'], + \nre\configs\AppConfig::$media['charactergroup']['height'] + ); + break; default: throw new ParamsNotValidException($action); break; diff --git a/models/CharactergroupsModel.inc b/models/CharactergroupsModel.inc index fd424d47..41708502 100644 --- a/models/CharactergroupsModel.inc +++ b/models/CharactergroupsModel.inc @@ -211,7 +211,7 @@ // Get and return Character groups return $this->db->query( - 'SELECT id, name, url, xps, motto, seminaryupload_id '. + 'SELECT id, name, url, xps, motto, charactergroupsmedia_id '. 'FROM v_charactergroups '. 'WHERE charactergroupsgroup_id = ? '. "ORDER BY $sortorder", @@ -230,7 +230,7 @@ public function getGroupsForCharacter($characterId) { return $this->db->query( - 'SELECT charactergroups.id, charactergroups.charactergroupsgroup_id, charactergroups.name, charactergroups.url, charactergroups.seminaryupload_id, charactergroups.xps, charactergroupsgroups.id AS charactergroupsgroup_id, charactergroupsgroups.name AS charactergroupsgroup_name, charactergroupsgroups.url AS charactergroupsgroup_url '. + 'SELECT charactergroups.id, charactergroups.charactergroupsgroup_id, charactergroups.name, charactergroups.url, charactergroups.charactergroupsmedia_id, charactergroups.xps, charactergroupsgroups.id AS charactergroupsgroup_id, charactergroupsgroups.name AS charactergroupsgroup_name, charactergroupsgroups.url AS charactergroupsgroup_url '. 'FROM characters_charactergroups '. 'LEFT JOIN v_charactergroups AS charactergroups ON charactergroups.id = characters_charactergroups.charactergroup_id '. 'LEFT JOIN charactergroupsgroups ON charactergroupsgroups.id = charactergroups.charactergroupsgroup_id '. @@ -252,7 +252,7 @@ public function getGroupByUrl($groupsgroupId, $groupUrl) { $data = $this->db->query( - 'SELECT id, name, url, xps, motto, seminaryupload_id '. + 'SELECT id, name, url, xps, motto, charactergroupsmedia_id '. 'FROM v_charactergroups '. 'WHERE charactergroupsgroup_id = ? AND url = ?', 'is', @@ -277,7 +277,7 @@ public function getGroupById($groupId) { $data = $this->db->query( - 'SELECT id, name, url, xps, motto, seminaryupload_id '. + 'SELECT id, name, url, xps, motto, charactergroupsmedia_id '. 'FROM v_charactergroups '. 'WHERE id = ?', 'i', @@ -371,6 +371,25 @@ } + /** + * Set the media for a Character group. + * + * @param int $groupId ID of Character group to set media for + * @param int $mediaId ID of Character groups media + */ + public function setMediaForGroup($groupId, $mediaId) + { + $this->db->query( + 'UPDATE charactergroups '. + 'SET charactergroupsmedia_id = ? '. + 'WHERE id = ?', + 'ii', + $mediaId, + $groupId + ); + } + + /** * Edit a Character group. * diff --git a/models/MediaModel.inc b/models/MediaModel.inc index 031df753..e9cce0db 100644 --- a/models/MediaModel.inc +++ b/models/MediaModel.inc @@ -190,6 +190,88 @@ 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 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 + $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.'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; + } + } ?> diff --git a/views/binary/media/charactergroup.tpl b/views/binary/media/charactergroup.tpl new file mode 100644 index 00000000..0d6fb0df --- /dev/null +++ b/views/binary/media/charactergroup.tpl @@ -0,0 +1 @@ + diff --git a/views/html/charactergroups/editgroup.tpl b/views/html/charactergroups/editgroup.tpl index fd0bd6e5..4be4bb24 100644 --- a/views/html/charactergroups/editgroup.tpl +++ b/views/html/charactergroups/editgroup.tpl @@ -49,10 +49,17 @@ -
+ + +
+ 0) : ?> />
+ + />
+ + />
diff --git a/views/html/charactergroups/group.tpl b/views/html/charactergroups/group.tpl index 4d59cc19..2bf19656 100644 --- a/views/html/charactergroups/group.tpl +++ b/views/html/charactergroups/group.tpl @@ -9,20 +9,20 @@
  • - 0) : ?> + 0 || in_array(\hhu\z\controllers\SeminaryController::$character['id'], array_map(function($c) { return $c['id']; }, $group['characters']))) : ?>
    - - - - - +

    ""

    diff --git a/views/html/charactergroups/managegroup.tpl b/views/html/charactergroups/managegroup.tpl index 37432cc9..c62fa8c7 100644 --- a/views/html/charactergroups/managegroup.tpl +++ b/views/html/charactergroups/managegroup.tpl @@ -10,11 +10,7 @@
    - - - - - +

    ""

    diff --git a/views/html/characters/character.tpl b/views/html/characters/character.tpl index 7f2f37ac..ec68abd1 100644 --- a/views/html/characters/character.tpl +++ b/views/html/characters/character.tpl @@ -97,11 +97,7 @@
  • - - - - - +