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