]> 91.132.146.200 Git - insipid.git/commitdiff
resolve bug #7 for tags
authorBanana <mail@bananas-playground.net>
Sat, 12 Sep 2020 10:31:54 +0000 (12:31 +0200)
committerBanana <mail@bananas-playground.net>
Sat, 12 Sep 2020 10:31:54 +0000 (12:31 +0200)
webroot/lib/tag.class.php
webroot/view/edittags.inc.php

index 8d0ff870896a8638a96d30695dce28bafab4b960..cf921ebcef4b7356df9aa2d62b6fb2fa21c4bd23 100644 (file)
@@ -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;
+        }
+    }
+
 }
index 29c453acc2b42696ed4491b17cfb1f6e81d4d547..b900cd5be6d301efdbe39dfb90acf857ef0d587c 100644 (file)
@@ -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) {