}
/**
- * 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' => ts('Membership Status'),
- 'type' => ts('Membership Type'),
- '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;
- }
-
- // 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, membership_type_id as Membership__membership_type_id, status_id as Membership__status_id, 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');
+ 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 {
- parent::evaluateToken($row, $entity, $field, $prefetch);
- }
+ /**
+ * 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',
+ ],
+ ];
}
}