X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContribute%2FTokens.php;h=ca1b952cc7fb842cfc24209b783d14d6f0c90d87;hb=b5e0905cb40f7d91842653767ea7e2ae02e20bd8;hp=a8ddf76aafcb5ddf142fe1bc5539e222f139dcce;hpb=2108bdad9cb984c7b71310fc95f3636217c69d09;p=civicrm-core.git diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index a8ddf76aaf..ca1b952cc7 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -10,11 +10,6 @@ +--------------------------------------------------------------------+ */ -use Civi\ActionSchedule\Event\MailingQueryEvent; -use Civi\Token\AbstractTokenSubscriber; -use Civi\Token\TokenProcessor; -use Civi\Token\TokenRow; - /** * Class CRM_Contribute_Tokens * @@ -23,44 +18,71 @@ use Civi\Token\TokenRow; * At time of writing, we don't have any particularly special tokens -- we just * do some basic formatting based on the corresponding DB field. */ -class CRM_Contribute_Tokens extends AbstractTokenSubscriber { +class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { + + /** + * @return string + */ + protected function getEntityName(): string { + return 'contribution'; + } + + /** + * @return string + */ + protected function getEntityAlias(): string { + return 'contrib_'; + } /** - * Get a list of tokens whose name and title match the DB fields. + * Get the entity name for api v4 calls. + * + * In practice this IS just ucfirst($this->GetEntityName) + * but declaring it seems more legible. + * + * @return string + */ + protected function getApiEntityName(): string { + return 'Contribution'; + } + + /** + * 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 { - return [ + protected function getExposedFields(): array { + $fields = [ 'contribution_page_id', + 'source', + 'id', 'receive_date', 'total_amount', 'fee_amount', 'net_amount', + 'non_deductible_amount', 'trxn_id', 'invoice_id', 'currency', - 'contribution_cancel_date', + 'cancel_date', 'receipt_date', 'thankyou_date', 'tax_amount', 'contribution_status_id', + 'financial_type_id', + 'payment_instrument_id', + 'cancel_reason', + 'amount_level', + 'check_number', ]; - } - - /** - * Get alias tokens. - * - * @return array - */ - protected function getAliasTokens(): array { - return [ - 'id' => 'contribution_id', - 'payment_instrument' => 'payment_instrument_id', - 'source' => 'contribution_source', - 'status' => 'contribution_status_id', - 'type' => 'financial_type_id', - 'cancel_date' => 'contribution_cancel_date', - ]; + if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + $fields[] = 'campaign_id'; + } + return $fields; } /** @@ -74,88 +96,12 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { * * @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); + public function getBasicTokens(): array { + $return = []; + foreach ($this->getExposedFields() as $fieldName) { + $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; } + return $return; } }