From 9c0b0d8d07eacd83eedc0e5b3050d6f9bee7c15d Mon Sep 17 00:00:00 2001 From: oliver Date: Sun, 10 Apr 2016 14:28:22 +0200 Subject: [PATCH] implement position for sorting of Character groups Quests (implements #157) --- configs/AppConfig.inc | 12 +-- .../CharactergroupsquestsController.inc | 78 ++++++++++++++++++ db/create.sql | 2 + models/CharactergroupsquestsModel.inc | 81 +++++++++++++++++-- views/html/charactergroups/groupsgroup.tpl | 10 +++ views/html/charactergroupsquests/movedown.tpl | 0 views/html/charactergroupsquests/moveup.tpl | 0 www/css/desktop.css | 1 + 8 files changed, 170 insertions(+), 14 deletions(-) create mode 100644 views/html/charactergroupsquests/movedown.tpl create mode 100644 views/html/charactergroupsquests/moveup.tpl diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 4c3ab885..fa96882c 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -353,9 +353,9 @@ array('^charactergroups/([^/]+)/([^/]+)/(create)/?$', 'charactergroups/creategroup/$1/$2/$3', true), array('^charactergroups/([^/]+)/([^/]+)/([^/]+)/?$', 'charactergroups/group/$1/$2/$3', true), array('^charactergroups/([^/]+)/([^/]+)/([^/]+)/(manage|edit|delete)/?$', 'charactergroups/$4group/$1/$2/$3', true), - array('^charactergroupsquests/([^/]+)/([^/]+)/create/?$', 'charactergroupsquests/create/$1/$2', true), - array('^charactergroupsquests/([^/]+)/([^/]+)/([^/]+)/?$', 'charactergroupsquests/quest/$1/$2/$3', true), - array('^charactergroupsquests/([^/]+)/([^/]+)/([^/]+)/(edit|delete|manage)/?$', 'charactergroupsquests/$4/$1/$2/$3', true), + array('^charactergroupsquests/([^/]+)/([^/]+)/create/?$', 'charactergroupsquests/create/$1/$2', true), + array('^charactergroupsquests/([^/]+)/([^/]+)/([^/]+)/?$', 'charactergroupsquests/quest/$1/$2/$3', true), + array('^charactergroupsquests/([^/]+)/([^/]+)/([^/]+)/(edit|moveup|movedown|delete|manage)/?$', 'charactergroupsquests/$4/$1/$2/$3', true), array('^charactergroupsqueststations/([^/]+)/([^/]+)/([^/]+)/?$', 'charactergroupsqueststations/index/$1/$2/$3?layout=ajax', true), array('^charactergroupsqueststations/([^/]+)/([^/]+)/([^/]+)/create/?$', 'charactergroupsqueststations/create/$1/$2/$3', true), array('^charactergroupsqueststations/([^/]+)/([^/]+)/([^/]+)/([^/]+)/(edit|edittask|delete)/?$', 'charactergroupsqueststations/$5/$1/$2/$3/$4', true), @@ -418,9 +418,9 @@ array('^charactergroups/creategroup/([^/]+)/([^/]+)$', 'charactergroups/$1/$2/create', true), array('^charactergroups/group/([^/]+)/([^/]+)/([^/]+)$', 'charactergroups/$1/$2/$3', true), array('^charactergroups/(manage|edit|delete)group/([^/]+)/([^/]+)/([^/]+)$', 'charactergroups/$2/$3/$4/$1', true), - array('^charactergroupsquests/create/([^/]+)/([^/]+)/?$', 'charactergroupsquests/$1/$2/create', true), - array('^charactergroupsquests/quest/(.*)$', 'charactergroupsquests/$1', true), - array('^charactergroupsquests/(edit|delete|manage)/([^/]+)/([^/]+)/([^/]+)$', 'charactergroupsquests/$2/$3/$4/$1', true), + array('^charactergroupsquests/create/([^/]+)/([^/]+)/?$', 'charactergroupsquests/$1/$2/create', true), + array('^charactergroupsquests/quest/(.*)$', 'charactergroupsquests/$1', true), + array('^charactergroupsquests/(edit|moveup|movedown|delete|manage)/([^/]+)/([^/]+)/([^/]+)$', 'charactergroupsquests/$2/$3/$4/$1', true), array('^charactergroupsqueststations/index/(.*)$', 'charactergroupsqueststations/$1', true), array('^charactergroupsqueststations/station/(.*)$', 'charactergroupsqueststations/$1', true), array('^charactergroupsqueststations/(create|edit|edittask|delete)/(.*)$', 'charactergroupsqueststations/$2/$1', true), diff --git a/controllers/CharactergroupsquestsController.inc b/controllers/CharactergroupsquestsController.inc index f26ae393..d5111421 100644 --- a/controllers/CharactergroupsquestsController.inc +++ b/controllers/CharactergroupsquestsController.inc @@ -42,6 +42,8 @@ 'manage' => array('admin', 'moderator', 'user'), 'create' => array('admin', 'moderator', 'user'), 'edit' => array('admin', 'moderator', 'user'), + 'moveup' => array('admin', 'moderator', 'user'), + 'movedown' => array('admin', 'moderator', 'user'), 'delete' => array('admin', 'moderator', 'user') ); /** @@ -54,6 +56,8 @@ 'manage' => array('admin', 'moderator'), 'create' => array('admin', 'moderator'), 'edit' => array('admin', 'moderator'), + 'moveup' => array('admin', 'moderator'), + 'movedown' => array('admin', 'moderator'), 'delete' => array('admin', 'moderator') ); @@ -672,6 +676,80 @@ } + /** + * Action: moveup. + * + * Move a Character groups Quest up (decrement position). + * + * @throws \nre\exceptions\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 moveup($seminaryUrl, $groupsgroupUrl, $questUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character groups-group + $groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl); + + // Get Character groups-group Quests + $quest = $this->Charactergroupsquests->getQuestByUrl($groupsgroup['id'], $questUrl); + + // Set position + $this->Charactergroupsquests->moveQuest($quest, true); + + $this->redirect( + $this->linker->link( + array( + 'charactergroups', + 'groupsgroup', + $seminary['url'], + $groupsgroup['url'] + ) + ) + ); + } + + + /** + * Action: movedown. + * + * Move a Character groups Quest down (increment position). + * + * @throws \nre\exceptions\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 movedown($seminaryUrl, $groupsgroupUrl, $questUrl) + { + // Get seminary + $seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl); + + // Get Character groups-group + $groupsgroup = $this->Charactergroups->getGroupsgroupByUrl($seminary['id'], $groupsgroupUrl); + + // Get Character groups-group Quests + $quest = $this->Charactergroupsquests->getQuestByUrl($groupsgroup['id'], $questUrl); + + // Set position + $this->Charactergroupsquests->moveQuest($quest, false); + + $this->redirect( + $this->linker->link( + array( + 'charactergroups', + 'groupsgroup', + $seminary['url'], + $groupsgroup['url'] + ) + ) + ); + } + + /** * Action: delete. * diff --git a/db/create.sql b/db/create.sql index b6c1edc6..d97b940b 100644 --- a/db/create.sql +++ b/db/create.sql @@ -502,6 +502,7 @@ CREATE TABLE `charactergroupsquests` ( `questgroups_id` int(11) NOT NULL, `title` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, `url` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `pos` int(10) unsigned NOT NULL, `description` text COLLATE utf8mb4_unicode_ci NOT NULL, `xps` int(10) unsigned NOT NULL, `rules` text COLLATE utf8mb4_unicode_ci NOT NULL, @@ -511,6 +512,7 @@ CREATE TABLE `charactergroupsquests` ( `public` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`), UNIQUE KEY `charactergroupsgroup_id_2` (`charactergroupsgroup_id`,`url`), + UNIQUE KEY `charactergroupsgroup_id_3` (`charactergroupsgroup_id`,`pos`), KEY `created_user_id` (`created_user_id`), KEY `charactergroupsgroup_id` (`charactergroupsgroup_id`), KEY `questgroups_id` (`questgroups_id`), diff --git a/models/CharactergroupsquestsModel.inc b/models/CharactergroupsquestsModel.inc index f3f144fe..41c729d4 100644 --- a/models/CharactergroupsquestsModel.inc +++ b/models/CharactergroupsquestsModel.inc @@ -50,10 +50,10 @@ public function getQuestsForCharactergroupsgroup($groupsgroupId) { return $this->db->query( - 'SELECT id, questgroups_id, title, url, xps, questsmedia_id, public '. + 'SELECT id, questgroups_id, title, url, pos, xps, questsmedia_id, public '. 'FROM charactergroupsquests '. 'WHERE charactergroupsgroup_id = ? '. - 'ORDER BY created ASC', + 'ORDER BY pos ASC', 'i', $groupsgroupId ); @@ -71,7 +71,7 @@ public function getQuestByUrl($groupsgroupId, $questUrl) { $data = $this->db->query( - 'SELECT id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, questsmedia_id, public '. + 'SELECT id, questgroups_id, charactergroupsgroup_id, title, url, pos, description, xps, rules, won_text, lost_text, questsmedia_id, public '. 'FROM charactergroupsquests '. 'WHERE charactergroupsgroup_id = ? AND url = ?', 'is', @@ -97,7 +97,7 @@ public function getQuestById($questId) { $data = $this->db->query( - 'SELECT id, charactergroupsgroup_id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, questsmedia_id, public '. + 'SELECT id, charactergroupsgroup_id, questgroups_id, title, url, pos, description, xps, rules, won_text, lost_text, questsmedia_id, public '. 'FROM charactergroupsquests '. 'WHERE id = ?', 'i', @@ -320,17 +320,29 @@ */ public function createQuest($userId, $groupsgroupId, $questgroupId, $title, $description, $xps, $rules, $wonText, $lostText) { + // Get last position + $pos = $this->db->query( + 'SELECT COALESCE(MAX(pos),0) AS pos '. + 'FROM charactergroupsquests '. + 'WHERE charactergroupsgroup_id = ?', + 'i', + $groupsgroupId + ); + $pos = intval($pos[0]['pos']); + + // Add new Quest $this->db->query( 'INSERT INTO charactergroupsquests '. - '(created_user_id, charactergroupsgroup_id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, public) '. + '(created_user_id, charactergroupsgroup_id, questgroups_id, title, url, pos, description, xps, rules, won_text, lost_text, public) '. 'VALUES '. - '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)', + '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)', 'iiisssdsss', $userId, $groupsgroupId, $questgroupId, $title, \nre\core\Linker::createLinkParam($title), + $pos + 1, $description, $xps, $rules, @@ -339,6 +351,7 @@ ); + // Return ID return $this->db->getInsertId(); } @@ -379,6 +392,58 @@ } + /** + * Move a Character groups Quest up (decrement position) or down + * (increment position). + * + * @param array $quest Character groups Quest to move + * @param boolean $up True for moving up, false for down + */ + public function moveQuest($quest, $up) + { + $this->db->setAutocommit(false); + try { + // Set temporary position + $this->db->query( + 'UPDATE charactergroupsquests '. + 'SET pos = 0 '. + 'WHERE id = ?', + 'i', + $quest['id'] + ); + + // Switch entry + $this->db->query( + 'UPDATE charactergroupsquests '. + 'SET pos = ? '. + 'WHERE charactergroupsgroup_id = ? AND pos = ?', + 'iii', + $quest['pos'], + $quest['charactergroupsgroup_id'], + $quest['pos'] + ($up ? -1 : 1) + ); + + // Set new position + $this->db->query( + 'UPDATE charactergroupsquests '. + 'SET pos = ? '. + 'WHERE id = ?', + 'ii', + $quest['pos'] + ($up ? -1 : 1), + $quest['id'] + ); + + $this->db->commit(); + } + catch(\nre\exceptions\DatamodelException $e) { + $this->db->rollback(); + $this->db->setAutocommit(true); + throw $e; + } + $this->db->setAutocommit(true); + } + + /** * Copy all Character groups Quests from a Seminary. * @@ -402,8 +467,8 @@ // Copy Quest $this->db->query( 'INSERT INTO charactergroupsquests '. - '(created_user_id, charactergroupsgroup_id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, public) '. - 'SELECT ?, ?, ?, title, url, description, xps, rules, won_text, lost_text, public '. + '(created_user_id, charactergroupsgroup_id, questgroups_id, title, url, pos, description, xps, rules, won_text, lost_text, public) '. + 'SELECT ?, ?, ?, title, url, pos, description, xps, rules, won_text, lost_text, public '. 'FROM charactergroupsquests '. 'WHERE id = ?', 'iiii', diff --git a/views/html/charactergroups/groupsgroup.tpl b/views/html/charactergroups/groupsgroup.tpl index 8b0e74ac..4f0228c2 100644 --- a/views/html/charactergroups/groupsgroup.tpl +++ b/views/html/charactergroups/groupsgroup.tpl @@ -42,6 +42,16 @@
  • + 0) : ?> +

    + 1) : ?> + + + + + +

    +
  • diff --git a/views/html/charactergroupsquests/movedown.tpl b/views/html/charactergroupsquests/movedown.tpl new file mode 100644 index 00000000..e69de29b diff --git a/views/html/charactergroupsquests/moveup.tpl b/views/html/charactergroupsquests/moveup.tpl new file mode 100644 index 00000000..e69de29b diff --git a/www/css/desktop.css b/www/css/desktop.css index 8ebf13a9..99106b8e 100644 --- a/www/css/desktop.css +++ b/www/css/desktop.css @@ -276,6 +276,7 @@ span.ctitle{font-size:.75em;padding:0;margin:0} .cgqlist li{background:#fff;margin-bottom:5px;padding:3px} .cgqlist img{float:left;width:40px;height:40px;border-radius:3px;margin-right:10px} .cgqlist p{margin:5px 0 0} +.cgqlist p small{float:right} /** Charactergroup Profile **/