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 +--------------------------------------------------------------------+
13 use Civi\ActionSchedule\Event\MailingQueryEvent
;
14 use Civi\Token\AbstractTokenSubscriber
;
15 use Civi\Token\TokenProcessor
;
16 use Civi\Token\TokenRow
;
19 * Class CRM_Contribute_Tokens
21 * Generate "contribution.*" tokens.
23 * At time of writing, we don't have any particularly special tokens -- we just
24 * do some basic formatting based on the corresponding DB field.
26 class CRM_Contribute_Tokens
extends AbstractTokenSubscriber
{
29 * Get a list of tokens whose name and title match the DB fields.
32 protected function getPassthruTokens(): array {
34 'contribution_page_id',
42 'contribution_cancel_date',
54 protected function getAliasTokens(): array {
56 'id' => 'contribution_id',
57 'payment_instrument' => 'payment_instrument_id',
58 'source' => 'contribution_source',
59 'status' => 'contribution_status_id',
60 'type' => 'financial_type_id',
61 'cancel_date' => 'contribution_cancel_date',
68 public function __construct() {
69 $tokens = CRM_Utils_Array
::subset(
70 CRM_Utils_Array
::collect('title', CRM_Contribute_DAO_Contribution
::fields()),
71 $this->getPassthruTokens()
73 $tokens['id'] = ts('Contribution ID');
74 $tokens['payment_instrument'] = ts('Payment Instrument');
75 $tokens['source'] = ts('Contribution Source');
76 $tokens['status'] = ts('Contribution Status');
77 $tokens['type'] = ts('Financial Type');
78 $tokens = array_merge($tokens, CRM_Utils_Token
::getCustomFieldTokens('Contribution'));
79 parent
::__construct('contribution', $tokens);
83 * Check if the token processor is active.
85 * @param \Civi\Token\TokenProcessor $processor
89 public function checkActive(TokenProcessor
$processor) {
90 return !empty($processor->context
['actionMapping'])
91 && $processor->context
['actionMapping']->getEntity() === 'civicrm_contribution';
95 * Alter action schedule query.
97 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
99 public function alterActionScheduleQuery(MailingQueryEvent
$e): void
{
100 if ($e->mapping
->getEntity() !== 'civicrm_contribution') {
104 $fields = CRM_Contribute_DAO_Contribution
::fields();
105 foreach ($this->getPassthruTokens() as $token) {
106 $e->query
->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
108 foreach ($this->getAliasTokens() as $alias => $orig) {
109 $e->query
->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}");
116 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
117 $actionSearchResult = $row->context
['actionSearchResult'];
118 $fieldValue = $actionSearchResult->{"contrib_$field"} ??
NULL;
120 $aliasTokens = $this->getAliasTokens();
121 if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) {
122 return $row->format('text/plain')->tokens($entity, $field,
123 \CRM_Utils_Money
::format($fieldValue, $actionSearchResult->contrib_currency
));
125 elseif (isset($aliasTokens[$field])) {
126 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
128 elseif ($cfID = \CRM_Core_BAO_CustomField
::getKeyID($field)) {
129 $row->customToken($entity, $cfID, $actionSearchResult->entity_id
);
132 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);