CRM-13244 - Split prepareMailingQuery() into entity-specific pieces.
[civicrm-core.git] / CRM / Member / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2015 |
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
43 public function __construct() {
44 parent::__construct('membership', array(
45 'fee' => ts('Membership Fee'),
46 'id' => ts('Membership ID'),
47 'join_date' => ts('Membership Join Date'),
48 'start_date' => ts('Membership Start Date'),
49 'end_date' => ts('Membership End Date'),
50 'status' => ts('Membership Status'),
51 'type' => ts('Membership Type'),
52 ));
53 }
54
55 public function checkActive(\Civi\Token\TokenProcessor $processor) {
56 // Extracted from scheduled-reminders code. See the class description.
57 return
58 !empty($processor->context['actionMapping'])
59 && $processor->context['actionMapping']->getEntity() === 'civicrm_membership';
60 }
61
62 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
63 if ($e->mapping->getEntity() !== 'civicrm_membership') {
64 return;
65 }
66
67 $e->query
68 ->select('e.*') // FIXME: seems too broad.
69 ->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')
70 ->join('mt', "!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id")
71 ->join('ms', "!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id");
72 }
73
74 /**
75 * Evaluate the content of a single token.
76 *
77 * @param \Civi\Token\TokenRow $row
78 * The record for which we want token values.
79 * @param string $field
80 * The name of the token field.
81 * @param mixed $prefetch
82 * Any data that was returned by the prefetch().
83 * @return mixed
84 */
85 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
86 $actionSearchResult = $row->context['actionSearchResult'];
87
88 if (in_array($field, array('start_date', 'end_date', 'join_date'))) {
89 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
90 }
91 elseif (isset($actionSearchResult->$field)) {
92 $row->tokens($entity, $field, $actionSearchResult->$field);
93 }
94 else {
95 $row->tokens($entity, $field, '');
96 }
97 }
98
99 }