/**
* Reads the given data from the address book and returns the results.
*
* @param string $key The primary key field to use.
* @param mixed $ids The ids of the contacts to load.
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields, array $blobFields = array(), array $dateFields = array())
{
$this->connect();
$results = array();
if (!is_array($ids)) {
$ids = array($ids);
}
$count = count($fields);
foreach ($ids as $id) {
if (!isset($this->_contacts_cache[$id])) {
continue;
}
$object = $this->_contacts_cache[$id];
if (!isset($object['__type']) || $object['__type'] == 'Object') {
if ($count) {
$result = array();
foreach ($fields as $field) {
if (isset($object[$field])) {
$result[$field] = $object[$field];
}
}
$results[] = $result;
} else {
$results[] = $object;
}
} else {
$member_ids = array();
if (isset($object['member'])) {
foreach ($object['member'] as $member) {
if (isset($member['uid'])) {
$id = Horde_Url::uriB64Encode($member['uid']);
$found = false;
try {
$this->getObject($id);
$found = true;
} catch (Horde_Exception_NotFound $e) {
foreach (Turba::listShares() as $share) {
$driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($share->getName());
try {
$driver->getObject($id);
$id = $share->getName() . ':' . $id;
$found = true;
break;
} catch (Horde_Exception_NotFound $e) {
continue;
}
}
}
if ($found) {
$member_ids[] = $id;
}
continue;
}
$display_name = $member['display-name'];
$smtp_address = $member['smtp-address'];
$criteria = array('AND' => array(array('field' => 'full-name', 'op' => 'LIKE', 'test' => $display_name, 'begin' => false), array('field' => 'emails', 'op' => 'LIKE', 'test' => $smtp_address, 'begin' => false)));
$fields = array('uid');
// we expect only one result here!!!
$contacts = $this->_search($criteria, $fields);
// and drop everything else except the first search
// result
$member_ids[] = $contacts[0]['__key'];
}
$object['__members'] = serialize($member_ids);
unset($object['member']);
}
$results[] = $object;
}
}
if (!$results) {
throw new Horde_Exception_NotFound();
}
return $results;
}