3
* The Turba_List:: class provides an interface for dealing with a
4
* list of Turba_Objects.
6
* Copyright 2000-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 Chuck Hagenbuch <chuck@horde.org>
12
* @author Jon Parise <jon@csh.rit.edu>
14
* @license http://www.horde.org/licenses/apache ASL
17
class Turba_List implements Countable
20
* The array containing the Turba_Objects represented in this list.
24
public $objects = array();
27
* The field to compare objects by.
31
protected $_usortCriteria;
36
public function __construct(array $ids = array())
38
foreach ($ids as $value) {
39
list($source, $key) = explode(':', $value);
41
$driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($source);
42
$this->insert($driver->getObject($key));
43
} catch (Horde_Exception $e) {
49
* Inserts a new object into the list.
51
* @param Turba_Object $object The object to insert.
53
public function insert(Turba_Object $object)
55
if ($object instanceof Turba_Object) {
56
$key = $object->getSource() . ':' . $object->getValue('__key');
57
if (!isset($this->objects[$key])) {
58
$this->objects[$key] = $object;
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.
67
* @return Turba_Object The next object in the list.
69
public function reset()
71
return reset($this->objects);
75
* Returns the next Turba_Object in the list. Use this to hide internal
76
* implementation details from client objects.
78
* @return Turba_Object The next object in the list.
80
public function next()
82
list(,$tmp) = each($this->objects);
87
* Filters/Sorts the list based on the specified sort routine.
88
* The default sort order is by last name, ascending.
90
* @param array $order Array of hashes describing sort fields. Each
91
* hash has the following fields:
93
* ascending - (boolean) Sort direction.
94
* field - (string) Sort field.
97
public function sort($order = null)
103
'field' => 'lastname'
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';
120
if ($field['field'] == 'lastname') {
121
$field['field'] = '__lastname';
122
$need_lastname = true;
125
if ($field['field'] == 'firstname') {
126
$field['field'] = '__firstname';
127
$need_firstname = true;
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');
139
$lastname = Turba::guessLastname($name);
142
switch ($name_format) {
144
$firstname = preg_replace('/' . preg_quote($lastname, '/') . ',\s*/', '', $name);
147
$firstname = preg_replace('/\s+' . preg_quote($lastname, '/') . '/', '', $name);
150
$firstname = preg_replace('/\s*' . preg_quote($lastname, '/') . '(,\s*)?/', '', $name);
154
$object->setValue('__lastname', $lastname);
155
$object->setValue('__firstname', $firstname);
156
$sorted_objects[$key] = $object;
159
$sorted_objects = $this->objects;
162
$this->_usortCriteria = $order;
164
/* Exceptions thrown inside a sort incorrectly cause an error. See
166
@usort($sorted_objects, array($this, '_cmp'));
168
$this->objects = $sorted_objects;
172
* Usort helper function.
174
* Compares two Turba_Objects based on the member variable
175
* $_usortCriteria, taking care to sort numerically if it is an integer
178
* @param Turba_Object $a The first Turba_Object to compare.
179
* @param Turba_Object $b The second Turba_Object to compare.
181
* @return integer Comparison of the two field values.
183
protected function _cmp(Turba_Object $a, Turba_Object $b)
185
foreach ($this->_usortCriteria as $field) {
186
// Set the comparison type based on the type of attribute we're
188
$sortmethod = 'text';
189
if (isset($GLOBALS['attributes'][$field['field']])) {
190
$f = $GLOBALS['attributes'][$field['field']];
192
if (!empty($f['cmptype'])) {
193
$sortmethod = $f['cmptype'];
194
} elseif (in_array($f['type'], array('int', 'intlist', 'number'))) {
199
$f = $field['field'];
200
switch ($sortmethod) {
202
$result = ($a->getValue($f) > $b->getValue($f)) ? 1 : -1;
206
if (!isset($a->sortValue[$f])) {
207
$a->sortValue[$f] = Horde_String::lower($a->getValue($f), true, 'UTF-8');
209
if (!isset($b->sortValue[$f])) {
210
$b->sortValue[$f] = Horde_String::lower($b->getValue($f), true, 'UTF-8');
213
// Use strcoll for locale-safe comparisons.
214
$result = strcoll($a->sortValue[$f], $b->sortValue[$f]);
218
if (!$field['ascending']) {
229
/* Countable methods. */
231
public function count()
233
return count($this->objects);