dev/core#1588 Fix regression where empty string is passed to SettingsBag
[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 */
25class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
26
70599df6 27 /**
28 * Class constructor.
29 */
46f5566c 30 public function __construct() {
4e9b6a62 31 parent::__construct('membership', array_merge(
be2fb01f 32 [
4e9b6a62 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'),
be2fb01f 40 ],
18c017c8 41 CRM_Utils_Token::getCustomFieldTokens('Membership')
46f5566c
TO
42 ));
43 }
44
e8e8f3ad 45 /**
298795cd 46 * @inheritDoc
e8e8f3ad 47 */
46f5566c
TO
48 public function checkActive(\Civi\Token\TokenProcessor $processor) {
49 // Extracted from scheduled-reminders code. See the class description.
971e129b 50 return !empty($processor->context['actionMapping'])
46f5566c
TO
51 && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
52 }
53
f2ac86d1 54 /**
55 * Alter action schedule query.
56 *
57 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
58 */
f9ec2da6
TO
59 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
60 if ($e->mapping->getEntity() !== 'civicrm_membership') {
61 return;
62 }
63
c5c263ca 64 // FIXME: `select('e.*')` seems too broad.
f9ec2da6 65 $e->query
c5c263ca 66 ->select('e.*')
f9ec2da6
TO
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
46f5566c 72 /**
298795cd 73 * @inheritDoc
46f5566c
TO
74 */
75 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
76 $actionSearchResult = $row->context['actionSearchResult'];
77
be2fb01f 78 if (in_array($field, ['start_date', 'end_date', 'join_date'])) {
46f5566c
TO
79 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
80 }
57055e58 81 elseif ($field == 'fee') {
82 $row->tokens($entity, $field, \CRM_Utils_Money::format($actionSearchResult->$field, NULL, NULL, TRUE));
83 }
46f5566c
TO
84 elseif (isset($actionSearchResult->$field)) {
85 $row->tokens($entity, $field, $actionSearchResult->$field);
86 }
8640061b 87 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
88 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 89 }
46f5566c
TO
90 else {
91 $row->tokens($entity, $field, '');
92 }
93 }
94
95}