/**
  * Load the organization from the given organization id
  */
 protected function loadOrganization()
 {
     if (!$this->organizationId) {
         $this->loadOrganizationId();
     }
     $this->organization = $this->loader->getOrganization($this->organizationId);
 }
 /**
  *
  * @param boolean $detailed True when the current action is not in $summarizedActions.
  * @param string $action The current action.
  * @param int $defaultOrgId The default organization id or null if current organization
  * @return \Gems_Model_StaffModel
  */
 public function applySettings($detailed, $action, $defaultOrgId)
 {
     $this->resetOrder();
     $dbLookup = $this->util->getDbLookup();
     $editing = $action == 'edit' || $action == 'create';
     $translated = $this->util->getTranslated();
     $user = $this->loader->getCurrentUser();
     $yesNo = $translated->getYesNo();
     if ($editing) {
         $ucfirst = new \Zend_Filter_Callback('ucfirst');
         if ($this->project->isLoginShared()) {
             $this->set('gsf_login', 'validator', $this->createUniqueValidator('gsf_login', array('gsf_id_user')));
         } else {
             // per organization
             $this->set('gsf_login', 'validator', $this->createUniqueValidator(array('gsf_login', 'gsf_id_organization'), array('gsf_id_user')));
         }
     } else {
         $ucfirst = null;
     }
     $this->set('gsf_login', 'label', $this->_('Username'), 'minlength', 4, 'required', true, 'size', 15);
     if ($user->hasPrivilege('pr.staff.see.all') || !$editing) {
         // Select organization
         $options = $dbLookup->getOrganizations();
     } else {
         $options = $user->getAllowedOrganizations();
     }
     $this->set('gsf_id_organization', 'label', $this->_('Organization'), 'multiOptions', $options, 'required', true);
     if ($detailed) {
         $this->set('gsf_first_name', 'label', $this->_('First name'), 'filters[ucfirst]', $ucfirst);
         $this->set('gsf_surname_prefix', 'label', $this->_('Surname prefix'), 'description', $this->_('de, van der, \'t, etc...'));
         $this->set('gsf_last_name', 'label', $this->_('Last name'), 'required', true, 'filters[ucfirst]', $ucfirst);
     } else {
         $this->set('name', 'label', $this->_('Name'));
     }
     $this->set('gsf_gender', 'label', $this->_('Gender'), 'elementClass', 'Radio', 'multiOptions', $translated->getGenders(), 'separator', ' ');
     $this->set('gsf_email', 'label', $this->_('E-Mail'), 'itemDisplay', array('MUtil_Html_AElement', 'ifmail'), 'size', 30, 'validators[email]', 'SimpleEmail');
     $this->set('gsf_id_primary_group', 'label', $this->_('Primary function'), 'multiOptions', $editing ? $user->getAllowedStaffGroups() : $dbLookup->getStaffGroups());
     if ($detailed) {
         // Now try to load the current organization and find out if it has a default user definition
         // otherwise use the defaultStaffDefinition
         $organization = $this->loader->getOrganization($defaultOrgId ? $defaultOrgId : $user->getCurrentOrganizationId());
         $this->set('gsf_id_organization', 'default', $organization->getId());
         $this->set('gul_user_class', 'label', $this->_('User Definition'), 'default', $organization->get('gor_user_class', $this->defaultStaffDefinition), 'multiOptions', $this->loader->getUserLoader()->getAvailableStaffDefinitions());
         if ($editing) {
             $this->set('gul_user_class', 'order', 1, 'required', true);
         }
         $this->set('gsf_iso_lang', 'label', $this->_('Language'), 'default', $this->project->locale['default'], 'multiOptions', $this->util->getLocalized()->getLanguages());
         $this->set('gul_can_login', 'label', $this->_('Can login'), 'default', 1, 'description', $this->_('Users can only login when this box is checked.'), 'elementClass', 'Checkbox', 'multiOptions', $yesNo);
         $this->set('gsf_logout_on_survey', 'label', $this->_('Logout on survey'), 'description', $this->_('If checked the user will logoff when answering a survey.'), 'elementClass', 'Checkbox', 'multiOptions', $yesNo);
         $this->set('gsf_mail_watcher', 'label', $this->_('Check cron job mail'), 'description', $this->_('If checked the user will be mailed when the cron job does not run on time.'), 'elementClass', 'Checkbox', 'multiOptions', $yesNo);
     }
     $this->set('gsf_active', 'label', $this->_('Active'), 'elementClass', 'None', 'multiOptions', $yesNo);
     $this->setDeleteValues('gsf_active', 0, 'gul_can_login', 0);
     if (!$user->hasPrivilege('pr.staff.edit.all')) {
         $this->set('gsf_id_organization', 'elementClass', 'Exhibitor');
     }
     return $this;
 }
 /**
  * Hook that loads the form data from $_POST or the model
  *
  * Or from whatever other source you specify here.
  */
 protected function loadFormData()
 {
     parent::loadFormData();
     if (isset($this->formData['gor_id_organization']) && $this->formData['gor_id_organization']) {
         $model = $this->getModel();
         // Strip self from list of organizations
         $multiOptions = $model->get('gor_accessible_by', 'multiOptions');
         unset($multiOptions[$this->formData['gor_id_organization']]);
         $model->set('gor_accessible_by', 'multiOptions', $multiOptions);
         // Show allowed organisations
         $org = $this->loader->getOrganization($this->formData['gor_id_organization']);
         $allowedOrgs = $org->getAllowedOrganizations();
         //Strip self
         unset($allowedOrgs[$this->formData['gor_id_organization']]);
         $display = join(', ', $allowedOrgs);
         if (!$display) {
             $display = \MUtil_Html::create('em', $this->_('No access to other organizations.'));
         }
         $model->set('allowed', 'value', $display);
     }
 }
 /**
  * Returns the caption for this table
  *
  * @param boolean $onlyNotCurrent Only return a string when the organization is different
  * @return string
  */
 protected function getCaption($onlyNotCurrent = false)
 {
     $orgId = $this->request->getParam(\MUtil_Model::REQUEST_ID2);
     if ($orgId == $this->loader->getCurrentUser()->getCurrentOrganizationId()) {
         if ($onlyNotCurrent) {
             return;
         } else {
             return $this->_('Respondent information');
         }
     } else {
         return sprintf($this->_('%s respondent information'), $this->loader->getOrganization($orgId)->getName());
     }
 }
 /**
  * Hook that loads the form data from $_POST or the model
  *
  * Or from whatever other source you specify here.
  */
 protected function loadFormData()
 {
     parent::loadFormData();
     if ($this->request->isPost()) {
         if (!$this->_saveButton || !$this->_saveButton->isChecked()) {
             if (isset($this->formData['grs_ssn']) && $this->formData['grs_ssn']) {
                 $filter = array('grs_ssn' => $this->formData['grs_ssn'], 'gr2o_id_organization' => true);
                 if ($this->formData['gr2o_id_organization']) {
                     $orgId = $this->formData['gr2o_id_organization'];
                 } else {
                     $orgId = $this->model->get('gr2o_id_organization', 'default');
                 }
                 $order = array($this->db->quoteInto("CASE WHEN gr2o_id_organization = ? THEN 1 ELSE 2 END", $orgId) => SORT_ASC);
                 $data = $this->model->loadFirst($filter, $order);
                 if ($data && !isset($this->formData[$this->saveButtonId])) {
                     // \MUtil_Echo::track($this->formData);
                     // \MUtil_Echo::track($data);
                     // Do not use this value
                     unset($data['grs_ssn']);
                     if ($data['gr2o_id_organization'] == $orgId) {
                         // gr2o_patient_nr
                         // gr2o_id_organization
                         $this->addMessage($this->_('Known respondent.'));
                         //*
                         foreach ($data as $name => $value) {
                             if (substr($name, 0, 4) == 'grs_' || substr($name, 0, 5) == 'gr2o_') {
                                 if (array_key_exists($name, $this->formData)) {
                                     $this->formData[$name] = $value;
                                 }
                                 $cname = $this->model->getKeyCopyName($name);
                                 if (array_key_exists($cname, $this->formData)) {
                                     $this->formData[$cname] = $value;
                                 }
                             }
                         }
                         // */
                     } else {
                         $org = $this->loader->getOrganization($data['gr2o_id_organization']);
                         $this->addMessage(sprintf($this->_('Respondent data retrieved from %s.'), $org->getName()));
                         foreach ($data as $name => $value) {
                             if (substr($name, 0, 4) == 'grs_' && array_key_exists($name, $this->formData)) {
                                 $this->formData[$name] = $value;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Save a single model item.
  *
  * @param array $newValues The values to store for a single model item.
  * @param array $filter If the filter contains old key values these are used
  * to decide on update versus insert.
  * @return array The values as they are after saving (they may change).
  */
 public function save(array $newValues, array $filter = null, array $saveTables = null)
 {
     // If the respondent id is not set, check using the
     // patient number and then the ssn
     if (!(isset($newValues['grs_id_user']) && $newValues['grs_id_user'])) {
         $id = false;
         if (isset($newValues['gr2o_patient_nr'], $newValues['gr2o_id_organization'])) {
             $sql = 'SELECT gr2o_id_user
                     FROM gems__respondent2org
                     WHERE gr2o_patient_nr = ? AND gr2o_id_organization = ?';
             $id = $this->db->fetchOne($sql, array($newValues['gr2o_patient_nr'], $newValues['gr2o_id_organization']));
         }
         if (!$id && isset($newValues['grs_ssn']) && $this->hashSsn !== self::SSN_HIDE) {
             if (self::SSN_HASH === $this->hashSsn) {
                 $search = $this->saveSSN($newValues['grs_ssn']);
             } else {
                 $search = $newValues['grs_ssn'];
             }
             $sql = 'SELECT grs_id_user FROM gems__respondents WHERE grs_ssn = ?';
             $id = $this->db->fetchOne($sql, $search);
             // Check for change in patient ID
             if ($id && isset($newValues['gr2o_id_organization'])) {
                 $sql = 'SELECT gr2o_patient_nr
                         FROM gems__respondent2org
                         WHERE gr2o_id_user = ? AND gr2o_id_organization = ?';
                 $patientId = $this->db->fetchOne($sql, array($id, $newValues['gr2o_id_organization']));
                 if ($patientId) {
                     $copyId = $this->getKeyCopyName('gr2o_patient_nr');
                     $newValues[$copyId] = $patientId;
                 }
             }
         }
         if ($id) {
             $newValues['grs_id_user'] = $id;
             $newValues['gr2o_id_user'] = $id;
         }
         // If empty, then set by \Gems_Model->createGemsUserId()
     }
     $result = parent::save($newValues, $filter, $saveTables);
     if (isset($result['gr2o_id_organization']) && isset($result['grs_id_user'])) {
         // Tell the organization it has at least one user
         $org = $this->loader->getOrganization($result['gr2o_id_organization']);
         if ($org) {
             $org->setHasRespondents($this->currentUser->getUserId());
         }
         $this->handleRespondentChanged($result['gr2o_patient_nr'], $org, $result['grs_id_user']);
     }
     return $result;
 }
 /**
  *
  * @return \Gems_User_Organization
  */
 public function getOrganization()
 {
     return $this->loader->getOrganization($this->organizationId);
 }