From edaad9b47c16952ba9245e85fda26c0ca363c063 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Wed, 14 Mar 2012 13:57:02 +0100 Subject: [PATCH] Support contact undelete with new storage classes; only list undeleted imap objects --- .../lib/rcube_kolab_contacts.php | 49 +++---------------- plugins/libkolab/lib/kolab_format_contact.php | 2 +- plugins/libkolab/lib/kolab_storage.php | 27 +++++++++- plugins/libkolab/lib/kolab_storage_folder.php | 27 ++++++++-- 4 files changed, 57 insertions(+), 48 deletions(-) diff --git a/plugins/kolab_addressbook/lib/rcube_kolab_contacts.php b/plugins/kolab_addressbook/lib/rcube_kolab_contacts.php index 7fe80794..5609f6cb 100644 --- a/plugins/kolab_addressbook/lib/rcube_kolab_contacts.php +++ b/plugins/kolab_addressbook/lib/rcube_kolab_contacts.php @@ -632,55 +632,22 @@ class rcube_kolab_contacts extends rcube_addressbook if (!is_array($ids)) $ids = explode(',', $ids); - $count = 0; - $uids = array(); -/* - TODO: re-implement this using kolab_storage_folder::undelete(); - - $imap_uids = $_SESSION['kolab_delete_uids']; - - // convert contact IDs into IMAP UIDs - foreach ($ids as $id) - if ($uid = $imap_uids[$id]) - $uids[] = $uid; - - if (!empty($uids)) { - $session = &Horde_Kolab_Session::singleton(); - $imap = &$session->getImap(); - - if (is_object($imap) && is_a($imap, 'PEAR_Error')) { - $error = $imap; + $count = 0; + foreach ($ids as $id) { + $uid = $this->_id2uid($id); + if ($this->storagefolder->undelete($uid)) { + $count++; } else { - $result = $imap->select($this->imap_folder); - if (is_object($result) && is_a($result, 'PEAR_Error')) { - $error = $result; - } - else { - $result = $imap->undeleteMessages(implode(',', $uids)); - if (is_object($result) && is_a($result, 'PEAR_Error')) { - $error = $result; - } - else { - $this->_connect(); - $this->storagefolder->synchronize(); - } - } - } - - if ($error) { raise_error(array( 'code' => 600, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, - 'message' => "Error undeleting a contact object(s) from the Kolab server:" . $error->getMessage()), + 'message' => "Error undeleting a contact object $uid from the Kolab server"), true, false); } - - $rcmail = rcmail::get_instance(); - $rcmail->session->remove('kolab_delete_uids'); } -*/ - return count($uids); + + return $count; } diff --git a/plugins/libkolab/lib/kolab_format_contact.php b/plugins/libkolab/lib/kolab_format_contact.php index 200caed5..48260b34 100644 --- a/plugins/libkolab/lib/kolab_format_contact.php +++ b/plugins/libkolab/lib/kolab_format_contact.php @@ -217,7 +217,7 @@ class kolab_format_contact extends kolab_format $this->obj->setPhoto('',''); } - // handle spouse, children, profession, initials, pgppublickey, etc. + // TODO: handle spouse, children, profession, initials, pgppublickey, etc. // cache this data $this->data = $object; diff --git a/plugins/libkolab/lib/kolab_storage.php b/plugins/libkolab/lib/kolab_storage.php index 9d00d336..51468992 100644 --- a/plugins/libkolab/lib/kolab_storage.php +++ b/plugins/libkolab/lib/kolab_storage.php @@ -62,7 +62,7 @@ class kolab_storage /** * Get a list of storage folders for the given data type * - * @param string Data type to list folders for (contact,event,task,note) + * @param string Data type to list folders for (contact,distribution-list,event,task,note) * * @return array List of Kolab_Folder objects (folder names in UTF7-IMAP) */ @@ -94,6 +94,31 @@ class kolab_storage } + /** + * Getter for a single Kolab object, identified by its UID. + * This will search all folders storing objects of the given type. + * + * @param string Object UID + * @param string Object type (contact,distribution-list,event,task,note) + * @return array The Kolab object represented as hash array or false if not found + */ + public static function get_object($uid, $type) + { + $folder = null; + foreach ((array)self::$imap->list_folders('', '*', $type) as $foldername) { + if (!$folder) + $folder = new kolab_storage_folder($foldername, self::$imap); + else + $folder->set_folder($foldername); + + if ($object = $folder->get_object($uid)) + return $object; + } + + return false; + } + + /** * */ diff --git a/plugins/libkolab/lib/kolab_storage_folder.php b/plugins/libkolab/lib/kolab_storage_folder.php index f087048d..4dd29a22 100644 --- a/plugins/libkolab/lib/kolab_storage_folder.php +++ b/plugins/libkolab/lib/kolab_storage_folder.php @@ -52,8 +52,20 @@ class kolab_storage_folder */ function __construct($name, $imap = null) { - $this->name = $name; $this->imap = is_object($imap) ? $imap : rcmail::get_instance()->get_storage(); + $this->imap->set_options(array('skip_deleted' => false)); + $this->set_folder($name); + } + + + /** + * Set the IMAP folder name this instance connects to + * + * @param string The folder name/path + */ + public function set_folder($name) + { + $this->name = $name; $this->imap->set_folder($this->name); $metadata = $this->imap->get_metadata($this->name, array(kolab_storage::CTYPE_KEY)); @@ -146,7 +158,7 @@ class kolab_storage_folder // search by object type $ctype = self::KTYPE_PREFIX . $type; - $index = $this->imap->search_once($this->name, 'HEADER X-Kolab-Type ' . $ctype); + $index = $this->imap->search_once($this->name, 'UNDELETED HEADER X-Kolab-Type ' . $ctype); return $index->count(); } @@ -163,7 +175,7 @@ class kolab_storage_folder // search by object type $ctype = self::KTYPE_PREFIX . $type; - $search = 'HEADER X-Kolab-Type ' . $ctype; + $search = 'UNDELETED HEADER X-Kolab-Type ' . $ctype; $index = $this->imap->search_once($this->name, $search); $results = array(); @@ -194,7 +206,7 @@ class kolab_storage_folder if ($msguid && ($object = $this->read_object($msguid))) return $object; - return array('uid' => $uid); + return false; } @@ -380,7 +392,12 @@ class kolab_storage_folder */ public function undelete($uid) { - // TODO: implement this + if ($msguid = $this->uid2msguid($uid)) { + if ($this->imap->set_flag($msguid, 'UNDELETED', $this->name)) { + return $msguid; + } + } + return false; }