summaryrefslogtreecommitdiffstats
path: root/lib/vcategories.php
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2012-02-20 13:16:51 +0100
committerThomas Tanghus <thomas@tanghus.net>2012-02-20 13:16:51 +0100
commit430ccef09cb303e1b2136cd0599f3baddf4acbcd (patch)
tree0eb6c820fd8f21aa072c5ba4c0ecd5a6eff313e7 /lib/vcategories.php
parentc62673d3601abdefba2ca81ac7d8f8d42fd7f6a5 (diff)
downloadnextcloud-server-430ccef09cb303e1b2136cd0599f3baddf4acbcd.tar.gz
nextcloud-server-430ccef09cb303e1b2136cd0599f3baddf4acbcd.zip
Added OC_VCategories::rescan()
Diffstat (limited to 'lib/vcategories.php')
-rw-r--r--lib/vcategories.php55
1 files changed, 45 insertions, 10 deletions
diff --git a/lib/vcategories.php b/lib/vcategories.php
index 6cc7511e656..63ed367230b 100644
--- a/lib/vcategories.php
+++ b/lib/vcategories.php
@@ -36,18 +36,21 @@ class OC_VCategories {
*/
private $categories = array();
- private $app = '';
+ private $app = null;
+ private $user = null;
/**
* @brief Constructor.
* @param $app The application identifier e.g. 'contacts' or 'calendar'.
+ * @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.
*/
public function __construct($app, $user=null) {
$this->app = $app;
if(is_null($user)) {
- $user = OC_User::getUser();
+ $this->user = OC_User::getUser();
}
- $this->categories = OC_VObject::unescapeSemicolons(OC_Preferences::getValue($user, $app, 'extra categories', ''));
+ $this->categories = OC_VObject::unescapeSemicolons(OC_Preferences::getValue($this->user, $app, 'extra categories', ''));
}
/**
@@ -72,8 +75,8 @@ class OC_VCategories {
* @param $names A string with a name or an array of strings containing the name(s) of the categor(y|ies) to add.
* @returns bool Returns false on error.
*/
- public function add($names) {
- $user = OC_User::getUser();
+ public function add($names, $sync=true) {
+ $user = is_null($this->user) ? OC_User::getUser() : $this->user;
$newones = array();
if(!is_array($names)) {
$names = array($names);
@@ -86,7 +89,9 @@ class OC_VCategories {
}
if(count($newones) > 0) {
$this->categories = $this->cleanArray(array_merge($this->categories, $newones));
- OC_Preferences::setValue(OC_User::getUser(), $this->app, 'extra categories', OC_VObject::escapeSemicolons($this->categories));
+ if($sync) {
+ OC_Preferences::setValue($user, $this->app, 'extra categories', OC_VObject::escapeSemicolons($this->categories));
+ }
natcasesort($this->categories); // Dunno if this is necessary
}
return true;
@@ -97,8 +102,38 @@ class OC_VCategories {
* @param $vobject The instance of OC_VObject to load the categories from.
* @returns bool Returns false if the name already exist (case insensitive) or on error.
*/
- public function loadFromVObject($vobject) {
- $this->add($vobject->getAsArray('CATEGORIES'));
+ public function loadFromVObject($vobject, $sync=true) {
+ $this->add($vobject->getAsArray('CATEGORIES'), $sync);
+ }
+
+ /**
+ * @brief Reset saved categories and rescan supplied vobjects for categories.
+ * @param $objects An array of vobjects (as text).
+ * To get the object array, do something like:
+ * // For Addressbook:
+ * $categories = new OC_VCategories('contacts');
+ * $stmt = OC_DB::prepare( 'SELECT carddata FROM *PREFIX*contacts_cards' );
+ * $result = $stmt->execute();
+ * $objects = array();
+ * if(!is_null($result)) {
+ * while( $row = $result->fetchRow()){
+ * $objects[] = $row['carddata'];
+ * }
+ * }
+ * $categories->rescan($objects);
+ */
+ public function rescan($objects) {
+ $user = is_null($this->user) ? OC_User::getUser() : $this->user;
+ $this->categories = array();
+ foreach($objects as $object) {
+ $vobject = OC_VObject::parse($object);
+ if(!is_null($vobject)){
+ $this->loadFromVObject($vobject, false);
+ } else {
+ OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG);
+ }
+ }
+ OC_Preferences::setValue($user, $this->app, 'extra categories', OC_VObject::escapeSemicolons($this->categories));
}
/**
@@ -107,7 +142,7 @@ class OC_VCategories {
* @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) {
- $user = OC_User::getUser();
+ $user = is_null($this->user) ? OC_User::getUser() : $this->user;
if(!$this->hasCategory($name)) {
return;
}
@@ -126,7 +161,7 @@ class OC_VCategories {
$objects[$key] = $value;
}
} else {
- OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 10).'(...)', OC_Log::DEBUG);
+ OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG);
}
}
}