Merge pull request #21474 from MegaphoneJon/core-2318
[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 * Get all tokens.
38 *
39 * This function will be removed once the parent class can determine it.
40 */
41 public function getAllTokens(): array {
42 return array_merge(
43 [
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'),
49 'status' => ts('Membership Status'),
50 'type' => ts('Membership Type'),
51 'status_id:label' => ts('Membership Status'),
52 'membership_type_id:label' => ts('Membership Type'),
53 ],
54 CRM_Utils_Token::getCustomFieldTokens('Membership')
55 );
56 }
57
58 /**
59 * @inheritDoc
60 */
61 public function checkActive(\Civi\Token\TokenProcessor $processor) {
62 // Extracted from scheduled-reminders code. See the class description.
63 return !empty($processor->context['actionMapping'])
64 && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
65 }
66
67 /**
68 * Alter action schedule query.
69 *
70 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
71 */
72 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void {
73 if ($e->mapping->getEntity() !== 'civicrm_membership') {
74 return;
75 }
76
77 // FIXME: `select('e.*')` seems too broad.
78 $e->query
79 ->select('e.*')
80 ->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')
81 ->join('mt', '!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id')
82 ->join('ms', '!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id');
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
89 $actionSearchResult = $row->context['actionSearchResult'];
90
91 if (in_array($field, ['start_date', 'end_date', 'join_date'])) {
92 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
93 }
94 elseif ($field == 'fee') {
95 $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($actionSearchResult->$field));
96 }
97 elseif (isset($actionSearchResult->$field)) {
98 $row->tokens($entity, $field, $actionSearchResult->$field);
99 }
100 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
101 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
102 }
103 else {
104 parent::evaluateToken($row, $entity, $field, $prefetch);
105 }
106 }
107
108 }