Turba_Object_Group::createGroup PHP Method

createGroup() public static method

Add members to a group, creating the group if necessary.
public static createGroup ( string $source, array $members, array $opts = [] ) : object
$source string Destination source.
$members array Members of the group. Array of two-element arrays: source and key.
$opts array Additional options:
 - attr: (array) Array of attributes to use to create a new group.
         This should include 'name' at a minimum.
 - gid: (string) Existing Group ID.
 - name: (string) Group Name.
return object Object with the following properties:
  - errors: (array) List of errors when adding members.
  - group: (Turba_Object_Group) Group object.
  - success: (integer) Number of members sucessfully added.
    public static function createGroup($source, $members, array $opts = array())
    {
        global $injector;
        /* Throws Turba_Exception */
        $driver = $injector->getInstance('Turba_Factory_Driver')->create($source);
        if (isset($opts['gid'])) {
            /* Throws Turba_Exception */
            $group = $driver->getObject($opts['gid']);
        } elseif (isset($opts['attr']) && is_array($opts['attr'])) {
            if ($error = Turba::hasMaxContacts($driver)) {
                throw new Turba_Exception($error);
            }
            $newGroup = array_merge($opts['attr'], array('__owner' => $driver->getContactOwner(), '__type' => 'Group'));
            try {
                $group = $driver->getObject($driver->add($newGroup));
            } catch (Turba_Exception $e) {
            }
        } else {
            $group = null;
        }
        if (empty($group) || !$group->isGroup()) {
            throw new Turba_Exception(_("Could not create or add to group."));
        }
        $out = new stdClass();
        $out->errors = array();
        $out->success = 0;
        $out->group = $group;
        // Adding contact to an existing list.
        foreach ($members as $key) {
            try {
                $group->addMember($key[1], $key[0]);
                ++$out->success;
            } catch (Turba_Exception $e) {
                $out->errors[] = $e;
            }
            $group->store();
        }
        return $out;
    }

Usage Example

Exemplo n.º 1
0
Arquivo: Api.php Projeto: Gomez/horde
 /**
  * Adds a group (and its members) to the source provided.
  *
  * @param string $name    Group name.
  * @param array $members  An array of members to add to the group. Format
  *                        is the same as the 'array' argument to the
  *                        import() API function.
  * @param array $opts     Additional options:
  * <pre>
  *   - attr: (array) Additional attributes to add to group.
  *   - source: (string) Source to import contacts to.
  * </pre>
  *
  * @return array  An array with the following keys:
  * <pre>
  *   - added: (integer) The number of addresses added to the group.
  *   - uid: (string) The uid of the group object.
  * </pre>
  *
  * @throws Turba_Exception
  */
 public function addGroup($name, $members, array $opts = array())
 {
     global $injector;
     $source = $this->_getSource(isset($opts['source']) ? $opts['source'] : null);
     $driver = $injector->getInstance('Turba_Factory_Driver')->create($source);
     if (!$driver->hasPermission(Horde_Perms::EDIT)) {
         throw new Turba_Exception(_("Permission denied"));
     }
     $group_add = array();
     foreach ($members as $val) {
         $ob = null;
         $result = $driver->search($val);
         if (count($result)) {
             $ob = $result->reset();
         } else {
             try {
                 $ob = $driver->getObject($driver->add($this->_encodeContent($val)));
             } catch (Exception $e) {
             }
         }
         if ($ob) {
             $group_add[] = array($source, $ob->getValue('__key'));
         }
     }
     $res = Turba_Object_Group::createGroup($source, $group_add, array('attr' => array_merge(isset($opts['attr']) ? $opts['attr'] : array(), array('name' => $name))));
     return array('added' => $res->success, 'uid' => $res->group->getValue('__uid'));
 }
All Usage Examples Of Turba_Object_Group::createGroup