Commit | Line | Data |
---|---|---|
b5302d4e TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
b5302d4e | 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 | | |
b5302d4e TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
b5302d4e TO |
12 | |
13 | /** | |
14 | * Class CRM_Contribute_ActionMapping_ByType | |
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 type of contribution. | |
20 | */ | |
21 | class CRM_Contribute_ActionMapping_ByType 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 = 'contribtype'; | |
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 Type'); | |
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('Financial Type'); | |
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('financial_type_id', 'get', []); |
b5302d4e 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', []); |
b5302d4e 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 [ |
b5302d4e 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 | ]; |
b5302d4e 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. | |
b5302d4e | 126 | * |
b5302d4e | 127 | * @return array |
0effed37 TO |
128 | * array(string $value => string $label). |
129 | * Ex: array('assignee' => 'Activity Assignee'). | |
b5302d4e | 130 | */ |
0effed37 | 131 | public function getRecipientTypes() { |
be2fb01f | 132 | return [ |
0effed37 | 133 | 'soft_credit_type' => ts('Soft Credit Role'), |
be2fb01f | 134 | ]; |
b5302d4e TO |
135 | } |
136 | ||
137 | /** | |
0effed37 TO |
138 | * Get a list of recipients which match the given type. |
139 | * | |
140 | * Note: A single schedule may filter on *multiple* recipients. | |
141 | * When an admin chooses value(s), it's stored in $schedule->recipient_listing. | |
b5302d4e | 142 | * |
0effed37 TO |
143 | * @param string $recipientType |
144 | * Ex: 'participant_role'. | |
b5302d4e | 145 | * @return array |
0effed37 TO |
146 | * Array(mixed $name => string $label). |
147 | * Ex: array(1 => 'Attendee', 2 => 'Volunteer'). | |
148 | * @see getRecipientTypes | |
b5302d4e | 149 | */ |
0effed37 TO |
150 | public function getRecipientListing($recipientType) { |
151 | switch ($recipientType) { | |
152 | case 'soft_credit_type': | |
153 | return \CRM_Core_OptionGroup::values('soft_credit_type', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); | |
154 | ||
155 | default: | |
be2fb01f | 156 | return []; |
0effed37 | 157 | } |
b5302d4e TO |
158 | } |
159 | ||
7f0141d8 TO |
160 | /** |
161 | * Determine whether a schedule based on this mapping is sufficiently | |
162 | * complete. | |
163 | * | |
164 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
165 | * @return array | |
166 | * Array (string $code => string $message). | |
167 | * List of error messages. | |
168 | */ | |
169 | public function validateSchedule($schedule) { | |
be2fb01f | 170 | return []; |
7f0141d8 TO |
171 | } |
172 | ||
b5302d4e TO |
173 | /** |
174 | * Generate a query to locate contacts who match the given | |
175 | * schedule. | |
176 | * | |
177 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
178 | * @param string $phase | |
179 | * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. | |
180 | * @param array $defaultParams | |
181 | * Default parameters that should be included with query. | |
182 | * @return \CRM_Utils_SQL_Select | |
183 | * @see RecipientBuilder | |
184 | * @throws CRM_Core_Exception | |
185 | */ | |
186 | public function createQuery($schedule, $phase, $defaultParams) { | |
187 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); | |
188 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
189 | ||
fe7f4414 | 190 | $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams); |
b5302d4e TO |
191 | $query['casAddlCheckFrom'] = 'civicrm_contribution e'; |
192 | $query['casContactIdField'] = 'e.contact_id'; | |
193 | $query['casEntityIdField'] = 'e.id'; | |
194 | $query['casContactTableAlias'] = NULL; | |
195 | ||
196 | // $schedule->start_action_date is user-supplied data. validate. | |
197 | if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) { | |
198 | throw new CRM_Core_Exception("Invalid date field"); | |
199 | } | |
200 | $query['casDateField'] = $schedule->start_action_date; | |
201 | ||
202 | // build where clause | |
203 | if (!empty($selectedValues)) { | |
204 | $query->where("e.financial_type_id IN (@selectedValues)") | |
205 | ->param('selectedValues', $selectedValues); | |
206 | } | |
207 | if (!empty($selectedStatuses)) { | |
208 | $query->where("e.contribution_status_id IN (#selectedStatuses)") | |
209 | ->param('selectedStatuses', $selectedStatuses); | |
210 | } | |
211 | ||
0effed37 TO |
212 | if ($schedule->recipient_listing && $schedule->limit_to) { |
213 | switch ($schedule->recipient) { | |
214 | case 'soft_credit_type': | |
14d24938 | 215 | $query['casContactIdField'] = 'soft.contact_id'; |
0effed37 TO |
216 | $query->join('soft', 'INNER JOIN civicrm_contribution_soft soft ON soft.contribution_id = e.id') |
217 | ->where("soft.soft_credit_type_id IN (#recipList)") | |
218 | ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing)); | |
219 | break; | |
220 | } | |
221 | } | |
222 | ||
b5302d4e TO |
223 | return $query; |
224 | } | |
225 | ||
e08fae02 PH |
226 | /** |
227 | * Determine whether a schedule based on this mapping should | |
228 | * reset the reminder state if the trigger date changes. | |
229 | * | |
230 | * @return bool | |
231 | * | |
232 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
233 | */ | |
234 | public function resetOnTriggerDateChange($schedule) { | |
235 | return FALSE; | |
236 | } | |
237 | ||
3741f351 JG |
238 | /** |
239 | * Determine whether a schedule based on this mapping should | |
240 | * send to additional contacts. | |
241 | */ | |
242 | public function sendToAdditional($entityId): bool { | |
243 | return TRUE; | |
244 | } | |
245 | ||
b5302d4e | 246 | } |