Merge pull request #19844 from seamuslee001/php74_membership_api_fix
[civicrm-core.git] / CRM / Contribute / Tokens.php
CommitLineData
2045389a
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
2045389a 6 | |
bc77d7c0
TO
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 |
2045389a
TO
10 +--------------------------------------------------------------------+
11 */
12
c2a33d9c 13use Civi\ActionSchedule\Event\MailingQueryEvent;
14use Civi\Token\AbstractTokenSubscriber;
15use Civi\Token\TokenProcessor;
16use Civi\Token\TokenRow;
17
2045389a
TO
18/**
19 * Class CRM_Contribute_Tokens
20 *
21 * Generate "contribution.*" tokens.
22 *
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.
25 */
c2a33d9c 26class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
2045389a
TO
27
28 /**
29 * Get a list of tokens whose name and title match the DB fields.
30 * @return array
31 */
c2a33d9c 32 protected function getPassthruTokens(): array {
be2fb01f 33 return [
2045389a
TO
34 'contribution_page_id',
35 'receive_date',
36 'total_amount',
37 'fee_amount',
38 'net_amount',
39 'trxn_id',
40 'invoice_id',
41 'currency',
6e7cc0f5 42 'contribution_cancel_date',
2045389a
TO
43 'receipt_date',
44 'thankyou_date',
45 'tax_amount',
be2fb01f 46 ];
2045389a
TO
47 }
48
70599df6 49 /**
50 * Get alias tokens.
51 *
52 * @return array
53 */
c2a33d9c 54 protected function getAliasTokens(): array {
be2fb01f 55 return [
2045389a
TO
56 'id' => 'contribution_id',
57 'payment_instrument' => 'payment_instrument_id',
58 'source' => 'contribution_source',
59 'status' => 'contribution_status_id',
60 'type' => 'financial_type_id',
6e7cc0f5 61 'cancel_date' => 'contribution_cancel_date',
be2fb01f 62 ];
2045389a
TO
63 }
64
e8e8f3ad 65 /**
66 * Class constructor.
67 */
2045389a
TO
68 public function __construct() {
69 $tokens = CRM_Utils_Array::subset(
70 CRM_Utils_Array::collect('title', CRM_Contribute_DAO_Contribution::fields()),
71 $this->getPassthruTokens()
72 );
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');
18c017c8 78 $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution'));
2045389a
TO
79 parent::__construct('contribution', $tokens);
80 }
81
bc854509 82 /**
83 * Check if the token processor is active.
84 *
85 * @param \Civi\Token\TokenProcessor $processor
86 *
87 * @return bool
88 */
c2a33d9c 89 public function checkActive(TokenProcessor $processor) {
1330f57a 90 return !empty($processor->context['actionMapping'])
2045389a
TO
91 && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution';
92 }
93
bc854509 94 /**
95 * Alter action schedule query.
96 *
97 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
98 */
c2a33d9c 99 public function alterActionScheduleQuery(MailingQueryEvent $e): void {
2045389a
TO
100 if ($e->mapping->getEntity() !== 'civicrm_contribution') {
101 return;
102 }
103
104 $fields = CRM_Contribute_DAO_Contribution::fields();
105 foreach ($this->getPassthruTokens() as $token) {
106 $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
107 }
108 foreach ($this->getAliasTokens() as $alias => $orig) {
109 $e->query->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}");
110 }
111 }
112
113 /**
298795cd 114 * @inheritDoc
2045389a 115 */
c2a33d9c 116 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
2045389a 117 $actionSearchResult = $row->context['actionSearchResult'];
2e1f50d6 118 $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL;
2045389a
TO
119
120 $aliasTokens = $this->getAliasTokens();
be2fb01f 121 if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) {
2045389a
TO
122 return $row->format('text/plain')->tokens($entity, $field,
123 \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency));
124 }
125 elseif (isset($aliasTokens[$field])) {
126 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
127 }
8640061b 128 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
129 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 130 }
2045389a
TO
131 else {
132 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
133 }
134 }
135
136}