Overview

Packages

  • Horde
    • Data
  • None
  • Turba

Classes

  • Turba
  • Turba_Api
  • Turba_Driver
  • Turba_Driver_Facebook
  • Turba_Driver_Favourites
  • Turba_Driver_Group
  • Turba_Driver_Imsp
  • Turba_Driver_Kolab
  • Turba_Driver_Ldap
  • Turba_Driver_Prefs
  • Turba_Driver_Share
  • Turba_Driver_Sql
  • Turba_Driver_Vbook
  • Turba_Exception
  • Turba_Factory_Driver
  • Turba_Form_AddContact
  • Turba_Form_Contact
  • Turba_Form_ContactBase
  • Turba_Form_CreateAddressBook
  • Turba_Form_DeleteAddressBook
  • Turba_Form_EditAddressBook
  • Turba_Form_EditContact
  • Turba_Form_EditContactGroup
  • Turba_List
  • Turba_LoginTasks_SystemTask_Upgrade
  • Turba_Object
  • Turba_Object_Group
  • Turba_Test
  • Turba_View_Browse
  • Turba_View_Contact
  • Turba_View_DeleteContact
  • Turba_View_Duplicates
  • Turba_View_EditContact
  • Turba_View_List
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Turba_List:: class provides an interface for dealing with a
  4:  * list of Turba_Objects.
  5:  *
  6:  * Copyright 2000-2012 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 (Turba_Exception $e) {}
 44:         }
 45:     }
 46: 
 47:     /**
 48:      * Inserts a new object into the list.
 49:      *
 50:      * @param Turba_Object $object  The object to insert.
 51:      */
 52:     public function insert(Turba_Object $object)
 53:     {
 54:         if ($object instanceof Turba_Object) {
 55:             $key = $object->getSource() . ':' . $object->getValue('__key');
 56:             if (!isset($this->objects[$key])) {
 57:                 $this->objects[$key] = $object;
 58:             }
 59:         }
 60:     }
 61: 
 62:     /**
 63:      * Resets our internal pointer to the beginning of the list. Use this to
 64:      * hide the internal storage (array, list, etc.) from client objects.
 65:      *
 66:      * @return Turba_Object  The next object in the list.
 67:      */
 68:     public function reset()
 69:     {
 70:         return reset($this->objects);
 71:     }
 72: 
 73:     /**
 74:      * Returns the next Turba_Object in the list. Use this to hide internal
 75:      * implementation details from client objects.
 76:      *
 77:      * @return Turba_Object  The next object in the list.
 78:      */
 79:     public function next()
 80:     {
 81:         list(,$tmp) = each($this->objects);
 82:         return $tmp;
 83:     }
 84: 
 85:     /**
 86:      * Filters/Sorts the list based on the specified sort routine.
 87:      * The default sort order is by last name, ascending.
 88:      *
 89:      * @param array $order  Array of hashes describing sort fields.  Each
 90:      *                      hash has the following fields:
 91:      * <pre>
 92:      * ascending - (boolean) Sort direction.
 93:      * field - (string) Sort field.
 94:      * </pre>
 95:      */
 96:     public function sort($order = null)
 97:     {
 98:         if (!$order) {
 99:             $order = array(
100:                 array(
101:                     'ascending' => true,
102:                     'field' => 'lastname'
103:                 )
104:             );
105:         }
106: 
107:         $need_lastname = $need_firstname = false;
108:         $name_format = $GLOBALS['prefs']->getValue('name_format');
109:         $name_sort = $GLOBALS['prefs']->getValue('name_sort');
110:         foreach ($order as &$field) {
111:             if ($field['field'] == 'name') {
112:                 if ($name_sort == 'last_first') {
113:                     $field['field'] = 'lastname';
114:                 } elseif ($name_sort == 'first_last') {
115:                     $field['field'] = 'firstname';
116:                 }
117:             }
118: 
119:             if ($field['field'] == 'lastname') {
120:                 $field['field'] = '__lastname';
121:                 $need_lastname = true;
122:                 break;
123:             }
124:             if ($field['field'] == 'firstname') {
125:                 $field['field'] = '__firstname';
126:                 $need_firstname = true;
127:                 break;
128:             }
129:         }
130: 
131:         if ($need_firstname || $need_lastname) {
132:             $sorted_objects = array();
133:             foreach ($this->objects as $key => $object) {
134:                 $name = $object->getValue('name');
135:                 $firstname = $object->getValue('firstname');
136:                 $lastname = $object->getValue('lastname');
137:                 if (!$lastname) {
138:                     $lastname = Turba::guessLastname($name);
139:                 }
140:                 if (!$firstname) {
141:                     switch ($name_format) {
142:                     case 'last_first':
143:                         $firstname = preg_replace('/' . preg_quote($lastname, '/') . ',\s*/', '', $name);
144:                         break;
145:                     case 'first_last':
146:                         $firstname = preg_replace('/\s+' . preg_quote($lastname, '/') . '/', '', $name);
147:                         break;
148:                     default:
149:                         $firstname = preg_replace('/\s*' . preg_quote($lastname, '/') . '(,\s*)?/', '', $name);
150:                         break;
151:                     }
152:                 }
153:                 $object->setValue('__lastname', $lastname);
154:                 $object->setValue('__firstname', $firstname);
155:                 $sorted_objects[$key] = $object;
156:             }
157:         } else {
158:             $sorted_objects = $this->objects;
159:         }
160: 
161:         $this->_usortCriteria = $order;
162: 
163:         /* Exceptions thrown inside a sort incorrectly cause an error. See
164:          * Bug #9202. */
165:         @usort($sorted_objects, array($this, '_cmp'));
166: 
167:         $this->objects = $sorted_objects;
168:     }
169: 
170:     /**
171:      * Usort helper function.
172:      *
173:      * Compares two Turba_Objects based on the member variable
174:      * $_usortCriteria, taking care to sort numerically if it is an integer
175:      * field.
176:      *
177:      * @param Turba_Object $a  The first Turba_Object to compare.
178:      * @param Turba_Object $b  The second Turba_Object to compare.
179:      *
180:      * @return integer  Comparison of the two field values.
181:      */
182:     protected function _cmp(Turba_Object $a, Turba_Object $b)
183:     {
184:         foreach ($this->_usortCriteria as $field) {
185:             // Set the comparison type based on the type of attribute we're
186:             // sorting by.
187:             $sortmethod = 'text';
188:             if (isset($GLOBALS['attributes'][$field['field']])) {
189:                 $f = $GLOBALS['attributes'][$field['field']];
190: 
191:                 if (!empty($f['cmptype'])) {
192:                     $sortmethod = $f['cmptype'];
193:                 } elseif (in_array($f['type'], array('int', 'intlist', 'number'))) {
194:                     $sortmethod = 'int';
195:                 }
196:             }
197: 
198:             $f = $field['field'];
199:             switch ($sortmethod) {
200:             case 'int':
201:                 $result = ($a->getValue($f) > $b->getValue($f)) ? 1 : -1;
202:                 break;
203: 
204:             case 'text':
205:                 if (!isset($a->sortValue[$f])) {
206:                     $a->sortValue[$f] = Horde_String::lower($a->getValue($f), true, 'UTF-8');
207:                 }
208:                 if (!isset($b->sortValue[$f])) {
209:                     $b->sortValue[$f] = Horde_String::lower($b->getValue($f), true, 'UTF-8');
210:                 }
211: 
212:                 // Use strcoll for locale-safe comparisons.
213:                 $result = strcoll($a->sortValue[$f], $b->sortValue[$f]);
214:                 break;
215:             }
216: 
217:             if (!$field['ascending']) {
218:                 $result = -$result;
219:             }
220:             if ($result != 0) {
221:                 return $result;
222:             }
223:         }
224: 
225:         return 0;
226:     }
227: 
228:     /* Countable methods. */
229: 
230:     public function count()
231:     {
232:         return count($this->objects);
233:     }
234: 
235: }
236: 
API documentation generated by ApiGen