]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add rename() method to OC_VCategories.
authorThomas Tanghus <thomas@tanghus.net>
Mon, 20 May 2013 08:16:07 +0000 (10:16 +0200)
committerThomas Tanghus <thomas@tanghus.net>
Mon, 20 May 2013 08:16:07 +0000 (10:16 +0200)
lib/vcategories.php
tests/lib/vcategories.php

index 5975e688b75cad4ff9a5422c3052a3aedfc9a6b8..74864704e30f45e672d91c7a6212392daa4bde33 100644 (file)
@@ -324,6 +324,38 @@ class OC_VCategories {
                return $id;
        }
 
+       /**
+       * @brief Rename category.
+       * @param string $from The name of the existing category
+       * @param string $to The new name of the category.
+       * @returns bool
+       */
+       public function rename($from, $to) {
+               $id = $this->array_searchi($from, $this->categories);
+               if($id === false) {
+                       OCP\Util::writeLog('core', __METHOD__.', category: ' . $from. ' does not exist', OCP\Util::DEBUG);
+                       return false;
+               }
+
+               $sql = 'UPDATE `' . self::CATEGORY_TABLE . '` SET `category` = ? '
+                       . 'WHERE `uid` = ? AND `type` = ? AND `id` = ?';
+               try {
+                       $stmt = OCP\DB::prepare($sql);
+                       $result = $stmt->execute(array($to, $this->user, $this->type, $id));
+                       if (OC_DB::isError($result)) {
+                               echo 'DB error: ' . $result . "\n";
+                               OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+                               return false;
+                       }
+               } catch(Exception $e) {
+                       OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+                               OCP\Util::ERROR);
+                       return false;
+               }
+               $this->categories[$id] = $to;
+               return true;
+       }
+
        /**
        * @brief Add a new category.
        * @param $names A string with a name or an array of strings containing
index e79dd49870c89f33b5ef1f309b5a5cc41c5d4233..a8af6ae9f7a29ea79039aa49f3eee871464bdd61 100644 (file)
@@ -81,6 +81,16 @@ class Test_VCategories extends PHPUnit_Framework_TestCase {
 
        }
 
+       public function testrenameCategory() {
+               $defcategories = array('Friends', 'Family', 'Wrok', 'Other');
+               $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
+
+               $this->assertTrue($catmgr->rename('Wrok', 'Work'));
+               $this->assertTrue($catmgr->hasCategory('Work'));
+               $this->assertFalse($catmgr->hasCategory('Wrok'));
+
+       }
+
        public function testAddToCategory() {
                $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);