Skip to content

Commit

Permalink
[jan] Fix distribution list support in Kolab driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Nov 18, 2013
1 parent 0af8c18 commit cde0fb1
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 78 deletions.
1 change: 0 additions & 1 deletion turba/config/backends.php
Expand Up @@ -701,7 +701,6 @@
),
'export' => true,
'browse' => true,
'list_name_field' => 'lastname',
'use_shares' => true,
'all_shares' => true,
);
Expand Down
1 change: 1 addition & 0 deletions turba/docs/CHANGES
Expand Up @@ -2,6 +2,7 @@
v4.1.4-git
----------

[jan] Fix distribution list support in Kolab driver.


------
Expand Down
2 changes: 1 addition & 1 deletion turba/lib/Driver.php
Expand Up @@ -520,7 +520,7 @@ public function toTurbaKeys(array $entry)
$new_entry = array();
foreach ($this->map as $key => $val) {
if (!is_array($val)) {
$new_entry[$key] = (isset($entry[$val]) && strlen($entry[$val]))
$new_entry[$key] = (isset($entry[$val]) && (!empty($entry[$val]) || (is_string($entry[$val]) && strlen($entry[$val]))))
? trim($entry[$val])
: null;
}
Expand Down
210 changes: 137 additions & 73 deletions turba/lib/Driver/Kolab.php
Expand Up @@ -38,6 +38,13 @@ class Turba_Driver_Kolab extends Turba_Driver
*/
protected $_data;

/**
* The current addressbook, serving groups.
*
* @var Horde_Kolab_Storage_Data
*/
protected $_listData;

/**
* The current addressbook represented as share.
*
Expand Down Expand Up @@ -94,6 +101,14 @@ public function __construct($name = '', $params = array())
*/
public function toDriverKeys(array $hash)
{
if (isset($hash['__type']) && $hash['__type'] == 'Group') {
$name = $hash['name'];
unset($hash['name']);
$hash = parent::toDriverKeys($hash);
$hash['display-name'] = $name;
return $hash;
}

$hash = parent::toDriverKeys($hash);

if (isset($hash['name'])) {
Expand Down Expand Up @@ -194,32 +209,82 @@ public function toDriverKeys(array $hash)
}

/**
* Return the Kolab data handler for the current address book.
* Translates a hash from being keyed on driver-specific fields to being
* keyed on the generalized Turba attributes. The translation is based on
* the contents of $this->map.
*
* @param array $entry A hash using driver-specific keys.
*
* @return array Translated version of $entry.
*/
public function toTurbaKeys(array $entry)
{
if (isset($entry['__type']) &&
$entry['__type'] == 'Group' &&
isset($entry['display-name'])) {
$entry['last-name'] = $entry['display-name'];
}

return parent::toTurbaKeys($entry);
}

/**
* Returns the Kolab data handler for contacts in the current address book.
*
* @return Horde_Kolab_Storage_Data The data handler.
*/
protected function _getData()
{
if ($this->_data === null) {
if (!empty($this->_share)) {
$share = $this->_share;
} else {
if (empty($this->_name)) {
throw new Turba_Exception(
'The addressbook has been left undefined but is required!'
);
}
$this->_data = $this->_createData('contact');
}

return $this->_data;
}

/**
* Returns the Kolab data handler for distribution lists in the current
* address book.
*
* @return Horde_Kolab_Storage_Data The data handler.
*/
protected function _getListData()
{
if ($this->_listData === null) {
$this->_listData = $this->_createData('distribution-list');
}

$share = $GLOBALS['injector']
->getInstance('Turba_Shares')
->getShare($addressbook);
return $this->_listData;
}

/**
* Returns a new Kolab data handler for the current address book.
*
* @param string $type An object type, either 'contact' or
* 'distribution-list'.
*
* @return Horde_Kolab_Storage_Data A data handler.
*/
protected function _createData($type)
{
if (!empty($this->_share)) {
$share = $this->_share;
} else {
if (empty($this->_name)) {
throw new Turba_Exception(
'The addressbook has been left undefined but is required!'
);
}

$this->_data = $this->_kolab->getData($share->get('folder'), 'contact');
$this->setContactOwner($share->get('owner'));
$share = $GLOBALS['injector']
->getInstance('Turba_Shares')
->getShare($this->_name);
}

return $this->_data;
$data = $this->_kolab->getData($share->get('folder'), $type);
$this->setContactOwner($share->get('owner'));

return $data;
}

/**
Expand All @@ -241,6 +306,7 @@ public function connect()
$contacts = array();
foreach ($raw_contacts as $id => $contact) {
$contact = $contact->getData();
$contact['__type'] = 'Object';
$contact['__key'] = Horde_Url::uriB64Encode($id);

if (isset($contact['picture'])) {
Expand Down Expand Up @@ -309,13 +375,17 @@ public function connect()
}

/* Now we retrieve distribution-lists */
$groups = array();
//@todo: group support
/* $result = $this->_store->setObjectType('distribution-list'); */
/* $groups = $this->_store->getObjects(); */
/* if (!$groups) { */
/* $groups = array(); */
/* } */
$raw_groups = $this->_getListData()->getObjects();
if (!$raw_groups) {
$groups = array();
} else {
foreach ($raw_groups as $id => $group) {
$group = $group->getData();
$group['__type'] = 'Group';
$group['__key'] = Horde_Url::uriB64Encode($id);
$groups[Horde_Url::uriB64Encode($id)] = $group;
}
}

/* Store the results in our cache */
$this->_contacts_cache = array_merge($contacts, $groups);
Expand Down Expand Up @@ -354,12 +424,16 @@ protected function _search(array $criteria, array $fields,
$ids = $this->_removeDuplicated($ids);

/* Now we have a list of names, get the rest. */
$result = $this->_read(
'uid',
array_map(array('Horde_Url', 'uriB64Encode'), $ids),
null,
$fields
);
if ($ids) {
$result = $this->_read(
'uid',
array_map(array('Horde_Url', 'uriB64Encode'), $ids),
null,
$fields
);
} else {
$result = array();
}

Horde::logMessage(sprintf('Kolab returned %s results',
count($result)), 'DEBUG');
Expand Down Expand Up @@ -560,7 +634,7 @@ protected function _read($key, $ids, $owner, array $fields,
if (isset($object['member'])) {
foreach ($object['member'] as $member) {
if (isset($member['uid'])) {
$member_ids[] = $member['uid'];
$member_ids[] = Horde_Url::uriB64Encode($member['uid']);
continue;
}
$display_name = $member['display-name'];
Expand All @@ -587,16 +661,20 @@ protected function _read($key, $ids, $owner, array $fields,
$contacts = $this->_search($criteria, $fields);

// and drop everything else except the first search result
$member_ids[] = $contacts[0]['uid'];
$member_ids[] = $contacts[0]['__key'];
}
$object['__members'] = serialize($member_ids);
unset($object['member']);
}
$results[] = $object;;
$results[] = $object;
}
}
}

if (!$results) {
throw new Horde_Exception_NotFound();
}

return $results;
}

Expand Down Expand Up @@ -636,15 +714,12 @@ protected function _delete($object_key, $object_id)
throw new Turba_Exception(sprintf(_("Object with UID %s does not exist!"), $uid));
}

$group = isset($this->_contacts_cache[$object_id]['__type']) &&
$this->_contacts_cache[$object_id]['__type'] == 'Group';
$data = isset($this->_contacts_cache[$object_id]['__type']) &&
$this->_contacts_cache[$object_id]['__type'] == 'Group'
? $this->_getListData()
: $this->_getData();

if ($group) {
//@todo: group support
//$result = $this->_store->setObjectType('distribution-list');
}

$result = $this->_getData()->delete($this->_contacts_cache[$object_id]['uid']);
$result = $data->delete($this->_contacts_cache[$object_id]['uid']);

/* Invalidate cache. */
$this->_connected = false;
Expand All @@ -667,11 +742,7 @@ protected function _deleteAll($sourceName = null)

/* Delete contacts */
$this->_getData()->deleteAll();

/* Delete groups */
//@todo: group support
//$result = $this->_store->setObjectType('distribution-list');
//$result = $this->_store->deleteAll();
$this->_getListData()->deleteAll();

return $uids;
}
Expand Down Expand Up @@ -701,40 +772,34 @@ protected function _save(Turba_Object $object)
*/
protected function _store($attributes, $object_id = null)
{
$group = false;
if (isset($attributes['__type']) && $attributes['__type'] == 'Group') {
return;
//@todo: group support
/* $group = true; */
/* $result = $this->_store->setObjectType('distribution-list'); */
/* $this->_convertMembers($attributes); */
}

if (isset($attributes['photo']) && isset($attributes['phototype'])) {
$attributes['_attachments']['photo.attachment'] = array(
'type' => $attributes['phototype'],
'content' => $attributes['photo']
);
$attributes['picture'] = 'photo.attachment';
unset($attributes['photo'], $attributes['phototype']);
}
$this->_convertMembers($attributes);
$data = $this->_getListData();
} else {
if (isset($attributes['photo']) && isset($attributes['phototype'])) {
$attributes['_attachments']['photo.attachment'] = array(
'type' => $attributes['phototype'],
'content' => $attributes['photo']
);
$attributes['picture'] = 'photo.attachment';
unset($attributes['photo'], $attributes['phototype']);
}

// EAS sets the date fields to '' instead of null -> fix it up
$fix_date_fields = array('birthday', 'anniversary');
foreach($fix_date_fields as $fix_date) {
if (empty($attributes[$fix_date])) {
unset($attributes[$fix_date]);
// EAS sets the date fields to '' instead of null -> fix it up
$fix_date_fields = array('birthday', 'anniversary');
foreach ($fix_date_fields as $fix_date) {
if (empty($attributes[$fix_date])) {
unset($attributes[$fix_date]);
}
}
$data = $this->_getData();
}

if ($object_id === null) {
$object_id = $this->_getData()->create($attributes);
$object_id = $data->create($attributes);
} else {
$object_id = $this->_getData()->modify($attributes);
$data->modify($attributes);
}
/* if ($group) {
$result = $this->_store->setObjectType('contact');
} */

/* Invalidate cache. */
$this->_connected = false;
Expand All @@ -750,11 +815,10 @@ protected function _convertMembers(&$attributes)
if (isset($attributes['__members'])) {
$member_ids = unserialize($attributes['__members']);
$attributes['member'] = array();
foreach ($member_ids as $uid) {
$member_id = Horde_Url::uriB64Encode($uid);
foreach ($member_ids as $member_id) {
if (isset($this->_contacts_cache[$member_id])) {
$member = $this->_contacts_cache[$member_id];
$mail = array('uid' => $uid);
$mail = array('uid' => Horde_Url::uriB64Decode($member_id));
if (!empty($member['full-name'])) {
$mail['display-name'] = $member['full-name'];
}
Expand Down
14 changes: 14 additions & 0 deletions turba/lib/Driver/Share.php
Expand Up @@ -88,6 +88,20 @@ public function toDriverKeys(array $hash)
return $this->_driver->toDriverKeys($hash);
}

/**
* Translates a hash from being keyed on driver-specific fields to being
* keyed on the generalized Turba attributes. The translation is based on
* the contents of $this->map.
*
* @param array $entry A hash using driver-specific keys.
*
* @return array Translated version of $entry.
*/
public function toTurbaKeys(array $entry)
{
return $this->_driver->toTurbaKeys($entry);
}

/**
* Searches the current address book for duplicate entries.
*
Expand Down
2 changes: 1 addition & 1 deletion turba/lib/Driver/Sql.php
Expand Up @@ -505,7 +505,7 @@ protected function _deleteAll($sourceName = null)
* @return string The object id, possibly updated.
* @throws Turba_Exception
*/
function _save(Turba_Object $object)
protected function _save(Turba_Object $object)
{
list($object_key, $object_id) = each($this->toDriverKeys(array('__key' => $object->getValue('__key'))));
$attributes = $this->toDriverKeys($object->getAttributes());
Expand Down

0 comments on commit cde0fb1

Please sign in to comment.