Commit | Line | Data |
---|---|---|
7f254ad8 AE |
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 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2015 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Scheduling Reminders | |
38 | * | |
39 | */ | |
40 | class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { | |
41 | ||
42 | /** | |
43 | * Scheduled Reminder ID. | |
44 | */ | |
45 | protected $_id = NULL; | |
46 | ||
47 | public $_freqUnits; | |
48 | ||
49 | /** | |
50 | * Build the form object. | |
51 | * | |
52 | * @return void | |
53 | */ | |
54 | public function buildQuickForm() { | |
55 | parent::buildQuickForm(); | |
56 | $this->_mappingID = $mappingID = NULL; | |
57 | $providersCount = CRM_SMS_BAO_Provider::activeProviderCount(); | |
58 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
59 | ||
60 | //CRM-16777: Don't provide access to administer schedule reminder page, with user that does not have 'administer CiviCRM' permission | |
61 | if (empty($this->_context) && !CRM_Core_Permission::check('administer CiviCRM')) { | |
62 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | |
63 | } | |
64 | //CRM-16777: When user have ACLs 'edit' permission for specific event, do not give access to add, delete & updtae | |
65 | //schedule reminder for other events. | |
66 | else { | |
67 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this); | |
68 | if (!CRM_Event_BAO_Event::checkPermission($this->_compId, CRM_Core_Permission::EDIT)) { | |
69 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); | |
70 | } | |
71 | } | |
72 | ||
73 | if ($this->_action & (CRM_Core_Action::DELETE)) { | |
74 | $reminderName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'title'); | |
75 | if ($this->_context == 'event') { | |
76 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this); | |
77 | } | |
78 | $this->assign('reminderName', $reminderName); | |
79 | return; | |
80 | } | |
81 | elseif ($this->_action & (CRM_Core_Action::UPDATE)) { | |
82 | $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'mapping_id'); | |
83 | if ($this->_context == 'event') { | |
84 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this); | |
85 | } | |
86 | } | |
87 | elseif (!empty($this->_context)) { | |
88 | if ($this->_context == 'event') { | |
89 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this); | |
90 | $field = 'civicrm_event'; | |
91 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_compId, 'is_template'); | |
92 | if ($isTemplate) { | |
93 | $field = 'event_template'; | |
94 | } | |
95 | $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); | |
96 | if (!$this->_mappingID) { | |
97 | CRM_Core_Error::fatal('Could not find mapping for event scheduled reminders.'); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context)) { | |
103 | $mappingID = $_POST['entity'][0]; | |
104 | } | |
105 | elseif ($this->_mappingID) { | |
106 | $mappingID = $this->_mappingID; | |
107 | } | |
108 | ||
109 | $this->add( | |
110 | 'text', | |
111 | 'title', | |
112 | ts('Title'), | |
113 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'title'), | |
114 | TRUE | |
115 | ); | |
116 | ||
117 | $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($mappingID); | |
118 | extract($selectionOptions); | |
119 | $this->assign('entityMapping', json_encode($entityMapping)); | |
120 | $this->assign('recipientMapping', json_encode($recipientMapping)); | |
121 | ||
122 | if (empty($this->_context)) { | |
123 | if (empty($sel1)) { | |
124 | CRM_Core_Error::fatal('Could not find mapping for scheduled reminders.'); | |
125 | } | |
126 | ||
127 | $sel = &$this->add( | |
128 | 'hierselect', | |
129 | 'entity', | |
130 | ts('Entity'), | |
131 | array( | |
132 | 'name' => 'entity[0]', | |
133 | 'style' => 'vertical-align: top;', | |
134 | ) | |
135 | ); | |
136 | $sel->setOptions(array($sel1, $sel2, $sel3)); | |
137 | ||
138 | if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) { | |
139 | // make second selector a multi-select - | |
140 | $sel->_elements[1]->setMultiple(TRUE); | |
141 | $sel->_elements[1]->setSize(5); | |
142 | } | |
143 | ||
144 | if (is_a($sel->_elements[2], 'HTML_QuickForm_select')) { | |
145 | // make third selector a multi-select - | |
146 | $sel->_elements[2]->setMultiple(TRUE); | |
147 | $sel->_elements[2]->setSize(5); | |
148 | } | |
149 | } | |
150 | else { | |
151 | $options = $sel3[$this->_mappingID][0]; | |
152 | $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]); | |
153 | unset($options[0]); | |
154 | $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes); | |
155 | $this->assign('context', $this->_context); | |
156 | } | |
157 | ||
158 | //get the frequency units. | |
159 | $this->_freqUnits = CRM_Core_SelectValues::getRecurringFrequencyUnits(); | |
160 | ||
161 | //pass the mapping ID in UPDATE mode | |
162 | $mappings = CRM_Core_BAO_ActionSchedule::getMapping($mappingID); | |
163 | ||
164 | $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30); | |
165 | ||
166 | //reminder_interval | |
167 | $this->add('select', 'start_action_offset', ts('When'), $numericOptions); | |
168 | $isActive = ts('Send email'); | |
169 | $recordActivity = ts('Record activity for automated email'); | |
170 | if ($providersCount) { | |
171 | $this->assign('sms', $providersCount); | |
172 | $isActive = ts('Send email or SMS'); | |
173 | $recordActivity = ts('Record activity for automated email or SMS'); | |
174 | $options = CRM_Core_OptionGroup::values('msg_mode'); | |
175 | $this->add('select', 'mode', ts('Send as'), $options); | |
176 | ||
177 | $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc'); | |
178 | ||
179 | $providerSelect = array(); | |
180 | foreach ($providers as $provider) { | |
181 | $providerSelect[$provider['id']] = $provider['title']; | |
182 | } | |
183 | $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE); | |
184 | } | |
185 | ||
186 | foreach ($this->_freqUnits as $val => $label) { | |
187 | $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label)); | |
188 | } | |
189 | ||
190 | $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing')); | |
191 | ||
192 | //reminder_frequency | |
193 | $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE); | |
194 | ||
195 | $condition = array( | |
196 | 'before' => ts('before'), | |
197 | 'after' => ts('after'), | |
198 | ); | |
199 | //reminder_action | |
200 | $this->add('select', 'start_action_condition', ts('Action Condition'), $condition); | |
201 | ||
202 | $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE); | |
203 | ||
204 | $this->addElement('checkbox', 'record_activity', $recordActivity); | |
205 | ||
206 | $this->addElement('checkbox', 'is_repeat', ts('Repeat'), | |
207 | NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);") | |
208 | ); | |
209 | ||
210 | $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay); | |
211 | $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions); | |
212 | $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay); | |
213 | $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions); | |
214 | $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE); | |
215 | $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE); | |
216 | ||
217 | $this->add('text', 'from_name', ts('From Name')); | |
218 | $this->add('text', 'from_email', ts('From Email')); | |
219 | ||
220 | $recipient = 'activity_contacts'; | |
221 | $recipientListingOptions = array(); | |
222 | ||
223 | if ($mappingID) { | |
224 | $recipient = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', | |
225 | $mappingID, | |
226 | 'entity_recipient' | |
227 | ); | |
228 | } | |
229 | ||
230 | $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include')); | |
231 | ||
232 | $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients')); | |
233 | $this->assign('recipientLabels', $recipientLabels); | |
234 | ||
235 | $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);")); | |
236 | ||
237 | $this->add('select', 'recipient', $recipientLabels['other'], $sel5[$recipient], | |
238 | FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);") | |
239 | ); | |
240 | ||
241 | if (!empty($this->_submitValues['recipient_listing'])) { | |
242 | if (!empty($this->_context)) { | |
243 | $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_mappingID, $this->_submitValues['recipient']); | |
244 | } | |
245 | else { | |
246 | $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($_POST['entity'][0], $_POST['recipient']); | |
247 | } | |
248 | } | |
249 | elseif (!empty($this->_values['recipient_listing'])) { | |
250 | $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_values['mapping_id'], $this->_values['recipient']); | |
251 | } | |
252 | ||
253 | $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE, | |
254 | array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE)); | |
255 | ||
256 | $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE)); | |
257 | ||
258 | $this->add('select', 'group_id', ts('Group'), | |
259 | CRM_Core_PseudoConstant::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge') | |
260 | ); | |
261 | ||
262 | CRM_Mailing_BAO_Mailing::commonCompose($this); | |
263 | ||
264 | $this->add('text', 'subject', ts('Subject'), | |
265 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject') | |
266 | ); | |
267 | ||
268 | $this->add('checkbox', 'is_active', $isActive); | |
269 | ||
270 | $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this); | |
271 | ||
272 | $this->setPageTitle(ts('Scheduled Reminder')); | |
273 | } | |
274 | ||
275 | /** | |
276 | * Global form rule. | |
277 | * | |
278 | * @param array $fields | |
279 | * The input form values. | |
280 | * | |
281 | * @return bool|array | |
282 | * true if no errors, else array of errors | |
283 | */ | |
284 | public static function formRule($fields, $files, $self) { | |
285 | $errors = array(); | |
286 | if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) || | |
287 | (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0) | |
288 | ) { | |
289 | $errors['entity'] = ts('Please select appropriate value'); | |
290 | } | |
291 | ||
292 | if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) { | |
293 | if (count($fields['entity'][1]) > 1) { | |
294 | $errors['entity'] = ts('You may only select one contact field per reminder'); | |
295 | } | |
296 | elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) { | |
297 | $errors['entity'] = ts('Please select whether the reminder is sent each year.'); | |
298 | } | |
299 | } | |
300 | ||
301 | if (!empty($fields['is_active']) && | |
302 | CRM_Utils_Array::value('mode', $fields) == 'SMS' && | |
303 | CRM_Utils_System::isNull($fields['subject']) | |
304 | ) { | |
305 | $errors['subject'] = ts('Subject is a required field.'); | |
306 | } | |
307 | ||
308 | if (empty($self->_context) && CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) { | |
309 | $errors['entity'] = ts('Please select entity value'); | |
310 | } | |
311 | ||
312 | if (!CRM_Utils_System::isNull($fields['absolute_date'])) { | |
313 | if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('Ymd'))) { | |
314 | $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.'); | |
315 | } | |
316 | } | |
317 | ||
318 | $recipientKind = array( | |
319 | 'participant_role' => array( | |
320 | 'name' => 'participant role', | |
321 | 'target_id' => 'recipient_listing', | |
322 | ), | |
323 | 'manual' => array( | |
324 | 'name' => 'recipient', | |
325 | 'target_id' => 'recipient_manual_id', | |
326 | ), | |
327 | ); | |
328 | if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) { | |
329 | $errors[$recipientKind[$fields['recipient']]['target_id']] = ts('If "Also include" or "Limit to" are selected, you must specify at least one %1', array(1 => $recipientKind[$fields['recipient']]['name'])); | |
330 | } | |
331 | ||
332 | if (!empty($errors)) { | |
333 | return $errors; | |
334 | } | |
335 | ||
336 | return empty($errors) ? TRUE : $errors; | |
337 | } | |
338 | ||
339 | /** | |
340 | * @return int | |
341 | */ | |
342 | public function setDefaultValues() { | |
343 | if ($this->_action & CRM_Core_Action::ADD) { | |
344 | $defaults['is_active'] = 1; | |
345 | $defaults['mode'] = 'Email'; | |
346 | $defaults['record_activity'] = 1; | |
347 | } | |
348 | else { | |
349 | $defaults = $this->_values; | |
350 | $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_value', $defaults)); | |
351 | $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_status', $defaults)); | |
352 | if (empty($this->_context)) { | |
353 | $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults); | |
354 | $defaults['entity'][1] = $entityValue; | |
355 | $defaults['entity'][2] = $entityStatus; | |
356 | } | |
357 | else { | |
358 | $defaults['entity'] = $entityStatus; | |
359 | } | |
360 | if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $defaults)) { | |
361 | list($date, $time) = CRM_Utils_Date::setDateDefaults($absoluteDate); | |
362 | $defaults['absolute_date'] = $date; | |
363 | } | |
364 | ||
365 | if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) { | |
366 | $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
367 | $recipientListing | |
368 | ); | |
369 | } | |
370 | $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults); | |
371 | $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults); | |
372 | $defaults['sms_text_message'] = CRM_Utils_Array::value('sms_body_text', $defaults); | |
373 | $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults); | |
374 | $defaults['SMStemplate'] = CRM_Utils_Array::value('sms_template_id', $defaults); | |
375 | if (!empty($defaults['group_id'])) { | |
376 | $defaults['recipient'] = 'group'; | |
377 | } | |
378 | elseif (!empty($defaults['recipient_manual'])) { | |
379 | $defaults['recipient'] = 'manual'; | |
380 | $defaults['recipient_manual_id'] = $defaults['recipient_manual']; | |
381 | } | |
382 | } | |
383 | ||
384 | return $defaults; | |
385 | } | |
386 | ||
387 | /** | |
388 | * Process the form submission. | |
389 | * | |
390 | * | |
391 | * @return void | |
392 | */ | |
393 | public function postProcess() { | |
394 | if ($this->_action & CRM_Core_Action::DELETE) { | |
395 | // delete reminder | |
396 | CRM_Core_BAO_ActionSchedule::del($this->_id); | |
397 | CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success'); | |
398 | if ($this->_context == 'event' && $this->_compId) { | |
399 | $url = CRM_Utils_System::url('civicrm/event/manage/reminder', | |
400 | "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1" | |
401 | ); | |
402 | $session = CRM_Core_Session::singleton(); | |
403 | $session->pushUserContext($url); | |
404 | } | |
405 | return; | |
406 | } | |
407 | $values = $this->controller->exportValues($this->getName()); | |
408 | ||
409 | $keys = array( | |
410 | 'title', | |
411 | 'subject', | |
412 | 'absolute_date', | |
413 | 'group_id', | |
414 | 'record_activity', | |
415 | 'limit_to', | |
416 | 'mode', | |
417 | 'sms_provider_id', | |
418 | 'from_name', | |
419 | 'from_email', | |
420 | ); | |
421 | foreach ($keys as $key) { | |
422 | $params[$key] = CRM_Utils_Array::value($key, $values); | |
423 | } | |
424 | ||
425 | $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0); | |
426 | ||
427 | $moreKeys = array( | |
428 | 'start_action_offset', | |
429 | 'start_action_unit', | |
430 | 'start_action_condition', | |
431 | 'start_action_date', | |
432 | 'repetition_frequency_unit', | |
433 | 'repetition_frequency_interval', | |
434 | 'end_frequency_unit', | |
435 | 'end_frequency_interval', | |
436 | 'end_action', | |
437 | 'end_date', | |
438 | ); | |
439 | ||
440 | if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) { | |
441 | $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate); | |
442 | $params['is_repeat'] = 0; | |
443 | foreach ($moreKeys as $mkey) { | |
444 | $params[$mkey] = 'null'; | |
445 | } | |
446 | } | |
447 | else { | |
448 | $params['absolute_date'] = 'null'; | |
449 | foreach ($moreKeys as $mkey) { | |
450 | $params[$mkey] = CRM_Utils_Array::value($mkey, $values); | |
451 | } | |
452 | } | |
453 | ||
454 | $params['body_text'] = CRM_Utils_Array::value('text_message', $values); | |
455 | $params['sms_body_text'] = CRM_Utils_Array::value('sms_text_message', $values); | |
456 | $params['body_html'] = CRM_Utils_Array::value('html_message', $values); | |
457 | ||
458 | if (CRM_Utils_Array::value('recipient', $values) == 'manual') { | |
459 | $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values); | |
460 | $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
461 | } | |
462 | elseif (CRM_Utils_Array::value('recipient', $values) == 'group') { | |
463 | $params['group_id'] = $values['group_id']; | |
464 | $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
465 | } | |
466 | elseif (!CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) { | |
467 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
468 | $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
469 | CRM_Utils_Array::value('recipient_listing', $values) | |
470 | ); | |
471 | $params['group_id'] = $params['recipient_manual'] = 'null'; | |
472 | } | |
473 | else { | |
474 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
475 | $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null'; | |
476 | } | |
477 | ||
478 | if (!empty($this->_mappingID) && !empty($this->_compId)) { | |
479 | $params['mapping_id'] = $this->_mappingID; | |
480 | $params['entity_value'] = $this->_compId; | |
481 | $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']); | |
482 | } | |
483 | else { | |
484 | $params['mapping_id'] = $values['entity'][0]; | |
485 | $entity_value = $values['entity'][1]; | |
486 | $entity_status = $values['entity'][2]; | |
487 | if ($params['mapping_id'] == 1) { | |
488 | $params['limit_to'] = 1; | |
489 | } | |
490 | ||
491 | foreach (array('entity_value', 'entity_status') as $key) { | |
492 | $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key); | |
493 | } | |
494 | } | |
495 | ||
496 | $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0); | |
497 | ||
498 | if (CRM_Utils_Array::value('is_repeat', $values) == 0) { | |
499 | $params['repetition_frequency_unit'] = 'null'; | |
500 | $params['repetition_frequency_interval'] = 'null'; | |
501 | $params['end_frequency_unit'] = 'null'; | |
502 | $params['end_frequency_interval'] = 'null'; | |
503 | $params['end_action'] = 'null'; | |
504 | $params['end_date'] = 'null'; | |
505 | } | |
506 | ||
507 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
508 | $params['id'] = $this->_id; | |
509 | } | |
510 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
511 | // we do this only once, so name never changes | |
512 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); | |
513 | } | |
514 | ||
515 | $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS'); | |
516 | ||
517 | if ($params['mode'] == 'Email' || empty($params['sms_provider_id'])) { | |
518 | unset($modePrefixes['SMS']); | |
519 | } | |
520 | elseif ($params['mode'] == 'SMS') { | |
521 | unset($modePrefixes['Mail']); | |
522 | } | |
523 | ||
524 | //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes | |
525 | ||
526 | $composeFields = array( | |
527 | 'template', | |
528 | 'saveTemplate', | |
529 | 'updateTemplate', | |
530 | 'saveTemplateName', | |
531 | ); | |
532 | $msgTemplate = NULL; | |
533 | //mail template is composed | |
534 | ||
535 | foreach ($modePrefixes as $prefix) { | |
536 | $composeParams = array(); | |
537 | foreach ($composeFields as $key) { | |
538 | $key = $prefix . $key; | |
539 | if (!empty($values[$key])) { | |
540 | $composeParams[$key] = $values[$key]; | |
541 | } | |
542 | } | |
543 | ||
544 | if (!empty($composeParams[$prefix . 'updateTemplate'])) { | |
545 | $templateParams = array('is_active' => TRUE); | |
546 | if ($prefix == 'SMS') { | |
547 | $templateParams += array( | |
548 | 'msg_text' => $params['sms_body_text'], | |
549 | 'is_sms' => TRUE, | |
550 | ); | |
551 | } | |
552 | else { | |
553 | $templateParams += array( | |
554 | 'msg_text' => $params['body_text'], | |
555 | 'msg_html' => $params['body_html'], | |
556 | 'msg_subject' => $params['subject'], | |
557 | ); | |
558 | } | |
559 | $templateParams['id'] = $values[$prefix . 'template']; | |
560 | ||
561 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); | |
562 | } | |
563 | ||
564 | if (!empty($composeParams[$prefix . 'saveTemplate'])) { | |
565 | $templateParams = array('is_active' => TRUE); | |
566 | if ($prefix == 'SMS') { | |
567 | $templateParams += array( | |
568 | 'msg_text' => $params['sms_body_text'], | |
569 | 'is_sms' => TRUE, | |
570 | ); | |
571 | } | |
572 | else { | |
573 | $templateParams += array( | |
574 | 'msg_text' => $params['body_text'], | |
575 | 'msg_html' => $params['body_html'], | |
576 | 'msg_subject' => $params['subject'], | |
577 | ); | |
578 | } | |
579 | $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName']; | |
580 | ||
581 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); | |
582 | } | |
583 | ||
584 | if ($prefix == 'SMS') { | |
585 | if (isset($msgTemplate->id)) { | |
586 | $params['sms_template_id'] = $msgTemplate->id; | |
587 | } | |
588 | else { | |
589 | $params['sms_template_id'] = CRM_Utils_Array::value('SMStemplate', $values); | |
590 | } | |
591 | } | |
592 | else { | |
593 | if (isset($msgTemplate->id)) { | |
594 | $params['msg_template_id'] = $msgTemplate->id; | |
595 | } | |
596 | else { | |
597 | $params['msg_template_id'] = CRM_Utils_Array::value('template', $values); | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | $bao = CRM_Core_BAO_ActionSchedule::add($params); | |
603 | // we need to set this on the form so that hooks can identify the created entity | |
604 | $this->set('id', $bao->id); | |
605 | $bao->free(); | |
606 | ||
607 | $status = ts("Your new Reminder titled %1 has been saved.", | |
608 | array(1 => "<strong>{$values['title']}</strong>") | |
609 | ); | |
610 | ||
611 | if ($this->_action) { | |
612 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
613 | $status = ts("Your Reminder titled %1 has been updated.", | |
614 | array(1 => "<strong>{$values['title']}</strong>") | |
615 | ); | |
616 | } | |
617 | ||
618 | if ($this->_context == 'event' && $this->_compId) { | |
619 | $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"); | |
620 | $session = CRM_Core_Session::singleton(); | |
621 | $session->pushUserContext($url); | |
622 | } | |
623 | } | |
624 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
625 | } | |
626 | ||
627 | } |