/**
  * 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)
 {
     $keys = $this->getKeys();
     // This is the only key to save on, no matter
     // the keys used to initiate the model.
     $this->setKeys($this->_getKeysFor('gems__respondent2track'));
     // Change the end date until the end of the day
     if (isset($newValues['gr2t_end_date']) && $newValues['gr2t_end_date']) {
         $displayFormat = $this->get('gr2t_end_date', 'dateFormat');
         if (!$displayFormat) {
             $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat');
         }
         // Of course do not do so when we got a time format
         if (!\MUtil_Date_Format::getTimeFormat($displayFormat)) {
             $newValues['gr2t_end_date'] = new \MUtil_Date($newValues['gr2t_end_date'], $displayFormat);
             $newValues['gr2t_end_date']->setTimeToDayEnd();
         }
     }
     $newValues = parent::save($newValues, $filter);
     $this->setKeys($keys);
     return $newValues;
 }
 /**
  * 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'])) {
         $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 !== \Gems_Model_RespondentModel::SSN_HIDE) {
             if (\Gems_Model_RespondentModel::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;
         }
     }
     $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->loader->getCurrentUser()->getUserId());
         }
     }
     return $result;
 }