Merge pull request #23709 from eileenmcnaughton/buttons
[civicrm-core.git] / CRM / Member / Tokens.php
index 9db8a15420ae08310ede86ab797f2f8268e1dfd6..35ba54a3557d5fa6db017dbe8a8bfa5923c70cad 100644 (file)
@@ -34,59 +34,92 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens {
   }
 
   /**
-   * Get all tokens.
+   * List out the fields that are exposed.
    *
-   * This function will be removed once the parent class can determine it.
+   * For historical reasons these are the only exposed fields.
+   *
+   * It is also possible to list 'skippedFields'
+   *
+   * @return string[]
    */
-  public function getAllTokens(): array {
-    return array_merge(
-      [
-        'fee' => ts('Membership Fee'),
-        'id' => ts('Membership ID'),
-        'join_date' => ts('Membership Join Date'),
-        'start_date' => ts('Membership Start Date'),
-        'end_date' => ts('Membership End Date'),
-        'status_id:label' => ts('Membership Status'),
-        'membership_type_id:label' => ts('Membership Type'),
-      ],
-      CRM_Utils_Token::getCustomFieldTokens('Membership')
-    );
+  protected function getExposedFields(): array {
+    return [
+      'id',
+      'join_date',
+      'start_date',
+      'end_date',
+      'status_id',
+      'membership_type_id',
+      'source',
+      'status_override_end_date',
+    ];
   }
 
   /**
    * @inheritDoc
+   * @throws \CiviCRM_API3_Exception
    */
-  public function checkActive(\Civi\Token\TokenProcessor $processor) {
-    // Extracted from scheduled-reminders code. See the class description.
-    return !empty($processor->context['actionMapping'])
-      && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
+  public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
+    if ($field === 'fee') {
+      $membershipType = CRM_Member_BAO_MembershipType::getMembershipType($this->getFieldValue($row, 'membership_type_id'));
+      $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($membershipType['minimum_fee']));
+    }
+    else {
+      parent::evaluateToken($row, $entity, $field, $prefetch);
+    }
   }
 
   /**
-   * Alter action schedule query.
+   * Get any overrides for token metadata.
+   *
+   * This is most obviously used for setting the audience, which
+   * will affect widget-presence.
    *
-   * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
+   * Changing the audience is done in order to simplify the
+   * UI for more general users.
+   *
+   * @return \string[][]
    */
-  public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void {
-    if ($e->mapping->getEntity() !== 'civicrm_membership') {
-      return;
-    }
-    parent::alterActionScheduleQuery($e);
-    $e->query
-      ->select('mt.minimum_fee as ' . $this->getEntityAlias() . 'fee')
-      ->join('mt', '!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id');
+  protected function getTokenMetadataOverrides(): array {
+    return [
+      'owner_membership_id' => ['audience' => 'sysadmin'],
+      'max_related' => ['audience' => 'sysadmin'],
+      'contribution_recur_id' => ['audience' => 'sysadmin'],
+      'is_override' => ['audience' => 'sysadmin'],
+      'is_test' => ['audience' => 'sysadmin'],
+      // Pay later is considered to be unreliable in the schema
+      // and will eventually be removed.
+      'is_pay_later' => ['audience' => 'deprecated'],
+    ];
   }
 
   /**
-   * @inheritDoc
+   * Get fields which need to be returned to render another token.
+   *
+   * @return array
    */
-  public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
-    if ($field === 'fee') {
-      $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->getFieldValue($row, $field)));
-    }
-    else {
-      parent::evaluateToken($row, $entity, $field, $prefetch);
-    }
+  public function getDependencies(): array {
+    return ['fee' => 'membership_type_id'];
+  }
+
+  /**
+   * Get any tokens with custom calculation.
+   *
+   * In this case 'fee' should be converted to{membership.membership_type_id.fee}
+   * but we don't have the formatting support to do that with no
+   * custom intervention yet.
+   */
+  protected function getBespokeTokens(): array {
+    return [
+      'fee' => [
+        'title' => ts('Membership Fee'),
+        'name' => 'fee',
+        'type' => 'calculated',
+        'options' => NULL,
+        'data_type' => 'integer',
+        'audience' => 'user',
+      ],
+    ];
   }
 
 }