4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2015 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Class CRM_Contribute_Tokens
32 * Generate "contribution.*" tokens.
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.
37 class CRM_Contribute_Tokens
extends \Civi\Token\AbstractTokenSubscriber
{
40 * Get a list of tokens whose name and title match the DB fields.
43 protected function getPassthruTokens() {
45 'contribution_page_id',
65 protected function getAliasTokens() {
67 'id' => 'contribution_id',
68 'payment_instrument' => 'payment_instrument_id',
69 'source' => 'contribution_source',
70 'status' => 'contribution_status_id',
71 'type' => 'financial_type_id',
78 public function __construct() {
79 $tokens = CRM_Utils_Array
::subset(
80 CRM_Utils_Array
::collect('title', CRM_Contribute_DAO_Contribution
::fields()),
81 $this->getPassthruTokens()
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');
88 parent
::__construct('contribution', $tokens);
91 public function checkActive(\Civi\Token\TokenProcessor
$processor) {
93 !empty($processor->context
['actionMapping'])
94 && $processor->context
['actionMapping']->getEntity() === 'civicrm_contribution';
97 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent
$e) {
98 if ($e->mapping
->getEntity() !== 'civicrm_contribution') {
102 $fields = CRM_Contribute_DAO_Contribution
::fields();
103 foreach ($this->getPassthruTokens() as $token) {
104 $e->query
->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
106 foreach ($this->getAliasTokens() as $alias => $orig) {
107 $e->query
->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}");
112 * Evaluate the content of a single token.
114 * @param \Civi\Token\TokenRow $row
115 * The record for which we want token values.
116 * @param string $entity
117 * @param string $field
118 * The name of the token field.
119 * @param mixed $prefetch
120 * Any data that was returned by the prefetch().
123 * @throws \CRM_Core_Exception
125 public function evaluateToken(\Civi\Token\TokenRow
$row, $entity, $field, $prefetch = NULL) {
126 $actionSearchResult = $row->context
['actionSearchResult'];
127 $fieldValue = isset($actionSearchResult->{"contrib_$field"}) ?
$actionSearchResult->{"contrib_$field"} : NULL;
129 $aliasTokens = $this->getAliasTokens();
130 if (in_array($field, array('total_amount', 'fee_amount', 'net_amount'))) {
131 return $row->format('text/plain')->tokens($entity, $field,
132 \CRM_Utils_Money
::format($fieldValue, $actionSearchResult->contrib_currency
));
134 elseif (isset($aliasTokens[$field])) {
135 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
138 $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);