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

« back to all changes in this revision

Viewing changes to turba-4.1.0/lib/List.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
 
 * The Turba_List:: class provides an interface for dealing with a
4
 
 * list of Turba_Objects.
5
 
 *
6
 
 * Copyright 2000-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   Chuck Hagenbuch <chuck@horde.org>
12
 
 * @author   Jon Parise <jon@csh.rit.edu>
13
 
 * @category Horde
14
 
 * @license  http://www.horde.org/licenses/apache ASL
15
 
 * @package  Turba
16
 
 */
17
 
class Turba_List implements Countable
18
 
{
19
 
    /**
20
 
     * The array containing the Turba_Objects represented in this list.
21
 
     *
22
 
     * @var array
23
 
     */
24
 
    public $objects = array();
25
 
 
26
 
    /**
27
 
     * The field to compare objects by.
28
 
     *
29
 
     * @var string
30
 
     */
31
 
    protected $_usortCriteria;
32
 
 
33
 
    /**
34
 
     * Constructor.
35
 
     */
36
 
    public function __construct(array $ids = array())
37
 
    {
38
 
        foreach ($ids as $value) {
39
 
            list($source, $key) = explode(':', $value);
40
 
            try {
41
 
                $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($source);
42
 
                $this->insert($driver->getObject($key));
43
 
            } catch (Horde_Exception $e) {
44
 
            }
45
 
        }
46
 
    }
47
 
 
48
 
    /**
49
 
     * Inserts a new object into the list.
50
 
     *
51
 
     * @param Turba_Object $object  The object to insert.
52
 
     */
53
 
    public function insert(Turba_Object $object)
54
 
    {
55
 
        if ($object instanceof Turba_Object) {
56
 
            $key = $object->getSource() . ':' . $object->getValue('__key');
57
 
            if (!isset($this->objects[$key])) {
58
 
                $this->objects[$key] = $object;
59
 
            }
60
 
        }
61
 
    }
62
 
 
63
 
    /**
64
 
     * Resets our internal pointer to the beginning of the list. Use this to
65
 
     * hide the internal storage (array, list, etc.) from client objects.
66
 
     *
67
 
     * @return Turba_Object  The next object in the list.
68
 
     */
69
 
    public function reset()
70
 
    {
71
 
        return reset($this->objects);
72
 
    }
73
 
 
74
 
    /**
75
 
     * Returns the next Turba_Object in the list. Use this to hide internal
76
 
     * implementation details from client objects.
77
 
     *
78
 
     * @return Turba_Object  The next object in the list.
79
 
     */
80
 
    public function next()
81
 
    {
82
 
        list(,$tmp) = each($this->objects);
83
 
        return $tmp;
84
 
    }
85
 
 
86
 
    /**
87
 
     * Filters/Sorts the list based on the specified sort routine.
88
 
     * The default sort order is by last name, ascending.
89
 
     *
90
 
     * @param array $order  Array of hashes describing sort fields.  Each
91
 
     *                      hash has the following fields:
92
 
     * <pre>
93
 
     * ascending - (boolean) Sort direction.
94
 
     * field - (string) Sort field.
95
 
     * </pre>
96
 
     */
97
 
    public function sort($order = null)
98
 
    {
99
 
        if (!$order) {
100
 
            $order = array(
101
 
                array(
102
 
                    'ascending' => true,
103
 
                    'field' => 'lastname'
104
 
                )
105
 
            );
106
 
        }
107
 
 
108
 
        $need_lastname = $need_firstname = false;
109
 
        $name_format = $GLOBALS['prefs']->getValue('name_format');
110
 
        $name_sort = $GLOBALS['prefs']->getValue('name_sort');
111
 
        foreach ($order as &$field) {
112
 
            if ($field['field'] == 'name') {
113
 
                if ($name_sort == 'last_first') {
114
 
                    $field['field'] = 'lastname';
115
 
                } elseif ($name_sort == 'first_last') {
116
 
                    $field['field'] = 'firstname';
117
 
                }
118
 
            }
119
 
 
120
 
            if ($field['field'] == 'lastname') {
121
 
                $field['field'] = '__lastname';
122
 
                $need_lastname = true;
123
 
                break;
124
 
            }
125
 
            if ($field['field'] == 'firstname') {
126
 
                $field['field'] = '__firstname';
127
 
                $need_firstname = true;
128
 
                break;
129
 
            }
130
 
        }
131
 
 
132
 
        if ($need_firstname || $need_lastname) {
133
 
            $sorted_objects = array();
134
 
            foreach ($this->objects as $key => $object) {
135
 
                $name = $object->getValue('name');
136
 
                $firstname = $object->getValue('firstname');
137
 
                $lastname = $object->getValue('lastname');
138
 
                if (!$lastname) {
139
 
                    $lastname = Turba::guessLastname($name);
140
 
                }
141
 
                if (!$firstname) {
142
 
                    switch ($name_format) {
143
 
                    case 'last_first':
144
 
                        $firstname = preg_replace('/' . preg_quote($lastname, '/') . ',\s*/', '', $name);
145
 
                        break;
146
 
                    case 'first_last':
147
 
                        $firstname = preg_replace('/\s+' . preg_quote($lastname, '/') . '/', '', $name);
148
 
                        break;
149
 
                    default:
150
 
                        $firstname = preg_replace('/\s*' . preg_quote($lastname, '/') . '(,\s*)?/', '', $name);
151
 
                        break;
152
 
                    }
153
 
                }
154
 
                $object->setValue('__lastname', $lastname);
155
 
                $object->setValue('__firstname', $firstname);
156
 
                $sorted_objects[$key] = $object;
157
 
            }
158
 
        } else {
159
 
            $sorted_objects = $this->objects;
160
 
        }
161
 
 
162
 
        $this->_usortCriteria = $order;
163
 
 
164
 
        /* Exceptions thrown inside a sort incorrectly cause an error. See
165
 
         * Bug #9202. */
166
 
        @usort($sorted_objects, array($this, '_cmp'));
167
 
 
168
 
        $this->objects = $sorted_objects;
169
 
    }
170
 
 
171
 
    /**
172
 
     * Usort helper function.
173
 
     *
174
 
     * Compares two Turba_Objects based on the member variable
175
 
     * $_usortCriteria, taking care to sort numerically if it is an integer
176
 
     * field.
177
 
     *
178
 
     * @param Turba_Object $a  The first Turba_Object to compare.
179
 
     * @param Turba_Object $b  The second Turba_Object to compare.
180
 
     *
181
 
     * @return integer  Comparison of the two field values.
182
 
     */
183
 
    protected function _cmp(Turba_Object $a, Turba_Object $b)
184
 
    {
185
 
        foreach ($this->_usortCriteria as $field) {
186
 
            // Set the comparison type based on the type of attribute we're
187
 
            // sorting by.
188
 
            $sortmethod = 'text';
189
 
            if (isset($GLOBALS['attributes'][$field['field']])) {
190
 
                $f = $GLOBALS['attributes'][$field['field']];
191
 
 
192
 
                if (!empty($f['cmptype'])) {
193
 
                    $sortmethod = $f['cmptype'];
194
 
                } elseif (in_array($f['type'], array('int', 'intlist', 'number'))) {
195
 
                    $sortmethod = 'int';
196
 
                }
197
 
            }
198
 
 
199
 
            $f = $field['field'];
200
 
            switch ($sortmethod) {
201
 
            case 'int':
202
 
                $result = ($a->getValue($f) > $b->getValue($f)) ? 1 : -1;
203
 
                break;
204
 
 
205
 
            case 'text':
206
 
                if (!isset($a->sortValue[$f])) {
207
 
                    $a->sortValue[$f] = Horde_String::lower($a->getValue($f), true, 'UTF-8');
208
 
                }
209
 
                if (!isset($b->sortValue[$f])) {
210
 
                    $b->sortValue[$f] = Horde_String::lower($b->getValue($f), true, 'UTF-8');
211
 
                }
212
 
 
213
 
                // Use strcoll for locale-safe comparisons.
214
 
                $result = strcoll($a->sortValue[$f], $b->sortValue[$f]);
215
 
                break;
216
 
            }
217
 
 
218
 
            if (!$field['ascending']) {
219
 
                $result = -$result;
220
 
            }
221
 
            if ($result != 0) {
222
 
                return $result;
223
 
            }
224
 
        }
225
 
 
226
 
        return 0;
227
 
    }
228
 
 
229
 
    /* Countable methods. */
230
 
 
231
 
    public function count()
232
 
    {
233
 
        return count($this->objects);
234
 
    }
235
 
 
236
 
}