| 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 | /** |
| 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() { |
| 28 | return [ |
| 29 | 'contribution_page_id', |
| 30 | 'receive_date', |
| 31 | 'total_amount', |
| 32 | 'fee_amount', |
| 33 | 'net_amount', |
| 34 | 'trxn_id', |
| 35 | 'invoice_id', |
| 36 | 'currency', |
| 37 | 'contribution_cancel_date', |
| 38 | 'receipt_date', |
| 39 | 'thankyou_date', |
| 40 | 'tax_amount', |
| 41 | ]; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get alias tokens. |
| 46 | * |
| 47 | * @return array |
| 48 | */ |
| 49 | protected function getAliasTokens() { |
| 50 | return [ |
| 51 | 'id' => 'contribution_id', |
| 52 | 'payment_instrument' => 'payment_instrument_id', |
| 53 | 'source' => 'contribution_source', |
| 54 | 'status' => 'contribution_status_id', |
| 55 | 'type' => 'financial_type_id', |
| 56 | 'cancel_date' => 'contribution_cancel_date', |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Class constructor. |
| 62 | */ |
| 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'); |
| 73 | $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution')); |
| 74 | parent::__construct('contribution', $tokens); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check if the token processor is active. |
| 79 | * |
| 80 | * @param \Civi\Token\TokenProcessor $processor |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function checkActive(\Civi\Token\TokenProcessor $processor) { |
| 85 | return !empty($processor->context['actionMapping']) |
| 86 | && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution'; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Alter action schedule query. |
| 91 | * |
| 92 | * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e |
| 93 | */ |
| 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 | /** |
| 109 | * @inheritDoc |
| 110 | */ |
| 111 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { |
| 112 | $actionSearchResult = $row->context['actionSearchResult']; |
| 113 | $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL; |
| 114 | |
| 115 | $aliasTokens = $this->getAliasTokens(); |
| 116 | if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) { |
| 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 | } |
| 123 | elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { |
| 124 | $row->customToken($entity, $cfID, $actionSearchResult->entity_id); |
| 125 | } |
| 126 | else { |
| 127 | $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } |