Commit | Line | Data |
---|---|---|
46f5566c TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
3435af9a | 5 | | CiviCRM version 4.7 | |
46f5566c | 6 | +--------------------------------------------------------------------+ |
fa938177 | 7 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | */ | |
41 | class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber { | |
42 | ||
70599df6 | 43 | /** |
44 | * Class constructor. | |
45 | */ | |
46f5566c TO |
46 | public function __construct() { |
47 | parent::__construct('membership', array( | |
48 | 'fee' => ts('Membership Fee'), | |
49 | 'id' => ts('Membership ID'), | |
50 | 'join_date' => ts('Membership Join Date'), | |
51 | 'start_date' => ts('Membership Start Date'), | |
52 | 'end_date' => ts('Membership End Date'), | |
53 | 'status' => ts('Membership Status'), | |
54 | 'type' => ts('Membership Type'), | |
55 | )); | |
56 | } | |
57 | ||
e8e8f3ad | 58 | /** |
59 | * Is token active. | |
60 | * | |
61 | * @param \Civi\Token\TokenProcessor $processor | |
62 | * | |
63 | * @return bool | |
64 | */ | |
46f5566c TO |
65 | public function checkActive(\Civi\Token\TokenProcessor $processor) { |
66 | // Extracted from scheduled-reminders code. See the class description. | |
67 | return | |
68 | !empty($processor->context['actionMapping']) | |
69 | && $processor->context['actionMapping']->getEntity() === 'civicrm_membership'; | |
70 | } | |
71 | ||
f9ec2da6 TO |
72 | public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) { |
73 | if ($e->mapping->getEntity() !== 'civicrm_membership') { | |
74 | return; | |
75 | } | |
76 | ||
c5c263ca | 77 | // FIXME: `select('e.*')` seems too broad. |
f9ec2da6 | 78 | $e->query |
c5c263ca | 79 | ->select('e.*') |
f9ec2da6 TO |
80 | ->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') |
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 | ||
46f5566c TO |
85 | /** |
86 | * Evaluate the content of a single token. | |
87 | * | |
88 | * @param \Civi\Token\TokenRow $row | |
89 | * The record for which we want token values. | |
ad37ac8e | 90 | * @param string $entity |
46f5566c TO |
91 | * @param string $field |
92 | * The name of the token field. | |
93 | * @param mixed $prefetch | |
94 | * Any data that was returned by the prefetch(). | |
ad37ac8e | 95 | * |
46f5566c TO |
96 | * @return mixed |
97 | */ | |
98 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
99 | $actionSearchResult = $row->context['actionSearchResult']; | |
100 | ||
101 | if (in_array($field, array('start_date', 'end_date', 'join_date'))) { | |
102 | $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field)); | |
103 | } | |
104 | elseif (isset($actionSearchResult->$field)) { | |
105 | $row->tokens($entity, $field, $actionSearchResult->$field); | |
106 | } | |
107 | else { | |
108 | $row->tokens($entity, $field, ''); | |
109 | } | |
110 | } | |
111 | ||
112 | } |