1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Turba_List implements Countable
18: {
19: 20: 21: 22: 23:
24: public $objects = array();
25:
26: 27: 28: 29: 30:
31: protected $_usortCriteria;
32:
33: 34: 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: 49: 50: 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: 64: 65: 66: 67:
68: public function reset()
69: {
70: return reset($this->objects);
71: }
72:
73: 74: 75: 76: 77: 78:
79: public function next()
80: {
81: list(,$tmp) = each($this->objects);
82: return $tmp;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 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: 164:
165: @usort($sorted_objects, array($this, '_cmp'));
166:
167: $this->objects = $sorted_objects;
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181:
182: protected function _cmp(Turba_Object $a, Turba_Object $b)
183: {
184: foreach ($this->_usortCriteria as $field) {
185:
186:
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:
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:
229:
230: public function count()
231: {
232: return count($this->objects);
233: }
234:
235: }
236: