File: Group.php

package info (click to toggle)
turba2 2.1.3-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 5,740 kB
  • ctags: 1,497
  • sloc: php: 6,641; xml: 998; sql: 180; makefile: 63; sh: 46; perl: 17
file content (192 lines) | stat: -rw-r--r-- 5,906 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
<?php

require_once TURBA_BASE . '/lib/Object.php';

/**
 * The Turba_Object_Group:: class provides a set of methods for dealing with
 * contact groups.
 *
 * $Horde: turba/lib/Object/Group.php,v 1.9.2.2 2005/11/14 21:03:27 mrubinsk Exp $
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @author  Jon Parise <jon@csh.rit.edu>
 * @package Turba
 */
class Turba_Object_Group extends Turba_Object {

    /**
     * Constructs a new Turba_Object_Group.
     *
     * @param Turba_Driver $driver  The driver object that this group comes
     *                              from.
     * @param array $attributes     Hash of attributes for this group.
     */
    function Turba_Object_Group(&$driver, $attributes = array())
    {
        parent::Turba_Object($driver, $attributes);
        $this->attributes['__type'] = 'Group';
    }

    /**
     * Returns true if this object is a group of multiple contacts.
     *
     * @return boolean  True.
     */
    function isGroup()
    {
        return true;
    }

    /**
     * Adds a new contact entry to this group.
     *
     * @param string $contactId  The id of the contact to add.
     * @param string $sourceId   The source $contactId is from.
     *
     * @since Turba 1.2
     */
    function addMember($contactId, $sourceId = null)
    {
        // Default to the same source as the group.
        if (is_null($sourceId)) {
            $sourceId = $this->getSource();
        }

        // Can't add a group to itself.
        if ($contactId == $this->attributes['__key']) {
            return PEAR::raiseError(_("Can't add a group to itself."));
        }

        // Try to find the contact being added.
        if ($sourceId == $this->getSource()) {
            $contact = $this->driver->getObject($contactId);
        } else {
            $driver = &Turba_Driver::singleton($sourceId);
            if (is_a($driver, 'PEAR_Error')) {
                return $driver;
            }
            $contact = $driver->getObject($contactId);
        }

        // Bail out if the contact being added doesn't exist or can't
        // be retrieved.
        if (is_a($contact, 'PEAR_Error')) {
            return $contact;
        }

        // Explode members.
        $members = @unserialize($this->attributes['__members']);
        if (!is_array($members)) {
            $members = array();
        }

        // If the contact is from a different source, store its source
        // id as well.
        if ($sourceId == $this->getSource()) {
            $members[] = $contactId;
        } else {
            $members[] = $sourceId . ':' . $contactId;
        }

        // Remove duplicates.
        $members = array_unique($members);

        $this->attributes['__members'] = serialize($members);

        return true;
    }

    /**
     * Deletes a contact from this group.
     *
     * @param string $contactId  The id of the contact to remove.
     * @param string $sourceId   The source $contactId is from.
     *
     * @since Turba 1.2
     */
    function removeMember($contactId, $sourceId = null)
    {
        $members = @unserialize($this->attributes['__members']);

        if (is_null($sourceId) || $sourceId == $this->getSource()) {
            $i = array_search($contactId, $members);
        } else {
            $i = array_search($sourceId . ':' . $contactId, $members);
        }

        if ($i !== false) {
            unset($members[$i]);
        }

        $this->attributes['__members'] = serialize($members);

        return true;
    }

    /**
     * Retrieve the Objects in this group
     *
     * @param $sort_criteria     The requested sort order which is passed to
     *                           Turba_List::sort().
     *
     * @param $sort_direction    The requested sort direction which is passed to
     *                           Turba_List::sort().
     *
     * @return Turba_List        List containing the members of this group
     *
     * @since Turba 1.2
     */
    function &listMembers($sort_criteria = 'lastname', $sort_direction = 0)
    {
        require_once TURBA_BASE . '/lib/List.php';
        $list = &new Turba_List();

        $children = unserialize($this->attributes['__members']);
        if (!is_array($children)) {
            $children = array();
        }

        reset($children);
        $modified = false;
        foreach ($children as $member) {
            if (strpos($member, ':') === false) {
                $contact = $this->driver->getObject($member);
                if (is_a($contact, 'PEAR_Error')) {
                    // Remove the contact if it no longer exists
                    $this->removeMember($member);
                    $modified = true;
                }
            } else {
                list($sourceId, $contactId) = explode(':', $member, 2);
                if (strpos($contactId, ':')) {
                    list($owner, $contactId) = explode(':', $contactId, 2);
                    $sourceId .= ':' . $owner;
                }
                $driver = &Turba_Driver::singleton($sourceId);
                // Don't prune contacts if the source is not
                // found, could just be temporarily unavailable.
                if (!is_a($driver, 'PEAR_Error')) {
                    $contact = $driver->getObject($contactId);
                    if (is_a($contact, 'PEAR_Error')) {
                        // Remove the contact if it no longer exists
                        $this->removeMember($contactId, $sourceId);
                        $modified = true;
                    }
                } else {
                    continue;
                }
            }

            $list->insert($contact);
        }

        // If we've pruned any dead entries, store the changes.
        if ($modified) {
            $this->store();
        }

        $list->sort($sort_criteria, $sort_direction);
        return $list;
    }

}