diff options
author | Thomas Tanghus <thomas@tanghus.net> | 2012-03-07 16:39:56 +0100 |
---|---|---|
committer | Thomas Tanghus <thomas@tanghus.net> | 2012-03-07 16:39:56 +0100 |
commit | 75323b86d157c48031b9ac8c151e4a41577a1481 (patch) | |
tree | 6e047d81b17a4bcbd15e9799ef8c5209c96a0ea5 /lib/vcategories.php | |
parent | faf6055baa224fbf4248a70d28ae4416c9c7eb1c (diff) | |
download | nextcloud-server-75323b86d157c48031b9ac8c151e4a41577a1481.tar.gz nextcloud-server-75323b86d157c48031b9ac8c151e4a41577a1481.zip |
Contacts: UI updates and ajax methods for categories.
Diffstat (limited to 'lib/vcategories.php')
-rw-r--r-- | lib/vcategories.php | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/lib/vcategories.php b/lib/vcategories.php index 250aa608c1d..7a31a5268d1 100644 --- a/lib/vcategories.php +++ b/lib/vcategories.php @@ -49,12 +49,14 @@ class OC_VCategories { * @param $user The user whos data the object will operate on. This * parameter should normally be omitted but to make an app able to * update categories for all users it is made possible to provide it. + * @param $defcategories An array of default categories to be used if none is stored. + * NOTE: Not implemented. */ - public function __construct($app, $user=null) { + public function __construct($app, $user=null, $defcategories=null) { $this->app = $app; $this->user = is_null($user) ? OC_User::getUser() : $user; $categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, '')); - $this->categories = $categories != '' ? OC_VObject::unescapeSemicolons($categories) : array(); + $this->categories = $categories != '' ? unserialize($categories) : array(); } /** @@ -62,6 +64,7 @@ class OC_VCategories { * @returns array containing the categories as strings. */ public function categories() { + OC_Log::write('core','OC_VCategories::categories: '.print_r($this->categories, true), OC_Log::DEBUG); return $this->categories; } @@ -81,7 +84,7 @@ class OC_VCategories { * @param $sync bool When true, save the categories * @returns bool Returns false on error. */ - public function add($names, $sync=true) { + public function add($names, $sync=false) { if(!is_array($names)) { $names = array($names); } @@ -95,7 +98,7 @@ class OC_VCategories { if(count($newones) > 0) { $this->categories = array_merge($this->categories, $newones); natcasesort($this->categories); // Dunno if this is necessary - if($sync) { + if($sync === true) { $this->save(); } } @@ -106,7 +109,7 @@ class OC_VCategories { * @brief Extracts categories from a vobject and add the ones not already present. * @param $vobject The instance of OC_VObject to load the categories from. */ - public function loadFromVObject($vobject, $sync=true) { + public function loadFromVObject($vobject, $sync=false) { $this->add($vobject->getAsArray('CATEGORIES'), $sync); } @@ -126,14 +129,15 @@ class OC_VCategories { * } * $categories->rescan($objects); */ - public function rescan($objects) { + public function rescan($objects, $sync=true) { $this->categories = array(); foreach($objects as $object) { + //OC_Log::write('core','OC_VCategories::rescan: '.substr($object, 0, 100).'(...)', OC_Log::DEBUG); $vobject = OC_VObject::parse($object); if(!is_null($vobject)) { - $this->loadFromVObject($vobject, false); + $this->loadFromVObject($vobject, $sync); } else { - OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG); + OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG); } } $this->save(); @@ -142,35 +146,51 @@ class OC_VCategories { /** * @brief Save the list with categories */ - public function save() { - $escaped_categories = OC_VObject::escapeSemicolons($this->categories); + private function save() { + $escaped_categories = serialize($this->categories); + OC_Log::write('core','OC_VCategories::save: '.print_r($this->categories, true), OC_Log::DEBUG); OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories); } /** - * @brief Delete a category from the db and from all the vobject supplied - * @param $name + * @brief Delete categories from the db and from all the vobject supplied + * @param $names An array of categories to delete * @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table. */ - public function delete($name, array &$objects) { - if(!$this->hasCategory($name)) { - return; + public function delete($names, array &$objects) { + if(!is_array($names)) { + $names = array($names); + } + OC_Log::write('core','OC_VCategories::delete, before: '.print_r($this->categories, true), OC_Log::DEBUG); + foreach($names as $name) { + OC_Log::write('core','OC_VCategories::delete: '.$name, OC_Log::DEBUG); + if($this->hasCategory($name)) { + OC_Log::write('core','OC_VCategories::delete: '.$name.' got it', OC_Log::DEBUG); + unset($this->categories[$this->array_searchi($name, $this->categories)]); + } } - unset($this->categories[$this->array_searchi($name, $this->categories)]); $this->save(); + OC_Log::write('core','OC_VCategories::delete, after: '.print_r($this->categories, true), OC_Log::DEBUG); foreach($objects as $key=>&$value) { $vobject = OC_VObject::parse($value[1]); if(!is_null($vobject)){ $categories = $vobject->getAsArray('CATEGORIES'); - $idx = $this->array_searchi($name, $categories); - if($idx) { - unset($categories[$this->array_searchi($name, $categories)]); - $vobject->setString('CATEGORIES', implode(',', $categories)); - $value[1] = $vobject->serialize(); - $objects[$key] = $value; + //OC_Log::write('core','OC_VCategories::delete, before: '.$key.': '.print_r($categories, true), OC_Log::DEBUG); + foreach($names as $name) { + $idx = $this->array_searchi($name, $categories); + OC_Log::write('core','OC_VCategories::delete, loop: '.$name.', '.print_r($idx, true), OC_Log::DEBUG); + if($idx !== false) { + OC_Log::write('core','OC_VCategories::delete, unsetting: '.$categories[$this->array_searchi($name, $categories)], OC_Log::DEBUG); + unset($categories[$this->array_searchi($name, $categories)]); + //unset($categories[$idx]); + } } + OC_Log::write('core','OC_VCategories::delete, after: '.$key.': '.print_r($categories, true), OC_Log::DEBUG); + $vobject->setString('CATEGORIES', implode(',', $categories)); + $value[1] = $vobject->serialize(); + $objects[$key] = $value; } else { - OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG); + OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG); } } } |