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