Commit | Line | Data |
---|---|---|
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 | 13 | use Civi\ActionSchedule\Event\MailingQueryEvent; |
14 | use Civi\Token\AbstractTokenSubscriber; | |
15 | use Civi\Token\TokenProcessor; | |
16 | use 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 | 26 | class 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', | |
cb31dd27 | 46 | 'contribution_status_id', |
be2fb01f | 47 | ]; |
2045389a TO |
48 | } |
49 | ||
70599df6 | 50 | /** |
51 | * Get alias tokens. | |
52 | * | |
53 | * @return array | |
54 | */ | |
c2a33d9c | 55 | protected function getAliasTokens(): array { |
be2fb01f | 56 | return [ |
2045389a TO |
57 | 'id' => 'contribution_id', |
58 | 'payment_instrument' => 'payment_instrument_id', | |
59 | 'source' => 'contribution_source', | |
60 | 'status' => 'contribution_status_id', | |
61 | 'type' => 'financial_type_id', | |
6e7cc0f5 | 62 | 'cancel_date' => 'contribution_cancel_date', |
be2fb01f | 63 | ]; |
2045389a TO |
64 | } |
65 | ||
cb31dd27 EM |
66 | /** |
67 | * Get tokens supporting the syntax we are migrating to. | |
68 | * | |
69 | * In general these are tokens that were not previously supported | |
70 | * so we can add them in the preferred way or that we have | |
71 | * undertaken some, as yet to be written, db update. | |
72 | * | |
73 | * See https://lab.civicrm.org/dev/core/-/issues/2650 | |
74 | * | |
75 | * @return string[] | |
76 | */ | |
77 | protected function getBasicTokens(): array { | |
78 | return ['contribution_status_id' => ts('Contribution Status ID')]; | |
79 | } | |
80 | ||
e8e8f3ad | 81 | /** |
82 | * Class constructor. | |
83 | */ | |
2045389a TO |
84 | public function __construct() { |
85 | $tokens = CRM_Utils_Array::subset( | |
86 | CRM_Utils_Array::collect('title', CRM_Contribute_DAO_Contribution::fields()), | |
87 | $this->getPassthruTokens() | |
88 | ); | |
89 | $tokens['id'] = ts('Contribution ID'); | |
90 | $tokens['payment_instrument'] = ts('Payment Instrument'); | |
91 | $tokens['source'] = ts('Contribution Source'); | |
cb31dd27 EM |
92 | // Per https://lab.civicrm.org/dev/core/-/issues/2650 |
93 | // the intent is to deprecate this field in favour of | |
94 | // {contribution.contribution_status_id:label} | |
2045389a TO |
95 | $tokens['status'] = ts('Contribution Status'); |
96 | $tokens['type'] = ts('Financial Type'); | |
18c017c8 | 97 | $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution')); |
2045389a TO |
98 | parent::__construct('contribution', $tokens); |
99 | } | |
100 | ||
bc854509 | 101 | /** |
102 | * Check if the token processor is active. | |
103 | * | |
104 | * @param \Civi\Token\TokenProcessor $processor | |
105 | * | |
106 | * @return bool | |
107 | */ | |
c2a33d9c | 108 | public function checkActive(TokenProcessor $processor) { |
1330f57a | 109 | return !empty($processor->context['actionMapping']) |
2045389a TO |
110 | && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution'; |
111 | } | |
112 | ||
bc854509 | 113 | /** |
114 | * Alter action schedule query. | |
115 | * | |
116 | * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e | |
117 | */ | |
c2a33d9c | 118 | public function alterActionScheduleQuery(MailingQueryEvent $e): void { |
2045389a TO |
119 | if ($e->mapping->getEntity() !== 'civicrm_contribution') { |
120 | return; | |
121 | } | |
122 | ||
123 | $fields = CRM_Contribute_DAO_Contribution::fields(); | |
124 | foreach ($this->getPassthruTokens() as $token) { | |
125 | $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}"); | |
126 | } | |
127 | foreach ($this->getAliasTokens() as $alias => $orig) { | |
128 | $e->query->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}"); | |
129 | } | |
130 | } | |
131 | ||
132 | /** | |
298795cd | 133 | * @inheritDoc |
2045389a | 134 | */ |
c2a33d9c | 135 | public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { |
2045389a | 136 | $actionSearchResult = $row->context['actionSearchResult']; |
2e1f50d6 | 137 | $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL; |
2045389a TO |
138 | |
139 | $aliasTokens = $this->getAliasTokens(); | |
be2fb01f | 140 | if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) { |
2045389a TO |
141 | return $row->format('text/plain')->tokens($entity, $field, |
142 | \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency)); | |
143 | } | |
cb31dd27 | 144 | if (isset($aliasTokens[$field])) { |
2045389a TO |
145 | $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); |
146 | } | |
8640061b | 147 | elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { |
148 | $row->customToken($entity, $cfID, $actionSearchResult->entity_id); | |
4e9b6a62 | 149 | } |
cb31dd27 EM |
150 | elseif (in_array($field, array_keys($this->getBasicTokens()))) { |
151 | // For now we just ensure that the label fields do not override the | |
152 | // id field here. | |
153 | // Later we will add support for contribution_status_id:label | |
154 | $row->tokens($entity, $field, $fieldValue); | |
155 | } | |
2045389a TO |
156 | else { |
157 | $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); | |
158 | } | |
159 | } | |
160 | ||
161 | } |