From 3213b4fc7fe0230ca96b296beefc4a447e8a4192 Mon Sep 17 00:00:00 2001 From: Banana Date: Sat, 12 Sep 2020 12:43:39 +0200 Subject: [PATCH] fixed #7 for categorties --- ChangeLog | 1 + TODO | 1 + webroot/lib/category.class.php | 86 ++++++++++++++++++++++------- webroot/lib/tag.class.php | 1 - webroot/view/editcategories.inc.php | 60 +++++++++++++++++--- 5 files changed, 119 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 12c3bc9..23e9048 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ version x.x.x - Darkmere () + Fixed bug #8 It is possible to add empty tags and categories. Special chars check done on server side. JS has problems with unicode. + + Fixed bug #7 Edit categories/tags. Rename and move version 2.5.1 - Caves of Circe (2020-03-22) diff --git a/TODO b/TODO index 2bfbb56..d193e30 100644 --- a/TODO +++ b/TODO @@ -6,3 +6,4 @@ TODO / Feature list ++ multiple user accounts and stuff + adapt new php 7 ++ http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op ++ combine cat and tag class into one. diff --git a/webroot/lib/category.class.php b/webroot/lib/category.class.php index 13f9bad..d14a71c 100644 --- a/webroot/lib/category.class.php +++ b/webroot/lib/category.class.php @@ -52,31 +52,40 @@ class Category { /** * by given string load the info from the DB and even create if not existing * @param string $string + * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated * @return bool|int */ - public function initbystring($string) { + public function initbystring($string, $doNotCreate=false) { + $ret = 0; $this->_id = false; - if(!empty($string)) { - $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category` + if(!empty($string)) { + $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category` WHERE `name` = '".$this->DB->real_escape_string($string)."'"; - $query = $this->DB->query($queryStr); - if(!empty($query) && $query->num_rows > 0) { - $result = $query->fetch_assoc(); - $this->_id = $result['id']; - $this->_data = $result; - } - else { - $queryStr = "INSERT INTO `".DB_PREFIX."_category` - SET `name` = '".$this->DB->real_escape_string($string)."'"; - $this->DB->query($queryStr); - if(!empty($this->DB->insert_id)) { - $this->_id = $this->DB->insert_id; - $this->_data['id'] = $this->_id; - $this->_data['name'] = $string; - } - } - } - return $this->_id; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + $result = $query->fetch_assoc(); + $this->_id = $result['id']; + $this->_data = $result; + $ret = 1; + } + else { + if(!$doNotCreate) { + $queryStr = "INSERT INTO `" . DB_PREFIX . "_category` + SET `name` = '" . $this->DB->real_escape_string($string) . "'"; + $this->DB->query($queryStr); + if (!empty($this->DB->insert_id)) { + $this->_id = $this->DB->insert_id; + $this->_data['id'] = $this->_id; + $this->_data['name'] = $string; + $ret = 2; + } + } + else { + $ret=3; + } + } + } + return $ret; } /** @@ -131,6 +140,26 @@ class Category { } } + /** + * Return an array of any linkid related to the current loaded category + * @return array + */ + public function getReleations() { + $ret = array(); + + $queryStr = "SELECT linkid + FROM `".DB_PREFIX."_categoryrelation` + WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'"; + $query = $this->DB->query($queryStr); + if(!empty($query) && $query->num_rows > 0) { + while($result = $query->fetch_assoc()) { + $ret[] = $result['linkid']; + } + } + + return $ret; + } + /** * deletes the current loaded category from db * @return boolean @@ -165,4 +194,19 @@ class Category { return $ret; } + + /** + * Rename current loaded cat name + * @param $newValue + * @return void + */ + public function rename($newValue) { + if(!empty($newValue)) { + $queryStr = "UPDATE `".DB_PREFIX."_category` + SET `name` = '".$this->DB->real_escape_string($newValue)."' + WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'"; + $this->DB->query($queryStr); + $this->_data['name'] = $newValue; + } + } } diff --git a/webroot/lib/tag.class.php b/webroot/lib/tag.class.php index cf921eb..0207e8c 100644 --- a/webroot/lib/tag.class.php +++ b/webroot/lib/tag.class.php @@ -207,5 +207,4 @@ class Tag { $this->_data['name'] = $newValue; } } - } diff --git a/webroot/view/editcategories.inc.php b/webroot/view/editcategories.inc.php index 160a17d..9f7da93 100644 --- a/webroot/view/editcategories.inc.php +++ b/webroot/view/editcategories.inc.php @@ -35,25 +35,69 @@ Summoner::simpleAuth(); if(isset($_POST['category']) && !empty($_POST['category']) && isset($_POST['updateCategories'])) { $categoryData = $_POST['category']; + $newCategory = $_POST['newCategory']; + + # first update then deletion and then add + # adding a new one which matches an existing one will update it. + + $submitFeedback['message'] = array(); + $submitFeedback['status'] = 'success'; + + $catToUpdate = array(); + foreach ($categoryData as $cid=>$cNewValue) { + $_c = Summoner::validate($cNewValue,'nospace'); + if($_c === true) { + $catToUpdate[$cid] = $cNewValue; + } + } $deleteCategoryData = array(); if(isset($_POST['deleteCategory'])) { $deleteCategoryData = $_POST['deleteCategory']; } - $newCategory = $_POST['newCategory']; - - # first deletion, then update and then add - # adding a new one which matches an existing one will update it. - - $submitFeedback['message'] = array(); - $submitFeedback['status'] = 'success'; + $catDoNotDeleteFromUpdate = array(); + if(!empty($catToUpdate)) { + $submitFeedback['message'][] = 'Categories renamed successfully.'; + foreach ($catToUpdate as $k=>$v) { + $catObjAlternative = new Category($DB); + $do = $catObjAlternative->initbystring($v,true); + if($do === 1) { # existing + // the target cat should not be removed! + $catDoNotDeleteFromUpdate[$catObjAlternative->getData('id')] = $catObjAlternative->getData('id'); + $catObjOld = new Category($DB); + if(!empty($catObjOld->initbyid($k))) { + $linksToUpdate = $catObjOld->getReleations(); + if(!empty($linksToUpdate)) { + foreach($linksToUpdate as $linkId) { + $catObjAlternative->setRelation($linkId); + } + $catObjOld->delete(); + } + } + else { + $submitFeedback['message'][] = 'Categories could not be renamed.'; + $submitFeedback['status'] = 'error'; + } + } + elseif ($do === 3) { # not existing one. Can be renamed + $catObjRename = new Category($DB); + if(!empty($catObjRename->initbyid($k))) { + $catObjRename->rename($v); + } + } + else { + $submitFeedback['message'][] = 'Categories could not be renamed.'; + $submitFeedback['status'] = 'error'; + } + } + } if(!empty($deleteCategoryData)) { $submitFeedback['message'][] = 'Categories deleted successfully.'; foreach($deleteCategoryData as $k=>$v) { - if($v == "delete") { + if($v == "delete" && !isset($catDoNotDeleteFromUpdate[$k])) { $catObj = new Category($DB); $load = $catObj->initbyid($k); if($load !== false) { -- 2.39.5