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