Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for scheduling reminders for Event | |
39 | * | |
40 | */ | |
41 | class CRM_Event_Form_ManageEvent_ScheduleReminders extends CRM_Event_Form_ManageEvent { | |
42 | ||
43 | /** | |
44 | * Function to set variables up before form is built | |
45 | * | |
46 | * @return void | |
47 | * @access public | |
48 | */ | |
49 | function preProcess() { | |
50 | parent::preProcess(); | |
51 | ||
52 | $newReminder = CRM_Utils_Request::retrieve('new', 'Boolean', $this, FALSE, FALSE); | |
53 | if ($this->_action & CRM_Core_Action::UPDATE && | |
54 | !$newReminder | |
55 | ) { | |
56 | $field = 'civicrm_event'; | |
57 | if ($this->_isTemplate) { | |
58 | $field = 'event_template'; | |
59 | } | |
60 | $reminderList = CRM_Core_BAO_ActionSchedule::getList(FALSE, $field, $this->_id ); | |
4b628e67 | 61 | if ($reminderList && is_array($reminderList)) { |
6a488035 TO |
62 | // Add action links to each of the reminders |
63 | foreach ($reminderList as & $format) { | |
64 | $action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE; | |
65 | if ($format['is_active']) { | |
66 | $action += CRM_Core_Action::DISABLE; | |
67 | } | |
68 | else { | |
69 | $action += CRM_Core_Action::ENABLE; | |
70 | } | |
f306fd82 | 71 | $scheduleReminder = new CRM_Admin_Page_ScheduleReminders; |
72 | $links = $scheduleReminder->links(); | |
6a488035 TO |
73 | $links[CRM_Core_Action::DELETE]['qs'] .= "&context=event&eventId={$this->_id}"; |
74 | $links[CRM_Core_Action::UPDATE]['qs'] .= "&context=event&eventId={$this->_id}"; | |
75 | $format['action'] = CRM_Core_Action::formLink( | |
76 | $links, | |
77 | $action, | |
d4aebf16 | 78 | array('id' => $format['id']), |
87dab4a4 AH |
79 | ts('more'), |
80 | FALSE, | |
81 | 'event.reminder.list', | |
82 | 'Event', | |
83 | $this->_id | |
6a488035 TO |
84 | ); |
85 | } | |
6a488035 | 86 | } |
4b628e67 CW |
87 | else { |
88 | $reminderList = TRUE; | |
89 | } | |
90 | $this->assign('rows', $reminderList); | |
5d92a7e7 CW |
91 | |
92 | // Update tab "disabled" css class | |
93 | $this->ajaxResponse['tabValid'] = !empty($reminderList) && is_array($reminderList); | |
6a488035 TO |
94 | } |
95 | } | |
96 | ||
97 | /** | |
98 | * This function sets the default values for the form. For edit/view mode | |
99 | * the default values are retrieved from the database | |
100 | * | |
101 | * @access public | |
102 | * | |
355ba699 | 103 | * @return void |
6a488035 TO |
104 | */ |
105 | function setDefaultValues() { | |
106 | $defaults = array(); | |
107 | $defaults['is_active'] = 1; | |
108 | $defaults['record_activity'] = 1; | |
109 | return $defaults; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Function to build the form | |
114 | * | |
355ba699 | 115 | * @return void |
6a488035 TO |
116 | * @access public |
117 | */ | |
118 | public function buildQuickForm() { | |
03e04002 | 119 | $field = 'civicrm_event'; |
6a488035 TO |
120 | if ($this->_isTemplate) { |
121 | $field = 'event_template'; | |
122 | } | |
123 | $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); | |
124 | if (!$this->_mappingID) { | |
125 | CRM_Core_Error::fatal('Could not find mapping for event scheduled reminders.'); | |
126 | } | |
127 | ||
128 | parent::buildQuickForm(); | |
129 | $this->add('text', 'title', ts('Reminder Name'), | |
130 | array( | |
f5a695dd | 131 | 'size' => 45, |
132 | 'maxlength' => 128 | |
133 | ), TRUE | |
6a488035 TO |
134 | ); |
135 | ||
136 | $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($this->_mappingID); | |
137 | extract($selectionOptions); | |
138 | ||
eb3b3957 | 139 | $this->assign('recipientMapping', json_encode($recipientMapping)); |
140 | ||
e1462487 CW |
141 | // Fixme: hack to adjust the output of CRM_Core_BAO_ActionSchedule::getSelection so it looks nice with the jQuery.select2 plugin |
142 | // TODO: fix this upstream | |
143 | $options = $sel3[$this->_mappingID][0]; | |
144 | $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]); | |
145 | unset($options[0]); | |
146 | $entity = $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes); | |
6a488035 TO |
147 | |
148 | //get the frequency units. | |
149 | $this->_freqUnits = array('hour' => 'hour') + CRM_Core_OptionGroup::values('recur_frequency_units'); | |
150 | ||
151 | $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30); | |
152 | ||
153 | //reminder_interval | |
154 | $this->add('select', 'start_action_offset', ts('When'), $numericOptions); | |
155 | ||
156 | foreach ($this->_freqUnits as $val => $label) { | |
157 | $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label)); | |
158 | } | |
159 | ||
160 | $this->addDate('absolute_date', ts('Start Date'), FALSE, | |
161 | array('formatType' => 'mailing') | |
162 | ); | |
163 | ||
164 | //reminder_frequency | |
165 | $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE); | |
166 | ||
167 | $condition = array('before' => ts('before'), | |
168 | 'after' => ts('after'), | |
169 | ); | |
170 | //reminder_action | |
171 | $this->add('select', 'start_action_condition', ts('Action Condition'), $condition); | |
172 | ||
173 | $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE); | |
174 | ||
175 | $this->addElement('checkbox', 'record_activity', ts('Record activity for automated email')); | |
176 | ||
177 | $this->addElement('checkbox', 'is_repeat', ts('Repeat'), | |
178 | NULL, array('onclick' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);") | |
179 | ); | |
180 | ||
181 | $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay); | |
182 | $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions); | |
183 | $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay); | |
184 | $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions); | |
185 | $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE); | |
186 | $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE); | |
187 | ||
188 | $recipient = 'event_contacts'; | |
f5a695dd | 189 | $limitOptions = array(1 => ts('Limit to'), 0 => ts('Addition to')); |
190 | $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions); | |
191 | ||
192 | $this->add('select', 'recipient', ts('Recipients'), $sel5[$recipient], | |
e1462487 | 193 | FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);") |
6a488035 TO |
194 | ); |
195 | $recipientListing = $this->add('select', 'recipient_listing', ts('Recipient Listing'), | |
e1462487 | 196 | $sel3[$this->_mappingID][0], FALSE, array('class' => 'crm-select2 huge') |
6a488035 TO |
197 | ); |
198 | $recipientListing->setMultiple(TRUE); | |
199 | ||
3366e07e | 200 | $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => true)); |
6a488035 | 201 | |
e1462487 CW |
202 | $this->add('select', 'group_id', ts('Group'), |
203 | CRM_Core_PseudoConstant::staticGroup(), FALSE, array('class' => 'crm-select2 huge') | |
6a488035 TO |
204 | ); |
205 | ||
206 | CRM_Mailing_BAO_Mailing::commonCompose($this); | |
207 | ||
208 | $this->add('text', 'subject', ts('Subject'), | |
209 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject') | |
210 | ); | |
211 | ||
212 | $this->add('checkbox', 'is_active', ts('Send email')); | |
213 | ||
214 | $this->addFormRule(array('CRM_Event_Form_ManageEvent_ScheduleReminders', 'formRule')); | |
215 | } | |
216 | ||
217 | /** | |
218 | * global validation rules for the form | |
219 | * | |
220 | * @param array $fields posted values of the form | |
221 | * | |
222 | * @return array list of errors to be posted back to the form | |
223 | * @static | |
224 | * @access public | |
225 | */ | |
226 | static function formRule($fields) { | |
227 | $errors = array(); | |
a7488080 | 228 | if (!empty($fields['is_active']) && |
6a488035 TO |
229 | CRM_Utils_System::isNull($fields['subject']) |
230 | ) { | |
231 | $errors['subject'] = ts('Subject is a required field.'); | |
232 | } | |
233 | ||
234 | if (!CRM_Utils_System::isNull($fields['absolute_date'])) { | |
235 | if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('YmdHi00'))) { | |
236 | $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.'); | |
237 | } | |
238 | } | |
239 | ||
240 | if (!empty($errors)) { | |
241 | return $errors; | |
242 | } | |
243 | ||
244 | return empty($errors) ? TRUE : $errors; | |
245 | } | |
246 | ||
247 | /** | |
248 | * Function to process the form | |
249 | * | |
250 | * @access public | |
251 | * | |
355ba699 | 252 | * @return void |
6a488035 TO |
253 | */ |
254 | public function postProcess() { | |
255 | if ($this->_action & CRM_Core_Action::DELETE) { | |
256 | // delete reminder | |
257 | CRM_Core_BAO_ActionSchedule::del($this->_id); | |
258 | CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success'); | |
259 | return; | |
260 | } | |
261 | ||
262 | $values = $this->controller->exportValues($this->getName()); | |
263 | $keys = array( | |
264 | 'title', | |
265 | 'subject', | |
266 | 'absolute_date', | |
267 | 'group_id', | |
f5a695dd | 268 | 'record_activity', |
269 | 'limit_to' | |
6a488035 TO |
270 | ); |
271 | foreach ($keys as $key) { | |
272 | $params[$key] = CRM_Utils_Array::value($key, $values); | |
273 | } | |
274 | ||
275 | $moreKeys = array( | |
f5a695dd | 276 | 'start_action_offset', |
277 | 'start_action_unit', | |
278 | 'start_action_condition', | |
279 | 'start_action_date', | |
6a488035 TO |
280 | 'repetition_frequency_unit', |
281 | 'repetition_frequency_interval', | |
282 | 'end_frequency_unit', | |
283 | 'end_frequency_interval', | |
f5a695dd | 284 | 'end_action', |
285 | 'end_date', | |
6a488035 TO |
286 | ); |
287 | ||
288 | if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) { | |
289 | $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate); | |
290 | foreach ($moreKeys as $mkey) { | |
291 | $params[$mkey] = 'null'; | |
292 | } | |
293 | } | |
294 | else { | |
295 | $params['absolute_date'] = 'null'; | |
296 | foreach ($moreKeys as $mkey) { | |
297 | $params[$mkey] = CRM_Utils_Array::value($mkey, $values); | |
298 | } | |
299 | } | |
300 | ||
301 | $params['body_text'] = CRM_Utils_Array::value('text_message', $values); | |
302 | $params['body_html'] = CRM_Utils_Array::value('html_message', $values); | |
303 | ||
304 | if (CRM_Utils_Array::value('recipient', $values) == 'manual') { | |
305 | $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values); | |
306 | $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
307 | } | |
308 | elseif (CRM_Utils_Array::value('recipient', $values) == 'group') { | |
309 | $params['group_id'] = $values['group_id']; | |
310 | $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null'; | |
311 | } | |
312 | elseif (!CRM_Utils_System::isNull($values['recipient_listing'])) { | |
313 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
314 | $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
315 | CRM_Utils_Array::value('recipient_listing', $values) | |
316 | ); | |
317 | $params['group_id'] = $params['recipient_manual'] = 'null'; | |
318 | } | |
319 | else { | |
320 | $params['recipient'] = CRM_Utils_Array::value('recipient', $values); | |
321 | $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null'; | |
322 | } | |
323 | ||
324 | $params['mapping_id'] = $this->_mappingID; | |
325 | ||
326 | $params['entity_value'] = $this->_id; | |
327 | $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']); | |
328 | $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0); | |
329 | $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0); | |
330 | ||
331 | if (CRM_Utils_Array::value('is_repeat', $values) == 0) { | |
332 | $params['repetition_frequency_unit'] = 'null'; | |
333 | $params['repetition_frequency_interval'] = 'null'; | |
334 | $params['end_frequency_unit'] = 'null'; | |
335 | $params['end_frequency_interval'] = 'null'; | |
336 | $params['end_action'] = 'null'; | |
337 | $params['end_date'] = 'null'; | |
338 | } | |
339 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); | |
340 | ||
341 | $composeFields = array( | |
342 | 'template', 'saveTemplate', | |
343 | 'updateTemplate', 'saveTemplateName', | |
344 | ); | |
345 | $msgTemplate = NULL; | |
346 | //mail template is composed | |
347 | $composeParams = array(); | |
348 | foreach ($composeFields as $key) { | |
a7488080 | 349 | if (!empty($values[$key])) { |
6a488035 TO |
350 | $composeParams[$key] = $values[$key]; |
351 | } | |
352 | } | |
353 | ||
a7488080 | 354 | if (!empty($composeParams['updateTemplate'])) { |
6a488035 TO |
355 | $templateParams = array( |
356 | 'msg_text' => $params['body_text'], | |
357 | 'msg_html' => $params['body_html'], | |
358 | 'msg_subject' => $params['subject'], | |
359 | 'is_active' => TRUE, | |
360 | ); | |
361 | ||
362 | $templateParams['id'] = $values['template']; | |
363 | ||
c6327d7d | 364 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
365 | } |
366 | ||
a7488080 | 367 | if (!empty($composeParams['saveTemplate'])) { |
6a488035 TO |
368 | $templateParams = array( |
369 | 'msg_text' => $params['body_text'], | |
370 | 'msg_html' => $params['body_html'], | |
371 | 'msg_subject' => $params['subject'], | |
372 | 'is_active' => TRUE, | |
373 | ); | |
374 | ||
375 | $templateParams['msg_title'] = $composeParams['saveTemplateName']; | |
376 | ||
c6327d7d | 377 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
378 | } |
379 | ||
380 | if (isset($msgTemplate->id)) { | |
381 | $params['msg_template_id'] = $msgTemplate->id; | |
382 | } | |
383 | else { | |
384 | $params['msg_template_id'] = CRM_Utils_Array::value('template', $values); | |
385 | } | |
386 | ||
f306fd82 | 387 | CRM_Core_BAO_ActionSchedule::add($params); |
6a488035 TO |
388 | |
389 | $status = ts("Your new Reminder titled %1 has been saved.", | |
390 | array(1 => "<strong>{$values['title']}</strong>") | |
391 | ); | |
392 | ||
393 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
394 | ||
395 | parent::endPostProcess(); | |
396 | } | |
397 | //end of function | |
398 | ||
399 | /** | |
400 | * Return a descriptive name for the page, used in wizard header | |
401 | * | |
402 | * @return string | |
403 | * @access public | |
404 | */ | |
405 | public function getTitle() { | |
406 | return ts('Event Schedule Reminder'); | |
407 | } | |
408 | } | |
409 |