From a807768c329badbbc3559959acf7a38ca430cab0 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Mon, 13 Oct 2014 12:38:57 +0200 Subject: [PATCH] Move email link/relation handling functions to libkolab for common use --- plugins/kolab_notes/kolab_notes.php | 78 +-------------- plugins/libkolab/lib/kolab_storage_config.php | 94 +++++++++++++++++++ 2 files changed, 99 insertions(+), 73 deletions(-) diff --git a/plugins/kolab_notes/kolab_notes.php b/plugins/kolab_notes/kolab_notes.php index 259f26d6..369f1c58 100644 --- a/plugins/kolab_notes/kolab_notes.php +++ b/plugins/kolab_notes/kolab_notes.php @@ -998,46 +998,9 @@ class kolab_notes extends rcube_plugin private function save_links($uid, $links) { - $config = kolab_storage_config::get_instance(); - $search = kolab_storage_config::build_member_url($uid); - $relations = $this->get_relations($uid); - - // @TODO: here we support only one-way relations, i.e. - // such relation can contain only note and mail members - // So, when we remove a note member the whole relation - // will be removed - - foreach ($relations as $relation) { - if (empty($links)) { - $config->delete($relation['uid']); - } - else { - // make relation members up-to-date - kolab_storage_config::resolve_members($relation); - - // assign all links to one relation, others will be removed - $members = array_merge($links, array($search)); - $diff1 = array_diff($members, $relation['members']); - $diff1 = array_diff($relation['members'], $members); - - if (count($diff1) || count($diff2)) { - $relation['members'] = $members; - $config->save($relation, 'relation'); - } - - $links = null; - } - } - - // create a new relation - if (!empty($links)) { - $relation = array( - 'members' => array_merge($links, array($search)), - 'category' => 'generic', - ); - - $config->save($relation, 'relation'); - } + $config = kolab_storage_config::get_instance(); + $remove = array_diff($config->get_object_links($uid), $links); + return $config->save_object_links($uid, $links, $remove); } /** @@ -1045,23 +1008,8 @@ class kolab_notes extends rcube_plugin */ private function get_links($uid) { - $result = array(); - $search = kolab_storage_config::build_member_url($uid); - - foreach ($this->get_relations($uid) as $relation) { - if (in_array($search, (array) $relation['members'])) { - // make relation members up-to-date - kolab_storage_config::resolve_members($relation); - - foreach ($relation['members'] as $member) { - if ($member != $search) { - $result[] = $member; - } - } - } - } - - return array_unique($result); + $config = kolab_storage_config::get_instance(); + return $config->get_object_links($uid); } /** @@ -1091,22 +1039,6 @@ class kolab_notes extends rcube_plugin return $result; } - /** - * Find relation objects referring to specified note - */ - private function get_relations($uid) - { - $config = kolab_storage_config::get_instance(); - $default = true; - $filter = array( - array('type', '=', 'relation'), - array('category', '=', 'generic'), - array('member', '=', $uid), - ); - - return $config->get_objects($filter, $default, 100); - } - /** * Resolve the email message reference from the given URI */ diff --git a/plugins/libkolab/lib/kolab_storage_config.php b/plugins/libkolab/lib/kolab_storage_config.php index 8a0fab5c..35d0d624 100644 --- a/plugins/libkolab/lib/kolab_storage_config.php +++ b/plugins/libkolab/lib/kolab_storage_config.php @@ -653,6 +653,100 @@ class kolab_storage_config return $result; } + /** + * Find objects linked with the given groupware object through a relation + * + * @param string Object UUID + * @param array List of related URIs + */ + public function get_object_links($uid) + { + $links = array(); + $object_uri = self::build_member_url($uid); + + foreach ($this->get_relations_for_member($uid) as $relation) { + if (in_array($object_uri, (array) $relation['members'])) { + // make relation members up-to-date + kolab_storage_config::resolve_members($relation); + + foreach ($relation['members'] as $member) { + if ($member != $object_uri) { + $links[] = $member; + } + } + } + } + + return array_unique($links); + } + + /** + * + */ + public function save_object_links($uid, $links, $remove = array()) + { + $object_uri = self::build_member_url($uid); + $relations = $this->get_relations_for_member($uid); + $done = false; + + foreach ($relations as $relation) { + // make relation members up-to-date + kolab_storage_config::resolve_members($relation); + + // remove and add links + $members = array_diff($relation['members'], (array)$remove); + $members = array_unique(array_merge($members, $links)); + + // make sure the object_uri is still a member + if (!in_array($object_uri, $members)) { + $members[$obejct_uri]; + } + + // remove relation if no other members remain + if (count($members) <= 1) { + $done = $this->delete($relation['uid']); + } + // update relation object if members changed + else if (count(array_diff($members, $relation['members'])) || count(array_diff($relation['members'], $members))) { + $relation['members'] = $members; + $done = $this->save($relation, 'relation'); + $links = array(); + } + // no changes, we're happy + else { + $done = true; + $links = array(); + } + } + + // create a new relation + if (!$done) { + $relation = array( + 'members' => array_merge($links, array($object_uri)), + 'category' => 'generic', + ); + + $ret = $this->save($relation, 'relation'); + } + + return $ret; + } + + /** + * Find relation objects referring to specified note + */ + public function get_relations_for_member($uid, $reltype = 'generic') + { + $default = true; + $filter = array( + array('type', '=', 'relation'), + array('category', '=', $reltype), + array('member', '=', $uid), + ); + + return $this->get_objects($filter, $default, 100); + } + /** * Find kolab objects assigned to specified e-mail message *