/**
  * Switch the active organization
  */
 public function changeUiAction()
 {
     $request = $this->getRequest();
     $orgId = urldecode($request->getParam('org'));
     $oldOrg = $this->currentUser->getCurrentOrganizationId();
     $origUrl = base64_decode($request->getParam('current_uri'));
     $allowedOrganizations = $this->currentUser->getAllowedOrganizations();
     if (isset($allowedOrganizations[$orgId])) {
         $this->currentUser->setCurrentOrganization($orgId);
         if ($origUrl) {
             // Check for organisation id in url, but not when a patient id is stated
             if (strpos($origUrl, '/' . \MUtil_Model::REQUEST_ID1 . '/') === false) {
                 foreach ($this->currentUser->possibleOrgIds as $key) {
                     $finds[] = '/' . $key . '/' . $oldOrg;
                     $replaces[] = '/' . $key . '/' . $orgId;
                 }
                 $correctUrl = str_replace($finds, $replaces, $origUrl);
             } else {
                 $correctUrl = $origUrl;
             }
             // \MUtil_Echo::track($origUrl, $correctUrl);
             $this->getResponse()->setRedirect($correctUrl);
         } else {
             $this->currentUser->gotoStartPage($this->menu, $request);
         }
         return;
     }
     throw new \Gems_Exception($this->_('Inaccessible or unknown organization'), 403, null, sprintf($this->_('Access to this page is not allowed for current role: %s.'), $this->currentUser->getRole()));
 }
 /**
  * Creates a model for getModel(). Called only for each new $action.
  *
  * The parameters allow you to easily adapt the model to the current action. The $detailed
  * parameter was added, because the most common use of action is a split between detailed
  * and summarized actions.
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @return \MUtil_Model_ModelAbstract
  */
 public function createModel($detailed, $action)
 {
     $defaultOrgId = null;
     if ($detailed) {
         // Make sure the user is loaded
         $user = $this->getSelectedUser();
         if ($user) {
             if (!($this->currentUser->hasPrivilege('pr.staff.see.all') || $this->currentUser->isAllowedOrganization($user->getBaseOrganizationId()))) {
                 throw new \Gems_Exception($this->_('No access to page'), 403, null, sprintf($this->_('You have no right to access users from the organization %s.'), $user->getBaseOrganization()->getName()));
             }
             switch ($action) {
                 case 'create':
                 case 'show':
                 case 'mail':
                     break;
                 default:
                     if (!$user->hasAllowedRole()) {
                         throw new \Gems_Exception($this->_('No access to page'), 403, null, sprintf($this->_('As %s user you have no right to access users with the role %s.'), $this->currentUser->getRole(), $user->getRole()));
                     }
             }
             $defaultOrgId = $user->getBaseOrganizationId();
         }
     }
     // \MUtil_Model::$verbose = true;
     $model = $this->loader->getModels()->getStaffModel(!('deactivate' === $action || 'reactivate' === $action));
     $model->applySettings($detailed, $action, $defaultOrgId);
     return $model;
 }
 /**
  * Stores the fields that can be used for sorting or filtering in the
  * sort / filter objects attached to this model.
  *
  * @param array $parameters
  * @param boolean $includeNumericFilters When true numeric filter keys (0, 1, 2...) are added to the filter as well
  * @return array The $parameters minus the sort & textsearch keys
  */
 public function applyParameters(array $parameters, $includeNumericFilters = false)
 {
     if ($parameters) {
         // Allow use when passed only an ID value
         if (isset($parameters[\MUtil_Model::REQUEST_ID]) && !isset($parameters[\MUtil_Model::REQUEST_ID1], $parameters[\MUtil_Model::REQUEST_ID2])) {
             $id = $parameters[\MUtil_Model::REQUEST_ID];
             $keys = $this->getKeys();
             $field = array_shift($keys);
             $parameters[$field] = $id;
             if ($field2 = array_shift($keys)) {
                 $parameters[$field2] = $this->getCurrentOrganization();
                 \MUtil_Echo::r('Still using old HiddenModel parameters.', 'DEPRECIATION WARNING');
                 \MUtil_Echo::r($parameters);
             }
             unset($parameters[\MUtil_Model::REQUEST_ID]);
         }
         if (isset($parameters[\MUtil_Model::REQUEST_ID2]) && !array_key_exists($parameters[\MUtil_Model::REQUEST_ID2], $this->currentUser->getAllowedOrganizations())) {
             $this->initTranslateable();
             throw new \Gems_Exception($this->_('Inaccessible or unknown organization'), 403, null, sprintf($this->_('Access to this page is not allowed for current role: %s.'), $this->currentUser->getRole()));
         }
         return parent::applyParameters($parameters, $includeNumericFilters);
     }
     return array();
 }