X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContribute%2FTokens.php;h=6569c6a72ee34146048117db326b0e4ba7be8f54;hb=11f2c79d9d08d946a0ed0c0ea817527be1fcf966;hp=e37b637f5b9a90d12a510fdbd8a3cb7a17256a9d;hpb=e586838d1cbe12b0d79bab8e980c5c382af7baf1;p=civicrm-core.git diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index e37b637f5b..6569c6a72e 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -10,10 +10,6 @@ +--------------------------------------------------------------------+ */ -use Civi\ActionSchedule\Event\MailingQueryEvent; -use Civi\Token\TokenProcessor; -use Civi\Token\TokenRow; - /** * Class CRM_Contribute_Tokens * @@ -27,179 +23,27 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { /** * @return string */ - protected function getEntityName(): string { - return 'contribution'; - } - - /** - * Get the relevant bao name. - */ - public function getBAOName(): string { - return CRM_Core_DAO_AllCoreTables::getFullName(ucfirst($this->getEntityName())); - } - - /** - * Metadata about the entity fields. - * - * @var array - */ - protected $fieldMetadata = []; - - /** - * Get a list of tokens whose name and title match the DB fields. - * @return array - */ - protected function getPassthruTokens(): array { - return [ - 'contribution_page_id', - 'source', - 'id', - 'receive_date', - 'total_amount', - 'fee_amount', - 'net_amount', - 'non_deductible_amount', - 'trxn_id', - 'invoice_id', - 'currency', - 'cancel_date', - 'receipt_date', - 'thankyou_date', - 'tax_amount', - 'contribution_status_id', - 'financial_type_id', - 'payment_instrument_id', - ]; + protected function getEntityAlias(): string { + return 'contrib_'; } /** - * 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. + * Get the entity name for api v4 calls. * - * See https://lab.civicrm.org/dev/core/-/issues/2650 + * In practice this IS just ucfirst($this->GetEntityName) + * but declaring it seems more legible. * - * @return string[] + * @return string */ - public function getBasicTokens(): array { - $return = []; - foreach (['contribution_status_id', 'payment_instrument_id', 'financial_type_id', 'contribution_page_id'] as $fieldName) { - $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; - } - return $return; + protected function getApiEntityName(): string { + return 'Contribution'; } /** - * 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 - * @throws \CRM_Core_Exception - */ - public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { - $actionSearchResult = $row->context['actionSearchResult']; - $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL; - - if (array_key_exists($field, $this->getPseudoTokens())) { - $split = explode(':', $field); - return $row->tokens($entity, $field, $this->getPseudoValue($split[0], $split[1], $actionSearchResult->{"contrib_$split[0]"} ?? NULL)); - } - if ($this->isMoneyField($field)) { - return $row->format('text/plain')->tokens($entity, $field, - \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency)); - } - if ($this->isDateField($field)) { - return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue)); - } - if ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $cfID, $actionSearchResult->entity_id); - } - else { - $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue); - } - } - - /** - * 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 - * - * @return string - * Eg. 'Completed' in the example above. - * - * @internal function will likely be protected soon. - */ - public function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string { - if ($pseudoKey === 'name') { - $fieldValue = (string) CRM_Core_PseudoConstant::getName($this->getBAOName(), $realField, $fieldValue); - } - if ($pseudoKey === 'label') { - $fieldValue = (string) CRM_Core_PseudoConstant::getLabel($this->getBAOName(), $realField, $fieldValue); - } - return (string) $fieldValue; + public function getCurrencyFieldName() { + return ['currency']; } }