CRM-13422 - Form/ScheduleReminders - Allow role filters on custom mappings
[civicrm-core.git] / CRM / Contribute / ActionMapping / ByPage.php
CommitLineData
2045389a
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28use Civi\ActionSchedule\RecipientBuilder;
29
30/**
31 * Class CRM_Contribute_ActionMapping_ByPage
32 *
33 * This defines the scheduled-reminder functionality for contribution
34 * entities. It is useful for sending a reminder based on:
35 * - The receipt-date, cancel-date, or thankyou-date.
36 * - The page on which the contribution was made.
37 */
38class CRM_Contribute_ActionMapping_ByPage implements \Civi\ActionSchedule\MappingInterface {
39
40 /**
41 * The value for civicrm_action_schedule.mapping_id which identifies the
42 * "Contribution Page" mapping.
43 */
44 const MAPPING_ID = 'contribpage';
45
46 /**
47 * Register Activity-related action mappings.
48 *
49 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
50 */
51 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
52 $registrations->register(new static());
53 }
54
55 /**
56 * @return mixed
57 */
58 public function getId() {
59 return self::MAPPING_ID;
60 }
61
62 /**
63 * @return string
64 */
65 public function getEntity() {
66 return 'civicrm_contribution';
67 }
68
69 /**
70 * Get a printable label for this mapping type.
71 *
72 * @return string
73 */
74 public function getLabel() {
75 return ts('Contribution Page');
76 }
77
78 /**
79 * Get a printable label to use as the header on the 'value' filter.
80 *
81 * @return string
82 */
83 public function getValueHeader() {
84 return ts('Contribution Page');
85 }
86
87 /**
88 * Get a printable label to use as the header on the 'status' filter.
89 *
90 * @return string
91 */
92 public function getStatusHeader() {
93 return ts('Contribution Status');
94 }
95
96 /**
97 * Get a list of value options.
98 *
99 * @return array
100 * Array(string $value => string $label).
101 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
102 * @throws CRM_Core_Exception
103 */
104 public function getValueLabels() {
105 return CRM_Contribute_BAO_Contribution::buildOptions('contribution_page_id', 'get', array());
106 }
107
108 /**
109 * Get a list of status options.
110 *
111 * @param string|int $value
112 * The list of status options may be contingent upon the selected filter value.
113 * This is the selected filter value.
114 * @return array
115 * Array(string $value => string $label).
116 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
117 * @throws CRM_Core_Exception
118 */
119 public function getStatusLabels($value) {
120 return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', array());
121 }
122
123 /**
124 * Get a list of available date fields.
125 *
126 * @return array
127 * Array(string $fieldName => string $fieldLabel).
128 */
129 public function getDateFields() {
130 return array(
131 'receive_date' => ts('Receive Date'),
132 'cancel_date' => ts('Cancel Date'),
133 'receipt_date' => ts('Receipt Date'),
134 'thankyou_date' => ts('Thank You Date'),
135 );
136 }
137
138 /**
139 * FIXME: Unsure. Not sure how it differs from getRecipientTypes... but it does...
140 *
141 * @param string $recipientType
142 * @return array
143 * Array(mixed $name => string $label).
144 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
145 */
146 public function getRecipientListing($recipientType) {
147 return array();
148 }
149
150 /**
151 * FIXME: Unsure. Not sure how it differs from getRecipientListing... but it does...
152 *
2045389a
TO
153 * @return array
154 * array(mixed $value => string $label).
155 * Ex: array('assignee' => 'Activity Assignee').
156 */
9d97a648 157 public function getRecipientTypes() {
2045389a
TO
158 return array();
159 }
160
7f0141d8
TO
161 /**
162 * Determine whether a schedule based on this mapping is sufficiently
163 * complete.
164 *
165 * @param \CRM_Core_DAO_ActionSchedule $schedule
166 * @return array
167 * Array (string $code => string $message).
168 * List of error messages.
169 */
170 public function validateSchedule($schedule) {
171 return array();
172 }
173
2045389a
TO
174 /**
175 * Generate a query to locate contacts who match the given
176 * schedule.
177 *
178 * @param \CRM_Core_DAO_ActionSchedule $schedule
179 * @param string $phase
180 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
181 * @param array $defaultParams
182 * Default parameters that should be included with query.
183 * @return \CRM_Utils_SQL_Select
184 * @see RecipientBuilder
185 * @throws CRM_Core_Exception
186 */
187 public function createQuery($schedule, $phase, $defaultParams) {
188 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
189 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
190
191 $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);;
192 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
193 $query['casContactIdField'] = 'e.contact_id';
194 $query['casEntityIdField'] = 'e.id';
195 $query['casContactTableAlias'] = NULL;
196
197 // $schedule->start_action_date is user-supplied data. validate.
198 if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
199 throw new CRM_Core_Exception("Invalid date field");
200 }
201 $query['casDateField'] = $schedule->start_action_date;
202
203 // build where clause
204 if (!empty($selectedValues)) {
205 $query->where("e.contribution_page_id IN (@selectedValues)")
206 ->param('selectedValues', $selectedValues);
207 }
208 if (!empty($selectedStatuses)) {
209 $query->where("e.contribution_status_id IN (#selectedStatuses)")
210 ->param('selectedStatuses', $selectedStatuses);
211 }
212
213 return $query;
214 }
215
216}