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