Merge pull request #14249 from yashodha/959_dev
[civicrm-core.git] / CRM / Member / Tokens.php
CommitLineData
46f5566c
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
46f5566c 6 +--------------------------------------------------------------------+
6b83d5bd 7 | Copyright CiviCRM LLC (c) 2004-2019 |
46f5566c
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * Class CRM_Member_Tokens
31 *
32 * Generate "member.*" tokens.
33 *
34 * This TokenSubscriber was produced by refactoring the code from the
35 * scheduled-reminder system with the goal of making that system
36 * more flexible. The current implementation is still coupled to
37 * scheduled-reminders. It would be good to figure out a more generic
38 * implementation which is not tied to scheduled reminders, although
39 * that is outside the current scope.
40 */
41class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
42
70599df6 43 /**
44 * Class constructor.
45 */
46f5566c 46 public function __construct() {
4e9b6a62 47 parent::__construct('membership', array_merge(
be2fb01f 48 [
4e9b6a62 49 'fee' => ts('Membership Fee'),
50 'id' => ts('Membership ID'),
51 'join_date' => ts('Membership Join Date'),
52 'start_date' => ts('Membership Start Date'),
53 'end_date' => ts('Membership End Date'),
54 'status' => ts('Membership Status'),
55 'type' => ts('Membership Type'),
be2fb01f 56 ],
18c017c8 57 CRM_Utils_Token::getCustomFieldTokens('Membership')
46f5566c
TO
58 ));
59 }
60
e8e8f3ad 61 /**
298795cd 62 * @inheritDoc
e8e8f3ad 63 */
46f5566c
TO
64 public function checkActive(\Civi\Token\TokenProcessor $processor) {
65 // Extracted from scheduled-reminders code. See the class description.
971e129b 66 return !empty($processor->context['actionMapping'])
46f5566c
TO
67 && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
68 }
69
f2ac86d1 70 /**
71 * Alter action schedule query.
72 *
73 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
74 */
f9ec2da6
TO
75 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
76 if ($e->mapping->getEntity() !== 'civicrm_membership') {
77 return;
78 }
79
c5c263ca 80 // FIXME: `select('e.*')` seems too broad.
f9ec2da6 81 $e->query
c5c263ca 82 ->select('e.*')
f9ec2da6
TO
83 ->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')
84 ->join('mt', "!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id")
85 ->join('ms', "!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id");
86 }
87
46f5566c 88 /**
298795cd 89 * @inheritDoc
46f5566c
TO
90 */
91 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
92 $actionSearchResult = $row->context['actionSearchResult'];
93
be2fb01f 94 if (in_array($field, ['start_date', 'end_date', 'join_date'])) {
46f5566c
TO
95 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
96 }
97 elseif (isset($actionSearchResult->$field)) {
98 $row->tokens($entity, $field, $actionSearchResult->$field);
99 }
8640061b 100 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
101 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 102 }
46f5566c
TO
103 else {
104 $row->tokens($entity, $field, '');
105 }
106 }
107
108}