public function execute()
 {
     $ids = waRequest::request('id', array(), 'array_int');
     if (!$ids) {
         throw new waException('Contact id not specified.');
     }
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException(_w('Access denied'));
     }
     $groups = waRequest::post('groups', array(), 'array_int');
     $counters = array();
     $ugm = new waUserGroupsModel();
     if ($this->getRequest()->request('set')) {
         foreach ($ids as $id) {
             $ugm->delete($id, array());
         }
     }
     foreach ($ids as $id) {
         if ($groups) {
             $ugm->add(array_map(wa_lambda('$gid', 'return array(' . $id . ', $gid);'), $groups));
         }
     }
     $gm = new waGroupModel();
     foreach ($groups as $gid) {
         $cnt = $ugm->countByField(array('group_id' => $gid));
         $gm->updateCount($gid, $cnt);
         $counters[$gid] = $cnt;
     }
     $this->response['counters'] = $counters;
     $this->response['message'] = _w("%d user has been added", "%d users have been added", count($ids));
     $this->response['message'] .= ' ';
     $this->response['message'] .= _w("to %d group", "to %d groups", count($groups));
 }
 public function execute()
 {
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException('Access denied.');
     }
     $group_model = new waGroupModel();
     // Create a group or retreive by id
     $id = waRequest::post('id');
     $name = waRequest::post('name');
     if (!$id) {
         if (!$name && $name !== '0') {
             throw new waException('No group id and no name given.');
         }
         $id = $group_model->add($name);
         $this->log('group_add', 1);
     } else {
         if ($name || $name === '0') {
             $group_model->updateById($id, array('name' => $name));
         }
     }
     if (!$id) {
         throw new waException('Still no id here...');
         // should not happen
     }
     $group = $group_model->getById($id);
     if (!$group) {
         throw new waException('No group with such id: ' . $id);
     }
     $this->response['id'] = $id;
     $users = waRequest::post('users', array(), 'array_int');
     $type = waRequest::post('user_operation');
     $user_groups_model = new waUserGroupsModel();
     switch ($type) {
         case 'del':
             if ($users) {
                 $user_groups_model->delete($users, $id);
             }
             break;
         case 'set':
             $user_groups_model->emptyGroup($id);
             // breakthrough
         // breakthrough
         case 'add':
         default:
             if (!$users) {
                 break;
             }
             $data = array();
             foreach ($users as $contact_id) {
                 $data[] = array($contact_id, $id);
             }
             $user_groups_model->add($data);
             if ($type == 'set') {
                 $group_model->updateCount($id, count($users));
             }
             break;
     }
 }
 public function execute()
 {
     if (!($id = (int) waRequest::get('id'))) {
         throw new waException('Contact id not specified.');
     }
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException('Access denied.');
     }
     $groups = waRequest::post('groups', array(), 'array_int');
     $ugm = new waUserGroupsModel();
     $ugm->delete($id, array());
     if ($groups) {
         $ugm->add(array_map(wa_lambda('$gid', 'return array(' . $id . ', $gid);'), $groups));
     }
     $this->response = 'ok';
 }