RoleModel::deleteAndReplace PHP Method

deleteAndReplace() public method

Delete a role.
public deleteAndReplace ( integer $roleID, integer $newRoleID ) : boolean
$roleID integer The ID of the role to delete.
$newRoleID integer Assign users of the deleted role to this new role.
return boolean Returns **true** on success or **false** on failure.
    public function deleteAndReplace($roleID, $newRoleID)
    {
        // First update users that will be orphaned
        if (is_numeric($newRoleID) && $newRoleID > 0) {
            $this->SQL->options('Ignore', true)->update('UserRole')->join('UserRole urs', 'UserRole.UserID = urs.UserID')->groupBy('urs.UserID')->having('count(urs.RoleID) =', '1', true, false)->set('UserRole.RoleID', $newRoleID)->where(array('UserRole.RoleID' => $roleID))->put();
        } else {
            $this->SQL->delete('UserRole', array('RoleID' => $roleID));
        }
        // Remove permissions for this role.
        $PermissionModel = Gdn::permissionModel();
        $PermissionModel->delete($roleID);
        // Remove the role
        $result = $this->SQL->delete('Role', array('RoleID' => $roleID));
        return $result;
    }

Usage Example

Example #1
0
 /**
  * Remove a role.
  *
  * @since 2.0.0
  * @access public
  */
 public function delete($RoleID = false)
 {
     if (!$this->_permission($RoleID)) {
         return;
     }
     $this->title(t('Delete Role'));
     $this->addSideMenu('dashboard/role');
     $Role = $this->RoleModel->getByRoleID($RoleID);
     if ($Role->Deletable == '0') {
         $this->Form->addError('You cannot delete this role.');
     }
     // Make sure the form knows which item we are deleting.
     $this->Form->addHidden('RoleID', $RoleID);
     // Figure out how many users will be affected by this deletion
     $this->AffectedUsers = $this->RoleModel->getUserCount($RoleID);
     // Figure out how many users will be orphaned by this deletion
     $this->OrphanedUsers = $this->RoleModel->getUserCount($RoleID, true);
     // Get a list of roles other than this one that can act as a replacement
     $this->ReplacementRoles = $this->RoleModel->getByNotRoleID($RoleID);
     if ($this->Form->authenticatedPostBack()) {
         // Make sure that a replacement role has been selected if there were going to be orphaned users
         if ($this->OrphanedUsers > 0) {
             $Validation = new Gdn_Validation();
             $Validation->applyRule('ReplacementRoleID', 'Required', 'You must choose a replacement role for orphaned users.');
             $Validation->validate($this->Form->formValues());
             $this->Form->setValidationResults($Validation->results());
         }
         if ($this->Form->errorCount() == 0) {
             // Go ahead and delete the Role
             $this->RoleModel->deleteAndReplace($RoleID, $this->Form->getValue('ReplacementRoleID'));
             $this->RedirectUrl = url('dashboard/role');
             $this->informMessage(t('Deleting role...'));
         }
     }
     $this->render();
 }