core#1590: Don't send reminders to add'l recipients on deleted/inactive/template...
[civicrm-core.git] / CRM / Contribute / ActionMapping / ByPage.php
CommitLineData
2045389a
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
2045389a 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
2045389a
TO
9 +--------------------------------------------------------------------+
10 */
11
2045389a
TO
12
13/**
14 * Class CRM_Contribute_ActionMapping_ByPage
15 *
16 * This defines the scheduled-reminder functionality for contribution
17 * entities. It is useful for sending a reminder based on:
18 * - The receipt-date, cancel-date, or thankyou-date.
19 * - The page on which the contribution was made.
20 */
21class CRM_Contribute_ActionMapping_ByPage implements \Civi\ActionSchedule\MappingInterface {
22
23 /**
24 * The value for civicrm_action_schedule.mapping_id which identifies the
25 * "Contribution Page" mapping.
26 */
27 const MAPPING_ID = 'contribpage';
28
29 /**
30 * Register Activity-related action mappings.
31 *
32 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
33 */
34 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
35 $registrations->register(new static());
36 }
37
38 /**
39 * @return mixed
40 */
41 public function getId() {
42 return self::MAPPING_ID;
43 }
44
45 /**
46 * @return string
47 */
48 public function getEntity() {
49 return 'civicrm_contribution';
50 }
51
52 /**
53 * Get a printable label for this mapping type.
54 *
55 * @return string
56 */
57 public function getLabel() {
58 return ts('Contribution Page');
59 }
60
61 /**
62 * Get a printable label to use as the header on the 'value' filter.
63 *
64 * @return string
65 */
66 public function getValueHeader() {
67 return ts('Contribution Page');
68 }
69
70 /**
71 * Get a printable label to use as the header on the 'status' filter.
72 *
73 * @return string
74 */
75 public function getStatusHeader() {
76 return ts('Contribution Status');
77 }
78
79 /**
80 * Get a list of value options.
81 *
82 * @return array
83 * Array(string $value => string $label).
84 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
85 * @throws CRM_Core_Exception
86 */
87 public function getValueLabels() {
be2fb01f 88 return CRM_Contribute_BAO_Contribution::buildOptions('contribution_page_id', 'get', []);
2045389a
TO
89 }
90
91 /**
92 * Get a list of status options.
93 *
94 * @param string|int $value
95 * The list of status options may be contingent upon the selected filter value.
96 * This is the selected filter value.
97 * @return array
98 * Array(string $value => string $label).
99 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
100 * @throws CRM_Core_Exception
101 */
102 public function getStatusLabels($value) {
be2fb01f 103 return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', []);
2045389a
TO
104 }
105
106 /**
107 * Get a list of available date fields.
108 *
109 * @return array
110 * Array(string $fieldName => string $fieldLabel).
111 */
112 public function getDateFields() {
be2fb01f 113 return [
2045389a
TO
114 'receive_date' => ts('Receive Date'),
115 'cancel_date' => ts('Cancel Date'),
116 'receipt_date' => ts('Receipt Date'),
117 'thankyou_date' => ts('Thank You Date'),
be2fb01f 118 ];
2045389a
TO
119 }
120
121 /**
0effed37
TO
122 * Get a list of recipient types.
123 *
124 * Note: A single schedule may filter on *zero* or *one* recipient types.
125 * When an admin chooses a value, it's stored in $schedule->recipient.
2045389a 126 *
2045389a 127 * @return array
0effed37
TO
128 * array(string $value => string $label).
129 * Ex: array('assignee' => 'Activity Assignee').
2045389a 130 */
0effed37 131 public function getRecipientTypes() {
be2fb01f 132 return [];
2045389a
TO
133 }
134
135 /**
0effed37 136 * Get a list of recipients which match the given type.
2045389a 137 *
0effed37
TO
138 * Note: A single schedule may filter on *multiple* recipients.
139 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
140 *
141 * @param string $recipientType
142 * Ex: 'participant_role'.
2045389a 143 * @return array
0effed37
TO
144 * Array(mixed $name => string $label).
145 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
146 * @see getRecipientTypes
2045389a 147 */
0effed37 148 public function getRecipientListing($recipientType) {
be2fb01f 149 return [];
2045389a
TO
150 }
151
7f0141d8
TO
152 /**
153 * Determine whether a schedule based on this mapping is sufficiently
154 * complete.
155 *
156 * @param \CRM_Core_DAO_ActionSchedule $schedule
157 * @return array
158 * Array (string $code => string $message).
159 * List of error messages.
160 */
161 public function validateSchedule($schedule) {
be2fb01f 162 return [];
7f0141d8
TO
163 }
164
2045389a
TO
165 /**
166 * Generate a query to locate contacts who match the given
167 * schedule.
168 *
169 * @param \CRM_Core_DAO_ActionSchedule $schedule
170 * @param string $phase
171 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
172 * @param array $defaultParams
173 * Default parameters that should be included with query.
174 * @return \CRM_Utils_SQL_Select
175 * @see RecipientBuilder
176 * @throws CRM_Core_Exception
177 */
178 public function createQuery($schedule, $phase, $defaultParams) {
179 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
180 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
181
fe7f4414 182 $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);
2045389a
TO
183 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
184 $query['casContactIdField'] = 'e.contact_id';
185 $query['casEntityIdField'] = 'e.id';
186 $query['casContactTableAlias'] = NULL;
187
188 // $schedule->start_action_date is user-supplied data. validate.
189 if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
190 throw new CRM_Core_Exception("Invalid date field");
191 }
192 $query['casDateField'] = $schedule->start_action_date;
193
194 // build where clause
195 if (!empty($selectedValues)) {
196 $query->where("e.contribution_page_id IN (@selectedValues)")
197 ->param('selectedValues', $selectedValues);
198 }
199 if (!empty($selectedStatuses)) {
200 $query->where("e.contribution_status_id IN (#selectedStatuses)")
201 ->param('selectedStatuses', $selectedStatuses);
202 }
203
204 return $query;
205 }
206
e08fae02
PH
207 /**
208 * Determine whether a schedule based on this mapping should
209 * reset the reminder state if the trigger date changes.
210 *
211 * @return bool
212 *
213 * @param \CRM_Core_DAO_ActionSchedule $schedule
214 */
215 public function resetOnTriggerDateChange($schedule) {
216 return FALSE;
217 }
218
3741f351
JG
219 /**
220 * Determine whether a schedule based on this mapping should
221 * send to additional contacts.
222 */
223 public function sendToAdditional($entityId): bool {
224 return TRUE;
225 }
226
2045389a 227}