/**
  * Creates the model
  *
  * @return \MUtil_Model_ModelAbstract
  */
 protected function createModel()
 {
     $model = $this->loader->getTracker()->getRespondentTrackModel();
     $model->set('gtr_track_name', 'label', $this->_('Track'));
     $model->set('gr2t_track_info', 'label', $this->_('Description'), 'description', $this->_('Enter the particulars concerning the assignment to this respondent.'));
     $model->set('assigned_by', 'label', $this->_('Assigned by'));
     $model->set('gr2t_start_date', 'label', $this->_('Start'), 'dateFormat', 'dd-MM-yyyy', 'formatFunction', $this->loader->getUtil()->getTranslated()->formatDate, 'default', new \Zend_Date());
     $model->set('gr2t_reception_code');
     return $model;
 }
 /**
  * Get the relations for a given respondentId or patientNr + organizationId combination
  *
  * @param type $respondentId
  * @param type $patientNr
  * @param type $organizationId
  * @return array
  */
 public function getRelationsFor($respondentId, $patientNr = null, $organizationId = null, $onlyActive = true)
 {
     static $relationsCache = array();
     if (is_null($respondentId)) {
         $respondentId = $this->loader->getUtil()->getDbLookup()->getRespondentId($patientNr, $organizationId);
     }
     if (!array_key_exists($respondentId, $relationsCache)) {
         $relations = array();
         $filter = array('grr_id_respondent' => $respondentId);
         if ($onlyActive) {
             $filter['grr_active'] = 1;
         }
         $rawRelations = $this->load($filter);
         foreach ($rawRelations as $relation) {
             $relations[$relation['grr_id']] = join(' ', array($relation['grr_type'], $relation['grr_first_name'], $relation['grr_last_name']));
         }
         $relationsCache[$respondentId] = $relations;
     }
     return $relationsCache[$respondentId];
 }
 /**
  * Add the elements from the model to the bridge for file check step
  *
  * @param \MUtil_Model_Bridge_FormBridgeInterface $bridge
  * @param \MUtil_Model_ModelAbstract $model
  */
 protected function addStepChangeTrack(\MUtil_Model_Bridge_FormBridgeInterface $bridge, \MUtil_Model_ModelAbstract $model)
 {
     $this->displayHeader($bridge, $this->_('Change track information.'), 'h3');
     // Always add organization select, even when they were not exported
     $this->addItems($bridge, $model->getColNames('respondentData'));
     $import = $this->loadImportData();
     // \MUtil_Echo::track($this->formData);
     $all = $this->loader->getUtil()->getTrackData()->getAllSurveys();
     $available = array('' => $this->_('(skip rounds)')) + $all;
     // \MUtil_Echo::track($all);
     $form = $bridge->getForm();
     $surveyHeader = $form->createElement('Html', 'sheader1');
     $surveyHeader->h2($this->_('Survey export code links'));
     $form->addElement($surveyHeader);
     $surveySubHeader = $form->createElement('Html', 'sheader2');
     $surveySubHeader->strong($this->_('Linked survey name'));
     $surveySubHeader->setLabel($this->_('Import survey name'))->setDescription(sprintf($this->_('[%s]'), $this->_('export code')))->setRequired(true);
     $form->addElement($surveySubHeader);
     $this->addItems($bridge, $model->getColNames('isSurvey'));
     // \MUtil_Echo::track($this->_session->uploadFileName, $import->getArrayCopy());
 }
 /**
  *
  * @param array $job
  */
 public function execute($jobId = null)
 {
     $sql = $this->db->select()->from('gems__comm_jobs')->join('gems__comm_templates', 'gcj_id_message = gct_id_template')->where('gcj_active = 1')->where('gcj_id_job = ?', $jobId);
     $job = $this->db->fetchRow($sql);
     if (empty($job)) {
         throw new Exception($this->_('Mail job not found!'));
     }
     $dbLookup = $this->loader->getUtil()->getDbLookup();
     $mailLoader = $this->loader->getMailLoader();
     $sendByMail = $this->getUserEmail($job['gcj_id_user_as']);
     $filter = $dbLookup->getFilterForMailJob($job);
     $tracker = $this->loader->getTracker();
     $model = $tracker->getTokenModel();
     // Fix for #680: token with the valid from the longest in the past should be the
     // used as first token and when multiple rounds start at the same date the
     // lowest round order should be used.
     $model->setSort(array('gto_valid_from' => SORT_ASC, 'gto_round_order' => SORT_ASC));
     $multipleTokensData = $model->load($filter);
     $errors = 0;
     $mails = 0;
     $updates = 0;
     if (count($multipleTokensData)) {
         $sentMailAddresses = array();
         foreach ($multipleTokensData as $tokenData) {
             $mailer = $mailLoader->getMailer('token', $tokenData);
             /* @var $mailer \Gems_Mail_TokenMailer */
             $token = $mailer->getToken();
             $email = $token->getEmail();
             $respondentId = $token->getRespondent()->getId();
             $mail = false;
             $update = false;
             if (!empty($email)) {
                 // Set the from address to use in this job
                 switch ($job['gcj_from_method']) {
                     case 'O':
                         // Send on behalf of organization
                         $organization = $mailer->getOrganization();
                         $from = $organization->getEmail();
                         //$organization->getName() . ' <' . $organization->getEmail() . '>';
                         break;
                     case 'U':
                         // Send on behalf of fixed user
                         $from = $sendByMail;
                         break;
                     case 'F':
                         // Send on behalf of fixed email address
                         $from = $job['gcj_from_fixed'];
                         break;
                     default:
                         throw new \Gems_Exception(sprintf($this->_('Invalid option for `%s`'), $this->_('From address used')));
                 }
                 $mailer->setFrom($from);
                 $mailer->setBy($sendByMail);
                 try {
                     switch ($job['gcj_process_method']) {
                         case 'M':
                             // Each token sends an email
                             $mail = true;
                             $update = true;
                             break;
                         case 'A':
                             // Only first token mailed and marked
                             if (!isset($sentMailAddresses[$respondentId][$email])) {
                                 // When not mailed before
                                 $mail = true;
                                 $update = true;
                             }
                             break;
                         case 'O':
                             // Only first token mailed, all marked
                             if (!isset($sentMailAddresses[$respondentId][$email])) {
                                 // When not mailed before
                                 $mail = true;
                             }
                             $update = true;
                             break;
                         default:
                             throw new \Gems_Exception(sprintf($this->_('Invalid option for `%s`'), $this->_('Processing Method')));
                     }
                     if ($mail == true) {
                         $mailer->setTemplate($job['gcj_id_message']);
                         $mailer->send();
                         $mails++;
                         $sentMailAddresses[$respondentId][$email] = true;
                     }
                     if ($update == true) {
                         $mailer->updateToken();
                         $updates++;
                     }
                 } catch (\Zend_Mail_Exception $exception) {
                     $fields = $mailer->getMailFields(false);
                     $info = sprintf("Error mailing to %s respondent %s with email address %s.", $fields['organization'], $fields['full_name'], $fields['email']);
                     // Use a gems exception to pass extra information to the log
                     $gemsException = new \Gems_Exception($info, 0, $exception);
                     \Gems_Log::getLogger()->logError($gemsException);
                     $errors++;
                 }
             }
         }
     }
     $this->getBatch()->addMessage(sprintf($this->_('Sent %d e-mails with template %s, updated %d tokens.'), $mails, $job['gct_name'], $updates));
     if ($errors) {
         $this->getBatch()->addMessage(sprintf($this->_('%d error(s) occurred while creating mails for template %s. Check error log for details.'), $errors, $job['gct_name']));
     }
 }
 public function getHello($language)
 {
     $genderHello = $this->loader->getUtil()->getTranslated()->getGenderHello($language);
     $hello = $genderHello[$this->getGender()] . ' ' . ucfirst($this->getLastName());
     return $hello;
 }