Commit | Line | Data |
---|---|---|
2045389a TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
3435af9a | 5 | | CiviCRM version 4.7 | |
2045389a TO |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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 | */ | |
37 | class 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() { | |
44 | return array( | |
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', | |
57 | ); | |
58 | } | |
59 | ||
60 | protected function getAliasTokens() { | |
61 | return array( | |
62 | 'id' => 'contribution_id', | |
63 | 'payment_instrument' => 'payment_instrument_id', | |
64 | 'source' => 'contribution_source', | |
65 | 'status' => 'contribution_status_id', | |
66 | 'type' => 'financial_type_id', | |
67 | ); | |
68 | } | |
69 | ||
70 | public function __construct() { | |
71 | $tokens = CRM_Utils_Array::subset( | |
72 | CRM_Utils_Array::collect('title', CRM_Contribute_DAO_Contribution::fields()), | |
73 | $this->getPassthruTokens() | |
74 | ); | |
75 | $tokens['id'] = ts('Contribution ID'); | |
76 | $tokens['payment_instrument'] = ts('Payment Instrument'); | |
77 | $tokens['source'] = ts('Contribution Source'); | |
78 | $tokens['status'] = ts('Contribution Status'); | |
79 | $tokens['type'] = ts('Financial Type'); | |
80 | parent::__construct('contribution', $tokens); | |
81 | } | |
82 | ||
83 | public function checkActive(\Civi\Token\TokenProcessor $processor) { | |
84 | return | |
85 | !empty($processor->context['actionMapping']) | |
86 | && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution'; | |
87 | } | |
88 | ||
89 | public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) { | |
90 | if ($e->mapping->getEntity() !== 'civicrm_contribution') { | |
91 | return; | |
92 | } | |
93 | ||
94 | $fields = CRM_Contribute_DAO_Contribution::fields(); | |
95 | foreach ($this->getPassthruTokens() as $token) { | |
96 | $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}"); | |
97 | } | |
98 | foreach ($this->getAliasTokens() as $alias => $orig) { | |
99 | $e->query->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}"); | |
100 | } | |
101 | } | |
102 | ||
103 | /** | |
104 | * Evaluate the content of a single token. | |
105 | * | |
106 | * @param \Civi\Token\TokenRow $row | |
107 | * The record for which we want token values. | |
108 | * @param string $field | |
109 | * The name of the token field. | |
110 | * @param mixed $prefetch | |
111 | * Any data that was returned by the prefetch(). | |
112 | * @return mixed | |
113 | */ | |
114 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
115 | $actionSearchResult = $row->context['actionSearchResult']; | |
116 | $fieldValue = isset($actionSearchResult->{"contrib_$field"}) ? $actionSearchResult->{"contrib_$field"} : NULL; | |
117 | ||
118 | $aliasTokens = $this->getAliasTokens(); | |
119 | if (in_array($field, array('total_amount', 'fee_amount', 'net_amount'))) { | |
120 | return $row->format('text/plain')->tokens($entity, $field, | |
121 | \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency)); | |
122 | } | |
123 | elseif (isset($aliasTokens[$field])) { | |
124 | $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); | |
125 | } | |
126 | else { | |
127 | $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); | |
128 | } | |
129 | } | |
130 | ||
131 | } |