Merge pull request #1 from civicrm/master
[civicrm-core.git] / CRM / Contribute / Tokens.php
CommitLineData
2045389a
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
2045389a 6 +--------------------------------------------------------------------+
6b83d5bd 7 | Copyright CiviCRM LLC (c) 2004-2019 |
2045389a
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * Class CRM_Contribute_Tokens
31 *
32 * Generate "contribution.*" tokens.
33 *
34 * At time of writing, we don't have any particularly special tokens -- we just
35 * do some basic formatting based on the corresponding DB field.
36 */
37class CRM_Contribute_Tokens extends \Civi\Token\AbstractTokenSubscriber {
38
39 /**
40 * Get a list of tokens whose name and title match the DB fields.
41 * @return array
42 */
43 protected function getPassthruTokens() {
be2fb01f 44 return [
2045389a
TO
45 'contribution_page_id',
46 'receive_date',
47 'total_amount',
48 'fee_amount',
49 'net_amount',
50 'trxn_id',
51 'invoice_id',
52 'currency',
53 'cancel_date',
54 'receipt_date',
55 'thankyou_date',
56 'tax_amount',
be2fb01f 57 ];
2045389a
TO
58 }
59
70599df6 60 /**
61 * Get alias tokens.
62 *
63 * @return array
64 */
2045389a 65 protected function getAliasTokens() {
be2fb01f 66 return [
2045389a
TO
67 'id' => 'contribution_id',
68 'payment_instrument' => 'payment_instrument_id',
69 'source' => 'contribution_source',
70 'status' => 'contribution_status_id',
71 'type' => 'financial_type_id',
be2fb01f 72 ];
2045389a
TO
73 }
74
e8e8f3ad 75 /**
76 * Class constructor.
77 */
2045389a
TO
78 public function __construct() {
79 $tokens = CRM_Utils_Array::subset(
80 CRM_Utils_Array::collect('title', CRM_Contribute_DAO_Contribution::fields()),
81 $this->getPassthruTokens()
82 );
83 $tokens['id'] = ts('Contribution ID');
84 $tokens['payment_instrument'] = ts('Payment Instrument');
85 $tokens['source'] = ts('Contribution Source');
86 $tokens['status'] = ts('Contribution Status');
87 $tokens['type'] = ts('Financial Type');
18c017c8 88 $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution'));
2045389a
TO
89 parent::__construct('contribution', $tokens);
90 }
91
bc854509 92 /**
93 * Check if the token processor is active.
94 *
95 * @param \Civi\Token\TokenProcessor $processor
96 *
97 * @return bool
98 */
2045389a 99 public function checkActive(\Civi\Token\TokenProcessor $processor) {
1330f57a 100 return !empty($processor->context['actionMapping'])
2045389a
TO
101 && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution';
102 }
103
bc854509 104 /**
105 * Alter action schedule query.
106 *
107 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
108 */
2045389a
TO
109 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
110 if ($e->mapping->getEntity() !== 'civicrm_contribution') {
111 return;
112 }
113
114 $fields = CRM_Contribute_DAO_Contribution::fields();
115 foreach ($this->getPassthruTokens() as $token) {
116 $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
117 }
118 foreach ($this->getAliasTokens() as $alias => $orig) {
119 $e->query->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}");
120 }
121 }
122
123 /**
298795cd 124 * @inheritDoc
2045389a
TO
125 */
126 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
127 $actionSearchResult = $row->context['actionSearchResult'];
128 $fieldValue = isset($actionSearchResult->{"contrib_$field"}) ? $actionSearchResult->{"contrib_$field"} : NULL;
129
130 $aliasTokens = $this->getAliasTokens();
be2fb01f 131 if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) {
2045389a
TO
132 return $row->format('text/plain')->tokens($entity, $field,
133 \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency));
134 }
135 elseif (isset($aliasTokens[$field])) {
136 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
137 }
8640061b 138 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
139 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 140 }
2045389a
TO
141 else {
142 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
143 }
144 }
145
146}