Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright (C) 2011 Marty Wright | | |
7 | | Licensed to CiviCRM under the Academic Free License version 3.0. | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for Scheduling Reminders | |
39 | * | |
40 | */ | |
41 | class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { | |
42 | ||
43 | /** | |
44 | * Scheduled Reminder ID | |
45 | */ | |
46 | protected $_id = NULL; | |
47 | ||
48 | public $_freqUnits; | |
49 | ||
50 | /** | |
51 | * Function to build the form | |
52 | * | |
53 | * @return None | |
54 | * @access public | |
55 | */ | |
56 | public function buildQuickForm() { | |
57 | parent::buildQuickForm(); | |
58 | $this->_mappingID = $mappingID = NULL; | |
59 | ||
60 | if ($this->_action & (CRM_Core_Action::DELETE)) { | |
61 | $reminderName = | |
62 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'title'); | |
63 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
64 | if ($this->_context == 'event') { | |
65 | $this->_eventId = CRM_Utils_Request::retrieve('eventId', 'Integer', $this); | |
66 | } | |
67 | $this->assign('reminderName', $reminderName); | |
68 | return; | |
69 | } | |
70 | elseif ($this->_action & (CRM_Core_Action::UPDATE)) { | |
71 | $this->_mappingID = | |
72 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'mapping_id'); | |
73 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
74 | if ($this->_context == 'event') { | |
75 | $this->_eventId = CRM_Utils_Request::retrieve('eventId', 'Integer', $this); | |
76 | } | |
77 | } | |
78 | ||
79 | if (!empty($_POST) && CRM_Utils_Array::value('entity', $_POST)) { | |
80 | $mappingID = $_POST['entity'][0]; | |
81 | } | |
82 | elseif ($this->_mappingID) { | |
83 | $mappingID = $this->_mappingID; | |
84 | } | |
85 | ||
86 | $this->add( | |
87 | 'text', | |
88 | 'title', | |
89 | ts('Title'), | |
90 | array('size' => 45, 'maxlength' => 128), | |
91 | TRUE | |
92 | ); | |
93 | ||
94 | $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($mappingID); | |
95 | extract($selectionOptions); | |
96 | ||
97 | if (empty($sel1)) { | |
98 | CRM_Core_Error::fatal('Could not find mapping for scheduled reminders.'); | |
99 | } | |
100 | $this->assign('entityMapping', json_encode($entityMapping)); | |
101 | $this->assign('recipientMapping', json_encode($recipientMapping)); | |
102 | ||
3e315abc | 103 | $sel = & $this->add( |
6a488035 TO |
104 | 'hierselect', |
105 | 'entity', | |
106 | ts('Entity'), | |
107 | array( | |
108 | 'name' => 'entity[0]', | |
109 | 'style' => 'vertical-align: top;', | |
110 | ), | |
111 | TRUE | |
112 | ); | |
113 | $sel->setOptions(array($sel1, $sel2, $sel3)); | |
114 | ||
115 | if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) { | |
116 | // make second selector a multi-select - | |
117 | $sel->_elements[1]->setMultiple(TRUE); | |
118 | $sel->_elements[1]->setSize(5); | |
119 | } | |
120 | ||
121 | if (is_a($sel->_elements[2], 'HTML_QuickForm_select')) { | |
122 | // make third selector a multi-select - | |
123 | $sel->_elements[2]->setMultiple(TRUE); | |
124 | $sel->_elements[2]->setSize(5); | |
125 | } | |
126 | ||
127 | //get the frequency units. | |
128 | $this->_freqUnits = array('hour' => 'hour') + CRM_Core_OptionGroup::values('recur_frequency_units'); | |
129 | ||
130 | //pass the mapping ID in UPDATE mode | |
131 | $mappings = CRM_Core_BAO_ActionSchedule::getMapping($mappingID); | |
132 | ||
133 | $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30); | |
134 | ||
135 | //reminder_interval | |
136 | $this->add('select', 'start_action_offset', ts('When'), $numericOptions); | |
137 | ||
138 | foreach ($this->_freqUnits as $val => $label) { | |
139 | $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label)); | |
140 | } | |
141 | ||
142 | $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing')); | |
143 | ||
144 | //reminder_frequency | |
145 | $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE); | |
146 | ||
147 | $condition = array('before' => ts('before'), | |
148 | 'after' => ts('after'), | |
149 | ); | |
150 | //reminder_action | |
151 | $this->add('select', 'start_action_condition', ts('Action Condition'), $condition); | |
152 | ||
153 | $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE); | |
154 | ||
155 | $this->addElement('checkbox', 'record_activity', ts('Record activity for automated email')); | |
156 | ||
157 | $this->addElement('checkbox', 'is_repeat', ts('Repeat'), | |
158 | NULL, array('onclick' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);") | |
159 | ); | |
160 | ||
161 | $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay); | |
162 | $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions); | |
163 | $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay); | |
164 | $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions); | |
165 | $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE); | |
166 | $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE); | |
167 | ||
168 | $recipient = 'activity_contacts'; | |
169 | $recipientListingOptions = array(); | |
170 | ||
171 | if ($mappingID) { | |
172 | $recipient = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', | |
173 | $mappingID, | |
174 | 'entity_recipient' | |
175 | ); | |
176 | } | |
177 | ||
3e315abc | 178 | $limitOptions = array(1 => ts('Limit to'), 0 => ts('Addition to')); |
179 | $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions); | |
8ef12e64 | 180 | |
3e315abc | 181 | $this->add('select', 'recipient', ts('Recipients'), $sel5[$recipient], |
6a488035 TO |
182 | FALSE, array('onClick' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);") |
183 | ); | |
184 | ||
185 | if (CRM_Utils_Array::value('is_recipient_listing', $_POST)) { | |
186 | $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($_POST['entity'][0], $_POST['recipient']); | |
187 | } | |
188 | elseif (CRM_Utils_Array::value('recipient_listing', $this->_values)) { | |
189 | $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_values['mapping_id'], $this->_values['recipient']); | |
190 | } | |
191 | $recipientListing = $this->add('select', 'recipient_listing', ts('Recipient Listing'), $recipientListingOptions); | |
192 | $recipientListing->setMultiple(TRUE); | |
193 | $this->add('hidden', 'is_recipient_listing', empty($recipientListingOptions) ? FALSE : TRUE, array('id' => 'is_recipient_listing')); | |
194 | ||
3e315abc | 195 | //auto-complete url |
6a488035 TO |
196 | $dataUrl = CRM_Utils_System::url('civicrm/ajax/rest', |
197 | 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=activity&reset=1', | |
198 | FALSE, NULL, FALSE | |
199 | ); | |
200 | ||
201 | $this->assign('dataUrl', $dataUrl); | |
3e315abc | 202 | //token input url |
6a488035 TO |
203 | $tokenUrl = CRM_Utils_System::url('civicrm/ajax/checkemail', |
204 | 'noemail=1', | |
205 | FALSE, NULL, FALSE | |
206 | ); | |
207 | $this->assign('tokenUrl', $tokenUrl); | |
208 | $this->add('text', 'recipient_manual_id', ts('Manual Recipients')); | |
209 | ||
f10c69fa DL |
210 | $this->addElement( |
211 | 'select', | |
212 | 'group_id', | |
213 | ts('Group'), | |
214 | // CRM-13577 | |
215 | CRM_Core_PseudoConstant::group() | |
6a488035 TO |
216 | ); |
217 | ||
218 | CRM_Mailing_BAO_Mailing::commonCompose($this); | |
219 | ||
220 | $this->add('text', 'subject', ts('Subject'), | |
221 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject') | |
222 | ); | |
223 | ||
224 | $this->add('checkbox', 'is_active', ts('Send email')); | |
225 | ||
226 | $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule')); | |
227 | } | |
228 | /** | |
229 | * global form rule | |
230 | * | |
231 | * @param array $fields the input form values | |
232 | * | |
233 | * @return true if no errors, else array of errors | |
234 | * @access public | |
235 | * @static | |
236 | */ | |
237 | static function formRule($fields) { | |
238 | $errors = array(); | |
239 | if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] == 0) || | |
240 | (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0) | |
241 | ) { | |
242 | $errors['entity'] = ts('Please select appropriate value'); | |
243 | } | |
244 | ||
245 | if (CRM_Utils_Array::value('is_active', $fields) && | |
246 | CRM_Utils_System::isNull($fields['subject']) | |
247 | ) { | |
248 | $errors['subject'] = ts('Subject is a required field.'); | |
249 | } | |
250 | ||
251 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) { | |
252 | $errors['entity'] = ts('Please select entity value'); | |
253 | } | |
254 | ||
255 | if (!CRM_Utils_System::isNull($fields['absolute_date'])) { | |
256 | if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('YmdHi00'))) { | |
257 | $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.'); | |
258 | } | |
259 | } | |
260 | ||
261 | if (!empty($errors)) { | |
262 | return $errors; | |
263 | } | |
264 | ||
265 | return empty($errors) ? TRUE : $errors; | |
266 | } | |
267 | ||
268 | function setDefaultValues() { | |
269 | if ($this->_action & CRM_Core_Action::ADD) { | |
270 | $defaults['is_active'] = 1; | |
271 | $defaults['record_activity'] = 1; | |
272 | } | |
273 | else { | |
274 | $defaults = $this->_values; | |
275 | $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
276 | CRM_Utils_Array::value('entity_value', $defaults) | |
277 | ); | |
278 | $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
279 | CRM_Utils_Array::value('entity_status', $defaults) | |
280 | ); | |
281 | $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults); | |
282 | $defaults['entity'][1] = $entityValue; | |
283 | $defaults['entity'][2] = $entityStatus; | |
284 | if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $defaults)) { | |
285 | list($date, $time) = CRM_Utils_Date::setDateDefaults($absoluteDate); | |
286 | $defaults['absolute_date'] = $date; | |
287 | } | |
288 | ||
289 | if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) { | |
290 | $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
291 | $recipientListing | |
292 | ); | |
293 | } | |
294 | $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults); | |
295 | $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults); | |
296 | $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults); | |
297 | if (CRM_Utils_Array::value('group_id', $defaults)) { | |
298 | $defaults['recipient'] = 'group'; | |
299 | } | |
300 | elseif (CRM_Utils_Array::value('recipient_manual', $defaults)) { | |
301 | $defaults['recipient'] = 'manual'; | |
302 | $recipients = array(); | |
303 | foreach (explode(',', $defaults['recipient_manual']) as $cid) { | |
304 | $recipients[$cid] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
305 | $cid, | |
306 | 'sort_name' | |
307 | ); | |
308 | } | |
309 | $this->assign('recipients', $recipients); | |
310 | } | |
311 | } | |
312 | ||
313 | return $defaults; | |
314 | } | |
315 | ||
316 | /** | |
317 | * Function to process the form | |
318 | * | |
319 | * @access public | |
320 | * | |
321 | * @return None | |
322 | */ | |
323 | public function postProcess() { | |
324 | if ($this->_action & CRM_Core_Action::DELETE) { | |
325 | // delete reminder | |
326 | CRM_Core_BAO_ActionSchedule::del($this->_id); | |
327 | CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success'); | |
328 | if ($this->_context == 'event' && $this->_eventId) { | |
329 | $url = CRM_Utils_System::url('civicrm/event/manage/reminder', | |
330 | "reset=1&action=update&id={$this->_eventId}" | |
331 | ); | |
332 | $session = CRM_Core_Session::singleton(); | |
333 | $session->pushUserContext($url); | |
334 | } | |
335 | return; | |
336 | } | |
337 | $values = $this->controller->exportValues($this->getName()); | |
338 | ||
339 | $keys = array( | |
340 | 'title', | |
341 | 'subject', | |
342 | 'absolute_date', | |
343 | 'group_id', | |
3e315abc | 344 | 'record_activity', |
345 | 'limit_to' | |
6a488035 TO |
346 | ); |
347 | foreach ($keys as $key) { | |
348 | $params[$key] = CRM_Utils_Array::value($key, $values); | |
349 | } | |
350 | ||
351 | $moreKeys = array( | |
3e315abc | 352 | 'start_action_offset', |
353 | 'start_action_unit', | |
354 | 'start_action_condition', | |
355 | 'start_action_date', | |
6a488035 TO |
356 | 'repetition_frequency_unit', |
357 | 'repetition_frequency_interval', | |
358 | 'end_frequency_unit', | |
359 | 'end_frequency_interval', | |
3e315abc | 360 | 'end_action', |
361 | 'end_date', | |
6a488035 TO |
362 | ); |
363 | ||
364 | if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) { | |
365 | $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate); | |
366 | foreach ($moreKeys as $mkey) { | |
367 | $params[$mkey] = 'null'; | |
368 | } | |
369 | } | |
370 | else { | |
371 | $params['absolute_date'] = 'null'; | |
372 | foreach ($moreKeys as $mkey) { | |
373 | $params[$mkey] = CRM_Utils_Array::value($mkey, $values); | |
374 | } | |
375 | } | |
376 | ||
377 | $params['body_text'] = CRM_Utils_Array::value('text_message', $values); | |
378 | $params['body_html'] = CRM_Utils_Array::value('html_message', $values); | |
379 | ||
380 | if (CRM_Utils_Array::value('recipient', $values) == 'manual') { | |
381 | $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values); | |
382 | $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
383 | } | |
384 | elseif (CRM_Utils_Array::value('recipient', $values) == 'group') { | |
385 | $params['group_id'] = $values['group_id']; | |
386 | $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
387 | } | |
388 | elseif (!CRM_Utils_System::isNull($values['recipient_listing'])) { | |
389 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
390 | $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
391 | CRM_Utils_Array::value('recipient_listing', $values) | |
392 | ); | |
393 | $params['group_id'] = $params['recipient_manual'] = 'null'; | |
394 | } | |
395 | else { | |
396 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
397 | $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null'; | |
398 | } | |
399 | ||
400 | $params['mapping_id'] = $values['entity'][0]; | |
401 | $entity_value = $values['entity'][1]; | |
402 | $entity_status = $values['entity'][2]; | |
403 | ||
404 | foreach (array( | |
405 | 'entity_value', 'entity_status') as $key) { | |
406 | $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key); | |
407 | } | |
408 | ||
409 | $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0); | |
410 | $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0); | |
411 | ||
412 | if (CRM_Utils_Array::value('is_repeat', $values) == 0) { | |
413 | $params['repetition_frequency_unit'] = 'null'; | |
414 | $params['repetition_frequency_interval'] = 'null'; | |
415 | $params['end_frequency_unit'] = 'null'; | |
416 | $params['end_frequency_interval'] = 'null'; | |
417 | $params['end_action'] = 'null'; | |
418 | $params['end_date'] = 'null'; | |
419 | } | |
420 | ||
421 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
422 | $params['id'] = $this->_id; | |
423 | } | |
424 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
425 | // we do this only once, so name never changes | |
426 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); | |
427 | } | |
428 | ||
429 | $composeFields = array( | |
430 | 'template', 'saveTemplate', | |
431 | 'updateTemplate', 'saveTemplateName', | |
432 | ); | |
433 | $msgTemplate = NULL; | |
434 | //mail template is composed | |
435 | ||
436 | $composeParams = array(); | |
437 | foreach ($composeFields as $key) { | |
438 | if (CRM_Utils_Array::value($key, $values)) { | |
439 | $composeParams[$key] = $values[$key]; | |
440 | } | |
441 | } | |
442 | ||
443 | if (CRM_Utils_Array::value('updateTemplate', $composeParams)) { | |
444 | $templateParams = array( | |
445 | 'msg_text' => $params['body_text'], | |
446 | 'msg_html' => $params['body_html'], | |
447 | 'msg_subject' => $params['subject'], | |
448 | 'is_active' => TRUE, | |
449 | ); | |
450 | ||
451 | $templateParams['id'] = $values['template']; | |
452 | ||
c6327d7d | 453 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
454 | } |
455 | ||
456 | if (CRM_Utils_Array::value('saveTemplate', $composeParams)) { | |
457 | $templateParams = array( | |
458 | 'msg_text' => $params['body_text'], | |
459 | 'msg_html' => $params['body_html'], | |
460 | 'msg_subject' => $params['subject'], | |
461 | 'is_active' => TRUE, | |
462 | ); | |
463 | ||
464 | $templateParams['msg_title'] = $composeParams['saveTemplateName']; | |
465 | ||
c6327d7d | 466 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
467 | } |
468 | ||
469 | if (isset($msgTemplate->id)) { | |
470 | $params['msg_template_id'] = $msgTemplate->id; | |
471 | } | |
472 | else { | |
473 | $params['msg_template_id'] = CRM_Utils_Array::value('template', $values); | |
474 | } | |
475 | ||
24f14405 E |
476 | $bao = CRM_Core_BAO_ActionSchedule::add($params); |
477 | // we need to set this on the form so that hooks can identify the created entity | |
478 | $this->set('id', $bao->id); | |
479 | $bao->free(); | |
6a488035 TO |
480 | |
481 | $status = ts("Your new Reminder titled %1 has been saved.", | |
482 | array(1 => "<strong>{$values['title']}</strong>") | |
483 | ); | |
484 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
485 | $status = ts("Your Reminder titled %1 has been updated.", | |
486 | array(1 => "<strong>{$values['title']}</strong>") | |
487 | ); | |
488 | ||
489 | if ($this->_context == 'event' && $this->_eventId) { | |
490 | $url = CRM_Utils_System::url('civicrm/event/manage/reminder', | |
491 | "reset=1&action=update&id={$this->_eventId}" | |
492 | ); | |
493 | $session = CRM_Core_Session::singleton(); | |
494 | $session->pushUserContext($url); | |
495 | } | |
496 | } | |
497 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
498 | } | |
499 | } | |
500 |