From 4d06f23a04659ccb8c354376cca091eed4c35644 Mon Sep 17 00:00:00 2001 From: Banana Date: Sat, 12 Sep 2020 12:31:54 +0200 Subject: [PATCH] resolve bug #7 for tags --- webroot/lib/tag.class.php | 82 +++++++++++++++++++++++++++-------- webroot/view/edittags.inc.php | 64 ++++++++++++++++++++++----- 2 files changed, 118 insertions(+), 28 deletions(-) diff --git a/webroot/lib/tag.class.php b/webroot/lib/tag.class.php index 8d0ff87..cf921eb 100644 --- a/webroot/lib/tag.class.php +++ b/webroot/lib/tag.class.php @@ -49,11 +49,13 @@ class Tag { $this->DB = $databaseConnectionObject; } - /** - * by given string load the info from the DB and even create if not existing - * @param string $string - */ - public function initbystring($string) { + /** + * 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 + */ + public function initbystring($string, $doNotCreate=false) { + $ret = 0; $this->_id = false; if(!empty($string)) { $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag` @@ -63,18 +65,26 @@ class Tag { $result = $query->fetch_assoc(); $this->_id = $result['id']; $this->_data = $result; + $ret = 1; } else { - $queryStr = "INSERT INTO `".DB_PREFIX."_tag` - 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; - } + if(!$doNotCreate) { + $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag` + 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; } /** @@ -114,11 +124,11 @@ class Tag { return $ret; } - /** - * set the relation to the given link to the loaded tag - * @param int $linkid - * @return boolean - */ + /** + * set the relation to the given link to the loaded tag + * @param int $linkid + * @return void + */ public function setRelation($linkid) { if(!empty($linkid) && !empty($this->_id)) { $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation` @@ -128,6 +138,26 @@ class Tag { } } + /** + * Return an array of any linkid related to the current loaded tag + * @return array + */ + public function getReleations() { + $ret = array(); + + $queryStr = "SELECT linkid + FROM `".DB_PREFIX."_tagrelation` + WHERE tagid = '".$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 tag from db * @return boolean @@ -162,4 +192,20 @@ class Tag { return $ret; } + + /** + * Rename current loaded tag name + * @param $newValue + * @return void + */ + public function rename($newValue) { + if(!empty($newValue)) { + $queryStr = "UPDATE `".DB_PREFIX."_tag` + 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/view/edittags.inc.php b/webroot/view/edittags.inc.php index 29c453a..b900cd5 100644 --- a/webroot/view/edittags.inc.php +++ b/webroot/view/edittags.inc.php @@ -35,25 +35,69 @@ Summoner::simpleAuth(); if(isset($_POST['tag']) && !empty($_POST['tag']) && isset($_POST['updateTags'])) { $tagData = $_POST['tag']; + $newTag = $_POST['newTag']; - $deleteTagData = array(); - if(isset($_POST['deleteTag'])) { - $deleteTagData = $_POST['deleteTag']; - } + # 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'; - $newTag = $_POST['newTag']; + $tagToUpdate = array(); + foreach ($tagData as $tid=>$tNewValue) { + $_c = Summoner::validate($tNewValue,'nospace'); + if($_c === true) { + $tagToUpdate[$tid] = $tNewValue; + } + } - # first deletion, then update and then add - # adding a new one which matches an existing one will update it. + $deleteTagData = array(); + if(isset($_POST['deleteTag'])) { + $deleteTagData = $_POST['deleteTag']; + } - $submitFeedback['message'] = array(); - $submitFeedback['status'] = 'success'; + $tagDoNotDeleteFromUpdate = array(); + if(!empty($tagToUpdate)) { + $submitFeedback['message'][] = 'Tags renamed successfully.'; + foreach ($tagToUpdate as $k=>$v) { + $tagObjAlternative = new Tag($DB); + $do = $tagObjAlternative->initbystring($v,true); + if($do === 1) { # existing + // the target tag should not be removed! + $tagDoNotDeleteFromUpdate[$tagObjAlternative->getData('id')] = $tagObjAlternative->getData('id'); + $tagObjOld = new Tag($DB); + if(!empty($tagObjOld->initbyid($k))) { + $linksToUpdate = $tagObjOld->getReleations(); + if(!empty($linksToUpdate)) { + foreach($linksToUpdate as $linkId) { + $tagObjAlternative->setRelation($linkId); + } + $tagObjOld->delete(); + } + } + else { + $submitFeedback['message'][] = 'Tags could not be renamed.'; + $submitFeedback['status'] = 'error'; + } + } + elseif ($do === 3) { # not existing one. Can be renamed + $tagObjRename = new Tag($DB); + if(!empty($tagObjRename->initbyid($k))) { + $tagObjRename->rename($v); + } + } + else { + $submitFeedback['message'][] = 'Tags could not be renamed.'; + $submitFeedback['status'] = 'error'; + } + } + } if(!empty($deleteTagData)) { $submitFeedback['message'][] = 'Tags deleted successfully.'; foreach($deleteTagData as $k=>$v) { - if($v == "delete") { + if($v == "delete" && !isset($tagDoNotDeleteFromUpdate[$k])) { $tagObj = new Tag($DB); $load = $tagObj->initbyid($k); if($load !== false) { -- 2.39.5