'contribution_id', 'payment_instrument' => 'payment_instrument_id', 'source' => 'contribution_source', 'status' => 'contribution_status_id', 'type' => 'financial_type_id', 'cancel_date' => 'contribution_cancel_date', ]; } /** * Get tokens supporting the syntax we are migrating to. * * In general these are tokens that were not previously supported * so we can add them in the preferred way or that we have * undertaken some, as yet to be written, db update. * * See https://lab.civicrm.org/dev/core/-/issues/2650 * * @return string[] */ protected function getBasicTokens(): array { return ['contribution_status_id' => ts('Contribution Status ID')]; } /** * Class constructor. */ public function __construct() { $tokens = CRM_Utils_Array::subset( CRM_Utils_Array::collect('title', CRM_Contribute_DAO_Contribution::fields()), $this->getPassthruTokens() ); $tokens['id'] = ts('Contribution ID'); $tokens['payment_instrument'] = ts('Payment Instrument'); $tokens['source'] = ts('Contribution Source'); // Per https://lab.civicrm.org/dev/core/-/issues/2650 // the intent is to deprecate this field in favour of // {contribution.contribution_status_id:label} $tokens['status'] = ts('Contribution Status'); $tokens['type'] = ts('Financial Type'); $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution')); parent::__construct('contribution', $tokens); } /** * Check if the token processor is active. * * @param \Civi\Token\TokenProcessor $processor * * @return bool */ public function checkActive(TokenProcessor $processor) { return !empty($processor->context['actionMapping']) && $processor->context['actionMapping']->getEntity() === 'civicrm_contribution'; } /** * Alter action schedule query. * * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e */ public function alterActionScheduleQuery(MailingQueryEvent $e): void { if ($e->mapping->getEntity() !== 'civicrm_contribution') { return; } $fields = CRM_Contribute_DAO_Contribution::fields(); foreach ($this->getPassthruTokens() as $token) { $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}"); } foreach ($this->getAliasTokens() as $alias => $orig) { $e->query->select("e." . $fields[$orig]['name'] . " AS contrib_{$alias}"); } } /** * @inheritDoc */ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { $actionSearchResult = $row->context['actionSearchResult']; $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL; $aliasTokens = $this->getAliasTokens(); if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) { return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency)); } if (isset($aliasTokens[$field])) { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); } elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } elseif (in_array($field, array_keys($this->getBasicTokens()))) { // For now we just ensure that the label fields do not override the // id field here. // Later we will add support for contribution_status_id:label $row->tokens($entity, $field, $fieldValue); } else { $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue); } } }