From: Eileen McNaughton Date: Sat, 7 Aug 2021 05:07:05 +0000 (+1200) Subject: [REF] Move all the generic functions to the parent X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=45e8391a22328abc53d122c21c985452529b9e30;p=civicrm-core.git [REF] Move all the generic functions to the parent This is for clarity, note that I need agreement on https://lab.civicrm.org/dev/core/-/issues/2745 to decide whether to keep 'getExposedFields' or not - hence that is not moved at this stage --- diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index c2372a8edd..e0aa7b2054 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 * @@ -50,13 +46,6 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { return 'Contribution'; } - /** - * Metadata about the entity fields. - * - * @var array - */ - protected $fieldMetadata = []; - /** * Get a list of tokens for the entity for which access is permitted to. * @@ -108,64 +97,4 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { return $return; } - /** - * Class constructor. - */ - public function __construct() { - $tokens = $this->getAllTokens(); - 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() === $this->getExtendableTableName(); - } - - /** - * Alter action schedule query. - * - * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e - */ - public function alterActionScheduleQuery(MailingQueryEvent $e): void { - if ($e->mapping->getEntity() !== $this->getExtendableTableName()) { - return; - } - foreach ($this->getReturnFields() as $token) { - $e->query->select('e.' . $token . ' AS ' . $this->getEntityAlias() . $token); - } - } - - /** - * @inheritDoc - * @throws \CRM_Core_Exception - */ - public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { - $fieldValue = $this->getFieldValue($row, $field); - - if ($this->isPseudoField($field)) { - $split = explode(':', $field); - return $row->tokens($entity, $field, $this->getPseudoValue($split[0], $split[1], $this->getFieldValue($row, $split[0]))); - } - if ($this->isMoneyField($field)) { - return $row->format('text/plain')->tokens($entity, $field, - \CRM_Utils_Money::format($fieldValue, $this->getFieldValue($row, 'currency'))); - } - if ($this->isDateField($field)) { - return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue)); - } - if ($this->isCustomField($field)) { - $row->customToken($entity, \CRM_Core_BAO_CustomField::getKeyID($field), $this->getFieldValue($row, 'id')); - } - else { - $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue); - } - } - } diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 206b196ff8..542ff711ac 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -12,6 +12,8 @@ use Civi\Token\AbstractTokenSubscriber; use Civi\Token\TokenRow; +use Civi\ActionSchedule\Event\MailingQueryEvent; +use Civi\Token\TokenProcessor; /** * Class CRM_Core_EntityTokens @@ -26,13 +28,38 @@ use Civi\Token\TokenRow; class CRM_Core_EntityTokens extends AbstractTokenSubscriber { /** - * This is required for the parent - it will be filled out. - * * @inheritDoc + * @throws \CRM_Core_Exception */ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { + $fieldValue = $this->getFieldValue($row, $field); + + if ($this->isPseudoField($field)) { + $split = explode(':', $field); + return $row->tokens($entity, $field, $this->getPseudoValue($split[0], $split[1], $this->getFieldValue($row, $split[0]))); + } + if ($this->isMoneyField($field)) { + return $row->format('text/plain')->tokens($entity, $field, + \CRM_Utils_Money::format($fieldValue, $this->getFieldValue($row, 'currency'))); + } + if ($this->isDateField($field)) { + return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue)); + } + if ($this->isCustomField($field)) { + $row->customToken($entity, \CRM_Core_BAO_CustomField::getKeyID($field), $this->getFieldValue($row, 'id')); + } + else { + $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue); + } } + /** + * Metadata about the entity fields. + * + * @var array + */ + protected $fieldMetadata = []; + /** * Get the entity name for api v4 calls. * @@ -223,4 +250,38 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return $actionSearchResult->{$aliasedField} ?? NULL; } + /** + * Class constructor. + */ + public function __construct() { + $tokens = $this->getAllTokens(); + parent::__construct($this->getEntityName(), $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() === $this->getExtendableTableName(); + } + + /** + * Alter action schedule query. + * + * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e + */ + public function alterActionScheduleQuery(MailingQueryEvent $e): void { + if ($e->mapping->getEntity() !== $this->getExtendableTableName()) { + return; + } + foreach ($this->getReturnFields() as $token) { + $e->query->select('e.' . $token . ' AS ' . $this->getEntityAlias() . $token); + } + } + }