/**
  * Hook performing actual save
  *
  * @param string $newCode
  * @param int $userId
  * @return $changed
  */
 public function setReceptionCode($newCode, $userId)
 {
     // Get the code object
     $code = $this->util->getReceptionCode($newCode);
     // Use the token function as that cascades the consent code
     $changed = $this->token->setReceptionCode($code, $this->formData['gto_comment'], $userId);
     if ($code->isSuccess()) {
         $this->addMessage(sprintf($this->_('Token %s restored.'), $this->token->getTokenId()));
     } else {
         $this->addMessage(sprintf($this->_('Token %s deleted.'), $this->token->getTokenId()));
         if ($code->hasRedoCode()) {
             $newComment = sprintf($this->_('Redo of token %s.'), $this->token->getTokenId());
             if ($this->formData['gto_comment']) {
                 $newComment .= "\n\n";
                 $newComment .= $this->_('Old comment:');
                 $newComment .= "\n";
                 $newComment .= $this->formData['gto_comment'];
             }
             $this->_replacementTokenId = $this->token->createReplacement($newComment, $userId);
             // Create a link for the old token
             $oldToken = strtoupper($this->token->getTokenId());
             $menuItem = $this->menu->findAllowedController('track', 'show');
             if ($menuItem) {
                 $paramSource['gto_id_token'] = $this->token->getTokenId();
                 $paramSource[\Gems_Model::ID_TYPE] = 'token';
                 $href = $menuItem->toHRefAttribute($paramSource);
                 if ($href) {
                     // \MUtil_Echo::track($oldToken);
                     $link = \MUtil_Html::create('a', $href, $oldToken);
                     $oldToken = $link->setView($this->view);
                 }
             }
             // Tell what the user what happened
             $this->addMessage(new \MUtil_Html_Raw(sprintf($this->_('Created this token %s as replacement for token %s.'), strtoupper($this->_replacementTokenId), $oldToken)));
             // Lookup token
             $newToken = $this->loader->getTracker()->getToken($this->_replacementTokenId);
             // Make sure the Next token is set right
             $this->token->setNextToken($newToken);
             // Copy answers when requested.
             if ($code->hasRedoCopyCode()) {
                 $newToken->setRawAnswers($this->token->getRawAnswers());
             }
         }
     }
     $respTrack = $this->token->getRespondentTrack();
     if ($nextToken = $this->token->getNextToken()) {
         if ($recalc = $respTrack->checkTrackTokens($userId, $nextToken)) {
             $this->addMessage(sprintf($this->plural('%d token changed by recalculation.', '%d tokens changed by recalculation.', $recalc), $recalc));
         }
     }
     return $changed;
 }
 /**
  * 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;
 }