]> 91.132.146.200 Git - insipid.git/commitdiff
fixed #7 for categorties
authorBanana <mail@bananas-playground.net>
Sat, 12 Sep 2020 10:43:39 +0000 (12:43 +0200)
committerBanana <mail@bananas-playground.net>
Sat, 12 Sep 2020 10:43:39 +0000 (12:43 +0200)
ChangeLog
TODO
webroot/lib/category.class.php
webroot/lib/tag.class.php
webroot/view/editcategories.inc.php

index 12c3bc9846ee3b623d9ed59a18ecd97f5a0c16bb..23e90486a82c39a1c427c004daecfe36224028e3 100644 (file)
--- 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 2bfbb56168e9c7a68900f438587d21866df8362e..d193e30fb517cbd73fe187536a66b5fde5e33af4 100644 (file)
--- 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.
index 13f9bada2f5a97253fac6e90ebfcfd164c1e6359..d14a71c05eb594d2a959e5943c39d477ed0caadc 100644 (file)
@@ -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;
+        }
+    }
 }
index cf921ebcef4b7356df9aa2d62b6fb2fa21c4bd23..0207e8c3ad0b64318e66083baf56f0b4d3407e4a 100644 (file)
@@ -207,5 +207,4 @@ class Tag {
             $this->_data['name'] = $newValue;
         }
     }
-
 }
index 160a17d284f8407226720b4e13cc723933005780..9f7da93bb2ed410a02495f66103bf675f4c671a3 100644 (file)
@@ -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) {