Merge pull request #19679 from civicrm/5.35
[civicrm-core.git] / CRM / Contribute / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 use Civi\ActionSchedule\Event\MailingQueryEvent;
14 use Civi\Token\AbstractTokenSubscriber;
15 use Civi\Token\TokenProcessor;
16 use Civi\Token\TokenRow;
17
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 */
26 class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
27
28 /**
29 * Get a list of tokens whose name and title match the DB fields.
30 * @return array
31 */
32 protected function getPassthruTokens(): array {
33 return [
34 'contribution_page_id',
35 'receive_date',
36 'total_amount',
37 'fee_amount',
38 'net_amount',
39 'trxn_id',
40 'invoice_id',
41 'currency',
42 'contribution_cancel_date',
43 'receipt_date',
44 'thankyou_date',
45 'tax_amount',
46 ];
47 }
48
49 /**
50 * Get alias tokens.
51 *
52 * @return array
53 */
54 protected function getAliasTokens(): array {
55 return [
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',
62 ];
63 }
64
65 /**
66 * Class constructor.
67 */
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');
78 $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution'));
79 parent::__construct('contribution', $tokens);
80 }
81
82 /**
83 * Check if the token processor is active.
84 *
85 * @param \Civi\Token\TokenProcessor $processor
86 *
87 * @return bool
88 */
89 public function checkActive(TokenProcessor $processor) {
90 return !empty($processor->context['actionMapping'])
91 && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution';
92 }
93
94 /**
95 * Alter action schedule query.
96 *
97 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
98 */
99 public function alterActionScheduleQuery(MailingQueryEvent $e): void {
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 /**
114 * @inheritDoc
115 */
116 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
117 $actionSearchResult = $row->context['actionSearchResult'];
118 $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL;
119
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));
124 }
125 elseif (isset($aliasTokens[$field])) {
126 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
127 }
128 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
129 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
130 }
131 else {
132 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
133 }
134 }
135
136 }