Add kolab_storage_config class for managing configuration objects

This commit is contained in:
Aleksander Machniak 2014-07-31 03:49:28 -04:00
parent 5b7532a124
commit d33af1fac5
4 changed files with 235 additions and 143 deletions

View file

@ -31,10 +31,11 @@ class kolab_config extends rcube_plugin
public $task = 'utils'; public $task = 'utils';
private $enabled; private $enabled;
private $default; private $config;
private $folders;
private $dicts = array(); private $dicts = array();
const O_TYPE = 'dictionary';
/** /**
* Required startup method of a Roundcube plugin * 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_list', array($this, 'saved_search_list'));
$this->add_hook('saved_search_get', array($this, 'saved_search_get')); $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() private function load()
{ {
// nothing to be done here if ($this->loaded) {
if (isset($this->folders))
return; return;
}
$this->require_plugin('libkolab'); $this->require_plugin('libkolab');
$this->folders = kolab_storage::get_folders('configuration'); $this->config = kolab_storage_config::get_instance();
foreach ($this->folders as $folder) { $this->loaded = true;
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;
}
} }
/** /**
@ -96,24 +85,23 @@ class kolab_config extends rcube_plugin
{ {
$this->load(); $this->load();
if (!$this->enabled) { if (!$this->config->is_enabled()) {
return $args; return $args;
} }
$lang = $args['language']; $lang = $args['language'];
$dict = $this->read_dictionary($lang, true); $dict = $this->read_dictionary($lang, true);
$dict['type'] = 'dictionary';
$dict['language'] = $args['language']; $dict['language'] = $args['language'];
$dict['e'] = $args['dictionary']; $dict['e'] = $args['dictionary'];
if (empty($dict['e'])) { if (empty($dict['e'])) {
// Delete the object // Delete the object
$this->default->delete($dict); $this->config->delete($dict['uid']);
} }
else { else {
// Update the object // Update the object
$this->default->save($dict, 'configuration.dictionary', $dict['uid']); $this->config->save($dict, self::O_TYPE, $dict['uid']);
} }
$args['abort'] = true; $args['abort'] = true;
@ -132,7 +120,7 @@ class kolab_config extends rcube_plugin
{ {
$this->load(); $this->load();
if (!$this->enabled) { if (!$this->config->is_enabled()) {
return $args; return $args;
} }
@ -153,33 +141,30 @@ class kolab_config extends rcube_plugin
* *
* @param string The language (2 chars) to load * @param string The language (2 chars) to load
* @param boolean Only load objects from default folder * @param boolean Only load objects from default folder
*
* @return array Dictionary object as hash array * @return array Dictionary object as hash array
*/ */
private function read_dictionary($lang, $default = false) private function read_dictionary($lang, $default = false)
{ {
if (isset($this->dicts[$lang])) if (isset($this->dicts[$lang])) {
return $this->dicts[$lang]; return $this->dicts[$lang];
}
$query = array(array('type','=','dictionary'), array('tags','=',$lang)); $query = array(array('type','=',self::O_TYPE), array('tags','=',$lang));
foreach ($this->folders as $folder) { foreach ($this->config->get_objects($query, $default) as $object) {
// we only want to read from default folder if ($object['language'] == $lang || $object['language'] == 'XX') {
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])) if (is_array($this->dicts[$lang]))
$this->dicts[$lang]['e'] = array_merge((array)$this->dicts[$lang]['e'], $object['e']); $this->dicts[$lang]['e'] = array_merge((array)$this->dicts[$lang]['e'], $object['e']);
else else
$this->dicts[$lang] = $object; $this->dicts[$lang] = $object;
/*
// make sure the default object is cached // make sure the default object is cached
if ($folder->default && $object['language'] != 'XX') { if ($folder->default && $object['language'] != 'XX') {
$object['e'] = $this->dicts[$lang]['e']; $object['e'] = $this->dicts[$lang]['e'];
$this->dicts[$lang] = $object; $this->dicts[$lang] = $object;
} }
} */
} }
} }

View file

@ -16,10 +16,10 @@
<email>machniak@kolabsys.com</email> <email>machniak@kolabsys.com</email>
<active>yes</active> <active>yes</active>
</lead> </lead>
<date>2012-05-23</date> <date>2014-07-31</date>
<version> <version>
<release>2.0</release> <release>3.0</release>
<api>2.0</api> <api>3.0</api>
</version> </version>
<stability> <stability>
<release>stable</release> <release>stable</release>

View file

@ -23,51 +23,12 @@
class kolab_tags_backend class kolab_tags_backend
{ {
private $folders;
private $tag_cols = array('name', 'category', 'color', 'parent', 'iconName', 'priority', 'members'); private $tag_cols = array('name', 'category', 'color', 'parent', 'iconName', 'priority', 'members');
const FOLDER_TYPE = 'configuration'; const O_TYPE = 'relation';
const OBJECT_TYPE = 'relation'; const O_CATEGORY = 'tag';
const 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 * Tags list
* *
@ -77,27 +38,12 @@ class kolab_tags_backend
*/ */
public function list_tags($filter = array()) public function list_tags($filter = array())
{ {
$this->load(); $config = kolab_storage_config::get_instance();
$default = true; $default = true;
$taglist = array(); $filter[] = array('type', '=', self::O_TYPE);
$filter[] = array('type', '=', self::OBJECT_TYPE); $cat_filter = array('category' => self::O_CATEGORY);
foreach ($this->folders as $folder) { return $config->get_objects($filter, $default, $cat_filter);
// 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;
}
}
}
return $taglist;
} }
/** /**
@ -109,18 +55,12 @@ class kolab_tags_backend
*/ */
public function create($tag) public function create($tag)
{ {
$this->load(); $config = kolab_storage_config::get_instance();
if (!$this->default) {
return false;
}
$tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols)); $tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
$tag['type'] = 'relation'; $tag['category'] = self::O_CATEGORY;
$tag['category'] = self::CATEGORY;
// Create the object // 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; return $result ? $tag : false;
} }
@ -142,18 +82,12 @@ class kolab_tags_backend
return false; return false;
} }
$config = kolab_storage_config::get_instance();
$tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols)); $tag = array_intersect_key($tag, array_combine($this->tag_cols, $this->tag_cols));
$tag = array_merge($old_tag, $tag); $tag = array_merge($old_tag, $tag);
// find folder object
foreach ($this->folders as $folder) {
if ($folder->name == $tag['_mailbox']) {
break;
}
}
// Update the object // 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; return $result ? $tag : false;
} }
@ -167,21 +101,8 @@ class kolab_tags_backend
*/ */
public function remove($uid) public function remove($uid)
{ {
// get tag object data, we need _mailbox $config = kolab_storage_config::get_instance();
$list = $this->list_tags(array(array('uid', '=', $uid)));
$tag = $list[0];
if (!$tag) { return $config->delete($uid);
return false;
}
// find folder object
foreach ($this->folders as $folder) {
if ($folder->name == $tag['_mailbox']) {
break;
}
}
return $folder->delete($uid);
} }
} }

View file

@ -0,0 +1,186 @@
<?php
/**
* Kolab storage class providing access to configuration objects on a Kolab server.
*
* @version @package_version@
* @author Thomas Bruederli <bruederli@kolabsys.com>
* @author Aleksander Machniak <machniak@kolabsys.com>
*
* Copyright (C) 2012-2014, Kolab Systems AG <contact@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;
}
}