File: List.php

package info (click to toggle)
turba2 2.0.2-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,996 kB
  • ctags: 1,071
  • sloc: php: 4,725; xml: 617; sql: 136; makefile: 62; sh: 46; perl: 17
file content (233 lines) | stat: -rw-r--r-- 6,719 bytes parent folder | download
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
<?php
/**
 * The Turba_List:: class provides an interface for dealing with a list of
 * Turba_AbstractObjects.
 *
 * $Horde: turba/lib/List.php,v 1.41 2004/10/26 19:27:50 jan Exp $
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @author  Jon Parise <jon@csh.rit.edu>
 * @version $Revision: 1.41 $
 * @since   Turba 0.0.1
 * @package Turba
 */
class Turba_List {

    /**
     * The array containing the Turba_Objects represented in this list.  @var
     * array $objects
     */
    var $objects = array();

    /**
     * An array of objects which have just been added during this page load.
     * @var array $fresh
     */
    var $fresh = array();

    /**
     * The field to compare objects by.
     * @var string $_usortCriteria
     */
    var $_usortCriteria;

    /**
     * Inserts a new list.
     *
     * @param Turba_Object $object  The object to insert.
     * @param boolean $new          This object is from a new search (defaults
     *                              to true).
     */
    function insert($object, $new = true)
    {
        if (is_a($object, 'Turba_AbstractObject')) {
            $key = $object->driver->name . ':' . $object->getValue('__key');
            if (!isset($this->objects[$key])) {
                if ($new) {
                    $this->fresh[$key] = 1;
                }
                $this->objects[$key] = $object;
            }
        }
    }

    /**
     * Removes an entry from the list.
     *
     * @param string $key  The key of the object to remove.
     *
     * @since Turba 1.2
     */
    function remove($key)
    {
        if (isset($this->objects[$key])) {
            unset($this->objects[$key]);
        }
    }

    /**
     * Merges an existing Turba_List into this one.
     *
     * @param mixed $list   The list to merge - either a Turba_List object or
     *                      an array.
     * @param boolean $new  These objects are coming from a new search
     *                      (defaults to true).
     */
    function merge($list, $new = true)
    {
        if (is_object($list)) {
            $list = $list->objects;
        }
        if (is_array($list)) {
            foreach ($list as $object) {
                $this->insert($object, $new);
            }
        }
    }

    /**
     * Resets our internal pointer to the beginning of the list. Use this to
     * hide the internal storage (array, list, etc.) from client objects.
     */
    function reset()
    {
        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.
     */
    function next()
    {
        list(,$tmp) = each($this->objects);
        return $tmp;
    }

    /**
     * Returns the number of Turba_Objects that are in the list. Use this to
     * hide internal implementation details from client objects.
     *
     * @return integer  The number of objects in the list.
     */
    function count()
    {
        return count($this->objects);
    }

    /**
     * Filters/Sorts the list based on the specified sort routine.
     *
     * @param $sort  The sort method.
     * @param $low   The low end of the sort range.
     * @param $high  The high end of the sort range.
     * @param $dir   Sort direction, 0 = ascending, 1 = descending
     */
    function sort($sort = 'lastname', $dir = 0)
    {
        global $prefs, $attributes;

        $sorted_objects = array();

        foreach ($this->objects as $key => $object) {
            $lastname = $object->getValue('lastname');
            if (!$lastname) {
                $lastname = Turba::guessLastname($object->getValue('name'));
            }
            $object->setValue('lastname', $lastname);
            $sorted_objects[$key] = $object;
        }

        $this->_usortCriteria = $sort;

        // Set the comparison type based on the type of attribute we're
        // sorting by.
        $this->_usortType = 'text';
        if (isset($attributes[$sort])) {
            if (!empty($attributes[$sort]['cmptype'])) {
                $this->_usortType = $attributes[$sort]['cmptype'];
            } elseif ($attributes[$sort]['type'] == 'int' ||
                      $attributes[$sort]['type'] == 'intlist' ||
                      $attributes[$sort]['type'] == 'number') {
                $this->_usortType = 'int';
            }
        }

        usort($sorted_objects, array($this, 'cmp'));

        if ($dir == 1) {
            $this->objects = array_reverse($sorted_objects);
        } else {
            $this->objects = $sorted_objects;
        }
    }

    /**
     * Usort helper function.
     *
     * Compares two Turba_AbstractObjects based on the member variable
     * $_usortCriteria, taking care to sort numerically if it is an integer
     * field.
     *
     * @param $a  The first Turba_AbstractObject to compare.
     * @param $b  The second Turba_AbstroctObject to compare.
     *
     * @return integer  Comparison of the two field values.
     */
    function cmp($a, $b)
    {
        switch ($this->_usortType) {
        case 'int':
            return ($a->getValue($this->_usortCriteria) > $b->getValue($this->_usortCriteria)) ? 1 : -1;
            break;

        case 'text':
        default:
            $acmp = String::lower($a->getValue($this->_usortCriteria), true);
            $bcmp = String::lower($b->getValue($this->_usortCriteria), true);

            // Use strcoll for locale-safe comparisons.
            return strcoll($acmp, $bcmp);
        }
    }

    function isFresh($object)
    {
        return isset($this->fresh[$object->driver->name . ':' . $object->getValue('__key')]);
    }

    function serialize()
    {
        $data = array();
        $data['keys'] = array();
        $data['fresh'] = $this->fresh;
        foreach ($this->objects as $key => $object) {
            $data['keys'][] = $object->driver->name . ':' . $object->getValue('__key');
        }
        return $data;
    }

    function unserialize($data)
    {
        if (!isset($data) || !is_array($data)) {
            return null;
        }

        $tmp = &new Turba_List();
        $objects = array();
        foreach ($data['keys'] as $value) {
            list($source, $key) = explode(':', $value);
            if (isset($GLOBALS['cfgSources'][$source])) {
                $driver = &Turba_Driver::singleton($source, $GLOBALS['cfgSources'][$source]);
                $tmp->insert($driver->getObject($key));
            }
        }

        /* Not the best way of doing this, but it works for now. */
        $tmp->fresh = $data['fresh'];
        return $tmp;
    }

}