4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
14 * Class CRM_Contribute_Tokens
16 * Generate "contribution.*" tokens.
18 * At time of writing, we don't have any particularly special tokens -- we just
19 * do some basic formatting based on the corresponding DB field.
21 class CRM_Contribute_Tokens
extends \Civi\Token\AbstractTokenSubscriber
{
24 * Get a list of tokens whose name and title match the DB fields.
27 protected function getPassthruTokens() {
29 'contribution_page_id',
37 'contribution_cancel_date',
49 protected function getAliasTokens() {
51 'id' => 'contribution_id',
52 'payment_instrument' => 'payment_instrument_id',
53 'source' => 'contribution_source',
54 'status' => 'contribution_status_id',
55 'type' => 'financial_type_id',
56 'cancel_date' => 'contribution_cancel_date',
63 public function __construct() {
64 $tokens = CRM_Utils_Array
::subset(
65 CRM_Utils_Array
::collect('title', CRM_Contribute_DAO_Contribution
::fields()),
66 $this->getPassthruTokens()
68 $tokens['id'] = ts('Contribution ID');
69 $tokens['payment_instrument'] = ts('Payment Instrument');
70 $tokens['source'] = ts('Contribution Source');
71 $tokens['status'] = ts('Contribution Status');
72 $tokens['type'] = ts('Financial Type');
73 $tokens = array_merge($tokens, CRM_Utils_Token
::getCustomFieldTokens('Contribution'));
74 parent
::__construct('contribution', $tokens);
78 * Check if the token processor is active.
80 * @param \Civi\Token\TokenProcessor $processor
84 public function checkActive(\Civi\Token\TokenProcessor
$processor) {
85 return !empty($processor->context
['actionMapping'])
86 && $processor->context
['actionMapping']->getEntity() === 'civicrm_contribution';
90 * Alter action schedule query.
92 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
94 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent
$e) {
95 if ($e->mapping
->getEntity() !== 'civicrm_contribution') {
99 $fields = CRM_Contribute_DAO_Contribution
::fields();
100 foreach ($this->getPassthruTokens() as $token) {
101 $e->query
->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
103 foreach ($this->getAliasTokens() as $alias => $orig) {
104 $e->query
->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}");
111 public function evaluateToken(\Civi\Token\TokenRow
$row, $entity, $field, $prefetch = NULL) {
112 $actionSearchResult = $row->context
['actionSearchResult'];
113 $fieldValue = $actionSearchResult->{"contrib_$field"} ??
NULL;
115 $aliasTokens = $this->getAliasTokens();
116 if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) {
117 return $row->format('text/plain')->tokens($entity, $field,
118 \CRM_Utils_Money
::format($fieldValue, $actionSearchResult->contrib_currency
));
120 elseif (isset($aliasTokens[$field])) {
121 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
123 elseif ($cfID = \CRM_Core_BAO_CustomField
::getKeyID($field)) {
124 $row->customToken($entity, $cfID, $actionSearchResult->entity_id
);
127 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);