Merge pull request #13557 from vingle/patch-5
[civicrm-core.git] / CRM / Admin / Form / ScheduleReminders.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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
9351dc7e 46 protected $_compId;
47
2d69ef96 48 /**
9351dc7e 49 * @return mixed
50 */
51 public function getComponentID() {
52 return $this->_compId;
53 }
54
55 /**
56 * @param mixed $compId
2d69ef96 57 */
9351dc7e 58 public function setComponentID($compId) {
59 $this->_compId = $compId;
60 }
2d69ef96 61
6a488035 62 /**
eceb18cc 63 * Build the form object.
6a488035
TO
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67 $this->_mappingID = $mappingID = NULL;
a3e3eea1 68 $providersCount = CRM_SMS_BAO_Provider::activeProviderCount();
9351dc7e 69 $this->setContext();
70 $isEvent = $this->getContext() == 'event';
6a488035 71
9351dc7e 72 if ($isEvent) {
73 $this->setComponentID(CRM_Utils_Request::retrieve('compId', 'Integer', $this));
74 if (!CRM_Event_BAO_Event::checkPermission($this->getComponentID(), CRM_Core_Permission::EDIT)) {
75 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
121eba46
WA
76 }
77 }
9351dc7e 78 elseif (!CRM_Core_Permission::check('administer CiviCRM')) {
79 throw new CRM_Core_Exception(ts('You do not have permission to access this page.'));
80 }
9c74fabc 81
6a488035 82 if ($this->_action & (CRM_Core_Action::DELETE)) {
4d3e4dbe 83 $reminderName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'title');
6a488035
TO
84 $this->assign('reminderName', $reminderName);
85 return;
86 }
87 elseif ($this->_action & (CRM_Core_Action::UPDATE)) {
c301f76e 88 $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'mapping_id');
6a488035 89 }
9351dc7e 90 if ($isEvent) {
91 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->getComponentID(), 'is_template');
2d69ef96 92 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
93 'id' => $isTemplate ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
94 )));
95 if ($mapping) {
96 $this->_mappingID = $mapping->getId();
97 }
98 else {
9351dc7e 99 throw new CRM_Core_Exception('Could not find mapping for event scheduled reminders.');
4d3e4dbe 100 }
264ebe5d 101 }
102
9351dc7e 103 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->getContext())) {
02fc859b
TO
104 $mappingID = $_POST['entity'][0];
105 }
106 elseif ($this->_mappingID) {
107 $mappingID = $this->_mappingID;
9351dc7e 108 if ($isEvent) {
93fdcf55 109 $this->add('hidden', 'mappingID', $mappingID);
110 }
02fc859b 111 }
4d3e4dbe 112
6a488035
TO
113 $this->add(
114 'text',
115 'title',
116 ts('Title'),
0c843583 117 CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'title'),
6a488035
TO
118 TRUE
119 );
120
0905ee5d
TO
121 $mappings = CRM_Core_BAO_ActionSchedule::getMappings();
122 $selectedMapping = $mappings[$mappingID ? $mappingID : 1];
123 $entityRecipientLabels = $selectedMapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients();
124 $this->assign('entityMapping', json_encode(
125 CRM_Utils_Array::collectMethod('getEntity', $mappings)
126 ));
127 $this->assign('recipientMapping', json_encode(
128 array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels))
129 ));
6a488035 130
9351dc7e 131 if (!$this->getContext()) {
353ffa53 132 $sel = &$this->add(
4d3e4dbe 133 'hierselect',
353ffa53
TO
134 'entity',
135 ts('Entity'),
136 array(
137 'name' => 'entity[0]',
138 'style' => 'vertical-align: top;',
21c744ca 139 )
4d3e4dbe 140 );
0905ee5d
TO
141 $sel->setOptions(array(
142 CRM_Utils_Array::collectMethod('getLabel', $mappings),
143 CRM_Core_BAO_ActionSchedule::getAllEntityValueLabels(),
144 CRM_Core_BAO_ActionSchedule::getAllEntityStatusLabels(),
145 ));
6a488035 146
4d3e4dbe 147 if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) {
148 // make second selector a multi-select -
149 $sel->_elements[1]->setMultiple(TRUE);
150 $sel->_elements[1]->setSize(5);
151 }
152
153 if (is_a($sel->_elements[2], 'HTML_QuickForm_select')) {
154 // make third selector a multi-select -
155 $sel->_elements[2]->setMultiple(TRUE);
156 $sel->_elements[2]->setSize(5);
157 }
158 }
159 else {
0905ee5d
TO
160 // Dig deeper - this code is sublimely stupid.
161 $allEntityStatusLabels = CRM_Core_BAO_ActionSchedule::getAllEntityStatusLabels();
162 $options = $allEntityStatusLabels[$this->_mappingID][0];
4d3e4dbe 163 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
164 unset($options[0]);
165 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
9351dc7e 166 $this->assign('context', $this->getContext());
6a488035
TO
167 }
168
169 //get the frequency units.
8fe4b69f 170 $this->_freqUnits = CRM_Core_SelectValues::getRecurringFrequencyUnits();
6a488035 171
6a488035 172 //reminder_interval
dc5689ec 173 $this->add('number', 'start_action_offset', ts('When'), array('class' => 'six', 'min' => 0));
4054a23f
DRJ
174 $this->addRule('start_action_offset', ts('Value should be a positive number'), 'positiveInteger');
175
b30461e4 176 $isActive = ts('Scheduled Reminder Active');
a3e3eea1 177 $recordActivity = ts('Record activity for automated email');
178 if ($providersCount) {
7b007e61 179 $this->assign('sms', $providersCount);
a3e3eea1 180 $recordActivity = ts('Record activity for automated email or SMS');
181 $options = CRM_Core_OptionGroup::values('msg_mode');
182 $this->add('select', 'mode', ts('Send as'), $options);
183
184 $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc');
185
186 $providerSelect = array();
187 foreach ($providers as $provider) {
188 $providerSelect[$provider['id']] = $provider['title'];
189 }
1e035d58 190 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
a3e3eea1 191 }
6a488035
TO
192
193 foreach ($this->_freqUnits as $val => $label) {
194 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
195 }
196
b219e49f 197 $this->add('datepicker', 'absolute_date', ts('Start Date'), [], FALSE, array('time' => FALSE));
6a488035
TO
198
199 //reminder_frequency
200 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
201
02fc859b 202 $condition = array(
353ffa53 203 'before' => ts('before'),
6a488035
TO
204 'after' => ts('after'),
205 );
206 //reminder_action
207 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
208
0905ee5d 209 $this->add('select', 'start_action_date', ts('Date Field'), $selectedMapping->getDateFields(), TRUE);
6a488035 210
a3e3eea1 211 $this->addElement('checkbox', 'record_activity', $recordActivity);
6a488035
TO
212
213 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
581c7be2 214 NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
6a488035
TO
215 );
216
217 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
dc5689ec 218 $this->add('number', 'repetition_frequency_interval', ts('every'), array('class' => 'six', 'min' => 0));
4054a23f
DRJ
219 $this->addRule('repetition_frequency_interval', ts('Value should be a positive number'), 'positiveInteger');
220
6a488035 221 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
dc5689ec 222 $this->add('number', 'end_frequency_interval', ts('until'), array('class' => 'six', 'min' => 0));
4054a23f
DRJ
223 $this->addRule('end_frequency_interval', ts('Value should be a positive number'), 'positiveInteger');
224
6a488035 225 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
0905ee5d 226 $this->add('select', 'end_date', ts('Date Field'), $selectedMapping->getDateFields(), TRUE);
6a488035 227
1991bba1 228 $this->add('text', 'from_name', ts('From Name'));
1e035d58 229 $this->add('text', 'from_email', ts('From Email'));
1991bba1 230
6a488035
TO
231 $recipientListingOptions = array();
232
233 if ($mappingID) {
50a23755
TO
234 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
235 'id' => $mappingID,
236 )));
6a488035
TO
237 }
238
84a3e359 239 $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include'));
8ef12e64 240
84a3e359 241 $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients'));
242 $this->assign('recipientLabels', $recipientLabels);
243
244 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);"));
245
0905ee5d 246 $this->add('select', 'recipient', $recipientLabels['other'], $entityRecipientLabels,
581c7be2 247 FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
6a488035
TO
248 );
249
49d61c3a 250 if (!empty($this->_submitValues['recipient_listing'])) {
9351dc7e 251 if ($this->getContext()) {
4d3e4dbe 252 $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_mappingID, $this->_submitValues['recipient']);
253 }
254 else {
255 $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
256 }
6a488035 257 }
a7488080 258 elseif (!empty($this->_values['recipient_listing'])) {
6a488035
TO
259 $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_values['mapping_id'], $this->_values['recipient']);
260 }
4d3e4dbe 261
581c7be2
CW
262 $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE,
263 array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE));
6a488035 264
92f4a0b1 265 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
6a488035 266
581c7be2 267 $this->add('select', 'group_id', ts('Group'),
af5d5802 268 CRM_Core_PseudoConstant::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge')
6a488035
TO
269 );
270
776a4216 271 // multilingual only options
273f9849 272 $multilingual = CRM_Core_I18n::isMultilingual();
776a4216
SV
273 if ($multilingual) {
274 $smarty = CRM_Core_Smarty::singleton();
275 $smarty->assign('multilingual', $multilingual);
276
273f9849
SV
277 $languages = CRM_Core_I18n::languages(TRUE);
278 $languageFilter = $languages + array(CRM_Core_I18n::NONE => ts('Contacts with no preferred language'));
776a4216
SV
279 $element = $this->add('select', 'filter_contact_language', ts('Recipients language'), $languageFilter, FALSE,
280 array('multiple' => TRUE, 'class' => 'crm-select2', 'placeholder' => TRUE));
281
b3518210 282 $communicationLanguage = array(
273f9849 283 '' => ts('System default language'),
aeb3ba30 284 CRM_Core_I18n::AUTO => ts('Follow recipient preferred language'),
273f9849 285 );
b3518210 286 $communicationLanguage = $communicationLanguage + $languages;
776a4216
SV
287 $this->add('select', 'communication_language', ts('Communication language'), $communicationLanguage);
288 }
289
6a488035
TO
290 CRM_Mailing_BAO_Mailing::commonCompose($this);
291
292 $this->add('text', 'subject', ts('Subject'),
293 CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
294 );
295
a3e3eea1 296 $this->add('checkbox', 'is_active', $isActive);
6a488035 297
4d3e4dbe 298 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
581c7be2
CW
299
300 $this->setPageTitle(ts('Scheduled Reminder'));
6a488035 301 }
353ffa53 302
6a488035 303 /**
eceb18cc 304 * Global form rule.
6a488035 305 *
5173bd95
TO
306 * @param array $fields
307 * The input form values.
ea3ddccf 308 * @param array $files
7f0141d8 309 * @param CRM_Admin_Form_ScheduleReminders $self
6a488035 310 *
ea3ddccf 311 * @return array|bool
312 * True if no errors, else array of errors
6a488035 313 */
00be9182 314 public static function formRule($fields, $files, $self) {
6a488035 315 $errors = array();
8d657dde 316 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
6a488035
TO
317 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
318 ) {
319 $errors['entity'] = ts('Please select appropriate value');
320 }
321
1c4d4fb7 322 $mode = CRM_Utils_Array::value('mode', $fields, FALSE);
fb90d6bb 323 if (!empty($fields['is_active']) &&
1c4d4fb7 324 CRM_Utils_System::isNull($fields['subject']) && (!$mode || $mode != 'SMS')
6a488035
TO
325 ) {
326 $errors['subject'] = ts('Subject is a required field.');
327 }
02fdbd86 328 if (!empty($fields['is_active']) &&
1c4d4fb7 329 CRM_Utils_System::isNull(trim(strip_tags($fields['html_message']))) && (!$mode || $mode != 'SMS')
02fdbd86 330 ) {
331 $errors['html_message'] = ts('The HTML message is a required field.');
332 }
6a488035 333
1c4d4fb7
SL
334 if (!empty($mode) && ($mode == 'SMS' || $mode == 'User_Preference') && !empty($fields['is_active']) &&
335 CRM_Utils_System::isNull(trim(strip_tags($fields['sms_text_message'])))
336 ) {
337 $errors['sms_text_message'] = ts('The SMS message is a required field.');
338 }
339
9351dc7e 340 if (empty($self->getContext()) && CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) {
6a488035
TO
341 $errors['entity'] = ts('Please select entity value');
342 }
343
344 if (!CRM_Utils_System::isNull($fields['absolute_date'])) {
b219e49f 345 if ($fields['absolute_date'] < date('Y-m-d')) {
6a488035
TO
346 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
347 }
348 }
b219e49f
SL
349 else {
350 if (CRM_Utils_System::isNull($fields['start_action_offset'])) {
351 $errors['start_action_offset'] = ts('Start Action Offset must be filled in or Absolute Date set');
352 }
353 }
1c4d4fb7 354 if (!CRM_Utils_Rule::email($fields['from_email']) && (!$mode || $mode != 'SMS')) {
e09225f7
DV
355 $errors['from_email'] = ts('Please enter a valid email address.');
356 }
84a3e359 357 $recipientKind = array(
358 'participant_role' => array(
359 'name' => 'participant role',
21dfd5f5 360 'target_id' => 'recipient_listing',
84a3e359 361 ),
362 'manual' => array(
363 'name' => 'recipient',
21dfd5f5
TO
364 'target_id' => 'recipient_manual_id',
365 ),
84a3e359 366 );
fb90d6bb 367 if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
49d61c3a 368 $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 369 }
370
d1095f4a
BS
371 //CRM-21523
372 if (!empty($fields['is_repeat']) &&
373 (empty($fields['repetition_frequency_interval']) || ($fields['end_frequency_interval'] == NULL))
374 ) {
375 $errors['is_repeat'] = ts('If you are enabling repetition you must indicate the frequency and ending term.');
376 }
377
6f23e48a
JP
378 $self->_actionSchedule = $self->parseActionSchedule($fields);
379 if ($self->_actionSchedule->mapping_id) {
380 $mapping = CRM_Core_BAO_ActionSchedule::getMapping($self->_actionSchedule->mapping_id);
381 CRM_Utils_Array::extend($errors, $mapping->validateSchedule($self->_actionSchedule));
7f0141d8
TO
382 }
383
6a488035
TO
384 if (!empty($errors)) {
385 return $errors;
386 }
387
388 return empty($errors) ? TRUE : $errors;
389 }
390
e0ef6999
EM
391 /**
392 * @return int
393 */
00be9182 394 public function setDefaultValues() {
6a488035
TO
395 if ($this->_action & CRM_Core_Action::ADD) {
396 $defaults['is_active'] = 1;
a3e3eea1 397 $defaults['mode'] = 'Email';
6a488035
TO
398 $defaults['record_activity'] = 1;
399 }
400 else {
401 $defaults = $this->_values;
4d3e4dbe 402 $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_value', $defaults));
403 $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Utils_Array::value('entity_status', $defaults));
9351dc7e 404 if (empty($this->getContext())) {
4d3e4dbe 405 $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults);
406 $defaults['entity'][1] = $entityValue;
407 $defaults['entity'][2] = $entityStatus;
408 }
409 else {
410 $defaults['entity'] = $entityStatus;
411 }
6a488035
TO
412
413 if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) {
414 $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
415 $recipientListing
416 );
417 }
418 $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults);
419 $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults);
1e035d58 420 $defaults['sms_text_message'] = CRM_Utils_Array::value('sms_body_text', $defaults);
6a488035 421 $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults);
1e035d58 422 $defaults['SMStemplate'] = CRM_Utils_Array::value('sms_template_id', $defaults);
a7488080 423 if (!empty($defaults['group_id'])) {
6a488035
TO
424 $defaults['recipient'] = 'group';
425 }
a7488080 426 elseif (!empty($defaults['recipient_manual'])) {
6a488035 427 $defaults['recipient'] = 'manual';
92f4a0b1 428 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
6a488035 429 }
776a4216 430 if ($contactLanguage = CRM_Utils_Array::value('filter_contact_language', $defaults)) {
fadd4d37 431 $defaults['filter_contact_language'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, $contactLanguage);
776a4216 432 }
6a488035
TO
433 }
434
435 return $defaults;
436 }
437
438 /**
eceb18cc 439 * Process the form submission.
6a488035
TO
440 */
441 public function postProcess() {
442 if ($this->_action & CRM_Core_Action::DELETE) {
443 // delete reminder
444 CRM_Core_BAO_ActionSchedule::del($this->_id);
445 CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
9351dc7e 446 if ($this->getContext() == 'event' && $this->getComponentID()) {
6a488035 447 $url = CRM_Utils_System::url('civicrm/event/manage/reminder',
9351dc7e 448 "reset=1&action=browse&id=" . $this->getComponentID() . "&component=" . $this->getContext() . "&setTab=1"
6a488035
TO
449 );
450 $session = CRM_Core_Session::singleton();
451 $session->pushUserContext($url);
452 }
453 return;
454 }
455 $values = $this->controller->exportValues($this->getName());
6f23e48a
JP
456 if (empty($this->_actionSchedule)) {
457 $bao = $this->parseActionSchedule($values)->save();
458 }
459 else {
460 $bao = $this->_actionSchedule->save();
461 }
7f0141d8
TO
462
463 // we need to set this on the form so that hooks can identify the created entity
464 $this->set('id', $bao->id);
465 $bao->free();
466
467 $status = ts("Your new Reminder titled %1 has been saved.",
468 array(1 => "<strong>{$values['title']}</strong>")
469 );
470
471 if ($this->_action) {
472 if ($this->_action & CRM_Core_Action::UPDATE) {
473 $status = ts("Your Reminder titled %1 has been updated.",
474 array(1 => "<strong>{$values['title']}</strong>")
475 );
476 }
477
9351dc7e 478 if ($this->getContext() == 'event' && $this->getComponentID()) {
479 $url = CRM_Utils_System::url('civicrm/event/manage/reminder', "reset=1&action=browse&id=" . $this->getComponentID() . "&component=" . $this->getContext() . "&setTab=1");
7f0141d8
TO
480 $session = CRM_Core_Session::singleton();
481 $session->pushUserContext($url);
482 }
483 }
484 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
485 }
486
487 /**
488 * @param array $values
489 * The submitted form values.
490 * @return CRM_Core_DAO_ActionSchedule
491 */
492 public function parseActionSchedule($values) {
493 $params = array();
6a488035
TO
494
495 $keys = array(
496 'title',
497 'subject',
498 'absolute_date',
499 'group_id',
3e315abc 500 'record_activity',
a3e3eea1 501 'limit_to',
502 'mode',
1991bba1
DG
503 'sms_provider_id',
504 'from_name',
505 'from_email',
6a488035
TO
506 );
507 foreach ($keys as $key) {
508 $params[$key] = CRM_Utils_Array::value($key, $values);
509 }
510
2ef60934 511 $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0);
512
6a488035 513 $moreKeys = array(
3e315abc 514 'start_action_offset',
515 'start_action_unit',
516 'start_action_condition',
517 'start_action_date',
6a488035
TO
518 'repetition_frequency_unit',
519 'repetition_frequency_interval',
520 'end_frequency_unit',
521 'end_frequency_interval',
3e315abc 522 'end_action',
523 'end_date',
6a488035
TO
524 );
525
b219e49f 526 if (!CRM_Utils_Array::value('absolute_date', $params)) {
6a488035 527 $params['absolute_date'] = 'null';
dc6c330d 528 }
529 foreach ($moreKeys as $mkey) {
39bf7f1e
JP
530 if ($params['absolute_date'] != 'null' && CRM_Utils_String::startsWith($mkey, 'start_action')) {
531 $params[$mkey] = 'null';
532 continue;
6a488035 533 }
dc6c330d 534 $params[$mkey] = CRM_Utils_Array::value($mkey, $values);
6a488035
TO
535 }
536
537 $params['body_text'] = CRM_Utils_Array::value('text_message', $values);
1e035d58 538 $params['sms_body_text'] = CRM_Utils_Array::value('sms_text_message', $values);
6a488035
TO
539 $params['body_html'] = CRM_Utils_Array::value('html_message', $values);
540
541 if (CRM_Utils_Array::value('recipient', $values) == 'manual') {
542 $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values);
543 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
544 }
545 elseif (CRM_Utils_Array::value('recipient', $values) == 'group') {
546 $params['group_id'] = $values['group_id'];
547 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
548 }
7f0141d8 549 elseif (isset($values['recipient_listing']) && isset($values['limit_to']) && !CRM_Utils_System::isNull($values['recipient_listing']) && !CRM_Utils_System::isNull($values['limit_to'])) {
6a488035
TO
550 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
551 $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
552 CRM_Utils_Array::value('recipient_listing', $values)
553 );
554 $params['group_id'] = $params['recipient_manual'] = 'null';
555 }
556 else {
557 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
558 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
559 }
560
9351dc7e 561 if (!empty($this->_mappingID) && !empty($this->getComponentID())) {
353ffa53 562 $params['mapping_id'] = $this->_mappingID;
9351dc7e 563 $params['entity_value'] = $this->getComponentID();
4d3e4dbe 564 $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']);
84a3e359 565 }
4d3e4dbe 566 else {
567 $params['mapping_id'] = $values['entity'][0];
673cdfb4 568 if ($params['mapping_id'] == 1) {
4d3e4dbe 569 $params['limit_to'] = 1;
570 }
84a3e359 571
7f0141d8
TO
572 $entity_value = CRM_Utils_Array::value(1, $values['entity'], array());
573 $entity_status = CRM_Utils_Array::value(2, $values['entity'], array());
574 $params['entity_value'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $entity_value);
575 $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $entity_status);
6a488035
TO
576 }
577
578 $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);
6a488035
TO
579
580 if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
581 $params['repetition_frequency_unit'] = 'null';
582 $params['repetition_frequency_interval'] = 'null';
583 $params['end_frequency_unit'] = 'null';
584 $params['end_frequency_interval'] = 'null';
585 $params['end_action'] = 'null';
586 $params['end_date'] = 'null';
587 }
588
776a4216 589 // multilingual options
7f0141d8 590 $params['filter_contact_language'] = CRM_Utils_Array::value('filter_contact_language', $values, array());
776a4216 591 $params['filter_contact_language'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['filter_contact_language']);
fadd4d37 592 $params['communication_language'] = CRM_Utils_Array::value('communication_language', $values, NULL);
776a4216 593
6a488035
TO
594 if ($this->_action & CRM_Core_Action::UPDATE) {
595 $params['id'] = $this->_id;
596 }
597 elseif ($this->_action & CRM_Core_Action::ADD) {
598 // we do this only once, so name never changes
599 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
600 }
601
1e035d58 602 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
603
604 if ($params['mode'] == 'Email' || empty($params['sms_provider_id'])) {
605 unset($modePrefixes['SMS']);
606 }
607 elseif ($params['mode'] == 'SMS') {
608 unset($modePrefixes['Mail']);
609 }
610
611 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
612
6a488035 613 $composeFields = array(
353ffa53
TO
614 'template',
615 'saveTemplate',
616 'updateTemplate',
617 'saveTemplateName',
6a488035
TO
618 );
619 $msgTemplate = NULL;
620 //mail template is composed
621
1e035d58 622 foreach ($modePrefixes as $prefix) {
623 $composeParams = array();
624 foreach ($composeFields as $key) {
625 $key = $prefix . $key;
626 if (!empty($values[$key])) {
627 $composeParams[$key] = $values[$key];
628 }
6a488035 629 }
6a488035 630
1e035d58 631 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
632 $templateParams = array('is_active' => TRUE);
633 if ($prefix == 'SMS') {
634 $templateParams += array(
635 'msg_text' => $params['sms_body_text'],
636 'is_sms' => TRUE,
02fc859b 637 );
1e035d58 638 }
639 else {
640 $templateParams += array(
641 'msg_text' => $params['body_text'],
642 'msg_html' => $params['body_html'],
643 'msg_subject' => $params['subject'],
644 );
645 }
646 $templateParams['id'] = $values[$prefix . 'template'];
647
648 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
649 }
6a488035 650
1e035d58 651 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
652 $templateParams = array('is_active' => TRUE);
653 if ($prefix == 'SMS') {
654 $templateParams += array(
655 'msg_text' => $params['sms_body_text'],
656 'is_sms' => TRUE,
657 );
658 }
659 else {
660 $templateParams += array(
661 'msg_text' => $params['body_text'],
662 'msg_html' => $params['body_html'],
663 'msg_subject' => $params['subject'],
664 );
665 }
666 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
667
668 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
669 }
6a488035 670
1e035d58 671 if ($prefix == 'SMS') {
672 if (isset($msgTemplate->id)) {
673 $params['sms_template_id'] = $msgTemplate->id;
674 }
675 else {
676 $params['sms_template_id'] = CRM_Utils_Array::value('SMStemplate', $values);
677 }
678 }
679 else {
680 if (isset($msgTemplate->id)) {
681 $params['msg_template_id'] = $msgTemplate->id;
682 }
683 else {
684 $params['msg_template_id'] = CRM_Utils_Array::value('template', $values);
685 }
686 }
6a488035
TO
687 }
688
7f0141d8
TO
689 $actionSchedule = new CRM_Core_DAO_ActionSchedule();
690 $actionSchedule->copyValues($params);
691 return $actionSchedule;
6a488035 692 }
96025800 693
5ec6b0ad
TM
694 /**
695 * List available tokens for this form.
696 *
697 * @return array
698 */
699 public function listTokens() {
700 $tokens = CRM_Core_SelectValues::contactTokens();
701 $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens);
702 $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
703 $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
c3ab1f3d 704 $tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens);
5ec6b0ad
TM
705 return $tokens;
706 }
707
6a488035 708}