summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorthomasb <thomasb@208e9e7b-5314-0410-a742-e7e81cd9613c>2010-11-24 18:50:29 +0000
committerthomasb <thomasb@208e9e7b-5314-0410-a742-e7e81cd9613c>2010-11-24 18:50:29 +0000
commit806cf194321a5d644cc0195834118af1597a1e71 (patch)
tree982f7878cb05b05da0718a4ad62151acca5dd073 /plugins
parentbb7b2b628a80f18db2dba6863c0e6746662b42b0 (diff)
Implement insert/delete of Kolab contacts
git-svn-id: https://svn.roundcube.net/trunk@4266 208e9e7b-5314-0410-a742-e7e81cd9613c
Diffstat (limited to 'plugins')
-rw-r--r--plugins/kolab_addressbook/rcube_kolab_contacts.php155
1 files changed, 96 insertions, 59 deletions
diff --git a/plugins/kolab_addressbook/rcube_kolab_contacts.php b/plugins/kolab_addressbook/rcube_kolab_contacts.php
index c6ed4feed..797e96325 100644
--- a/plugins/kolab_addressbook/rcube_kolab_contacts.php
+++ b/plugins/kolab_addressbook/rcube_kolab_contacts.php
@@ -32,7 +32,7 @@ class rcube_kolab_contacts extends rcube_addressbook
'phone' => array(),
'im' => array('limit' => 1),
'website' => array('limit' => 1, 'subtypes' => null),
- 'address' => array('limit' => 2, 'subtypes' => array('home','work')),
+ 'address' => array('limit' => 2, 'subtypes' => array('home','business')),
'notes' => array(),
// define additional coltypes
'initials' => array('type' => 'text', 'size' => 6, 'limit' => 1),
@@ -53,6 +53,8 @@ class rcube_kolab_contacts extends rcube_addressbook
private $result;
private $imap_folder = 'INBOX/Contacts';
private $gender_map = array(0 => 'male', 1 => 'female');
+ private $phonetypemap = array('home' => 'home1', 'work' => 'business1', 'work2' => 'business2', 'workfax' => 'businessfax');
+ private $addresstypemap = array('work' => 'business');
private $fieldmap = array(
// kolab => roundcube
'full-name' => 'name',
@@ -82,6 +84,7 @@ class rcube_kolab_contacts extends rcube_addressbook
// extend coltypes configuration
$format = rcube_kolab::get_format('contact');
$this->coltypes['phone']['subtypes'] = $format->_phone_types;
+ $this->coltypes['address']['subtypes'] = $format->_address_types;
$this->coltypes['anniversary']['label'] = rcube_label('anniversary');
// fetch objects from the given IMAP folder
@@ -200,8 +203,8 @@ class rcube_kolab_contacts extends rcube_addressbook
*/
public function search($fields, $value, $strict=false, $select=true)
{
- // TODO: currently not implemented, just list all records
- return $this->list_records();
+ // TODO: currently not implemented
+ return new rcube_result_set(0, ($this->list_page-1) * $this->page_size);
}
@@ -281,41 +284,47 @@ class rcube_kolab_contacts extends rcube_addressbook
*/
public function insert($save_data, $check=false)
{
- if (is_object($save_data) && is_a($save_data, rcube_result_set))
- return $this->insert_recset($save_data, $check);
+ if (!is_array($save_data))
+ return false;
$insert_id = $existing = false;
// check for existing records by e-mail comparison
if ($check) {
- foreach ($this->_get_col_values('email', $save_data, true) as $email) {
- if ($existing = $this->search('email', $email, true, false))
+ foreach ($this->get_col_values('email', $save_data, true) as $email) {
+ if (($res = $this->search('email', $email, true, false)) && $res->count) {
+ $existing = true;
break;
+ }
}
}
- $object = $this->_from_rcube_contact($save_data);
- var_dump($object);
-
- // TODO: how to create new Kolab objects?
-
-
- return $insert_id;
- }
+ if (!$existing) {
+ // generate new Kolab contact item
+ $object = $this->_from_rcube_contact($save_data);
+ $object['uid'] = $this->contactstorage->generateUID();
+ $object['creation-date'] = time();
+ $object['last-modification-date'] = time();
- /**
- * Insert new contacts for each row in set
- *
- * @see rcube_kolab_contacts::insert()
- */
- private function insert_recset($result, $check=false)
- {
- $ids = array();
- while ($row = $result->next()) {
- if ($insert = $this->insert($row, $check))
- $ids[] = $insert;
+ $saved = $this->contactstorage->save($object);
+
+ if (PEAR::isError($saved)) {
+ raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Error saving contact object to Kolab server:" . $saved->getMessage()),
+ true, false);
+ }
+ else {
+ $contact = $this->_to_rcube_contact($object);
+ $id = $contact['ID'];
+ $this->contacts[$id] = $contact;
+ $this->id2uid[$id] = $object['uid'];
+ $insert_id = $id;
+ }
}
- return $ids;
+
+ return $insert_id;
}
@@ -336,7 +345,7 @@ class rcube_kolab_contacts extends rcube_addressbook
$old = $this->contactstorage->getObject($uid);
$object = array_merge($old, $this->_from_rcube_contact($save_data));
$object['last-modification-date'] = time();
-
+console($save_data, $object);
$saved = $this->contactstorage->save($object, $uid);
if (PEAR::isError($saved)) {
raise_error(array(
@@ -361,7 +370,31 @@ class rcube_kolab_contacts extends rcube_addressbook
*/
public function delete($ids)
{
+ $this->_fetch_contacts();
+
+ if (!is_array($ids))
+ $ids = explode(',', $ids);
+ $count = 0;
+ foreach ($ids as $id) {
+ if ($uid = $this->id2uid[$id]) {
+ $deleted = $this->contactstorage->delete($uid);
+
+ if (PEAR::isError($deleted)) {
+ raise_error(array(
+ 'code' => 600, 'type' => 'php',
+ 'file' => __FILE__, 'line' => __LINE__,
+ 'message' => "Error deleting a contact object from the Kolab server:" . $deleted->getMessage()),
+ true, false);
+ }
+ else {
+ unset($this->contacts[$id], $this->id2uid[$id]);
+ $count++;
+ }
+ }
+ }
+
+ return $count;
}
/**
@@ -369,7 +402,7 @@ class rcube_kolab_contacts extends rcube_addressbook
*/
public function delete_all()
{
- /* empty for read-only address books */
+ return !PEAR::isError($this->contactstorage->deleteAll());
}
@@ -498,6 +531,8 @@ class rcube_kolab_contacts extends rcube_addressbook
foreach (array_flip($this->fieldmap) as $rcube => $kolab) {
if (isset($contact[$rcube]))
$object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube];
+ else if ($rcube .= ':home' && isset($contact[$rcube]))
+ $object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube];
}
// format dates
@@ -510,48 +545,50 @@ class rcube_kolab_contacts extends rcube_addressbook
if (isset($contact['gender']))
$object['gender'] = $gendermap[$contact['gender']];
- foreach (($emails = $this->_get_col_values('email', $contact, true)) as $email)
- $object['email'][] = array('smtp-address' => $email, 'display-name' => $object['full-name']);
+ $emails = $this->get_col_values('email', $contact, true);
$object['emails'] = join(', ', $emails);
- foreach ($this->_get_col_values('phone', $contact) as $type => $values) {
+ foreach ($this->get_col_values('phone', $contact) as $type => $values) {
+ if ($this->phonetypemap[$type])
+ $type = $this->phonetypemap[$type];
foreach ((array)$values as $phone)
$object['phone'][] = array('number' => $phone, 'type' => $type);
}
- foreach ($this->_get_col_values('address', $contact) as $type => $values) {
+ foreach ($this->get_col_values('address', $contact) as $type => $values) {
+ if ($this->addresstypemap[$type])
+ $type = $this->addresstypemap[$type];
+
+ $basekey = 'addr-' . $type . '-';
foreach ((array)$values as $adr) {
- $object['address'][] = array(
- 'type' => $type,
- 'street' => $adr['street'],
- 'locality' => $adr['locality'],
- 'postal-code' => $adr['zipcode'],
- 'region' => $adr['region'],
- 'country' => $adr['country'],
- );
- }
- }
-
- return $object;
- }
-
-
- private function _get_col_values($col, $data, $flat = false)
- {
- $out = array();
- foreach ($data as $c => $values) {
- if (strpos($c, $col) === 0) {
- if ($flat) {
- $out = array_merge($out, (array)$values);
+ // switch type if slot is already taken
+ if (isset($object[$basekey . 'type'])) {
+ $type = $type == 'home' ? 'business' : 'home';
+ $basekey = 'addr-' . $type . '-';
+ }
+
+ if (!isset($object[$basekey . 'type'])) {
+ $object[$basekey . 'type'] = $type;
+ $object[$basekey . 'street'] = $adr['street'];
+ $object[$basekey . 'locality'] = $adr['locality'];
+ $object[$basekey . 'postal-code'] = $adr['zipcode'];
+ $object[$basekey . 'region'] = $adr['region'];
+ $object[$basekey . 'country'] = $adr['country'];
}
else {
- list($f, $type) = explode(':', $c);
- $out[$type] = array_merge((array)$out[$type], (array)$values);
+ $object['address'][] = array(
+ 'type' => $type,
+ 'street' => $adr['street'],
+ 'locality' => $adr['locality'],
+ 'postal-code' => $adr['zipcode'],
+ 'region' => $adr['region'],
+ 'country' => $adr['country'],
+ );
}
}
}
-
- return $out;
+
+ return $object;
}
}