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