Merge pull request #787 from eileenmcnaughton/whitespacelimited
[civicrm-core.git] / CRM / Event / Form / ManageEvent / ScheduleReminders.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
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 );
61 if (is_array($reminderList)) {
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 }
71 $links = CRM_Admin_Page_ScheduleReminders::links();
72 $links[CRM_Core_Action::DELETE]['qs'] .= "&context=event&eventId={$this->_id}";
73 $links[CRM_Core_Action::UPDATE]['qs'] .= "&context=event&eventId={$this->_id}";
74 $format['action'] = CRM_Core_Action::formLink(
75 $links,
76 $action,
77 array('id' => $format['id'])
78 );
79 }
80 $this->assign('rows', $reminderList);
81 }
82 }
83 }
84
85 /**
86 * This function sets the default values for the form. For edit/view mode
87 * the default values are retrieved from the database
88 *
89 * @access public
90 *
91 * @return None
92 */
93 function setDefaultValues() {
94 $defaults = array();
95 $defaults['is_active'] = 1;
96 $defaults['record_activity'] = 1;
97 return $defaults;
98 }
99
100 /**
101 * Function to build the form
102 *
103 * @return None
104 * @access public
105 */
106 public function buildQuickForm() {
107 $field = 'civicrm_event';
108 if ($this->_isTemplate) {
109 $field = 'event_template';
110 }
111 $this->_mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
112 if (!$this->_mappingID) {
113 CRM_Core_Error::fatal('Could not find mapping for event scheduled reminders.');
114 }
115
116 parent::buildQuickForm();
117 $this->add('text', 'title', ts('Reminder Name'),
118 array(
119 'size' => 45, 'maxlength' => 128), TRUE
120 );
121
122 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($this->_mappingID);
123 extract($selectionOptions);
124
125 $entity = $this->add('select', 'entity', ts('Recipient(s)'), $sel3[$this->_mappingID][0], TRUE);
126 $entity->setMultiple(TRUE);
127
128 //get the frequency units.
129 $this->_freqUnits = array('hour' => 'hour') + CRM_Core_OptionGroup::values('recur_frequency_units');
130
131 $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30);
132
133 //reminder_interval
134 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
135
136 foreach ($this->_freqUnits as $val => $label) {
137 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
138 }
139
140 $this->addDate('absolute_date', ts('Start Date'), FALSE,
141 array('formatType' => 'mailing')
142 );
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 = 'event_contacts';
169 $this->add('select', 'recipient', ts('Additional Recipient(s)'), $sel5[$recipient],
170 FALSE, array('onClick' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
171 );
172 $recipientListing = $this->add('select', 'recipient_listing', ts('Recipient Listing'),
173 $sel3[$this->_mappingID][0]
174 );
175 $recipientListing->setMultiple(TRUE);
176
177 //autocomplete url
178 $dataUrl = CRM_Utils_System::url('civicrm/ajax/rest',
179 "className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&context=activity&reset=1",
180 FALSE, NULL, FALSE
181 );
182
183 $this->assign('dataUrl', $dataUrl);
184 //tokeninput url
185 $tokenUrl = CRM_Utils_System::url('civicrm/ajax/checkemail',
186 'noemail=1',
187 FALSE, NULL, FALSE
188 );
189 $this->assign('tokenUrl', $tokenUrl);
190 $this->add('text', 'recipient_manual_id', ts('Manual Recipients'));
191
192 $this->addElement('select', 'group_id', ts('Group'),
193 CRM_Core_PseudoConstant::staticGroup()
194 );
195
196 CRM_Mailing_BAO_Mailing::commonCompose($this);
197
198 $this->add('text', 'subject', ts('Subject'),
199 CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
200 );
201
202 $this->add('checkbox', 'is_active', ts('Send email'));
203
204 $this->addFormRule(array('CRM_Event_Form_ManageEvent_ScheduleReminders', 'formRule'));
205 }
206
207 /**
208 * global validation rules for the form
209 *
210 * @param array $fields posted values of the form
211 *
212 * @return array list of errors to be posted back to the form
213 * @static
214 * @access public
215 */
216 static function formRule($fields) {
217 $errors = array();
218 if (CRM_Utils_Array::value('is_active', $fields) &&
219 CRM_Utils_System::isNull($fields['subject'])
220 ) {
221 $errors['subject'] = ts('Subject is a required field.');
222 }
223
224 if (!CRM_Utils_System::isNull($fields['absolute_date'])) {
225 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('YmdHi00'))) {
226 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
227 }
228 }
229
230 if (!empty($errors)) {
231 return $errors;
232 }
233
234 return empty($errors) ? TRUE : $errors;
235 }
236
237 /**
238 * Function to process the form
239 *
240 * @access public
241 *
242 * @return None
243 */
244 public function postProcess() {
245 if ($this->_action & CRM_Core_Action::DELETE) {
246 // delete reminder
247 CRM_Core_BAO_ActionSchedule::del($this->_id);
248 CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
249 return;
250 }
251
252 $values = $this->controller->exportValues($this->getName());
253 $keys = array(
254 'title',
255 'subject',
256 'absolute_date',
257 'group_id',
258 'record_activity'
259 );
260 foreach ($keys as $key) {
261 $params[$key] = CRM_Utils_Array::value($key, $values);
262 }
263
264 $moreKeys = array(
265 'start_action_offset', 'start_action_unit',
266 'start_action_condition', 'start_action_date',
267 'repetition_frequency_unit',
268 'repetition_frequency_interval',
269 'end_frequency_unit',
270 'end_frequency_interval',
271 'end_action', 'end_date',
272 );
273
274 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) {
275 $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate);
276 foreach ($moreKeys as $mkey) {
277 $params[$mkey] = 'null';
278 }
279 }
280 else {
281 $params['absolute_date'] = 'null';
282 foreach ($moreKeys as $mkey) {
283 $params[$mkey] = CRM_Utils_Array::value($mkey, $values);
284 }
285 }
286
287 $params['body_text'] = CRM_Utils_Array::value('text_message', $values);
288 $params['body_html'] = CRM_Utils_Array::value('html_message', $values);
289
290 if (CRM_Utils_Array::value('recipient', $values) == 'manual') {
291 $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values);
292 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
293 }
294 elseif (CRM_Utils_Array::value('recipient', $values) == 'group') {
295 $params['group_id'] = $values['group_id'];
296 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
297 }
298 elseif (!CRM_Utils_System::isNull($values['recipient_listing'])) {
299 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
300 $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
301 CRM_Utils_Array::value('recipient_listing', $values)
302 );
303 $params['group_id'] = $params['recipient_manual'] = 'null';
304 }
305 else {
306 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
307 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
308 }
309
310 $params['mapping_id'] = $this->_mappingID;
311
312 $params['entity_value'] = $this->_id;
313 $params['entity_status'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values['entity']);
314 $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);
315 $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0);
316
317 if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
318 $params['repetition_frequency_unit'] = 'null';
319 $params['repetition_frequency_interval'] = 'null';
320 $params['end_frequency_unit'] = 'null';
321 $params['end_frequency_interval'] = 'null';
322 $params['end_action'] = 'null';
323 $params['end_date'] = 'null';
324 }
325 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
326
327 $composeFields = array(
328 'template', 'saveTemplate',
329 'updateTemplate', 'saveTemplateName',
330 );
331 $msgTemplate = NULL;
332 //mail template is composed
333 $composeParams = array();
334 foreach ($composeFields as $key) {
335 if (CRM_Utils_Array::value($key, $values)) {
336 $composeParams[$key] = $values[$key];
337 }
338 }
339
340 if (CRM_Utils_Array::value('updateTemplate', $composeParams)) {
341 $templateParams = array(
342 'msg_text' => $params['body_text'],
343 'msg_html' => $params['body_html'],
344 'msg_subject' => $params['subject'],
345 'is_active' => TRUE,
346 );
347
348 $templateParams['id'] = $values['template'];
349
350 $msgTemplate = CRM_Core_BAO_MessageTemplates::add($templateParams);
351 }
352
353 if (CRM_Utils_Array::value('saveTemplate', $composeParams)) {
354 $templateParams = array(
355 'msg_text' => $params['body_text'],
356 'msg_html' => $params['body_html'],
357 'msg_subject' => $params['subject'],
358 'is_active' => TRUE,
359 );
360
361 $templateParams['msg_title'] = $composeParams['saveTemplateName'];
362
363 $msgTemplate = CRM_Core_BAO_MessageTemplates::add($templateParams);
364 }
365
366 if (isset($msgTemplate->id)) {
367 $params['msg_template_id'] = $msgTemplate->id;
368 }
369 else {
370 $params['msg_template_id'] = CRM_Utils_Array::value('template', $values);
371 }
372
373 CRM_Core_BAO_ActionSchedule::add($params, $ids);
374
375 $status = ts("Your new Reminder titled %1 has been saved.",
376 array(1 => "<strong>{$values['title']}</strong>")
377 );
378
379 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
380
381 parent::endPostProcess();
382 }
383 //end of function
384
385 /**
386 * Return a descriptive name for the page, used in wizard header
387 *
388 * @return string
389 * @access public
390 */
391 public function getTitle() {
392 return ts('Event Schedule Reminder');
393 }
394 }
395