3
* Read-only Turba directory driver implementation for favourite
4
* recipients. Relies on the contacts/favouriteRecipients API method.
6
* Copyright 2010-2013 Horde LLC (http://www.horde.org/)
8
* See the enclosed file LICENSE for license information (ASL). If you did
9
* did not receive this file, see http://www.horde.org/licenses/apache.
11
* @author Jan Schneider <jan@horde.org>
13
* @license http://www.horde.org/licenses/apache ASL
16
class Turba_Driver_Favourites extends Turba_Driver
19
* Checks if the current user has the requested permissions on this
22
* @param integer $perm The permission to check for.
24
* @return boolean True if the user has permission, otherwise false.
26
public function hasPermission($perm)
29
case Horde_Perms::DELETE:
30
case Horde_Perms::EDIT:
39
* Always returns true because the driver is read-only and there is
42
* @param string $user The user's data to remove.
44
* @return boolean Always true.
46
public function removeUserData($user)
52
* Searches the favourites list with the given criteria and returns a
53
* filtered list of results. If the criteria parameter is an empty array,
54
* all records will be returned.
56
* @param array $criteria Array containing the search criteria.
57
* @param array $fields List of fields to return.
58
* @param array $blobFields A list of fields that contain binary data.
60
* @return array Hash containing the search results.
61
* @throws Turba_Exception
63
protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
67
foreach ($this->_getAddressBook() as $key => $contact) {
68
if (!count($criteria)) {
69
$results[$key] = $contact;
72
foreach ($criteria as $op => $vals) {
78
foreach ($vals as $val) {
79
if (!$this->_match($contact, $val)) {
85
} elseif ($op == 'OR') {
87
foreach ($vals as $val) {
88
if ($this->_match($contact, $val)) {
98
$results[$key] = $contact;
102
return $count_only ? count($results) : $results;
106
* Returns whether a contact matches some criteria.
108
* @param array $contact A contact hash.
109
* @param array $val Some matching criterion, see _search().
111
* @return boolean True if the contact matches.
113
protected function _match($contact, $val)
115
if (!isset($contact[$val['field']])) {
118
switch ($val['op']) {
120
return (string)$contact[$val['field']] == (string)$val['test'];
122
return empty($val['test']) ||
123
stristr($contact[$val['field']], $val['test']) !== false;
128
* Reads the given data from the address book and returns the results.
130
* @param string $key The primary key field to use.
131
* @param mixed $ids The ids of the contacts to load.
132
* @param string $owner Only return contacts owned by this user.
133
* @param array $fields List of fields to return.
134
* @param array $blobFields Array of fields containing binary data.
136
* @return array Hash containing the search results.
137
* @throws Turba_Exception
139
protected function _read($key, $ids, $owner, array $fields,
140
array $blobFields = array())
142
$book = $this->_getAddressBook();
145
if (!is_array($ids)) {
149
foreach ($ids as $id) {
150
if (isset($book[$id])) {
151
$results[] = $book[$id];
161
* @throws Turba_Exception
163
protected function _getAddressBook()
167
if (!$registry->hasMethod('contacts/favouriteRecipients')) {
168
throw new Turba_Exception(_("No source for favourite recipients exists."));
172
$addresses = $registry->call('contacts/favouriteRecipients', array($this->_params['limit']));
173
} catch (Horde_Exception $e) {
174
if ($e->getCode() == Horde_Registry::AUTH_FAILURE ||
175
$e->getCode() == Horde_Registry::NOT_ACTIVE ||
176
$e->getCode() == Horde_Registry::PERMISSION_DENIED) {
179
throw new Turba_Exception($e);
180
} catch (Exception $e) {
181
throw new Turba_Exception($e);
184
$addressbook = array();
185
foreach ($addresses as $address) {
186
$addressbook[$address] = array('email' => $address);