implement icons for Character groups Quests and improve media handilng for Character groups

This commit is contained in:
coderkun 2014-05-07 00:44:45 +02:00
commit 51074ab748
12 changed files with 334 additions and 96 deletions

View file

@ -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(

View file

@ -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)
{

View file

@ -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);
}

View file

@ -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;

View file

@ -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.
*

View file

@ -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();
}
}
?>

View file

@ -0,0 +1 @@
<?=$file?>

View file

@ -18,6 +18,17 @@
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php switch($field) {
case 'icon':
switch($setting) {
case 'error': printf(_('Error during icon upload: %s'), $value);
break;
case 'mimetype': printf(_('Icon has wrong type “%s”'), $value);
break;
case 'size': echo _('Icon exceeds size maximum');
break;
default: echo _('Icon invalid');
}
break;
case 'charactergroupname':
switch($setting) {
case 'minlength': printf(_('Name is too short (min. %d chars)'), $value);

View file

@ -18,6 +18,17 @@
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php switch($field) {
case 'icon':
switch($setting) {
case 'error': printf(_('Error during icon upload: %s'), $value);
break;
case 'mimetype': printf(_('Icon has wrong type “%s”'), $value);
break;
case 'size': echo _('Icon exceeds size maximum');
break;
default: echo _('Icon invalid');
}
break;
case 'charactergroupname':
switch($setting) {
case 'minlength': printf(_('Name is too short (min. %d chars)'), $value);

View file

@ -18,6 +18,17 @@
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php switch($field) {
case 'icon':
switch($setting) {
case 'error': printf(_('Error during icon upload: %s'), $value);
break;
case 'mimetype': printf(_('Icon has wrong type “%s”'), $value);
break;
case 'size': echo _('Icon exceeds size maximum');
break;
default: echo _('Icon invalid');
}
break;
case 'title':
switch($setting) {
case 'minlength': printf(_('Title is too short (min. %d chars)'), $value);
@ -40,9 +51,6 @@
default: echo _('XPs invalid');
}
break;
default:
echo $exception->getMessage();
break;
} ?>
</li>
<?php endforeach ?>
@ -51,7 +59,17 @@
<?php endforeach ?>
</ul>
<?php endif ?>
<form method="post" action="" class="logreg">
<form method="post" action="" class="logreg" enctype="multipart/form-data">
<fieldset>
<legend><?=_('Icon')?></legend>
<input type="file" name="icon" />
<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>
</fieldset>
<fieldset>
<label for="title"><?=_('Title')?>:</label>
<input type="text" id="title" name="title" placeholder="<?=_('Title')?>" title="<?=_('Title')?>" maxlength="<?=$validationSettings['title']['maxlength']?>" value="<?=$title?>" <?=(array_key_exists('title', $validation)) ? 'class="invalid"' : null?> />

View file

@ -18,6 +18,17 @@
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php switch($field) {
case 'icon':
switch($setting) {
case 'error': printf(_('Error during icon upload: %s'), $value);
break;
case 'mimetype': printf(_('Icon has wrong type “%s”'), $value);
break;
case 'size': echo _('Icon exceeds size maximum');
break;
default: echo _('Icon invalid');
}
break;
case 'title':
switch($setting) {
case 'minlength': printf(_('Title is too short (min. %d chars)'), $value);
@ -40,9 +51,6 @@
default: echo _('XPs invalid');
}
break;
default:
echo $exception->getMessage();
break;
} ?>
</li>
<?php endforeach ?>
@ -51,7 +59,18 @@
<?php endforeach ?>
</ul>
<?php endif ?>
<form method="post" action="" class="logreg">
<form method="post" action="" class="logreg" enctype="multipart/form-data">
<fieldset>
<legend><?=_('Icon')?></legend>
<img src="<?=$linker->link(array('media','charactergroupsquest',$seminary['url'],$groupsgroup['url'],$quest['url']))?>" /><br />
<input type="file" name="icon" />
<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>
</fieldset>
<fieldset>
<label for="title"><?=_('Title')?>:</label>
<input type="text" id="title" name="title" placeholder="<?=_('Title')?>" title="<?=_('Title')?>" maxlength="<?=$validationSettings['title']['maxlength']?>" value="<?=$title?>" <?=(array_key_exists('title', $validation)) ? 'class="invalid"' : null?> />

View file

@ -18,11 +18,7 @@
<?php endif ?>
<div class="gbanner cf">
<?php if(!is_null($media)) : ?>
<img src="<?=$linker->link(array('media','seminary',$seminary['url'],$media['url']))?>" class="grpqimg" />
<?php else : ?>
<img src="<?=$linker->link(array('grafics','charactergroup.jpg'))?>" class="gbanner">
<?php endif ?>
<img src="<?=$linker->link(array('media','charactergroupsquest',$seminary['url'],$groupsgroup['url'],$quest['url']))?>" class="grpqimg" />
<h1><?=$quest['title']?></h1>
</div>
<ul class="gdata cf">