Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-16734 participant tokens in participant pdf #8260

Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1ce0f28
Add event tokens to participant pdf creation.
Jun 19, 2015
64ea3fb
Add custom field event tokens to participant pdf action
Jun 24, 2015
914aa69
fix custom token naming
Jun 24, 2015
6f515aa
Code style issues
Jun 29, 2015
6f7aa9d
Array to function result fix
Jun 29, 2015
328641a
Array to function result fix
Jun 29, 2015
8d05192
fix assignment
Jun 29, 2015
7ede346
change leftover $contactId
Jul 28, 2015
534ad5c
get correct balance token replacement
Jul 28, 2015
1cc405e
CRM-16599 - Rebased and adapted @mallezie's PR for CiviCRM 4.7.
johanv Apr 27, 2016
58aa8b7
CRM-16599 - removed event.fee_amount token.
johanv Apr 27, 2016
b112746
add participant tokens to participant pdf action
Jun 23, 2015
4aa283d
CRM-16734 - Now hook_civicrm_tokenValues is called again.
johanv Sep 18, 2015
2d70f39
CRM-16734 - This change was also done in commit:4aa283d1.
johanv Apr 27, 2016
343781b
CRM-16734 - Changed signature for getParticipantTokenReplacement.
johanv Apr 27, 2016
2eff8be
CRM-16734 - obligatory whitespace fix. :-)
johanv Apr 27, 2016
d6e7117
CRM-16734 - Fixed casing, thx @agh1.
johanv May 27, 2016
4ed5ba8
CRM-16734 - Balance is irrelevant for event, I think.
johanv May 27, 2016
5213a98
CRM-16734 - Remove specific date format.
johanv May 27, 2016
3bf5c4c
CRM-16734 - added a token for participant id.
johanv May 27, 2016
bc7ab86
Merge branch 'master' into CRM-16734-participant_tokens_in_participan…
johanv Sep 14, 2016
7b00c8a
CRM-16734 - processMessageTemplate was called with incorrect parameters.
johanv Sep 14, 2016
727796f
CRM-16734 - clean up this API calls.
johanv Sep 14, 2016
24e7dd5
Don't create letters for deceased people.
johanv Sep 14, 2016
065bc52
CRM-16734 - get rid of CRM_Event_Form_Task_PDFLetterCommon.
johanv Sep 14, 2016
1d742d8
CRM-16734 - Reverted some of the changes that don't seem relevant.
johanv Sep 14, 2016
7e1a411
This could also be reverted.
johanv Sep 14, 2016
a737429
Trying to fix a style error...
johanv Sep 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ public static function eventTokens() {
'{event.end_date}' => ts('Event End Date'),
'{event.event_type}' => ts('Event Type'),
'{event.summary}' => ts('Event Summary'),
// These are location_email and location_contact
// Should be cleaned up after ActionSchedule token replacement cleanup.
'{event.contact_email}' => ts('Event Contact Email'),
'{event.contact_phone}' => ts('Event Contact Phone'),
'{event.description}' => ts('Event Description'),
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CRM_Event_Form_Task extends CRM_Core_Form {
*
* @var array
*/
protected $_participantIds;
public $_participantIds;

/**
* Build all the data structures needed to build the form.
Expand Down
26 changes: 19 additions & 7 deletions CRM/Event/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,24 @@ class CRM_Event_Form_Task_PDF extends CRM_Event_Form_Task {
* Build all the data structures needed to build the form.
*/
public function preProcess() {
CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this);
parent::preProcess();

// we have all the participant ids, so now we get the contact ids
parent::setContactIDs();

$this->assign('single', $this->_single);
CRM_Contact_Form_Task_PDFLetterCommon::preProcess($this);
}

/**
* Build the form object.
*/
public function buildQuickForm() {
// We have all the participant ids, so now we get the contact ids.
$this->setContactIDs();
CRM_Contact_Form_Task_PDFLetterCommon::buildQuickForm($this);
}

/**
* Process the form after the input has been submitted and validated.
*/
public function postProcess() {
CRM_Contact_Form_Task_PDFLetterCommon::postProcess($this);
CRM_Event_Form_Task_PDFLetterCommon::postProcess($this);
}

/**
Expand All @@ -99,6 +96,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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure it needs to be CRM_Core_BAO_CustomField (i.e. Core in mixed case).


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;
}

Expand Down
99 changes: 99 additions & 0 deletions CRM/Event/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2015 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
*/

/**
* This class provides the common functionality for creating PDF letter for
* one or a group of participant ids.
*/
class CRM_Event_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLetterCommon {
/**
* Process the form after the input has been submitted and validated.
* @access public
*/
public static function postProcess(&$form) {

list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);

$skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
$skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;

foreach ($form->_participantIds as $participantID) {

$participant = civicrm_api3('participant', 'get', array('participant_id' => $participantID));
$participant = $participant['values'][$participantID];
$event = civicrm_api3('event', 'get', array('id' => $participant['event_id']));
$event = $event['values'][$participant['event_id']];

// get contact information
// Use 'getTokenDetails' so that hook_civicrm_tokenValues is called.
$contactId = $participant['contact_id'];
$params = array('id' => $contactId);
list($contact) = CRM_Utils_Token::getTokenDetails($params,
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
'CRM_Contact_Form_Task_PDFLetterCommon'
);

$tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], 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[$contactId], $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;
}

self::createActivities($form, $html_message, $form->_contactIds);

CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);

$form->postProcessHook();

CRM_Utils_System::civiExit(1);
}

}
180 changes: 179 additions & 1 deletion CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,6 @@ public static function getHookTokenReplacement(
if ($escapeSmarty) {
$value = self::tokenEscapeSmarty($value);
}

return $value;
}

Expand Down Expand Up @@ -1605,6 +1604,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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure it needs to be CRM_Core_BAO_CustomField (i.e. Core in mixed case).

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure it needs to be CRM_Core_BAO_CustomField (i.e. Core in mixed case).

foreach ($customtokens as $tokenkey => $tokenvalue) {
$participantTokens[] = 'custom_' . $tokenkey;
}
self::$_tokens[$key] = $participantTokens;
}
}

/**
* Replace tokens for an entity.
* @param string $entity
Expand Down Expand Up @@ -1758,6 +1795,147 @@ 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 'balance':
$info = CRM_Contribute_BAO_Contribution::getPaymentInfo($params['participant_id'], 'event');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no key participant_id in the $params array.

$balancePay = CRM_Utils_Array::value('balance', $info);
$balancePay = CRM_Utils_Money::format($balancePay);
$value = $balancePay;
break;

case 'title':
$value = $event['event_title'];
break;

case 'end_date':
case 'start_date':
$value = CRM_Utils_Date::customFormat($event[$token], "%d/%m/%Y");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date format should not be hard-coded

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 . "<br>";
}
}
else {
$value = $participant['participant_fee_level'];
}
break;

case 'event_end_date':
case 'event_start_date':
$value = CRM_Utils_Date::customFormat($participant[$token], "%d/%m/%Y");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date format should not be hard-coded

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
Expand Down