4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 use Civi\ActionSchedule\Event\MailingQueryEvent
;
14 use Civi\Token\TokenProcessor
;
15 use Civi\Token\TokenRow
;
18 * Class CRM_Contribute_Tokens
20 * Generate "contribution.*" tokens.
22 * At time of writing, we don't have any particularly special tokens -- we just
23 * do some basic formatting based on the corresponding DB field.
25 class CRM_Contribute_Tokens
extends CRM_Core_EntityTokens
{
30 protected function getEntityName(): string {
31 return 'contribution';
35 * Get the relevant bao name.
37 public function getBAOName(): string {
38 return CRM_Core_DAO_AllCoreTables
::getFullName(ucfirst($this->getEntityName()));
42 * Metadata about the entity fields.
46 protected $fieldMetadata = [];
49 * Get a list of tokens whose name and title match the DB fields.
52 protected function getPassthruTokens(): array {
54 'contribution_page_id',
61 'non_deductible_amount',
69 'contribution_status_id',
71 'payment_instrument_id',
76 * Get tokens supporting the syntax we are migrating to.
78 * In general these are tokens that were not previously supported
79 * so we can add them in the preferred way or that we have
80 * undertaken some, as yet to be written, db update.
82 * See https://lab.civicrm.org/dev/core/-/issues/2650
86 public function getBasicTokens(): array {
88 foreach (['contribution_status_id', 'payment_instrument_id', 'financial_type_id', 'contribution_page_id'] as $fieldName) {
89 $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title'];
95 * Get pseudoTokens - it tokens that reflect the name or label of a pseudoconstant.
97 * @internal - this function will likely be made protected soon.
101 public function getPseudoTokens(): array {
103 foreach (array_keys($this->getBasicTokens()) as $fieldName) {
104 if (!empty($this->fieldMetadata
[$fieldName]['pseudoconstant'])) {
105 $return[$fieldName . ':label'] = $this->fieldMetadata
[$fieldName]['html']['label'];
106 $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $this->fieldMetadata
[$fieldName]['html']['label'];
115 public function __construct() {
116 $tokens = CRM_Utils_Array
::subset(
117 CRM_Utils_Array
::collect('title', $this->getFieldMetadata()),
118 $this->getPassthruTokens()
120 $tokens = array_merge($tokens, $this->getPseudoTokens(), CRM_Utils_Token
::getCustomFieldTokens('Contribution'));
121 parent
::__construct('contribution', $tokens);
125 * Check if the token processor is active.
127 * @param \Civi\Token\TokenProcessor $processor
131 public function checkActive(TokenProcessor
$processor) {
132 return !empty($processor->context
['actionMapping'])
133 && $processor->context
['actionMapping']->getEntity() === 'civicrm_contribution';
137 * Alter action schedule query.
139 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
141 public function alterActionScheduleQuery(MailingQueryEvent
$e): void
{
142 if ($e->mapping
->getEntity() !== 'civicrm_contribution') {
146 $fields = $this->getFieldMetadata();
147 foreach ($this->getPassthruTokens() as $token) {
148 $e->query
->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
150 foreach (array_keys($this->getPseudoTokens()) as $token) {
151 $split = explode(':', $token);
152 $e->query
->select("e." . $fields[$split[0]]['name'] . " AS contrib_{$split[0]}");
158 * @throws \CRM_Core_Exception
160 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
161 $actionSearchResult = $row->context
['actionSearchResult'];
162 $fieldValue = $actionSearchResult->{"contrib_$field"} ??
NULL;
164 if (array_key_exists($field, $this->getPseudoTokens())) {
165 $split = explode(':', $field);
166 return $row->tokens($entity, $field, $this->getPseudoValue($split[0], $split[1], $actionSearchResult->{"contrib_$split[0]"} ??
NULL));
168 if ($this->isMoneyField($field)) {
169 return $row->format('text/plain')->tokens($entity, $field,
170 \CRM_Utils_Money
::format($fieldValue, $actionSearchResult->contrib_currency
));
172 if ($this->isDateField($field)) {
173 return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date
::customFormat($fieldValue));
175 if ($cfID = \CRM_Core_BAO_CustomField
::getKeyID($field)) {
176 $row->customToken($entity, $cfID, $actionSearchResult->entity_id
);
179 $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue);
184 * Get the value for the relevant pseudo field.
186 * @param string $realField e.g contribution_status_id
187 * @param string $pseudoKey e.g name
188 * @param int|string $fieldValue e.g 1
191 * Eg. 'Completed' in the example above.
193 * @internal function will likely be protected soon.
195 public function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string {
196 if ($pseudoKey === 'name') {
197 $fieldValue = (string) CRM_Core_PseudoConstant
::getName($this->getBAOName(), $realField, $fieldValue);
199 if ($pseudoKey === 'label') {
200 $fieldValue = (string) CRM_Core_PseudoConstant
::getLabel($this->getBAOName(), $realField, $fieldValue);
202 return (string) $fieldValue;