diff --git a/CRM/Event/Form/Task/PDF.php b/CRM/Event/Form/Task/PDF.php index 1632374e7aa2..b5adb31c42fd 100644 --- a/CRM/Event/Form/Task/PDF.php +++ b/CRM/Event/Form/Task/PDF.php @@ -80,7 +80,66 @@ public function buildQuickForm() { * Process the form after the input has been submitted and validated. */ public function postProcess() { - CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this); + // I think the most important thing the two lines below do, is returning + // $html_message. But it also takes care of saving a new template. + $formValues = $this->controller->exportValues($this->getName()); + + // This creates a dependency to CRM_Contact_Form_TAsk_PDFLetterCommon. + // Not sure how bad this is... + list($formValues, $categories, $html_message, $messageToken, $returnProperties) = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues); + + $skipOnHold = isset($this->skipOnHold) ? $this->skipOnHold : FALSE; + $skipDeceased = isset($this->skipDeceased) ? $this->skipDeceased : TRUE; + + foreach ($this->_participantIds as $participantID) { + $participant_result = civicrm_api3('Participant', 'get', array( + 'id' => $participantID, + 'api.Event.getsingle' => array('id' => '$value.event_id'), + )); + $participant = CRM_Utils_Array::first($participant_result['values']); + $event = $participant['api.Event.getsingle']; + + // get contact information + // Use 'getTokenDetails' so that hook_civicrm_tokenValues is called. + $contactId = $participant['contact_id']; + $tokenDetails = CRM_Utils_Token::getTokenDetails(array($contactId), + $returnProperties, + $skipOnHold, + $skipDeceased, + NULL, + $messageToken, + 'CRM_Event_Form_Task_PDF' + ); + + if (!CRM_Utils_Array::first($tokenDetails)) { + continue; + } + $contact = CRM_Utils_Array::first(CRM_Utils_Array::first($tokenDetails)); + + $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact, TRUE, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceEntityTokens('event', $event, $tokenHtml, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceEntityTokens('participant', $participant, $tokenHtml, $messageToken); + $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact, $categories, TRUE); + + if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { + $smarty = CRM_Core_Smarty::singleton(); + // also add the contact tokens to the template + $smarty->assign_by_ref('contact', $contact); + $smarty->assign_by_ref('event', $event); + $smarty->assign_by_ref('participant', $participant); + $tokenHtml = $smarty->fetch("string:$tokenHtml"); + } + + $html[] = $tokenHtml; + } + + CRM_Contact_Form_Task_PDFLetterCommon::createActivities($this, $html_message, $this->_contactIds); + + CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); + + $this->postProcessHook(); + + CRM_Utils_System::civiExit(1); } /** @@ -99,6 +158,21 @@ public function setDefaultValues() { */ public function listTokens() { $tokens = CRM_Core_SelectValues::contactTokens(); + $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens); + // unset contact_email and contact_phone tokens. + // These are location_email and location_contact + // Should be cleaned up after ActionSchedule token replacement cleanup. + unset($tokens['{event.contact_email}']); + unset($tokens['{event.contact_phone}']); + $customEventTokens = CRM_Core_BAO_CustomField::getFields('Event'); + + foreach ($customEventTokens as $customEventTokenKey => $customEventTokenValue) { + $tokens["{event.custom_$customEventTokenKey}"] = $customEventTokenValue['label'] . '::' . $customEventTokenValue['groupTitle']; + } + $tokens = array_merge(CRM_Core_SelectValues::participantTokens(), $tokens); + unset($tokens['{participant.template_title}']); + unset($tokens['{participant.fee_label}']); + unset($tokens['{participant.default_role_id}']); return $tokens; } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 63d6cbf82306..107b8b377b5a 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1605,6 +1605,44 @@ protected static function _buildMembershipTokens() { } } + /** + * store event tokens on the static _tokens array + */ + protected static function _buildEventTokens() { + $key = 'event'; + if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) { + $eventTokens = array(); + $tokens = CRM_Core_SelectValues::eventTokens(); + foreach ($tokens as $token => $dontCare) { + $eventTokens[] = substr($token, (strpos($token, '.') + 1), -1); + } + $customtokens = CRM_Core_BAO_CustomField::getFields('Event'); + foreach ($customtokens as $tokenkey => $tokenvalue) { + $eventTokens[] = 'custom_' . $tokenkey; + } + self::$_tokens[$key] = $eventTokens; + } + } + + /** + * store participant tokens on the static _tokens array + */ + protected static function _buildParticipantTokens() { + $key = 'participant'; + if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) { + $participantTokens = array(); + $tokens = CRM_Core_SelectValues::participantTokens(); + foreach ($tokens as $token => $dontCare) { + $participantTokens[] = substr($token, (strpos($token, '.') + 1), -1); + } + $customtokens = CRM_Core_BAO_CustomField::getFields('Participant'); + foreach ($customtokens as $tokenkey => $tokenvalue) { + $participantTokens[] = 'custom_' . $tokenkey; + } + self::$_tokens[$key] = $participantTokens; + } + } + /** * Replace tokens for an entity. * @param string $entity @@ -1758,6 +1796,146 @@ public static function replaceMultipleContributionTokens($separator, $str, &$con return self::replaceContributionTokens($str, $contribution, $html, $knownTokens, $escapeSmarty); } + /** + * Get replacement strings for any event tokens + * @param string $entity - name of entity type, should be Event. + * @param string $token + * @param array $event an api result array for a single event + * @param bool $escapeSmarty + * @return string token replacement + */ + public static function getEventTokenReplacement($entity, $token, $event, $escapeSmarty = FALSE) { + $entity = strtolower($entity); + if ($entity != 'event') { + // Not sure which exception is appropriate. + throw new Exception('$entity is expected to be "event".'); + } + + self::_buildEventTokens(); + + $params = array('entity_id' => $event['id'], 'entity_table' => 'civicrm_event'); + + switch ($token) { + case 'title': + $value = $event['event_title']; + break; + + case 'end_date': + case 'start_date': + $value = CRM_Utils_Date::customFormat($event[$token]); + break; + + case 'type': + $params = array( + 'option_group_id' => 14, + 'value' => $event['event_type_id'], + 'return' => 'label', + ); + $value = civicrm_api3('OptionValue', 'getvalue', $params); + break; + + case 'location': + $location = CRM_Core_BAO_Location::getValues($params, TRUE); + foreach ($location['address'] as $address) { + $value = $address['display_text']; + } + break; + + case 'info_url': + $value = CIVICRM_UF_BASEURL . 'civicrm/event/info?reset=1&id=' . $event['id']; + break; + + case 'registration_url': + $value = CIVICRM_UF_BASEURL . 'civicrm/event/register?reset=1&id=' . $event['id']; + break; + + case 'event_id': + $value = $event['id']; + break; + + case 'event_type': + $value = CRM_Utils_Array::value($event['event_type_id'], CRM_Event_PseudoConstant::eventType()); + break; + + default: + if (in_array($token, self::$_tokens[$entity])) { + $value = $event[$token]; + } + else { + $value = "{$entity}.{$token}"; + } + break; + } + + if ($escapeSmarty) { + $value = self::tokenEscapeSmarty($value); + } + return $value; + } + + /** + * Get replacement strings for any participant tokens + * @param string $entity - name of entity type, should be Participant. + * @param string $token + * @param array $participant an api result array for a single participant + * @param bool $escapeSmarty + * @return string token replacement + */ + public static function getParticipantTokenReplacement($entity, $token, $participant, $escapeSmarty = FALSE) { + $entity = strtolower($entity); + if ($entity != 'participant') { + // Not sure which exception is appropriate. + throw new Exception('$entity is expected to be "participant".'); + } + + self::_buildParticipantTokens(); + + $params = array('entity_id' => $participant['id'], 'entity_table' => 'civicrm_participant'); + + switch ($token) { + case 'currency': + $value = $participant['participant_fee_currency']; + break; + + case 'participant_fee_level': + if (is_array($participant['participant_fee_level'])) { + $value = ''; + foreach ($participant['participant_fee_level'] as $fee_level) { + $value .= $fee_level . "
"; + } + } + else { + $value = $participant['participant_fee_level']; + } + break; + + case 'event_end_date': + case 'event_start_date': + $value = CRM_Utils_Date::customFormat($participant[$token]); + break; + + case 'participant_id': + // I'm not sure why participant_id is not there by default. + // I add it here, because I have a use case for this ;-) + $value = $participant['id']; + break; + + default: + if (in_array($token, self::$_tokens[$entity])) { + $value = $participant[$token]; + } + else { + $value = "{$entity}.{$token}"; + } + break; + } + + if ($escapeSmarty) { + $value = self::tokenEscapeSmarty($value); + } + return $value; + } + /** * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance * - this is used by the pdfLetter task from membership search