summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2014-10-13 10:38:57 (GMT)
committerThomas Bruederli <bruederli@kolabsys.com>2014-10-13 10:38:57 (GMT)
commita807768c329badbbc3559959acf7a38ca430cab0 (patch)
tree899e192190bfb360fa4e809a50cc0272a6881ccc
parentab12a5c86780ce97a14612513fdb292d2f8e1a3c (diff)
downloadroundcubemail-plugins-kolab-a807768c329badbbc3559959acf7a38ca430cab0.tar.gz
Move email link/relation handling functions to libkolab for common use
-rw-r--r--plugins/kolab_notes/kolab_notes.php78
-rw-r--r--plugins/libkolab/lib/kolab_storage_config.php94
2 files changed, 99 insertions, 73 deletions
diff --git a/plugins/kolab_notes/kolab_notes.php b/plugins/kolab_notes/kolab_notes.php
index 259f26d..369f1c5 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);
}
/**
@@ -1092,22 +1040,6 @@ class kolab_notes extends rcube_plugin
}
/**
- * 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
*/
public function get_message_reference($uri, $resolve = false)
diff --git a/plugins/libkolab/lib/kolab_storage_config.php b/plugins/libkolab/lib/kolab_storage_config.php
index 8a0fab5..35d0d62 100644
--- a/plugins/libkolab/lib/kolab_storage_config.php
+++ b/plugins/libkolab/lib/kolab_storage_config.php
@@ -654,6 +654,100 @@ class kolab_storage_config
}
/**
+ * 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
*
* @param rcube_message $message E-mail message