Fix error on membership imports
[civicrm-core.git] / CRM / Member / Tokens.php
index 686cc71f4652102c7c29e474dfd94c4102fb0592..35ba54a3557d5fa6db017dbe8a8bfa5923c70cad 100644 (file)
  * implementation which is not tied to scheduled reminders, although
  * that is outside the current scope.
  */
-class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
+class CRM_Member_Tokens extends CRM_Core_EntityTokens {
 
   /**
-   * Class constructor.
+   * Get the entity name for api v4 calls.
+   *
+   * @return string
    */
-  public function __construct() {
-    parent::__construct('membership', 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' => ts('Membership Status'),
-        'type' => ts('Membership Type'),
-      ],
-      CRM_Utils_Token::getCustomFieldTokens('Membership')
-    ));
+  protected function getApiEntityName(): string {
+    return 'Membership';
   }
 
   /**
-   * @inheritDoc
+   * List out the fields that are exposed.
+   *
+   * For historical reasons these are the only exposed fields.
+   *
+   * It is also possible to list 'skippedFields'
+   *
+   * @return string[]
    */
-  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';
+  protected function getExposedFields(): array {
+    return [
+      'id',
+      'join_date',
+      'start_date',
+      'end_date',
+      'status_id',
+      'membership_type_id',
+      'source',
+      'status_override_end_date',
+    ];
   }
 
   /**
-   * Alter action schedule query.
-   *
-   * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
+   * @inheritDoc
+   * @throws \CiviCRM_API3_Exception
    */
-  public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
-    if ($e->mapping->getEntity() !== 'civicrm_membership') {
-      return;
+  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);
     }
+  }
 
-    // FIXME: `select('e.*')` seems too broad.
-    $e->query
-      ->select('e.*')
-      ->select('mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, ms.name as status, mt.name as type')
-      ->join('mt', "!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id")
-      ->join('ms', "!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id");
+  /**
+   * Get any overrides for token metadata.
+   *
+   * This is most obviously used for setting the audience, which
+   * will affect widget-presence.
+   *
+   * Changing the audience is done in order to simplify the
+   * UI for more general users.
+   *
+   * @return \string[][]
+   */
+  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) {
-    $actionSearchResult = $row->context['actionSearchResult'];
+  public function getDependencies(): array {
+    return ['fee' => 'membership_type_id'];
+  }
 
-    if (in_array($field, ['start_date', 'end_date', 'join_date'])) {
-      $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
-    }
-    elseif ($field == 'fee') {
-      $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($actionSearchResult->$field));
-    }
-    elseif (isset($actionSearchResult->$field)) {
-      $row->tokens($entity, $field, $actionSearchResult->$field);
-    }
-    elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
-      $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
-    }
-    else {
-      $row->tokens($entity, $field, '');
-    }
+  /**
+   * 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',
+      ],
+    ];
   }
 
 }