CRM-16355 - add the contact filter based on language and dynamic language change
[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
SV
257 // multilingual only options
258 $domain = new CRM_Core_DAO_Domain();
259 $domain->find(TRUE);
260 $multilingual = (bool) $domain->locales;
261 if ($multilingual) {
262 $smarty = CRM_Core_Smarty::singleton();
263 $smarty->assign('multilingual', $multilingual);
264
265 $languageFilter = array();
266 $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales);
267 $languages = CRM_Core_I18n::languages();
268 foreach ($locales as $locale) {
269 $languageFilter[$locale] = $languages[$locale];
270 }
07844ccf 271 $languageFilter[CRM_Core_I18n::NONE] = ts('Contacts with no preferred language');
776a4216
SV
272 $element = $this->add('select', 'filter_contact_language', ts('Recipients language'), $languageFilter, FALSE,
273 array('multiple' => TRUE, 'class' => 'crm-select2', 'placeholder' => TRUE));
274
275 $communicationLanguage = array();
276 $communicationLanguage[''] = ts('System default language');
07844ccf 277 $communicationLanguage[CRM_Core_I18n::AUTO] = ts('Follow recipient preferred language');
776a4216
SV
278 foreach ($locales as $locale) {
279 $communicationLanguage[$locale] = $languages[$locale];
280 }
281 $this->add('select', 'communication_language', ts('Communication language'), $communicationLanguage);
282 }
283
6a488035
TO
284 CRM_Mailing_BAO_Mailing::commonCompose($this);
285
286 $this->add('text', 'subject', ts('Subject'),
287 CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
288 );
289
a3e3eea1 290 $this->add('checkbox', 'is_active', $isActive);
6a488035 291
4d3e4dbe 292 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
581c7be2
CW
293
294 $this->setPageTitle(ts('Scheduled Reminder'));
6a488035 295 }
353ffa53 296
6a488035 297 /**
eceb18cc 298 * Global form rule.
6a488035 299 *
5173bd95
TO
300 * @param array $fields
301 * The input form values.
6a488035 302 *
72b3a70c
CW
303 * @return bool|array
304 * true if no errors, else array of errors
6a488035 305 */
00be9182 306 public static function formRule($fields, $files, $self) {
6a488035 307 $errors = array();
8d657dde 308 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
6a488035
TO
309 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
310 ) {
311 $errors['entity'] = ts('Please select appropriate value');
312 }
313
8d657dde
AH
314 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
315 if (count($fields['entity'][1]) > 1) {
316 $errors['entity'] = ts('You may only select one contact field per reminder');
317 }
318 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
319 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
320 }
321 }
322
fb90d6bb 323 if (!empty($fields['is_active']) &&
324 CRM_Utils_Array::value('mode', $fields) == 'SMS' &&
6a488035
TO
325 CRM_Utils_System::isNull($fields['subject'])
326 ) {
327 $errors['subject'] = ts('Subject is a required field.');
328 }
329
4d3e4dbe 330 if (empty($self->_context) && CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) {
6a488035
TO
331 $errors['entity'] = ts('Please select entity value');
332 }
333
334 if (!CRM_Utils_System::isNull($fields['absolute_date'])) {
2ea4f315 335 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('Ymd'))) {
6a488035
TO
336 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
337 }
338 }
339
84a3e359 340 $recipientKind = array(
341 'participant_role' => array(
342 'name' => 'participant role',
21dfd5f5 343 'target_id' => 'recipient_listing',
84a3e359 344 ),
345 'manual' => array(
346 'name' => 'recipient',
21dfd5f5
TO
347 'target_id' => 'recipient_manual_id',
348 ),
84a3e359 349 );
fb90d6bb 350 if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
49d61c3a 351 $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 352 }
353
6a488035
TO
354 if (!empty($errors)) {
355 return $errors;
356 }
357
358 return empty($errors) ? TRUE : $errors;
359 }
360
e0ef6999
EM
361 /**
362 * @return int
363 */
00be9182 364 public function setDefaultValues() {
6a488035
TO
365 if ($this->_action & CRM_Core_Action::ADD) {
366 $defaults['is_active'] = 1;
a3e3eea1 367 $defaults['mode'] = 'Email';
6a488035
TO
368 $defaults['record_activity'] = 1;
369 }
370 else {
371 $defaults = $this->_values;
4d3e4dbe 372 $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_value', $defaults));
373 $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_status', $defaults));
374 if (empty($this->_context)) {
375 $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults);
376 $defaults['entity'][1] = $entityValue;
377 $defaults['entity'][2] = $entityStatus;
378 }
379 else {
380 $defaults['entity'] = $entityStatus;
381 }
6a488035
TO
382 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $defaults)) {
383 list($date, $time) = CRM_Utils_Date::setDateDefaults($absoluteDate);
384 $defaults['absolute_date'] = $date;
385 }
386
387 if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) {
388 $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
389 $recipientListing
390 );
391 }
392 $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults);
393 $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults);
1e035d58 394 $defaults['sms_text_message'] = CRM_Utils_Array::value('sms_body_text', $defaults);
6a488035 395 $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults);
1e035d58 396 $defaults['SMStemplate'] = CRM_Utils_Array::value('sms_template_id', $defaults);
a7488080 397 if (!empty($defaults['group_id'])) {
6a488035
TO
398 $defaults['recipient'] = 'group';
399 }
a7488080 400 elseif (!empty($defaults['recipient_manual'])) {
6a488035 401 $defaults['recipient'] = 'manual';
92f4a0b1 402 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
6a488035 403 }
776a4216
SV
404 if ($contactLanguage = CRM_Utils_Array::value('filter_contact_language', $defaults)) {
405 $defaults['filter_contact_language'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $contactLanguage);
406 }
6a488035
TO
407 }
408
409 return $defaults;
410 }
411
412 /**
eceb18cc 413 * Process the form submission.
6a488035
TO
414 */
415 public function postProcess() {
416 if ($this->_action & CRM_Core_Action::DELETE) {
417 // delete reminder
418 CRM_Core_BAO_ActionSchedule::del($this->_id);
419 CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
4d3e4dbe 420 if ($this->_context == 'event' && $this->_compId) {
6a488035 421 $url = CRM_Utils_System::url('civicrm/event/manage/reminder',
4d3e4dbe 422 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
6a488035
TO
423 );
424 $session = CRM_Core_Session::singleton();
425 $session->pushUserContext($url);
426 }
427 return;
428 }
429 $values = $this->controller->exportValues($this->getName());
430
431 $keys = array(
432 'title',
433 'subject',
434 'absolute_date',
435 'group_id',
3e315abc 436 'record_activity',
a3e3eea1 437 'limit_to',
438 'mode',
1991bba1
DG
439 'sms_provider_id',
440 'from_name',
441 'from_email',
6a488035
TO
442 );
443 foreach ($keys as $key) {
444 $params[$key] = CRM_Utils_Array::value($key, $values);
445 }
446
2ef60934 447 $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0);
448
6a488035 449 $moreKeys = array(
3e315abc 450 'start_action_offset',
451 'start_action_unit',
452 'start_action_condition',
453 'start_action_date',
6a488035
TO
454 'repetition_frequency_unit',
455 'repetition_frequency_interval',
456 'end_frequency_unit',
457 'end_frequency_interval',
3e315abc 458 'end_action',
459 'end_date',
6a488035
TO
460 );
461
462 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) {
463 $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate);
2ef60934 464 $params['is_repeat'] = 0;
6a488035
TO
465 foreach ($moreKeys as $mkey) {
466 $params[$mkey] = 'null';
467 }
468 }
469 else {
470 $params['absolute_date'] = 'null';
471 foreach ($moreKeys as $mkey) {
472 $params[$mkey] = CRM_Utils_Array::value($mkey, $values);
473 }
474 }
475
476 $params['body_text'] = CRM_Utils_Array::value('text_message', $values);
1e035d58 477 $params['sms_body_text'] = CRM_Utils_Array::value('sms_text_message', $values);
6a488035
TO
478 $params['body_html'] = CRM_Utils_Array::value('html_message', $values);
479
480 if (CRM_Utils_Array::value('recipient', $values) == 'manual') {
481 $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values);
482 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
483 }
484 elseif (CRM_Utils_Array::value('recipient', $values) == 'group') {
485 $params['group_id'] = $values['group_id'];
486 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
487 }
26a77d0a 488 elseif (!CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) {
6a488035
TO
489 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
490 $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
491 CRM_Utils_Array::value('recipient_listing', $values)
492 );
493 $params['group_id'] = $params['recipient_manual'] = 'null';
494 }
495 else {
496 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
497 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
498 }
499
4d3e4dbe 500 if (!empty($this->_mappingID) && !empty($this->_compId)) {
353ffa53 501 $params['mapping_id'] = $this->_mappingID;
4d3e4dbe 502 $params['entity_value'] = $this->_compId;
503 $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']);
84a3e359 504 }
4d3e4dbe 505 else {
506 $params['mapping_id'] = $values['entity'][0];
507 $entity_value = $values['entity'][1];
508 $entity_status = $values['entity'][2];
673cdfb4 509 if ($params['mapping_id'] == 1) {
4d3e4dbe 510 $params['limit_to'] = 1;
511 }
84a3e359 512
4d3e4dbe 513 foreach (array('entity_value', 'entity_status') as $key) {
514 $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key);
515 }
6a488035
TO
516 }
517
518 $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);
6a488035
TO
519
520 if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
521 $params['repetition_frequency_unit'] = 'null';
522 $params['repetition_frequency_interval'] = 'null';
523 $params['end_frequency_unit'] = 'null';
524 $params['end_frequency_interval'] = 'null';
525 $params['end_action'] = 'null';
526 $params['end_date'] = 'null';
527 }
528
776a4216
SV
529 // multilingual options
530 $params['filter_contact_language'] = CRM_Utils_Array::value('filter_contact_language', $values, null);
531 $params['filter_contact_language'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['filter_contact_language']);
532 $params['communication_language'] = CRM_Utils_Array::value('communication_language', $values, null);
533
6a488035
TO
534 if ($this->_action & CRM_Core_Action::UPDATE) {
535 $params['id'] = $this->_id;
536 }
537 elseif ($this->_action & CRM_Core_Action::ADD) {
538 // we do this only once, so name never changes
539 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
540 }
541
1e035d58 542 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
543
544 if ($params['mode'] == 'Email' || empty($params['sms_provider_id'])) {
545 unset($modePrefixes['SMS']);
546 }
547 elseif ($params['mode'] == 'SMS') {
548 unset($modePrefixes['Mail']);
549 }
550
551 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
552
6a488035 553 $composeFields = array(
353ffa53
TO
554 'template',
555 'saveTemplate',
556 'updateTemplate',
557 'saveTemplateName',
6a488035
TO
558 );
559 $msgTemplate = NULL;
560 //mail template is composed
561
1e035d58 562 foreach ($modePrefixes as $prefix) {
563 $composeParams = array();
564 foreach ($composeFields as $key) {
565 $key = $prefix . $key;
566 if (!empty($values[$key])) {
567 $composeParams[$key] = $values[$key];
568 }
6a488035 569 }
6a488035 570
1e035d58 571 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
572 $templateParams = array('is_active' => TRUE);
573 if ($prefix == 'SMS') {
574 $templateParams += array(
575 'msg_text' => $params['sms_body_text'],
576 'is_sms' => TRUE,
02fc859b 577 );
1e035d58 578 }
579 else {
580 $templateParams += array(
581 'msg_text' => $params['body_text'],
582 'msg_html' => $params['body_html'],
583 'msg_subject' => $params['subject'],
584 );
585 }
586 $templateParams['id'] = $values[$prefix . 'template'];
587
588 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
589 }
6a488035 590
1e035d58 591 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
592 $templateParams = array('is_active' => TRUE);
593 if ($prefix == 'SMS') {
594 $templateParams += array(
595 'msg_text' => $params['sms_body_text'],
596 'is_sms' => TRUE,
597 );
598 }
599 else {
600 $templateParams += array(
601 'msg_text' => $params['body_text'],
602 'msg_html' => $params['body_html'],
603 'msg_subject' => $params['subject'],
604 );
605 }
606 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
607
608 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
609 }
6a488035 610
1e035d58 611 if ($prefix == 'SMS') {
612 if (isset($msgTemplate->id)) {
613 $params['sms_template_id'] = $msgTemplate->id;
614 }
615 else {
616 $params['sms_template_id'] = CRM_Utils_Array::value('SMStemplate', $values);
617 }
618 }
619 else {
620 if (isset($msgTemplate->id)) {
621 $params['msg_template_id'] = $msgTemplate->id;
622 }
623 else {
624 $params['msg_template_id'] = CRM_Utils_Array::value('template', $values);
625 }
626 }
6a488035
TO
627 }
628
24f14405
E
629 $bao = CRM_Core_BAO_ActionSchedule::add($params);
630 // we need to set this on the form so that hooks can identify the created entity
631 $this->set('id', $bao->id);
632 $bao->free();
6a488035
TO
633
634 $status = ts("Your new Reminder titled %1 has been saved.",
635 array(1 => "<strong>{$values['title']}</strong>")
636 );
6a488035 637
4d3e4dbe 638 if ($this->_action) {
639 if ($this->_action & CRM_Core_Action::UPDATE) {
640 $status = ts("Your Reminder titled %1 has been updated.",
353ffa53 641 array(1 => "<strong>{$values['title']}</strong>")
6a488035 642 );
4d3e4dbe 643 }
644
645 if ($this->_context == 'event' && $this->_compId) {
646 $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
6a488035
TO
647 $session = CRM_Core_Session::singleton();
648 $session->pushUserContext($url);
649 }
650 }
651 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
652 }
96025800 653
5ec6b0ad
TM
654 /**
655 * List available tokens for this form.
656 *
657 * @return array
658 */
659 public function listTokens() {
660 $tokens = CRM_Core_SelectValues::contactTokens();
661 $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens);
662 $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
663 $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
664 return $tokens;
665 }
666
6a488035 667}