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