1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: class Turba_Object_Group extends Turba_Object
11: {
12: 13: 14: 15: 16: 17: 18:
19: public function __construct(Turba_Driver $driver, array $attributes = array())
20: {
21: parent::__construct($driver, $attributes);
22: $this->attributes['__type'] = 'Group';
23: }
24:
25: 26: 27: 28: 29:
30: public function isGroup()
31: {
32: return true;
33: }
34:
35: 36: 37: 38: 39:
40: public function url($view = null, $full = false)
41: {
42: return Horde::url('browse.php', $full)->add(array(
43: 'source' => $this->getSource(),
44: 'key' => $this->getValue('__key')
45: ));
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55:
56: public function addMember($contactId, $sourceId = null)
57: {
58:
59: if (is_null($sourceId)) {
60: $sourceId = $this->getSource();
61: }
62:
63:
64: if ($contactId == $this->attributes['__key']) {
65: throw new Turba_Exception(_("Can't add a group to itself."));
66: }
67:
68:
69: if ($sourceId == $this->getSource()) {
70: $contact = $this->driver->getObject($contactId);
71: } else {
72: $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
73: $contact = $driver->getObject($contactId);
74: }
75:
76:
77: $members = @unserialize($this->attributes['__members']);
78: if (!is_array($members)) {
79: $members = array();
80: }
81:
82:
83:
84: $members[] = ($sourceId == $this->getSource())
85: ? $contactId
86: : $sourceId . ':' . $contactId;
87:
88:
89: $this->attributes['__members'] = serialize(array_unique($members));
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function removeMember($contactId, $sourceId = null)
99: {
100: $members = @unserialize($this->attributes['__members']);
101:
102: if (is_null($sourceId) || $sourceId == $this->getSource()) {
103: $i = array_search($contactId, $members);
104: } else {
105: $i = array_search($sourceId . ':' . $contactId, $members);
106: }
107:
108: if ($i !== false) {
109: unset($members[$i]);
110: }
111:
112: $this->attributes['__members'] = serialize($members);
113:
114: return true;
115: }
116:
117: 118: 119: 120: 121:
122: public function count()
123: {
124: $children = @unserialize($this->attributes['__members']);
125: if (!is_array($children)) {
126: return 0;
127: } else {
128: return count($children);
129: }
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139:
140: public function listMembers($sort = null)
141: {
142: $list = new Turba_List();
143:
144: $children = unserialize($this->attributes['__members']);
145: if (!is_array($children)) {
146: $children = array();
147: }
148:
149: reset($children);
150: $modified = false;
151: foreach ($children as $member) {
152: if (strpos($member, ':') === false) {
153: try {
154: $contact = $this->driver->getObject($member);
155: } catch (Turba_Exception $e) {
156:
157: $this->removeMember($member);
158: $modified = true;
159: continue;
160: }
161: } else {
162: list($sourceId, $contactId) = explode(':', $member, 2);
163: if (strpos($contactId, ':')) {
164: list($owner, $contactId) = explode(':', $contactId, 2);
165: $sourceId .= ':' . $owner;
166: }
167:
168: try {
169: $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
170: } catch (Turba_Exception $e) {
171: continue;
172: }
173:
174: try {
175: $contact = $driver->getObject($contactId);
176: } catch (Turba_Exception $e) {
177:
178: $this->removeMember($member);
179: $modified = true;
180: continue;
181: }
182: }
183:
184: $list->insert($contact);
185: }
186:
187:
188: if ($modified) {
189: $this->store();
190: }
191:
192: $list->sort($sort);
193: return $list;
194: }
195:
196: }
197: