dev/core#2832 Extend Membership tokens to 'listen' and add test cover
[civicrm-core.git] / CRM / Member / Tokens.php
CommitLineData
46f5566c
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
46f5566c 6 | |
bc77d7c0
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
46f5566c
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 * Class CRM_Member_Tokens
15 *
16 * Generate "member.*" tokens.
17 *
18 * This TokenSubscriber was produced by refactoring the code from the
19 * scheduled-reminder system with the goal of making that system
20 * more flexible. The current implementation is still coupled to
21 * scheduled-reminders. It would be good to figure out a more generic
22 * implementation which is not tied to scheduled reminders, although
23 * that is outside the current scope.
24 */
dd2f879a 25class CRM_Member_Tokens extends CRM_Core_EntityTokens {
46f5566c 26
70599df6 27 /**
dd2f879a
EM
28 * Get the entity name for api v4 calls.
29 *
30 * @return string
31 */
32 protected function getApiEntityName(): string {
33 return 'Membership';
34 }
35
36 /**
37 * Get all tokens.
38 *
39 * This function will be removed once the parent class can determine it.
70599df6 40 */
dd2f879a
EM
41 public function getAllTokens(): array {
42 return array_merge(
be2fb01f 43 [
4e9b6a62 44 'fee' => ts('Membership Fee'),
45 'id' => ts('Membership ID'),
46 'join_date' => ts('Membership Join Date'),
47 'start_date' => ts('Membership Start Date'),
48 'end_date' => ts('Membership End Date'),
dd2f879a
EM
49 'status_id:label' => ts('Membership Status'),
50 'membership_type_id:label' => ts('Membership Type'),
be2fb01f 51 ],
18c017c8 52 CRM_Utils_Token::getCustomFieldTokens('Membership')
dd2f879a 53 );
46f5566c
TO
54 }
55
e8e8f3ad 56 /**
298795cd 57 * @inheritDoc
d568dbe0 58 * @throws \CiviCRM_API3_Exception
46f5566c
TO
59 */
60 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
b024d6a1 61 if ($field === 'fee') {
d568dbe0
EM
62 $membershipType = CRM_Member_BAO_MembershipType::getMembershipType($this->getFieldValue($row, 'membership_type_id'));
63 $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($membershipType['minimum_fee']));
4e9b6a62 64 }
46f5566c 65 else {
dd2f879a 66 parent::evaluateToken($row, $entity, $field, $prefetch);
46f5566c
TO
67 }
68 }
69
d568dbe0
EM
70 /**
71 * Get fields which need to be returned to render another token.
72 *
73 * @return array
74 */
75 public function getDependencies(): array {
76 return ['fee' => 'membership_type_id'];
77 }
78
46f5566c 79}