From 4a526c1be86533e9025116ccc52203def72f4950 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 6 Aug 2021 15:17:56 +1200 Subject: [PATCH] Reconcile tokens misnamed on legacy class This switches to using the same metadata based record for both classes --- CRM/Contribute/Tokens.php | 19 ++++++++++--------- CRM/Core/EntityTokens.php | 9 +++++++++ CRM/Core/SelectValues.php | 17 +++-------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index cc40775a23..0cdfc560fc 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -58,10 +58,15 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { protected $fieldMetadata = []; /** - * Get a list of tokens whose name and title match the DB fields. + * Get a list of tokens for the entity for which access is permitted to. + * + * This list is historical and we need to question whether we + * should filter out any fields (other than those fields, like api_key + * on the contact entity) with permissions defined. + * * @return array */ - protected function getPassthruTokens(): array { + protected function getExposedFields(): array { return [ 'contribution_page_id', 'source', @@ -97,7 +102,7 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { */ public function getBasicTokens(): array { $return = []; - foreach (['contribution_status_id', 'payment_instrument_id', 'financial_type_id', 'contribution_page_id'] as $fieldName) { + foreach ($this->getExposedFields() as $fieldName) { $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; } return $return; @@ -107,11 +112,7 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { * 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')); + $tokens = $this->getAllTokens(); parent::__construct('contribution', $tokens); } @@ -138,7 +139,7 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { } $fields = $this->getFieldMetadata(); - foreach ($this->getPassthruTokens() as $token) { + foreach (array_keys($this->getBasicTokens()) as $token) { $e->query->select('e.' . $fields[$token]['name'] . ' AS ' . $this->getEntityAlias() . $token); } foreach (array_keys($this->getPseudoTokens()) as $token) { diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 37f49afa9c..bf677ad1f2 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -61,6 +61,15 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return CRM_Core_DAO_AllCoreTables::getFullName($this->getApiEntityName()); } + /** + * Get all the tokens supported by this processor. + * + * @return array|string[] + */ + public function getAllTokens(): array { + return array_merge($this->getBasicTokens(), $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens('Contribution')); + } + /** * Is the given field a date field. * diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 6d7220acbc..94e6da9cdc 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -565,27 +565,16 @@ class CRM_Core_SelectValues { public static function contributionTokens(): array { $tokens = []; $processor = new CRM_Contribute_Tokens(); - foreach (array_merge($processor->getPseudoTokens(), $processor->getBasicTokens()) as $token => $title) { + foreach ($processor->getAllTokens() as $token => $title) { $tokens['{contribution.' . $token . '}'] = $title; } return array_merge($tokens, [ - '{contribution.id}' => ts('Contribution ID'), - '{contribution.total_amount}' => ts('Total Amount'), - '{contribution.fee_amount}' => ts('Fee Amount'), - '{contribution.net_amount}' => ts('Net Amount'), - '{contribution.non_deductible_amount}' => ts('Non-deductible Amount'), - '{contribution.receive_date}' => ts('Contribution Date Received'), - '{contribution.trxn_id}' => ts('Transaction ID'), - '{contribution.invoice_id}' => ts('Invoice ID'), - '{contribution.currency}' => ts('Currency'), - '{contribution.cancel_date}' => ts('Contribution Cancel Date'), '{contribution.cancel_reason}' => ts('Contribution Cancel Reason'), - '{contribution.receipt_date}' => ts('Receipt Date'), - '{contribution.thankyou_date}' => ts('Thank You Date'), - '{contribution.source}' => ts('Contribution Source'), '{contribution.amount_level}' => ts('Amount Level'), '{contribution.check_number}' => ts('Check Number'), '{contribution.campaign}' => ts('Contribution Campaign'), + // @todo - we shouldn't need to include custom fields here - + // remove, with test. ], CRM_Utils_Token::getCustomFieldTokens('Contribution', TRUE)); } -- 2.25.1