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