INFRA-132 - Docblock @param and @return tag fixes
[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 +--------------------------------------------------------------------+
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 */
41class 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 */
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)) {
70 $this->_mappingID =
71 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'mapping_id');
6a488035 72 if ($this->_context == 'event') {
4d3e4dbe 73 $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this);
6a488035
TO
74 }
75 }
4d3e4dbe 76 elseif (!empty($this->_context)) {
77 if ($this->_context == 'event') {
78 $this->_compId = CRM_Utils_Request::retrieve('compId', 'Integer', $this);
79 $field = 'civicrm_event';
80 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_compId, 'is_template');
81 if ($isTemplate) {
82 $field = 'event_template';
83 }
84 $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
85 if (!$this->_mappingID) {
86 CRM_Core_Error::fatal('Could not find mapping for event scheduled reminders.');
87 }
88 }
264ebe5d 89 }
90
02fc859b
TO
91 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context)) {
92 $mappingID = $_POST['entity'][0];
93 }
94 elseif ($this->_mappingID) {
95 $mappingID = $this->_mappingID;
96 }
4d3e4dbe 97
6a488035
TO
98 $this->add(
99 'text',
100 'title',
101 ts('Title'),
102 array('size' => 45, 'maxlength' => 128),
103 TRUE
104 );
105
106 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($mappingID);
107 extract($selectionOptions);
6a488035
TO
108 $this->assign('entityMapping', json_encode($entityMapping));
109 $this->assign('recipientMapping', json_encode($recipientMapping));
110
4d3e4dbe 111 if (empty($this->_context)) {
112 if (empty($sel1)) {
113 CRM_Core_Error::fatal('Could not find mapping for scheduled reminders.');
114 }
6a488035 115
353ffa53 116 $sel = &$this->add(
4d3e4dbe 117 'hierselect',
353ffa53
TO
118 'entity',
119 ts('Entity'),
120 array(
121 'name' => 'entity[0]',
122 'style' => 'vertical-align: top;',
123 ),
4d3e4dbe 124 TRUE
125 );
126 $sel->setOptions(array($sel1, $sel2, $sel3));
6a488035 127
4d3e4dbe 128 if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) {
129 // make second selector a multi-select -
130 $sel->_elements[1]->setMultiple(TRUE);
131 $sel->_elements[1]->setSize(5);
132 }
133
134 if (is_a($sel->_elements[2], 'HTML_QuickForm_select')) {
135 // make third selector a multi-select -
136 $sel->_elements[2]->setMultiple(TRUE);
137 $sel->_elements[2]->setSize(5);
138 }
139 }
140 else {
141 $options = $sel3[$this->_mappingID][0];
142 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
143 unset($options[0]);
144 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
145 $this->assign('context', $this->_context);
6a488035
TO
146 }
147
148 //get the frequency units.
9775f926 149 $this->_freqUnits = CRM_Core_SelectValues::getScheduleReminderFrequencyUnits();
6a488035
TO
150
151 //pass the mapping ID in UPDATE mode
152 $mappings = CRM_Core_BAO_ActionSchedule::getMapping($mappingID);
153
154 $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30);
155
156 //reminder_interval
157 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
a3e3eea1 158 $isActive = ts('Send email');
159 $recordActivity = ts('Record activity for automated email');
160 if ($providersCount) {
7b007e61 161 $this->assign('sms', $providersCount);
a3e3eea1 162 $isActive = ts('Send email or SMS');
163 $recordActivity = ts('Record activity for automated email or SMS');
164 $options = CRM_Core_OptionGroup::values('msg_mode');
165 $this->add('select', 'mode', ts('Send as'), $options);
166
167 $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc');
168
169 $providerSelect = array();
170 foreach ($providers as $provider) {
171 $providerSelect[$provider['id']] = $provider['title'];
172 }
1e035d58 173 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
a3e3eea1 174 }
6a488035
TO
175
176 foreach ($this->_freqUnits as $val => $label) {
177 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
178 }
179
180 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
181
182 //reminder_frequency
183 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
184
02fc859b 185 $condition = array(
353ffa53 186 'before' => ts('before'),
6a488035
TO
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 263 }
353ffa53 264
6a488035 265 /**
100fef9d 266 * Global form rule
6a488035 267 *
5173bd95
TO
268 * @param array $fields
269 * The input form values.
6a488035 270 *
72b3a70c
CW
271 * @return bool|array
272 * true if no errors, else array of errors
6a488035
TO
273 * @static
274 */
00be9182 275 public static function formRule($fields, $files, $self) {
6a488035 276 $errors = array();
8d657dde 277 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
6a488035
TO
278 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
279 ) {
280 $errors['entity'] = ts('Please select appropriate value');
281 }
282
8d657dde
AH
283 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
284 if (count($fields['entity'][1]) > 1) {
285 $errors['entity'] = ts('You may only select one contact field per reminder');
286 }
287 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
288 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
289 }
290 }
291
1b7a39f5 292 if (!empty($fields['is_active']) && $fields['mode'] != 'SMS' &&
6a488035
TO
293 CRM_Utils_System::isNull($fields['subject'])
294 ) {
295 $errors['subject'] = ts('Subject is a required field.');
296 }
297
4d3e4dbe 298 if (empty($self->_context) && CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) {
6a488035
TO
299 $errors['entity'] = ts('Please select entity value');
300 }
301
302 if (!CRM_Utils_System::isNull($fields['absolute_date'])) {
2ea4f315 303 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('Ymd'))) {
6a488035
TO
304 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
305 }
306 }
307
84a3e359 308 $recipientKind = array(
309 'participant_role' => array(
310 'name' => 'participant role',
21dfd5f5 311 'target_id' => 'recipient_listing',
84a3e359 312 ),
313 'manual' => array(
314 'name' => 'recipient',
21dfd5f5
TO
315 'target_id' => 'recipient_manual_id',
316 ),
84a3e359 317 );
49d61c3a 318 if (!empty($fields['limit_to']) && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
319 $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 320 }
321
6a488035
TO
322 if (!empty($errors)) {
323 return $errors;
324 }
325
326 return empty($errors) ? TRUE : $errors;
327 }
328
e0ef6999
EM
329 /**
330 * @return int
331 */
00be9182 332 public function setDefaultValues() {
6a488035
TO
333 if ($this->_action & CRM_Core_Action::ADD) {
334 $defaults['is_active'] = 1;
a3e3eea1 335 $defaults['mode'] = 'Email';
6a488035
TO
336 $defaults['record_activity'] = 1;
337 }
338 else {
339 $defaults = $this->_values;
4d3e4dbe 340 $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_value', $defaults));
341 $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_status', $defaults));
342 if (empty($this->_context)) {
343 $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults);
344 $defaults['entity'][1] = $entityValue;
345 $defaults['entity'][2] = $entityStatus;
346 }
347 else {
348 $defaults['entity'] = $entityStatus;
349 }
6a488035
TO
350 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $defaults)) {
351 list($date, $time) = CRM_Utils_Date::setDateDefaults($absoluteDate);
352 $defaults['absolute_date'] = $date;
353 }
354
355 if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) {
356 $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
357 $recipientListing
358 );
359 }
360 $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults);
361 $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults);
1e035d58 362 $defaults['sms_text_message'] = CRM_Utils_Array::value('sms_body_text', $defaults);
6a488035 363 $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults);
1e035d58 364 $defaults['SMStemplate'] = CRM_Utils_Array::value('sms_template_id', $defaults);
a7488080 365 if (!empty($defaults['group_id'])) {
6a488035
TO
366 $defaults['recipient'] = 'group';
367 }
a7488080 368 elseif (!empty($defaults['recipient_manual'])) {
6a488035 369 $defaults['recipient'] = 'manual';
92f4a0b1 370 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
6a488035
TO
371 }
372 }
373
374 return $defaults;
375 }
376
377 /**
c490a46a 378 * Process the form submission
6a488035 379 *
6a488035 380 *
355ba699 381 * @return void
6a488035
TO
382 */
383 public function postProcess() {
384 if ($this->_action & CRM_Core_Action::DELETE) {
385 // delete reminder
386 CRM_Core_BAO_ActionSchedule::del($this->_id);
387 CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
4d3e4dbe 388 if ($this->_context == 'event' && $this->_compId) {
6a488035 389 $url = CRM_Utils_System::url('civicrm/event/manage/reminder',
4d3e4dbe 390 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
6a488035
TO
391 );
392 $session = CRM_Core_Session::singleton();
393 $session->pushUserContext($url);
394 }
395 return;
396 }
397 $values = $this->controller->exportValues($this->getName());
398
399 $keys = array(
400 'title',
401 'subject',
402 'absolute_date',
403 'group_id',
3e315abc 404 'record_activity',
a3e3eea1 405 'limit_to',
406 'mode',
1991bba1
DG
407 'sms_provider_id',
408 'from_name',
409 'from_email',
6a488035
TO
410 );
411 foreach ($keys as $key) {
412 $params[$key] = CRM_Utils_Array::value($key, $values);
413 }
414
2ef60934 415 $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0);
416
6a488035 417 $moreKeys = array(
3e315abc 418 'start_action_offset',
419 'start_action_unit',
420 'start_action_condition',
421 'start_action_date',
6a488035
TO
422 'repetition_frequency_unit',
423 'repetition_frequency_interval',
424 'end_frequency_unit',
425 'end_frequency_interval',
3e315abc 426 'end_action',
427 'end_date',
6a488035
TO
428 );
429
430 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) {
431 $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate);
2ef60934 432 $params['is_repeat'] = 0;
6a488035
TO
433 foreach ($moreKeys as $mkey) {
434 $params[$mkey] = 'null';
435 }
436 }
437 else {
438 $params['absolute_date'] = 'null';
439 foreach ($moreKeys as $mkey) {
440 $params[$mkey] = CRM_Utils_Array::value($mkey, $values);
441 }
442 }
443
444 $params['body_text'] = CRM_Utils_Array::value('text_message', $values);
1e035d58 445 $params['sms_body_text'] = CRM_Utils_Array::value('sms_text_message', $values);
6a488035
TO
446 $params['body_html'] = CRM_Utils_Array::value('html_message', $values);
447
448 if (CRM_Utils_Array::value('recipient', $values) == 'manual') {
449 $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values);
450 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
451 }
452 elseif (CRM_Utils_Array::value('recipient', $values) == 'group') {
453 $params['group_id'] = $values['group_id'];
454 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
455 }
26a77d0a 456 elseif (!CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) {
6a488035
TO
457 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
458 $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
459 CRM_Utils_Array::value('recipient_listing', $values)
460 );
461 $params['group_id'] = $params['recipient_manual'] = 'null';
462 }
463 else {
464 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
465 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
466 }
467
4d3e4dbe 468 if (!empty($this->_mappingID) && !empty($this->_compId)) {
353ffa53 469 $params['mapping_id'] = $this->_mappingID;
4d3e4dbe 470 $params['entity_value'] = $this->_compId;
471 $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']);
84a3e359 472 }
4d3e4dbe 473 else {
474 $params['mapping_id'] = $values['entity'][0];
475 $entity_value = $values['entity'][1];
476 $entity_status = $values['entity'][2];
673cdfb4 477 if ($params['mapping_id'] == 1) {
4d3e4dbe 478 $params['limit_to'] = 1;
479 }
84a3e359 480
4d3e4dbe 481 foreach (array('entity_value', 'entity_status') as $key) {
482 $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key);
483 }
6a488035
TO
484 }
485
486 $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);
6a488035
TO
487
488 if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
489 $params['repetition_frequency_unit'] = 'null';
490 $params['repetition_frequency_interval'] = 'null';
491 $params['end_frequency_unit'] = 'null';
492 $params['end_frequency_interval'] = 'null';
493 $params['end_action'] = 'null';
494 $params['end_date'] = 'null';
495 }
496
497 if ($this->_action & CRM_Core_Action::UPDATE) {
498 $params['id'] = $this->_id;
499 }
500 elseif ($this->_action & CRM_Core_Action::ADD) {
501 // we do this only once, so name never changes
502 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
503 }
504
1e035d58 505 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
506
507 if ($params['mode'] == 'Email' || empty($params['sms_provider_id'])) {
508 unset($modePrefixes['SMS']);
509 }
510 elseif ($params['mode'] == 'SMS') {
511 unset($modePrefixes['Mail']);
512 }
513
514 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
515
6a488035 516 $composeFields = array(
353ffa53
TO
517 'template',
518 'saveTemplate',
519 'updateTemplate',
520 'saveTemplateName',
6a488035
TO
521 );
522 $msgTemplate = NULL;
523 //mail template is composed
524
1e035d58 525 foreach ($modePrefixes as $prefix) {
526 $composeParams = array();
527 foreach ($composeFields as $key) {
528 $key = $prefix . $key;
529 if (!empty($values[$key])) {
530 $composeParams[$key] = $values[$key];
531 }
6a488035 532 }
6a488035 533
1e035d58 534 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
535 $templateParams = array('is_active' => TRUE);
536 if ($prefix == 'SMS') {
537 $templateParams += array(
538 'msg_text' => $params['sms_body_text'],
539 'is_sms' => TRUE,
02fc859b 540 );
1e035d58 541 }
542 else {
543 $templateParams += array(
544 'msg_text' => $params['body_text'],
545 'msg_html' => $params['body_html'],
546 'msg_subject' => $params['subject'],
547 );
548 }
549 $templateParams['id'] = $values[$prefix . 'template'];
550
551 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
552 }
6a488035 553
1e035d58 554 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
555 $templateParams = array('is_active' => TRUE);
556 if ($prefix == 'SMS') {
557 $templateParams += array(
558 'msg_text' => $params['sms_body_text'],
559 'is_sms' => TRUE,
560 );
561 }
562 else {
563 $templateParams += array(
564 'msg_text' => $params['body_text'],
565 'msg_html' => $params['body_html'],
566 'msg_subject' => $params['subject'],
567 );
568 }
569 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
570
571 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
572 }
6a488035 573
1e035d58 574 if ($prefix == 'SMS') {
575 if (isset($msgTemplate->id)) {
576 $params['sms_template_id'] = $msgTemplate->id;
577 }
578 else {
579 $params['sms_template_id'] = CRM_Utils_Array::value('SMStemplate', $values);
580 }
581 }
582 else {
583 if (isset($msgTemplate->id)) {
584 $params['msg_template_id'] = $msgTemplate->id;
585 }
586 else {
587 $params['msg_template_id'] = CRM_Utils_Array::value('template', $values);
588 }
589 }
6a488035
TO
590 }
591
24f14405
E
592 $bao = CRM_Core_BAO_ActionSchedule::add($params);
593 // we need to set this on the form so that hooks can identify the created entity
594 $this->set('id', $bao->id);
595 $bao->free();
6a488035
TO
596
597 $status = ts("Your new Reminder titled %1 has been saved.",
598 array(1 => "<strong>{$values['title']}</strong>")
599 );
6a488035 600
4d3e4dbe 601 if ($this->_action) {
602 if ($this->_action & CRM_Core_Action::UPDATE) {
603 $status = ts("Your Reminder titled %1 has been updated.",
353ffa53 604 array(1 => "<strong>{$values['title']}</strong>")
6a488035 605 );
4d3e4dbe 606 }
607
608 if ($this->_context == 'event' && $this->_compId) {
609 $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
6a488035
TO
610 $session = CRM_Core_Session::singleton();
611 $session->pushUserContext($url);
612 }
613 }
614 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
615 }
616}