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