From 14b09065a223774fca9ece4193975534447e6653 Mon Sep 17 00:00:00 2001 From: oliver Date: Sat, 26 Mar 2016 17:05:42 +0100 Subject: [PATCH] implement CRUD for Character titles --- agents/intermediate/CharactertitlesAgent.inc | 79 ++++++ configs/AppConfig.inc | 8 + controllers/CharactertitlesController.inc | 261 +++++++++++++++++++ models/CharactertitlesModel.inc | 75 ++++-- views/html/characters/index.tpl | 1 + views/html/charactertitles/create.tpl | 55 ++++ views/html/charactertitles/delete.tpl | 12 + views/html/charactertitles/edit.tpl | 55 ++++ views/html/charactertitles/index.tpl | 22 ++ 9 files changed, 551 insertions(+), 17 deletions(-) create mode 100644 agents/intermediate/CharactertitlesAgent.inc create mode 100644 controllers/CharactertitlesController.inc create mode 100644 views/html/charactertitles/create.tpl create mode 100644 views/html/charactertitles/delete.tpl create mode 100644 views/html/charactertitles/edit.tpl create mode 100644 views/html/charactertitles/index.tpl diff --git a/agents/intermediate/CharactertitlesAgent.inc b/agents/intermediate/CharactertitlesAgent.inc new file mode 100644 index 00000000..cd68a5d8 --- /dev/null +++ b/agents/intermediate/CharactertitlesAgent.inc @@ -0,0 +1,79 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\agents\intermediate; + + + /** + * Agent to handle Character titles of a Seminary. + * + * @author Oliver Hanraths + */ + class CharactertitlesAgent extends \nre\agents\IntermediateAgent + { + + + + + /** + * Action: index. + * + * @param \nre\core\Request $request Current request + * @param \nre\core\Response $response Current response + */ + public function index(\nre\core\Request $request, \nre\core\Response $response) + { + // Add Moodpic + $this->addSubAgent('Moodpic', 'seminary', $request->getParam(3), $request->getParam(1)); + } + + + /** + * Action: create. + * + * @param \nre\core\Request $request Current request + * @param \nre\core\Response $response Current response + */ + public function create(\nre\core\Request $request, \nre\core\Response $response) + { + // Add Moodpic + $this->addSubAgent('Moodpic', 'seminary', $request->getParam(3), $request->getParam(1)); + } + + + /** + * Action: edit. + * + * @param \nre\core\Request $request Current request + * @param \nre\core\Response $response Current response + */ + public function edit(\nre\core\Request $request, \nre\core\Response $response) + { + // Add Moodpic + $this->addSubAgent('Moodpic', 'seminary', $request->getParam(3), $request->getParam(1)); + } + + + /** + * Action: delete. + * + * @param \nre\core\Request $request Current request + * @param \nre\core\Response $response Current response + */ + public function delete(\nre\core\Request $request, \nre\core\Response $response) + { + // Add Moodpic + $this->addSubAgent('Moodpic', 'seminary', $request->getParam(3), $request->getParam(1)); + } + + } + +?> diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 1e8a13f0..b1f37595 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -295,6 +295,14 @@ 'answer' => array( 'minlength' => 1, 'maxlength' => 255 + ), + 'title_male' => array( + 'minlength' => 3, + 'maxlength' => 24 + ), + 'title_female' => array( + 'minlength' => 3, + 'maxlength' => 24 ) ); diff --git a/controllers/CharactertitlesController.inc b/controllers/CharactertitlesController.inc new file mode 100644 index 00000000..9b44caa3 --- /dev/null +++ b/controllers/CharactertitlesController.inc @@ -0,0 +1,261 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\controllers; + + + /** + * Controller of the CharactertitlesAgent to handle Character titles of a + * Seminary. + * + * @author Oliver Hanraths + */ + class CharactertitlesController extends \hhu\z\controllers\SeminaryController + { + /** + * Required components + * + * @var array + */ + public $components = array('validation'); + /** + * Required models + * + * @var array + */ + public $models = array('charactertitles'); + /** + * User permissions + * + * @var array + */ + public $permissions = array( + 'index' => array('admin', 'moderator', 'user'), + 'create' => array('admin', 'moderator', 'user'), + 'edit' => array('admin', 'moderator', 'user'), + 'delete' => array('admin', 'moderator', 'user') + ); + + + + + /** + * Action: index. + * + * List Character titles. + * + * @throws \nre\exceptions\IdNotFoundException + * @param string $seminaryUrl URL-Title of a Seminary + */ + public function index($seminaryUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character titles + $titles = $this->Charactertitles->getTitlesForSeminary($seminary['id']); + + + // Set titile + $this->addTitleLocalized('Charactertitles'); + $this->addTitle($seminary['title']); + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('titles', $titles); + } + + + /** + * Action: create. + * + * Create a new Character title for a Seminary. + * + * @throws \nre\exceptions\IdNotFoundException + * @param string $seminaryUrl URL-Title of a Seminary + */ + public function create($seminaryUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Values + $titleMale = ''; + $titleFemale = ''; + $fields = array('title_male', 'title_female'); + $validation = array(); + + // Create new Charactertype + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('create'))) + { + // Get params and validate them + $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); + $titleMale = $this->request->getPostParam('title_male'); + if($this->Charactertitles->titleExists($seminary['id'], $titleMale)) { + $validation = $this->Validation->addValidationResult($validation, 'title_male', 'exist', true); + } + $titleFemale = $this->request->getPostParam('title_female'); + if($this->Charactertitles->titleExists($seminary['id'], $titleFemale)) { + $validation = $this->Validation->addValidationResult($validation, 'title_female', 'exist', true); + } + + // Create new Character title + if($validation === true) + { + $titleId = $this->Charactertitles->createTitle( + $this->Auth->getUserId(), + $seminary['id'], + $titleMale, + $titleFemale + ); + $title = $this->Charactertitles->getTitleById($titleId); + + // Redirect to editing + $this->redirect($this->linker->link(array('edit', $seminary['url'], $title['hash']), 1)); + } + } + + // Get validation settings + $validationSettings = array(); + foreach($fields as &$field) { + $validationSettings[$field] = \nre\configs\AppConfig::$validation[$field]; + } + + + // Set titile + $this->addTitleLocalized('Create new Charactertitle'); + $this->addTitle($seminary['title']); + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('titleMale', $titleMale); + $this->set('titleFemale', $titleFemale); + $this->set('validation', $validation); + $this->set('validationSettings', $validationSettings); + } + + + /** + * Action: edit. + * + * Edit a Character title for a Seminary. + * + * @throws \nre\exceptions\IdNotFoundException + * @param string $seminaryUrl URL-title of a Seminary + * @param string $titleHash Hash of Character title + */ + public function edit($seminaryUrl, $titleHash) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character title + $title = $this->Charactertitles->getTitleByHash($titleHash); + + // Values + $titleMale = $title['title_male']; + $titleFemale = $title['title_female']; + $fields = array('title_male', 'title_female'); + $validation = array(); + + // Check request method + if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('edit'))) + { + // Get params and validate them + $validation = $this->Validation->validateParams($this->request->getPostParams(), $fields); + $titleMale = $this->request->getPostParam('title_male'); + if($this->Charactertitles->titleExists($seminary['id'], $titleMale, $title['id'])) { + $validation = $this->Validation->addValidationResult($validation, 'title_male', 'exist', true); + } + $titleFemale = $this->request->getPostParam('title_female'); + if($this->Charactertitles->titleExists($seminary['id'], $titleFemale, $title['id'])) { + $validation = $this->Validation->addValidationResult($validation, 'title_female', 'exist', true); + } + + // Edit Character title + if($validation === true) + { + $this->Charactertitles->editTitle( + $title['id'], + $titleMale, + $titleFemale + ); + $title = $this->Charactertitles->getTitleById($title['id']); + + // Redirect to overview + $this->redirect($this->linker->link(array('index', $seminary['url']), 1)); + } + } + + // Get validation settings + $validationSettings = array(); + foreach($fields as &$field) { + $validationSettings[$field] = \nre\configs\AppConfig::$validation[$field]; + } + + + // Set titile + $this->addTitleLocalized('Edit Charactertitle'); + $this->addTitle($seminary['title']); + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('titleMale', $titleMale); + $this->set('titleFemale', $titleFemale); + $this->set('validation', $validation); + $this->set('validationSettings', $validationSettings); + } + + + /** + * Action: delete. + * + * Delete a Character title for a Seminary. + * + * @throws \nre\exceptions\IdNotFoundException + * @param string $seminaryUrl URL-title of a Seminary + * @param string $titleHash Hash of Character title + */ + public function delete($seminaryUrl, $titleHash) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character title + $title = $this->Charactertitles->getTitleByHash($titleHash); + + // Check request method + if($this->request->getRequestMethod() == 'POST') + { + // Check confirmation + if(!is_null($this->request->getPostParam('delete'))) + { + // Delete Character title + $this->Charactertitles->deleteTitle($title['id']); + } + + // Redirect to overview + $this->redirect($this->linker->link(array('index', $seminary['url']), 1)); + } + + + // Set titile + $this->addTitleLocalized('Delete Charactertitle'); + $this->addTitle($seminary['title']); + + // Pass data to view + $this->set('seminary', $seminary); + $this->set('title', $title); + } + + } + +?> diff --git a/models/CharactertitlesModel.inc b/models/CharactertitlesModel.inc index 9776f229..e61f1e26 100644 --- a/models/CharactertitlesModel.inc +++ b/models/CharactertitlesModel.inc @@ -44,7 +44,7 @@ public function getTitleById($titleId) { $data = $this->db->query( - 'SELECT id, title_male, title_female '. + 'SELECT id, hash, title_male, title_female '. 'FROM charactertitles '. 'WHERE id = ?', 'i', @@ -59,6 +59,50 @@ } + /** + * Get a Character title by its hash. + * + * @throws \nre\exceptions\IdNotFoundException + * @param int $titleHash Hash of title + * @return array Title data + */ + public function getTitleByHash($titleHash) + { + $data = $this->db->query( + 'SELECT id, hash, title_male, title_female '. + 'FROM charactertitles '. + 'WHERE hash = ?', + 's', + $titleHash + ); + if(empty($data)) { + throw new \nre\exceptions\IdNotFoundException($titleHash); + } + + + return $data[0]; + } + + + /** + * Get all Character titles of a Seminary. + * + * @param int $seminaryId ID of Seminary to get titles for + * @return array List of title data + */ + public function getTitlesForSeminary($seminaryId) + { + return $this->db->query( + 'SELECT id, hash, title_male, title_female '. + 'FROM charactertitles '. + 'WHERE seminary_id = ? '. + 'ORDER BY title_male, title_female ', + 'i', + $seminaryId + ); + } + + /** * Get all titles a Character owns. * @@ -79,17 +123,15 @@ } - // TODO addTitleToCharacter - /** - * TODO Check if a Character title already exists. + * Check if a title for a Characters already exists. * - * @param string $title Character title to check * @param int $seminaryId ID of Seminary - * @param int $questId Do not check this ID (for editing) + * @param string $title Character title to check + * @param int $titleId Do not check this ID (for editing) * @return boolean Whether Character title exists or not */ - public function characterTitleExists($titleMale, $titleFemale, $seminaryId, $titleId=null) + public function titleExists($seminaryId, $title, $titleId=null) { $data = $this->db->query( 'SELECT id '. @@ -97,9 +139,8 @@ 'WHERE seminary_id = ? AND (title_male = ? OR title_female = ?)', 'iss', $seminaryId, - $titleMale, - $titleFemale - //\nre\core\Linker::createLinkParam($title) + $title, + $title ); return (!empty($data) && (is_null($titleId) || $titleId != $data[0]['id'])); @@ -107,7 +148,7 @@ /** - * TODO Create a new Character title. + * Create a new Character title. * * @param int $userId User-ID that creates the new title * @param int $seminaryId ID of Questgroup @@ -119,11 +160,12 @@ { $this->db->query( 'INSERT INTO charactertitles '. - '(created_user_id, seminary_id, title_male, title_female) '. + '(created_user_id, seminary_id, hash, title_male, title_female) '. 'VALUES '. - '(?, ?, ?, ?)', - 'iiss', + '(?, ?, ?, ?, ?)', + 'iisss', $userId, $seminaryId, + \hhu\z\Utils::createRandomHash(), $titleMale, $titleFemale //\nre\core\Linker::createLinkParam($title), ); @@ -134,7 +176,7 @@ /** - * TODO Edit a Character title. + * Edit a Character title. * * @param int $titleId ID of Character title to edit * @param string $titleMale Title for male Characters @@ -149,14 +191,13 @@ 'ssi', $titleMale, $titleFemale, - //\nre\core\Linker::createLinkParam($title), $titleId ); } /** - * TODO Delete a Character title. + * Delete a Character title. * * @param int $titleId ID of Character title to delete */ diff --git a/views/html/characters/index.tpl b/views/html/characters/index.tpl index 79c2e9e6..6fd4440f 100644 --- a/views/html/characters/index.tpl +++ b/views/html/characters/index.tpl @@ -8,6 +8,7 @@