Merge pull request #19595 from eileenmcnaughton/tokens
[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 \Civi\Token\AbstractTokenSubscriber {
26
27 /**
28 * Class constructor.
29 */
30 public function __construct() {
31 parent::__construct('membership', array_merge(
32 [
33 'fee' => ts('Membership Fee'),
34 'id' => ts('Membership ID'),
35 'join_date' => ts('Membership Join Date'),
36 'start_date' => ts('Membership Start Date'),
37 'end_date' => ts('Membership End Date'),
38 'status' => ts('Membership Status'),
39 'type' => ts('Membership Type'),
40 ],
41 CRM_Utils_Token::getCustomFieldTokens('Membership')
42 ));
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function checkActive(\Civi\Token\TokenProcessor $processor) {
49 // Extracted from scheduled-reminders code. See the class description.
50 return !empty($processor->context['actionMapping'])
51 && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
52 }
53
54 /**
55 * Alter action schedule query.
56 *
57 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
58 */
59 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
60 if ($e->mapping->getEntity() !== 'civicrm_membership') {
61 return;
62 }
63
64 // FIXME: `select('e.*')` seems too broad.
65 $e->query
66 ->select('e.*')
67 ->select('mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, ms.name as status, mt.name as type')
68 ->join('mt', "!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id")
69 ->join('ms', "!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id");
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
76 $actionSearchResult = $row->context['actionSearchResult'];
77
78 if (in_array($field, ['start_date', 'end_date', 'join_date'])) {
79 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
80 }
81 elseif ($field == 'fee') {
82 $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($actionSearchResult->$field));
83 }
84 elseif (isset($actionSearchResult->$field)) {
85 $row->tokens($entity, $field, $actionSearchResult->$field);
86 }
87 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
88 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
89 }
90 else {
91 $row->tokens($entity, $field, '');
92 }
93 }
94
95 }