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