insertProfileGroup() public static method

Add a profile to a group.
public static insertProfileGroup ( array $values ) : integer
$values array Membership data.
return integer
Example #1
0
 /**
  * Execute the action.
  */
 public function execute()
 {
     // call parent, this will probably add some general CSS/JS or other required files
     parent::execute();
     // action to execute
     $action = \SpoonFilter::getGetValue('action', array('addToGroup', 'delete'), '');
     $ids = isset($_GET['id']) ? (array) $_GET['id'] : array();
     $newGroupId = \SpoonFilter::getGetValue('newGroup', array_keys(BackendProfilesModel::getGroups()), '');
     // no ids provided
     if (empty($ids)) {
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=no-profiles-selected');
     }
     // delete the given profiles
     if ($action === 'delete') {
         BackendProfilesModel::delete($ids);
         $report = 'deleted';
     } elseif ($action === 'addToGroup') {
         // add the profiles to the given group
         // no group id provided
         if ($newGroupId == '') {
             $this->redirect(BackendModel::createURLForAction('Index') . '&error=no-group-selected');
         }
         // set new status
         foreach ($ids as $id) {
             // profile must exist
             if (BackendProfilesModel::exists($id)) {
                 // make sure the user is not already part of this group without an expiration date
                 foreach (BackendProfilesModel::getProfileGroups($id) as $existingGroup) {
                     // if he is, skip to the next user
                     if ($existingGroup['group_id'] === $newGroupId) {
                         continue 2;
                     }
                 }
                 // OK, it's safe to add the user to this group
                 BackendProfilesModel::insertProfileGroup(array('profile_id' => $id, 'group_id' => $newGroupId, 'starts_on' => BackendModel::getUTCDate()));
             }
         }
         // report
         $report = 'added-to-group';
     } else {
         // unknown action
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=unknown-action');
     }
     // report
     $report = (count($ids) > 1 ? 'profiles-' : 'profile-') . $report;
     // redirect
     $this->redirect(BackendModel::createURLForAction('Index', null, null, array('offset' => \SpoonFilter::getGetValue('offset', null, ''), 'order' => \SpoonFilter::getGetValue('order', null, ''), 'sort' => \SpoonFilter::getGetValue('sort', null, ''), 'email' => \SpoonFilter::getGetValue('email', null, ''), 'status' => \SpoonFilter::getGetValue('status', null, ''), 'group' => \SpoonFilter::getGetValue('group', null, ''))) . '&report=' . $report);
 }
Example #2
0
 /**
  * Validate the form.
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // get fields
         $ddmGroup = $this->frm->getField('group');
         $txtExpirationDate = $this->frm->getField('expiration_date');
         $txtExpirationTime = $this->frm->getField('expiration_time');
         // fields filled?
         $ddmGroup->isFilled(BL::getError('FieldIsRequired'));
         if ($txtExpirationDate->isFilled()) {
             $txtExpirationDate->isValid(BL::getError('DateIsInvalid'));
         }
         if ($txtExpirationTime->isFilled()) {
             $txtExpirationTime->isValid(BL::getError('TimeIsInvalid'));
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $values['profile_id'] = $this->id;
             $values['group_id'] = $ddmGroup->getSelected();
             $values['starts_on'] = BackendModel::getUTCDate();
             // only format date if not empty
             if ($txtExpirationDate->isFilled() && $txtExpirationTime->isFilled()) {
                 // format date
                 $values['expires_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($txtExpirationDate, $txtExpirationTime));
             }
             // insert values
             $id = BackendProfilesModel::insertProfileGroup($values);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_profile_add_to_group', array('item' => $values));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Edit') . '&id=' . $values['profile_id'] . '&report=membership-added&highlight=row-' . $id . '#tabGroups');
         }
     }
 }