Merge pull request #21645 from colemanw/displayName
[civicrm-core.git] / CRM / Member / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 |
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 */
25 class CRM_Member_Tokens extends CRM_Core_EntityTokens {
26
27 /**
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 * List out the fields that are exposed.
38 *
39 * For historical reasons these are the only exposed fields.
40 *
41 * It is also possible to list 'skippedFields'
42 *
43 * @return string[]
44 */
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',
53 ];
54 }
55
56 /**
57 * @inheritDoc
58 * @throws \CiviCRM_API3_Exception
59 */
60 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
61 if ($field === 'fee') {
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']));
64 }
65 else {
66 parent::evaluateToken($row, $entity, $field, $prefetch);
67 }
68 }
69
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
79 /**
80 * Get any tokens with custom calculation.
81 *
82 * In this case 'fee' should be converted to{membership.membership_type_id.fee}
83 * but we don't have the formatting support to do that with no
84 * custom intervention yet.
85 */
86 protected function getBespokeTokens(): array {
87 return [
88 'fee' => [
89 'title' => ts('Membership Fee'),
90 'name' => 'fee',
91 'type' => 'calculated',
92 'options' => NULL,
93 'data_type' => 'integer',
94 'audience' => 'user',
95 ],
96 ];
97 }
98
99 }