/**
  * Common handler utility to initialize tokens from parameters
  *
  * @return boolean True if there is a real token specified in the request
  */
 protected function _initToken()
 {
     if ($this->tracker) {
         return $this->token && $this->token->exists;
     }
     $this->tracker = $this->loader->getTracker();
     $this->tokenId = $this->tracker->filterToken($this->_getParam(\MUtil_Model::REQUEST_ID));
     if (!$this->tokenId) {
         return false;
     }
     $this->token = $this->tracker->getToken($this->tokenId);
     if (!$this->token->exists) {
         return false;
     }
     if (!($this->currentUser->isActive() || $this->token->getSurvey()->isTakenByStaff())) {
         $tokenLang = strtolower($this->token->getRespondentLanguage());
         // \MUtil_Echo::track($tokenLang, $this->locale->getLanguage());
         if ($tokenLang != $this->locale->getLanguage()) {
             $this->locale->setLocale($tokenLang);
             $this->translateAdapter->setLocale($this->locale);
             $this->currentUser->setLocale($tokenLang);
             \Gems_Cookies::setLocale($tokenLang, $this->basepath->getBasePath());
         }
         $currentOrg = $this->loader->getOrganization();
         $tokenOrgId = $this->token->getOrganizationId();
         if ($tokenOrgId != $currentOrg->getId()) {
             $this->loader->getOrganization($tokenOrgId)->setAsCurrentOrganization();
         }
     }
     return true;
 }
 /**
  * Process the data and return the answers that should be filled in beforehand.
  *
  * Storing the changed values is handled by the calling function.
  *
  * @param \Gems_Tracker_Token $token Gems token object
  * @return array Containing the changed values
  */
 public function processTokenInsertion(\Gems_Tracker_Token $token)
 {
     if ($token->hasSuccesCode() && !$token->isCompleted()) {
         // Preparation for a more general object class
         $code = $token->getSurvey()->getCode();
         $prev = $token;
         while ($prev = $prev->getPreviousToken()) {
             if ($prev->hasSuccesCode() && $prev->isCompleted()) {
                 // Check first on survey id and when that does not work by name.
                 if ($prev->getSurvey()->getCode() == $code) {
                     return $prev->getRawAnswers();
                 }
             }
         }
     }
 }
 /**
  * Get config options for this token
  *
  * Order of reading is track/round, survey, survey code
  *
  * @param \Gems_Tracker_Token $token
  */
 public function getConfig($token)
 {
     try {
         $trackId = $token->getTrackId();
         $roundId = $token->getRoundId();
         $db = \Zend_Db_Table::getDefaultAdapter();
         $select = $db->select()->from('gems__chart_config')->where('gcc_tid = ?', $trackId)->where('gcc_rid = ?', $roundId);
         if ($result = $select->query()->fetch()) {
             $config = \Zend_Json::decode($result['gcc_config']);
             return $config;
         }
         $surveyId = $token->getSurveyId();
         $select = $db->select()->from('gems__chart_config')->where('gcc_sid = ?', $surveyId);
         if ($result = $select->query()->fetch()) {
             $config = \Zend_Json::decode($result['gcc_config']);
             return $config;
         }
         $surveyCode = $token->getSurvey()->getCode();
         $select = $db->select()->from('gems__chart_config')->where('gcc_code = ?', $surveyCode);
         $config = $select->query()->fetch();
         if ($config !== false) {
             $config = \Zend_Json::decode($config['gcc_config']);
         }
         return $config;
     } catch (\Exception $exc) {
         // Just ignore...
     }
     // If all fails, we might be missing the config table
     return false;
 }
 /**
  * Returns an array of {field_names} => values for this token for
  * use in an e-mail tamplate.
  *
  * @param array $tokenData
  * @return array
  */
 public function tokenMailFields()
 {
     if ($this->token) {
         $locale = $this->respondent->getLanguage();
         $survey = $this->token->getSurvey();
         // Count todo
         $tSelect = $this->loader->getTracker()->getTokenSelect(array('all' => 'COUNT(*)', 'track' => $this->db->quoteInto('SUM(CASE WHEN gto_id_respondent_track = ? THEN 1 ELSE 0 END)', $this->token->getRespondentTrackId())));
         $tSelect->andSurveys(array())->forRespondent($this->token->getRespondentId(), $this->organizationId)->forGroupId($survey->getGroupId())->onlyValid();
         $todo = $tSelect->fetchRow();
         // Set the basic fields
         $result['round'] = $this->token->getRoundDescription();
         $organizationLoginUrl = $this->organization->getLoginUrl();
         $result['site_ask_url'] = $organizationLoginUrl . '/ask/';
         // Url's
         $url = $organizationLoginUrl . '/ask/forward/' . \MUtil_Model::REQUEST_ID . '/';
         $url .= $this->token->getTokenId();
         $urlInput = $result['site_ask_url'] . 'index/' . \MUtil_Model::REQUEST_ID . '/' . $this->token->getTokenId();
         $result['survey'] = $survey->getName();
         $result['todo_all'] = sprintf($this->translate->plural('%d survey', '%d surveys', $todo['all'], $locale), $todo['all']);
         $result['todo_all_count'] = $todo['all'];
         $result['todo_track'] = sprintf($this->translate->plural('%d survey', '%d surveys', $todo['track'], $locale), $todo['track']);
         $result['todo_track_count'] = $todo['track'];
         $result['token'] = strtoupper($this->token->getTokenId());
         $result['token_from'] = \MUtil_Date::format($this->token->getValidFrom(), \Zend_Date::DATE_LONG, 'yyyy-MM-dd', $locale);
         $result['token_link'] = '[url=' . $url . ']' . $survey->getName() . '[/url]';
         $result['token_until'] = \MUtil_Date::format($this->token->getValidUntil(), \Zend_Date::DATE_LONG, 'yyyy-MM-dd', $locale);
         $result['token_url'] = $url;
         $result['token_url_input'] = $urlInput;
         $result['track'] = $this->token->getTrackName();
         // Add the code fields
         $codes = $this->token->getRespondentTrack()->getCodeFields();
         foreach ($codes as $code => $data) {
             $key = 'track.' . $code;
             if (is_array($data)) {
                 $data = implode(' ', $data);
             }
             $result[$key] = $data;
         }
         if ($this->token->hasRelation()) {
             $allFields = $this->getMailFields(false);
             // Set about to patient name
             $results['relation_about'] = $allFields['name'];
             $results['relation_about_first_name'] = $allFields['first_name'];
             $results['relation_about_full_name'] = $allFields['full_name'];
             $results['relation_about_greeting'] = $allFields['greeting'];
             $results['relation_about_last_name'] = $allFields['last_name'];
             $results['relation_field_name'] = $this->token->getRelationFieldName();
             if ($relation = $this->token->getRelation()) {
                 // Now update all respondent fields to be of the relation
                 $results['name'] = $relation->getName();
                 $results['first_name'] = $relation->getFirstName();
                 $results['last_name'] = $relation->getLastName();
                 $results['full_name'] = $relation->getHello($locale);
                 $results['greeting'] = $relation->getGreeting($locale);
                 $results['to'] = $relation->getEmail();
                 $this->addTo($results['to'], $results['name']);
             } else {
                 $results['name'] = $this->translate->getAdapter()->_('Undefined relation');
                 $results['first_name'] = '';
                 $results['last_name'] = '';
                 $results['full_name'] = '';
                 $results['greeting'] = '';
                 $results['to'] = '';
             }
         } else {
             $results['relation_about'] = $this->translate->getAdapter()->_('yourself', $this->token->getRespondentLanguage());
             $results['relation_about_first_name'] = '';
             $results['relation_about_full_name'] = '';
             $results['relation_about_greeting'] = '';
             $results['relation_about_last_name'] = '';
             $results['relation_field_name'] = '';
         }
     } else {
         $result['round'] = '';
         $result['site_ask_url'] = '';
         $result['survey'] = '';
         $result['todo_all'] = '';
         $result['todo_all_count'] = '';
         $result['todo_track'] = '';
         $result['todo_track_count'] = '';
         $result['token'] = '';
         $result['token_from'] = '';
         $result['token_link'] = '';
         $result['token_until'] = '';
         $result['token_url'] = '';
         $result['token_url_input'] = '';
         $result['track'] = '';
         $result['relation_about'] = $this->translate->getAdapter()->_('yourself');
         $result['relation_about_first_name'] = '';
         $result['relation_about_full_name'] = '';
         $result['relation_about_greeting'] = '';
         $result['relation_about_last_name'] = '';
         $result['relation_field_name'] = '';
     }
     return $result;
 }
 /**
  * Determines if this particular token should be included
  * in the report
  *
  * @param  \Gems_Tracker_Token $token
  * @return boolean This dummy implementation always returns true
  */
 protected function _isTokenInFilter(\Gems_Tracker_Token $token)
 {
     $result = false;
     // Only if token has a success code
     if ($token->getReceptionCode()->isSuccess()) {
         $result = true;
     }
     if ($result) {
         $tokenInfo = array('code' => $token->getSurvey()->getCode(), 'surveyid' => $token->getSurveyId(), 'tokenid' => $token->getTokenId());
         // Now check if the tokenfilter is true
         if (empty($this->tokenFilter)) {
             $result = true;
         } else {
             $result = false;
             // Now read the filter and split by track code or track id
             foreach ($this->tokenFilter as $filter) {
                 $remaining = array_diff_assoc($filter, $tokenInfo);
                 if (empty($remaining)) {
                     $result = true;
                     break;
                 }
             }
         }
     }
     return $result;
 }