Commit | Line | Data |
---|---|---|
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 | 25 | class 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 | /** | |
e9841a51 | 37 | * List out the fields that are exposed. |
dd2f879a | 38 | * |
e9841a51 EM |
39 | * For historical reasons these are the only exposed fields. |
40 | * | |
41 | * It is also possible to list 'skippedFields' | |
42 | * | |
43 | * @return string[] | |
70599df6 | 44 | */ |
e9841a51 EM |
45 | protected function getExposedFields(): array { |
46 | return [ | |
47 | 'id', | |
48 | 'join_date', | |
49 | 'start_date', | |
50 | 'end_date', | |
51 | 'status_id', | |
52 | 'membership_type_id', | |
889b0617 EM |
53 | 'source', |
54 | 'status_override_end_date', | |
e9841a51 | 55 | ]; |
46f5566c TO |
56 | } |
57 | ||
e8e8f3ad | 58 | /** |
298795cd | 59 | * @inheritDoc |
d568dbe0 | 60 | * @throws \CiviCRM_API3_Exception |
46f5566c TO |
61 | */ |
62 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
b024d6a1 | 63 | if ($field === 'fee') { |
d568dbe0 EM |
64 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipType($this->getFieldValue($row, 'membership_type_id')); |
65 | $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($membershipType['minimum_fee'])); | |
4e9b6a62 | 66 | } |
46f5566c | 67 | else { |
dd2f879a | 68 | parent::evaluateToken($row, $entity, $field, $prefetch); |
46f5566c TO |
69 | } |
70 | } | |
71 | ||
889b0617 EM |
72 | /** |
73 | * Get any overrides for token metadata. | |
74 | * | |
75 | * This is most obviously used for setting the audience, which | |
76 | * will affect widget-presence. | |
77 | * | |
78 | * Changing the audience is done in order to simplify the | |
79 | * UI for more general users. | |
80 | * | |
81 | * @return \string[][] | |
82 | */ | |
83 | protected function getTokenMetadataOverrides(): array { | |
84 | return [ | |
85 | 'owner_membership_id' => ['audience' => 'sysadmin'], | |
86 | 'max_related' => ['audience' => 'sysadmin'], | |
87 | 'contribution_recur_id' => ['audience' => 'sysadmin'], | |
88 | 'is_override' => ['audience' => 'sysadmin'], | |
89 | 'is_test' => ['audience' => 'sysadmin'], | |
90 | // Pay later is considered to be unreliable in the schema | |
91 | // and will eventually be removed. | |
92 | 'is_pay_later' => ['audience' => 'deprecated'], | |
93 | ]; | |
94 | } | |
95 | ||
d568dbe0 EM |
96 | /** |
97 | * Get fields which need to be returned to render another token. | |
98 | * | |
99 | * @return array | |
100 | */ | |
101 | public function getDependencies(): array { | |
102 | return ['fee' => 'membership_type_id']; | |
103 | } | |
104 | ||
e9841a51 EM |
105 | /** |
106 | * Get any tokens with custom calculation. | |
107 | * | |
108 | * In this case 'fee' should be converted to{membership.membership_type_id.fee} | |
109 | * but we don't have the formatting support to do that with no | |
110 | * custom intervention yet. | |
111 | */ | |
112 | protected function getBespokeTokens(): array { | |
113 | return [ | |
114 | 'fee' => [ | |
115 | 'title' => ts('Membership Fee'), | |
116 | 'name' => 'fee', | |
117 | 'type' => 'calculated', | |
118 | 'options' => NULL, | |
119 | 'data_type' => 'integer', | |
120 | 'audience' => 'user', | |
121 | ], | |
122 | ]; | |
123 | } | |
124 | ||
46f5566c | 125 | } |