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_Object_Group:: class provides a set of methods for dealing with
  4:  * contact groups.
  5:  *
  6:  * @author  Chuck Hagenbuch <chuck@horde.org>
  7:  * @author  Jon Parise <jon@csh.rit.edu>
  8:  * @package Turba
  9:  */
 10: class Turba_Object_Group extends Turba_Object
 11: {
 12:     /**
 13:      * Constructs a new Turba_Object_Group.
 14:      *
 15:      * @param Turba_Driver $driver  The driver object that this group comes
 16:      *                              from.
 17:      * @param array $attributes     Hash of attributes for this group.
 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:      * Returns true if this object is a group of multiple contacts.
 27:      *
 28:      * @return boolean  True.
 29:      */
 30:     public function isGroup()
 31:     {
 32:         return true;
 33:     }
 34: 
 35:     /**
 36:      * Contact url.
 37:      *
 38:      * @return Horde_Url
 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:      * Adds a new contact entry to this group.
 50:      *
 51:      * @param string $contactId  The id of the contact to add.
 52:      * @param string $sourceId   The source $contactId is from.
 53:      *
 54:      * @throws Turba_Exception
 55:      */
 56:     public function addMember($contactId, $sourceId = null)
 57:     {
 58:         // Default to the same source as the group.
 59:         if (is_null($sourceId)) {
 60:             $sourceId = $this->getSource();
 61:         }
 62: 
 63:         // Can't add a group to itself.
 64:         if ($contactId == $this->attributes['__key']) {
 65:             throw new Turba_Exception(_("Can't add a group to itself."));
 66:         }
 67: 
 68:         // Try to find the contact being added.
 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:         // Explode members.
 77:         $members = @unserialize($this->attributes['__members']);
 78:         if (!is_array($members)) {
 79:             $members = array();
 80:         }
 81: 
 82:         // If the contact is from a different source, store its source
 83:         // id as well.
 84:         $members[] = ($sourceId == $this->getSource())
 85:             ? $contactId
 86:             : $sourceId . ':' . $contactId;
 87: 
 88:         // Remove duplicates.
 89:         $this->attributes['__members'] = serialize(array_unique($members));
 90:     }
 91: 
 92:     /**
 93:      * Deletes a contact from this group.
 94:      *
 95:      * @param string $contactId  The id of the contact to remove.
 96:      * @param string $sourceId   The source $contactId is from.
 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:      * Count the number of contacts in this group.
119:      *
120:      * @return integer
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:      * Retrieve the Objects in this group
134:      *
135:      * @param array $sort   The requested sort order which is passed to
136:      *                      Turba_List::sort().
137:      *
138:      * @return Turba_List   List containing the members of this group
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:                     // Remove the contact if it no longer exists
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:                     // Remove the contact if it no longer exists
178:                     $this->removeMember($member);
179:                     $modified = true;
180:                     continue;
181:                 }
182:             }
183: 
184:             $list->insert($contact);
185:         }
186: 
187:         // If we've pruned any dead entries, store the changes.
188:         if ($modified) {
189:             $this->store();
190:         }
191: 
192:         $list->sort($sort);
193:         return $list;
194:     }
195: 
196: }
197: 
API documentation generated by ApiGen