plugins/kolab_config plugins/kolab_tags plugins/libkolab

Aleksander Machniak machniak at kolabsys.com
Thu Jul 31 09:49:57 CEST 2014


 plugins/kolab_config/kolab_config.php         |   75 ++++------
 plugins/kolab_config/package.xml              |    6 
 plugins/kolab_tags/lib/kolab_tags_backend.php |  113 ++-------------
 plugins/libkolab/lib/kolab_storage_config.php |  186 ++++++++++++++++++++++++++
 4 files changed, 236 insertions(+), 144 deletions(-)

New commits:
commit d33af1fac53e04d98bf1775dae552746861778dd
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Thu Jul 31 03:49:28 2014 -0400

    Add kolab_storage_config class for managing configuration objects

diff --git a/plugins/kolab_config/kolab_config.php b/plugins/kolab_config/kolab_config.php
index 78979ca..d3e6636 100644
--- a/plugins/kolab_config/kolab_config.php
+++ b/plugins/kolab_config/kolab_config.php
@@ -31,10 +31,11 @@ class kolab_config extends rcube_plugin
     public $task = 'utils';
 
     private $enabled;
-    private $default;
-    private $folders;
+    private $config;
     private $dicts = array();
 
+    const O_TYPE = 'dictionary';
+
     /**
      * Required startup method of a Roundcube plugin
      */
@@ -54,6 +55,8 @@ class kolab_config extends rcube_plugin
         $this->add_hook('saved_search_list', array($this, 'saved_search_list'));
         $this->add_hook('saved_search_get', array($this, 'saved_search_get'));
 */
+
+        // @TODO: responses (snippets)
     }
 
     /**
@@ -61,28 +64,14 @@ class kolab_config extends rcube_plugin
      */
     private function load()
     {
-        // nothing to be done here
-        if (isset($this->folders))
+        if ($this->loaded) {
             return;
-
-        $this->require_plugin('libkolab');
-
-        $this->folders = kolab_storage::get_folders('configuration');
-        foreach ($this->folders as $folder) {
-            if ($folder->default) {
-                $this->default = $folder;
-                break;
-            }
         }
 
-        // if no folder is set as default, choose the first one
-        if (!$this->default)
-            $this->default = reset($this->folders);
+        $this->require_plugin('libkolab');
 
-        // check if configuration folder exist
-        if ($this->default && $this->default->name) {
-            $this->enabled = true;
-        }
+        $this->config = kolab_storage_config::get_instance();
+        $this->loaded = true;
     }
 
     /**
@@ -96,24 +85,23 @@ class kolab_config extends rcube_plugin
     {
         $this->load();
 
-        if (!$this->enabled) {
+        if (!$this->config->is_enabled()) {
             return $args;
         }
 
         $lang = $args['language'];
         $dict = $this->read_dictionary($lang, true);
 
-        $dict['type']     = 'dictionary';
         $dict['language'] = $args['language'];
         $dict['e']        = $args['dictionary'];
 
         if (empty($dict['e'])) {
             // Delete the object
-            $this->default->delete($dict);
+            $this->config->delete($dict['uid']);
         }
         else {
             // Update the object
-            $this->default->save($dict, 'configuration.dictionary', $dict['uid']);
+            $this->config->save($dict, self::O_TYPE, $dict['uid']);
         }
 
         $args['abort'] = true;
@@ -132,7 +120,7 @@ class kolab_config extends rcube_plugin
     {
         $this->load();
 
-        if (!$this->enabled) {
+        if (!$this->config->is_enabled()) {
             return $args;
         }
 
@@ -153,33 +141,30 @@ class kolab_config extends rcube_plugin
      *
      * @param string The language (2 chars) to load
      * @param boolean Only load objects from default folder
+     *
      * @return array Dictionary object as hash array
      */
     private function read_dictionary($lang, $default = false)
     {
-        if (isset($this->dicts[$lang]))
+        if (isset($this->dicts[$lang])) {
             return $this->dicts[$lang];
+        }
 
-        $query = array(array('type','=','dictionary'), array('tags','=',$lang));
-
-        foreach ($this->folders as $folder) {
-            // we only want to read from default folder
-            if ($default && !$folder->default)
-                continue;
-
-            foreach ($folder->select($query) as $object) {
-                if ($object['type'] == 'dictionary' && ($object['language'] == $lang || $object['language'] == 'XX')) {
-                    if (is_array($this->dicts[$lang]))
-                        $this->dicts[$lang]['e'] = array_merge((array)$this->dicts[$lang]['e'], $object['e']);
-                    else
-                        $this->dicts[$lang] = $object;
-
-                    // make sure the default object is cached
-                    if ($folder->default && $object['language'] != 'XX') {
-                        $object['e'] = $this->dicts[$lang]['e'];
-                        $this->dicts[$lang] = $object;
-                    }
+        $query = array(array('type','=',self::O_TYPE), array('tags','=',$lang));
+
+        foreach ($this->config->get_objects($query, $default) as $object) {
+            if ($object['language'] == $lang || $object['language'] == 'XX') {
+                if (is_array($this->dicts[$lang]))
+                    $this->dicts[$lang]['e'] = array_merge((array)$this->dicts[$lang]['e'], $object['e']);
+                else
+                    $this->dicts[$lang] = $object;
+/*
+                // make sure the default object is cached
+                if ($folder->default && $object['language'] != 'XX') {
+                    $object['e'] = $this->dicts[$lang]['e'];
+                    $this->dicts[$lang] = $object;
                 }
+*/
             }
         }
 
diff --git a/plugins/kolab_config/package.xml b/plugins/kolab_config/package.xml
index ac293b0..5e6d307 100644
--- a/plugins/kolab_config/package.xml
+++ b/plugins/kolab_config/package.xml
@@ -16,10 +16,10 @@
 		<email>machniak at kolabsys.com</email>
 		<active>yes</active>
 	</lead>
-	<date>2012-05-23</date>
+	<date>2014-07-31</date>
 	<version>
-		<release>2.0</release>
-		<api>2.0</api>
+		<release>3.0</release>
+		<api>3.0</api>
 	</version>
 	<stability>
 		<release>stable</release>
diff --git a/plugins/kolab_tags/lib/kolab_tags_backend.php b/plugins/kolab_tags/lib/kolab_tags_backend.php
index d79a6bf..032215d 100644
--- a/plugins/kolab_tags/lib/kolab_tags_backend.php
+++ b/plugins/kolab_tags/lib/kolab_tags_backend.php
@@ -23,52 +23,13 @@
 
 class kolab_tags_backend
 {
-    private $folders;
     private $tag_cols = array('name', 'category', 'color', 'parent', 'iconName', 'priority', 'members');
 
-    const FOLDER_TYPE  = 'configuration';
-    const OBJECT_TYPE  = 'relation';
-    const CATEGORY     = 'tag';
+    const O_TYPE     = 'relation';
+    const O_CATEGORY = 'tag';
 
 
     /**
-     * Class constructor
-     */
-    public function __construct()
-    {
-    }
-
-    /**
-     * Initializes config object and dependencies
-     */
-    private function load()
-    {
-        // nothing to be done here
-        if (isset($this->folders)) {
-            return;
-        }
-
-        $this->folders = kolab_storage::get_folders(self::FOLDER_TYPE);
-
-        foreach ($this->folders as $folder) {
-            if ($folder->default) {
-                $this->default = $folder;
-                break;
-            }
-        }
-
-        // if no folder is set as default, choose the first one
-        if (!$this->default) {
-            $this->default = reset($this->folders);
-        }
-
-        // check if configuration folder exist
-        if ($this->default && $this->default->name) {
-            $this->enabled = true;
-        }
-    }
-
-    /**
      * Tags list
      *
      * @param array $filter Search filter
@@ -77,27 +38,12 @@ class kolab_tags_backend
      */
     public function list_tags($filter = array())
     {
-        $this->load();
-
-        $default  = true;
-        $taglist  = array();
-        $filter[] = array('type', '=', self::OBJECT_TYPE);
-
-        foreach ($this->folders as $folder) {
-            // we only want to read from default folder
-            if ($default && !$folder->default) {
-                continue;
-            }
-
-            foreach ($folder->select($filter) as $object) {
-                if ($object['category'] == self::CATEGORY) {
-                    // @TODO: we need uid, name, color and members only?
-                    $taglist[] = $object;
-                }
-            }
-        }
+        $config     = kolab_storage_config::get_instance();
+        $default    = true;
+        $filter[]   = array('type', '=', self::O_TYPE);
+        $cat_filter = array('category' => self::O_CATEGORY);
 
-        return $taglist;
+        return $config->get_objects($filter, $default, $cat_filter);
     }
 
     /**
@@ -109,18 +55,12 @@ class kolab_tags_backend
      */
     public function create($tag)
     {
-        $this->load();
-
-        if (!$this->default) {
-            return false;
-        }
-
-        $tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
-        $tag['type']     = 'relation';
-        $tag['category'] = self::CATEGORY;
+        $config = kolab_storage_config::get_instance();
+        $tag    = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
+        $tag['category'] = self::O_CATEGORY;
 
         // Create the object
-        $result = $this->default->save($tag, self::FOLDER_TYPE . '.' . self::OBJECT_TYPE);
+        $result = $config->save($tag, self::O_TYPE);
 
         return $result ? $tag : false;
     }
@@ -142,18 +82,12 @@ class kolab_tags_backend
             return false;
         }
 
-        $tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
-        $tag = array_merge($old_tag, $tag);
-
-        // find folder object
-        foreach ($this->folders as $folder) {
-            if ($folder->name == $tag['_mailbox']) {
-                break;
-            }
-        }
+        $config = kolab_storage_config::get_instance();
+        $tag    = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
+        $tag    = array_merge($old_tag, $tag);
 
         // Update the object
-        $result = $folder->save($tag, self::FOLDER_TYPE . '.' . self::OBJECT_TYPE, $tag['uid']);
+        $result = $config->save($tag, self::O_TYPE, $tag['uid']);
 
         return $result ? $tag : false;
     }
@@ -167,21 +101,8 @@ class kolab_tags_backend
      */
     public function remove($uid)
     {
-        // get tag object data, we need _mailbox
-        $list = $this->list_tags(array(array('uid', '=', $uid)));
-        $tag  = $list[0];
-
-        if (!$tag) {
-            return false;
-        }
-
-        // find folder object
-        foreach ($this->folders as $folder) {
-            if ($folder->name == $tag['_mailbox']) {
-                break;
-            }
-        }
+        $config = kolab_storage_config::get_instance();
 
-        return $folder->delete($uid);
+        return $config->delete($uid);
     }
 }
diff --git a/plugins/libkolab/lib/kolab_storage_config.php b/plugins/libkolab/lib/kolab_storage_config.php
new file mode 100644
index 0000000..b5fdafe
--- /dev/null
+++ b/plugins/libkolab/lib/kolab_storage_config.php
@@ -0,0 +1,186 @@
+<?php
+
+/**
+ * Kolab storage class providing access to configuration objects on a Kolab server.
+ *
+ * @version @package_version@
+ * @author Thomas Bruederli <bruederli at kolabsys.com>
+ * @author Aleksander Machniak <machniak at kolabsys.com>
+ *
+ * Copyright (C) 2012-2014, Kolab Systems AG <contact at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class kolab_storage_config
+{
+    const FOLDER_TYPE = 'configuration';
+
+    /**
+     * Singleton instace of kolab_storage_config
+     *
+     * @var kolab_storage_config
+     */
+    static protected $instance;
+
+    private $folders;
+    private $default;
+    private $enabled;
+
+
+    /**
+     * This implements the 'singleton' design pattern
+     *
+     * @return kolab_storage_config The one and only instance
+     */
+    static function get_instance()
+    {
+        if (!self::$instance) {
+            self::$instance = new kolab_storage_config();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * Private constructor
+     */
+    private function __construct()
+    {
+        $this->folders = kolab_storage::get_folders(self::FOLDER_TYPE);
+
+        foreach ($this->folders as $folder) {
+            if ($folder->default) {
+                $this->default = $folder;
+                break;
+            }
+        }
+
+        // if no folder is set as default, choose the first one
+        if (!$this->default) {
+            $this->default = reset($this->folders);
+        }
+
+        // check if configuration folder exist
+        if ($this->default && $this->default->name) {
+            $this->enabled = true;
+        }
+    }
+
+    /**
+     * Check wether any configuration storage (folder) exists
+     *
+     * @return bool
+     */
+    public function is_enabled()
+    {
+        return $this->enabled;
+    }
+
+    /**
+     * Get configuration objects
+     *
+     * @param array $filter      Search filter
+     * @param bool  $default     Enable to get objects only from default folder
+     * @param array $data_filter Additional object data filter
+     *
+     * @return array List of objects
+     */
+    public function get_objects($filter = array(), $default = false, $data_filter = array())
+    {
+        $list = array();
+
+        foreach ($this->folders as $folder) {
+            // we only want to read from default folder
+            if ($default && !$folder->default) {
+                continue;
+            }
+
+            foreach ($folder->select($filter) as $object) {
+                foreach ($data_filter as $key => $val) {
+                    if ($object[$key] != $val) {
+                        continue 2;
+                    }
+                }
+
+                $list[] = $object;
+            }
+        }
+
+        return $list;
+    }
+
+    /**
+     * Create/update configuration object
+     *
+     * @param array  $object Object data
+     * @param string $type   Object type
+     *
+     * @return bool True on success, False on failure
+     */
+    public function save($object, $type)
+    {
+        if (!$this->enabled) {
+            return false;
+        }
+
+        $folder = $this->find_folder($object);
+
+        $object['type'] = $type;
+
+        return $folder->save($object, self::FOLDER_TYPE . '.' . $type, $object['uid']);
+    }
+
+    /**
+     * Remove configuration object
+     *
+     * @param string $uid Object UID
+     *
+     * @return bool True on success, False on failure
+     */
+    public function delete($uid)
+    {
+        // fetch the object to find folder
+        $list   = $this->get_objects(array(array('uid', '=', $uid)));
+        $object = $list[0];
+
+        if (!$object) {
+            return false;
+        }
+
+        $folder = $this->find_folder($object);
+
+        return $folder->delete($uid);
+    }
+
+    /**
+     * Find folder
+     */
+    private function find_folder($object = array())
+    {
+        // find folder object
+        if ($object['_mailbox']) {
+            foreach ($this->folders as $folder) {
+                if ($folder->name == $object['_mailbox']) {
+                    break;
+                }
+            }
+        }
+        else {
+            $folder = $this->default;
+        }
+
+        return $folder;
+    }
+}




More information about the commits mailing list