- * See https://lab.civicrm.org/dev/core/-/issues/2650
- *
- * @return string[]
- */
- public function getBasicTokens(): array {
- $return = [];
- foreach (['contribution_status_id', 'payment_instrument_id', 'financial_type_id'] as $fieldName) {
- $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title'];
- }
- return $return;
- }
-
- /**
- * Get pseudoTokens - it tokens that reflect the name or label of a pseudoconstant.
- *
- * @internal - this function will likely be made protected soon.
- *
- * @return array
- */
- public function getPseudoTokens(): array {
- $return = [];
- foreach (array_keys($this->getBasicTokens()) as $fieldName) {
- if (!empty($this->fieldMetadata[$fieldName]['pseudoconstant'])) {
- $return[$fieldName . ':label'] = $this->fieldMetadata[$fieldName]['html']['label'];
- $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $this->fieldMetadata[$fieldName]['html']['label'];
- }
- }
- return $return;
- }
-
- /**
- * Class constructor.
- */
- public function __construct() {
- $tokens = CRM_Utils_Array::subset(
- CRM_Utils_Array::collect('title', $this->getFieldMetadata()),
- $this->getPassthruTokens()
- );
- $tokens = array_merge($tokens, $this->getPseudoTokens(), 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 = $this->getFieldMetadata();
- foreach ($this->getPassthruTokens() as $token) {
- $e->query->select("e." . $fields[$token]['name'] . " AS contrib_{$token}");
- }
- foreach (array_keys($this->getPseudoTokens()) as $token) {
- $split = explode(':', $token);
- $e->query->select("e." . $fields[$split[0]]['name'] . " AS contrib_{$split[0]}");
- }
- }
-
- /**
- * @inheritDoc
- */
- public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
- $actionSearchResult = $row->context['actionSearchResult'];
- $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL;
-
- 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));
- }
- elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
- $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
- }
- elseif (array_key_exists($field, $this->getPseudoTokens())) {
- $split = explode(':', $field);
- $row->tokens($entity, $field, $this->getPseudoValue($split[0], $split[1], $actionSearchResult->{"contrib_$split[0]"} ?? NULL));
- }
- elseif (in_array($field, array_keys($this->getBasicTokens()))) {
- $row->tokens($entity, $field, $fieldValue);
- }
- elseif (!array_key_exists($field, CRM_Contribute_BAO_Contribution::fields())) {
- if ($this->isDateField($field)) {
- $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue));
- }
- else {
- $row->format('text/plain')->tokens($entity, $field, $fieldValue);
- }
- }
- else {
- $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
- }
- }
-
- /**
- * Is the given field a date field.
- *
- * @param string $fieldName
- *
- * @return bool
- */
- public function isDateField($fieldName): bool {
- return $this->getFieldMetadata()[$fieldName]['type'] === (\CRM_Utils_Type::T_DATE + \CRM_Utils_Type::T_TIME);
- }
-
- /**
- * Get the value for the relevant pseudo field.
- *
- * @param string $realField e.g contribution_status_id
- * @param string $pseudoKey e.g name
- * @param int|string $fieldValue e.g 1