Reconcile remaining fields between scheduled reminders and legacy tokens
[civicrm-core.git] / CRM / Contribute / Tokens.php
index cc40775a23d08d2b5ebddfc474485a47a95b930b..ca1b952cc7fb842cfc24209b783d14d6f0c90d87 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-use Civi\ActionSchedule\Event\MailingQueryEvent;
-use Civi\Token\TokenProcessor;
-use Civi\Token\TokenRow;
-
 /**
  * Class CRM_Contribute_Tokens
  *
@@ -51,18 +47,16 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens {
   }
 
   /**
-   * Metadata about the entity 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.
    *
-   * @var array
-   */
-  protected $fieldMetadata = [];
-
-  /**
-   * Get a list of tokens whose name and title match the DB fields.
    * @return array
    */
-  protected function getPassthruTokens(): array {
-    return [
+  protected function getExposedFields(): array {
+    $fields = [
       'contribution_page_id',
       'source',
       'id',
@@ -81,7 +75,14 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens {
       'contribution_status_id',
       'financial_type_id',
       'payment_instrument_id',
+      'cancel_reason',
+      'amount_level',
+      'check_number',
     ];
+    if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
+      $fields[] = 'campaign_id';
+    }
+    return $fields;
   }
 
   /**
@@ -97,82 +98,10 @@ 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;
   }
 
-  /**
-   * 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 ' . $this->getEntityAlias() . $token);
-    }
-    foreach (array_keys($this->getPseudoTokens()) as $token) {
-      $split = explode(':', $token);
-      $e->query->select('e.' . $fields[$split[0]]['name'] . ' AS ' . $this->getEntityAlias() . $split[0]);
-    }
-  }
-
-  /**
-   * @inheritDoc
-   * @throws \CRM_Core_Exception
-   */
-  public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
-    $actionSearchResult = $row->context['actionSearchResult'];
-    $aliasedField = $this->getEntityAlias() . $field;
-    $fieldValue = $actionSearchResult->{$aliasedField} ?? NULL;
-
-    if ($this->isPseudoField($field)) {
-      $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 ($this->isCustomField($field)) {
-      $row->customToken($entity, \CRM_Core_BAO_CustomField::getKeyID($field), $actionSearchResult->entity_id);
-    }
-    else {
-      $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue);
-    }
-  }
-
 }