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