Turba_Object_Group::addMember PHP Method

addMember() public method

Adds a new contact entry to this group.
public addMember ( string $contactId, string $sourceId = null )
$contactId string The id of the contact to add.
$sourceId string The source $contactId is from.
    public 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']) {
            throw new Turba_Exception(_("Can't add a contact list to itself."));
        }
        // Try to find the contact being added.
        if ($sourceId == $this->getSource()) {
            $contact = $this->driver->getObject($contactId);
        } else {
            $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($sourceId);
            $contact = $driver->getObject($contactId);
        }
        // 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.
        $members[] = $sourceId == $this->getSource() ? $contactId : $sourceId . ':' . $contactId;
        // Remove duplicates.
        $this->attributes['__members'] = serialize(array_unique($members));
    }

Usage Example

Exemplo n.º 1
0
Arquivo: data.php Projeto: horde/horde
                 $notification->push(sprintf(_("There was an error importing the data: %s"), $e->getMessage()), 'horde.error');
                 $error = true;
                 break;
             }
         }
     }
     // Now attempt to create Turba group objects.
     foreach ($contact_groups as $group) {
         $attributes = $group;
         unset($attributes['__members']);
         $group_obj = new Turba_Object_Group($driver, $attributes);
         foreach (explode(',', $group['__members']) as $uid) {
             $results = $driver->search(array('__uid' => $uid));
             if (count($results->objects)) {
                 $object = array_pop($results->objects);
                 $group_obj->addMember($object->getValue('__key'), $object->getSource());
             }
         }
         // We don't actually use the group object to save to storage since
         // it's not an existing group. We use it so it's responsible for
         // properly formatting the __members data, which we pull out and
         // place in the attributes array.
         $attributes['__members'] = $group_obj->getValue('__members');
         $attributes['__type'] = 'group';
         $driver->add($attributes);
     }
     if (!$error && $imported) {
         $notification->push(sprintf(_("%s file successfully imported."), $file_types[$data->storage->get('format')]), 'horde.success');
     }
 }
 $next_step = $data->cleanup();