~ubuntu-branches/debian/sid/php-horde-turba/sid

« back to all changes in this revision

Viewing changes to turba-4.1.0/lib/Driver/Favourites.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 13:16:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130811131625-z91stjvq51jr9onv
Tags: 4.1.1-1
New upstream version 4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Read-only Turba directory driver implementation for favourite
4
 
 * recipients. Relies on the contacts/favouriteRecipients API method.
5
 
 *
6
 
 * Copyright 2010-2013 Horde LLC (http://www.horde.org/)
7
 
 *
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.
10
 
 *
11
 
 * @author   Jan Schneider <jan@horde.org>
12
 
 * @category Horde
13
 
 * @license  http://www.horde.org/licenses/apache ASL
14
 
 * @package  Turba
15
 
 */
16
 
class Turba_Driver_Favourites extends Turba_Driver
17
 
{
18
 
    /**
19
 
     * Checks if the current user has the requested permissions on this
20
 
     * source.
21
 
     *
22
 
     * @param integer $perm  The permission to check for.
23
 
     *
24
 
     * @return boolean  True if the user has permission, otherwise false.
25
 
     */
26
 
     public function hasPermission($perm)
27
 
     {
28
 
         switch ($perm) {
29
 
         case Horde_Perms::DELETE:
30
 
         case Horde_Perms::EDIT:
31
 
             return false;
32
 
 
33
 
         default:
34
 
             return true;
35
 
         }
36
 
     }
37
 
 
38
 
    /**
39
 
     * Always returns true because the driver is read-only and there is
40
 
     * nothing to remove.
41
 
     *
42
 
     * @param string $user  The user's data to remove.
43
 
     *
44
 
     * @return boolean  Always true.
45
 
     */
46
 
    public function removeUserData($user)
47
 
    {
48
 
        return true;
49
 
    }
50
 
 
51
 
    /**
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.
55
 
     *
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.
59
 
     *
60
 
     * @return array  Hash containing the search results.
61
 
     * @throws Turba_Exception
62
 
     */
63
 
    protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
64
 
    {
65
 
        $results = array();
66
 
 
67
 
        foreach ($this->_getAddressBook() as $key => $contact) {
68
 
            if (!count($criteria)) {
69
 
                $results[$key] = $contact;
70
 
                continue;
71
 
            }
72
 
            foreach ($criteria as $op => $vals) {
73
 
                if ($op == 'AND') {
74
 
                    if (!count($vals)) {
75
 
                        $found = false;
76
 
                    } else {
77
 
                        $found = true;
78
 
                        foreach ($vals as $val) {
79
 
                            if (!$this->_match($contact, $val)) {
80
 
                                $found = false;
81
 
                                break;
82
 
                            }
83
 
                        }
84
 
                    }
85
 
                } elseif ($op == 'OR') {
86
 
                    $found = false;
87
 
                    foreach ($vals as $val) {
88
 
                        if ($this->_match($contact, $val)) {
89
 
                            $found = true;
90
 
                            break;
91
 
                        }
92
 
                    }
93
 
                } else {
94
 
                    $found = false;
95
 
                }
96
 
            }
97
 
            if ($found) {
98
 
                $results[$key] = $contact;
99
 
            }
100
 
        }
101
 
 
102
 
        return $count_only ? count($results) : $results;
103
 
    }
104
 
 
105
 
    /**
106
 
     * Returns whether a contact matches some criteria.
107
 
     *
108
 
     * @param array $contact  A contact hash.
109
 
     * @param array $val      Some matching criterion, see _search().
110
 
     *
111
 
     * @return boolean  True if the contact matches.
112
 
     */
113
 
    protected function _match($contact, $val)
114
 
    {
115
 
        if (!isset($contact[$val['field']])) {
116
 
            return false;
117
 
        }
118
 
        switch ($val['op']) {
119
 
        case '=':
120
 
            return (string)$contact[$val['field']] == (string)$val['test'];
121
 
        case 'LIKE':
122
 
            return empty($val['test']) ||
123
 
                stristr($contact[$val['field']], $val['test']) !== false;
124
 
        }
125
 
    }
126
 
 
127
 
    /**
128
 
     * Reads the given data from the address book and returns the results.
129
 
     *
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.
135
 
     *
136
 
     * @return array  Hash containing the search results.
137
 
     * @throws Turba_Exception
138
 
     */
139
 
    protected function _read($key, $ids, $owner, array $fields,
140
 
                             array $blobFields = array())
141
 
    {
142
 
        $book = $this->_getAddressBook();
143
 
 
144
 
        $results = array();
145
 
        if (!is_array($ids)) {
146
 
            $ids = array($ids);
147
 
        }
148
 
 
149
 
        foreach ($ids as $id) {
150
 
            if (isset($book[$id])) {
151
 
                $results[] = $book[$id];
152
 
            }
153
 
        }
154
 
 
155
 
        return $results;
156
 
    }
157
 
 
158
 
    /**
159
 
     * TODO
160
 
     *
161
 
     * @throws Turba_Exception
162
 
     */
163
 
    protected function _getAddressBook()
164
 
    {
165
 
        global $registry;
166
 
 
167
 
        if (!$registry->hasMethod('contacts/favouriteRecipients')) {
168
 
            throw new Turba_Exception(_("No source for favourite recipients exists."));
169
 
        }
170
 
 
171
 
        try {
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) {
177
 
                return array();
178
 
            }
179
 
            throw new Turba_Exception($e);
180
 
        } catch (Exception $e) {
181
 
            throw new Turba_Exception($e);
182
 
        }
183
 
 
184
 
        $addressbook = array();
185
 
        foreach ($addresses as $address) {
186
 
            $addressbook[$address] = array('email' => $address);
187
 
        }
188
 
 
189
 
        return $addressbook;
190
 
    }
191
 
 
192
 
}