CRM-12882 - Allow all admins to view payment processors page. Clarify error message...
[civicrm-core.git] / CRM / Admin / Form / ScheduleReminders.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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
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
39 *
40 */
41class 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 *
355ba699 53 * @return void
6a488035
TO
54 * @access public
55 */
56 public function buildQuickForm() {
57 parent::buildQuickForm();
58 $this->_mappingID = $mappingID = NULL;
a3e3eea1 59 $providersCount = CRM_SMS_BAO_Provider::activeProviderCount();
6a488035
TO
60
61 if ($this->_action & (CRM_Core_Action::DELETE)) {
62 $reminderName =
63 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'title');
64 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
65 if ($this->_context == 'event') {
66 $this->_eventId = CRM_Utils_Request::retrieve('eventId', 'Integer', $this);
67 }
68 $this->assign('reminderName', $reminderName);
69 return;
70 }
71 elseif ($this->_action & (CRM_Core_Action::UPDATE)) {
72 $this->_mappingID =
73 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id, 'mapping_id');
74 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
75 if ($this->_context == 'event') {
76 $this->_eventId = CRM_Utils_Request::retrieve('eventId', 'Integer', $this);
77 }
78 }
79
8cc574cf 80 if (!empty($_POST) && !empty($_POST['entity'])) {
6a488035
TO
81 $mappingID = $_POST['entity'][0];
82 }
83 elseif ($this->_mappingID) {
84 $mappingID = $this->_mappingID;
85 }
86
87 $this->add(
88 'text',
89 'title',
90 ts('Title'),
91 array('size' => 45, 'maxlength' => 128),
92 TRUE
93 );
94
95 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection($mappingID);
96 extract($selectionOptions);
97
98 if (empty($sel1)) {
99 CRM_Core_Error::fatal('Could not find mapping for scheduled reminders.');
100 }
101 $this->assign('entityMapping', json_encode($entityMapping));
102 $this->assign('recipientMapping', json_encode($recipientMapping));
103
3e315abc 104 $sel = & $this->add(
6a488035
TO
105 'hierselect',
106 'entity',
107 ts('Entity'),
108 array(
109 'name' => 'entity[0]',
110 'style' => 'vertical-align: top;',
111 ),
112 TRUE
113 );
114 $sel->setOptions(array($sel1, $sel2, $sel3));
115
116 if (is_a($sel->_elements[1], 'HTML_QuickForm_select')) {
117 // make second selector a multi-select -
118 $sel->_elements[1]->setMultiple(TRUE);
119 $sel->_elements[1]->setSize(5);
120 }
121
122 if (is_a($sel->_elements[2], 'HTML_QuickForm_select')) {
123 // make third selector a multi-select -
124 $sel->_elements[2]->setMultiple(TRUE);
125 $sel->_elements[2]->setSize(5);
126 }
127
128 //get the frequency units.
9775f926 129 $this->_freqUnits = CRM_Core_SelectValues::getScheduleReminderFrequencyUnits();
6a488035
TO
130
131 //pass the mapping ID in UPDATE mode
132 $mappings = CRM_Core_BAO_ActionSchedule::getMapping($mappingID);
133
134 $numericOptions = CRM_Core_SelectValues::getNumericOptions(0, 30);
135
136 //reminder_interval
137 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
a3e3eea1 138 $title = ts('Email');
139 $isActive = ts('Send email');
140 $recordActivity = ts('Record activity for automated email');
141 if ($providersCount) {
7b007e61 142 $this->assign('sms', $providersCount);
a3e3eea1 143 $title = ts('Email or SMS');
144 $isActive = ts('Send email or SMS');
145 $recordActivity = ts('Record activity for automated email or SMS');
146 $options = CRM_Core_OptionGroup::values('msg_mode');
147 $this->add('select', 'mode', ts('Send as'), $options);
148
149 $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc');
150
151 $providerSelect = array();
152 foreach ($providers as $provider) {
153 $providerSelect[$provider['id']] = $provider['title'];
154 }
155 $this->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE);
156 }
6a488035 157
a3e3eea1 158 $this->assign('title', $title);
6a488035
TO
159 foreach ($this->_freqUnits as $val => $label) {
160 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
161 }
162
163 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
164
165 //reminder_frequency
166 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
167
168 $condition = array('before' => ts('before'),
169 'after' => ts('after'),
170 );
171 //reminder_action
172 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
173
174 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
175
a3e3eea1 176 $this->addElement('checkbox', 'record_activity', $recordActivity);
6a488035
TO
177
178 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
179 NULL, array('onclick' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
180 );
181
182 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
183 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
184 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
185 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
186 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
187 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
188
189 $recipient = 'activity_contacts';
190 $recipientListingOptions = array();
191
192 if ($mappingID) {
193 $recipient = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping',
194 $mappingID,
195 'entity_recipient'
196 );
197 }
198
3e315abc 199 $limitOptions = array(1 => ts('Limit to'), 0 => ts('Addition to'));
200 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions);
8ef12e64 201
3e315abc 202 $this->add('select', 'recipient', ts('Recipients'), $sel5[$recipient],
6a488035
TO
203 FALSE, array('onClick' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
204 );
205
a7488080 206 if (!empty($_POST['is_recipient_listing'])) {
6a488035
TO
207 $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
208 }
a7488080 209 elseif (!empty($this->_values['recipient_listing'])) {
6a488035
TO
210 $recipientListingOptions = CRM_Core_BAO_ActionSchedule::getRecipientListing($this->_values['mapping_id'], $this->_values['recipient']);
211 }
212 $recipientListing = $this->add('select', 'recipient_listing', ts('Recipient Listing'), $recipientListingOptions);
213 $recipientListing->setMultiple(TRUE);
214 $this->add('hidden', 'is_recipient_listing', empty($recipientListingOptions) ? FALSE : TRUE, array('id' => 'is_recipient_listing'));
215
92f4a0b1 216 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
6a488035 217
f10c69fa
DL
218 $this->addElement(
219 'select',
220 'group_id',
221 ts('Group'),
222 // CRM-13577
223 CRM_Core_PseudoConstant::group()
6a488035
TO
224 );
225
226 CRM_Mailing_BAO_Mailing::commonCompose($this);
227
228 $this->add('text', 'subject', ts('Subject'),
229 CRM_Core_DAO::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
230 );
231
a3e3eea1 232 $this->add('checkbox', 'is_active', $isActive);
6a488035
TO
233
234 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'));
235 }
236 /**
237 * global form rule
238 *
239 * @param array $fields the input form values
240 *
241 * @return true if no errors, else array of errors
242 * @access public
243 * @static
244 */
245 static function formRule($fields) {
246 $errors = array();
247 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] == 0) ||
248 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
249 ) {
250 $errors['entity'] = ts('Please select appropriate value');
251 }
252
a7488080 253 if (!empty($fields['is_active']) &&
6a488035
TO
254 CRM_Utils_System::isNull($fields['subject'])
255 ) {
256 $errors['subject'] = ts('Subject is a required field.');
257 }
258
259 if (CRM_Utils_System::isNull(CRM_Utils_Array::value(1, $fields['entity']))) {
260 $errors['entity'] = ts('Please select entity value');
261 }
262
263 if (!CRM_Utils_System::isNull($fields['absolute_date'])) {
2ea4f315 264 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date::format(date('Ymd'))) {
6a488035
TO
265 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
266 }
267 }
268
269 if (!empty($errors)) {
270 return $errors;
271 }
272
273 return empty($errors) ? TRUE : $errors;
274 }
275
276 function setDefaultValues() {
277 if ($this->_action & CRM_Core_Action::ADD) {
278 $defaults['is_active'] = 1;
a3e3eea1 279 $defaults['mode'] = 'Email';
6a488035
TO
280 $defaults['record_activity'] = 1;
281 }
282 else {
283 $defaults = $this->_values;
284 $entityValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
285 CRM_Utils_Array::value('entity_value', $defaults)
286 );
287 $entityStatus = explode(CRM_Core_DAO::VALUE_SEPARATOR,
288 CRM_Utils_Array::value('entity_status', $defaults)
289 );
290 $defaults['entity'][0] = CRM_Utils_Array::value('mapping_id', $defaults);
291 $defaults['entity'][1] = $entityValue;
292 $defaults['entity'][2] = $entityStatus;
293 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $defaults)) {
294 list($date, $time) = CRM_Utils_Date::setDateDefaults($absoluteDate);
295 $defaults['absolute_date'] = $date;
296 }
297
298 if ($recipientListing = CRM_Utils_Array::value('recipient_listing', $defaults)) {
299 $defaults['recipient_listing'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
300 $recipientListing
301 );
302 }
303 $defaults['text_message'] = CRM_Utils_Array::value('body_text', $defaults);
304 $defaults['html_message'] = CRM_Utils_Array::value('body_html', $defaults);
305 $defaults['template'] = CRM_Utils_Array::value('msg_template_id', $defaults);
a7488080 306 if (!empty($defaults['group_id'])) {
6a488035
TO
307 $defaults['recipient'] = 'group';
308 }
a7488080 309 elseif (!empty($defaults['recipient_manual'])) {
6a488035 310 $defaults['recipient'] = 'manual';
92f4a0b1 311 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
6a488035
TO
312 }
313 }
314
315 return $defaults;
316 }
317
318 /**
319 * Function to process the form
320 *
321 * @access public
322 *
355ba699 323 * @return void
6a488035
TO
324 */
325 public function postProcess() {
326 if ($this->_action & CRM_Core_Action::DELETE) {
327 // delete reminder
328 CRM_Core_BAO_ActionSchedule::del($this->_id);
329 CRM_Core_Session::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
330 if ($this->_context == 'event' && $this->_eventId) {
331 $url = CRM_Utils_System::url('civicrm/event/manage/reminder',
332 "reset=1&action=update&id={$this->_eventId}"
333 );
334 $session = CRM_Core_Session::singleton();
335 $session->pushUserContext($url);
336 }
337 return;
338 }
339 $values = $this->controller->exportValues($this->getName());
340
341 $keys = array(
342 'title',
343 'subject',
344 'absolute_date',
345 'group_id',
3e315abc 346 'record_activity',
a3e3eea1 347 'limit_to',
348 'mode',
349 'sms_provider_id'
6a488035
TO
350 );
351 foreach ($keys as $key) {
352 $params[$key] = CRM_Utils_Array::value($key, $values);
353 }
354
355 $moreKeys = array(
3e315abc 356 'start_action_offset',
357 'start_action_unit',
358 'start_action_condition',
359 'start_action_date',
6a488035
TO
360 'repetition_frequency_unit',
361 'repetition_frequency_interval',
362 'end_frequency_unit',
363 'end_frequency_interval',
3e315abc 364 'end_action',
365 'end_date',
6a488035
TO
366 );
367
368 if ($absoluteDate = CRM_Utils_Array::value('absolute_date', $params)) {
369 $params['absolute_date'] = CRM_Utils_Date::processDate($absoluteDate);
370 foreach ($moreKeys as $mkey) {
371 $params[$mkey] = 'null';
372 }
373 }
374 else {
375 $params['absolute_date'] = 'null';
376 foreach ($moreKeys as $mkey) {
377 $params[$mkey] = CRM_Utils_Array::value($mkey, $values);
378 }
379 }
380
381 $params['body_text'] = CRM_Utils_Array::value('text_message', $values);
382 $params['body_html'] = CRM_Utils_Array::value('html_message', $values);
383
384 if (CRM_Utils_Array::value('recipient', $values) == 'manual') {
385 $params['recipient_manual'] = CRM_Utils_Array::value('recipient_manual_id', $values);
386 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
387 }
388 elseif (CRM_Utils_Array::value('recipient', $values) == 'group') {
389 $params['group_id'] = $values['group_id'];
390 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
391 }
392 elseif (!CRM_Utils_System::isNull($values['recipient_listing'])) {
393 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
394 $params['recipient_listing'] = implode(CRM_Core_DAO::VALUE_SEPARATOR,
395 CRM_Utils_Array::value('recipient_listing', $values)
396 );
397 $params['group_id'] = $params['recipient_manual'] = 'null';
398 }
399 else {
400 $params['recipient'] = CRM_Utils_Array::value('recipient', $values);
401 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
402 }
403
404 $params['mapping_id'] = $values['entity'][0];
405 $entity_value = $values['entity'][1];
406 $entity_status = $values['entity'][2];
407
408 foreach (array(
409 'entity_value', 'entity_status') as $key) {
410 $params[$key] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $$key);
411 }
412
413 $params['is_active'] = CRM_Utils_Array::value('is_active', $values, 0);
414 $params['is_repeat'] = CRM_Utils_Array::value('is_repeat', $values, 0);
415
416 if (CRM_Utils_Array::value('is_repeat', $values) == 0) {
417 $params['repetition_frequency_unit'] = 'null';
418 $params['repetition_frequency_interval'] = 'null';
419 $params['end_frequency_unit'] = 'null';
420 $params['end_frequency_interval'] = 'null';
421 $params['end_action'] = 'null';
422 $params['end_date'] = 'null';
423 }
424
425 if ($this->_action & CRM_Core_Action::UPDATE) {
426 $params['id'] = $this->_id;
427 }
428 elseif ($this->_action & CRM_Core_Action::ADD) {
429 // we do this only once, so name never changes
430 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
431 }
432
433 $composeFields = array(
434 'template', 'saveTemplate',
435 'updateTemplate', 'saveTemplateName',
436 );
437 $msgTemplate = NULL;
438 //mail template is composed
439
440 $composeParams = array();
441 foreach ($composeFields as $key) {
a7488080 442 if (!empty($values[$key])) {
6a488035
TO
443 $composeParams[$key] = $values[$key];
444 }
445 }
446
a7488080 447 if (!empty($composeParams['updateTemplate'])) {
6a488035
TO
448 $templateParams = array(
449 'msg_text' => $params['body_text'],
450 'msg_html' => $params['body_html'],
451 'msg_subject' => $params['subject'],
452 'is_active' => TRUE,
453 );
454
455 $templateParams['id'] = $values['template'];
456
c6327d7d 457 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
6a488035
TO
458 }
459
a7488080 460 if (!empty($composeParams['saveTemplate'])) {
6a488035
TO
461 $templateParams = array(
462 'msg_text' => $params['body_text'],
463 'msg_html' => $params['body_html'],
464 'msg_subject' => $params['subject'],
465 'is_active' => TRUE,
466 );
467
468 $templateParams['msg_title'] = $composeParams['saveTemplateName'];
469
c6327d7d 470 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
6a488035
TO
471 }
472
473 if (isset($msgTemplate->id)) {
474 $params['msg_template_id'] = $msgTemplate->id;
475 }
476 else {
477 $params['msg_template_id'] = CRM_Utils_Array::value('template', $values);
478 }
479
24f14405
E
480 $bao = CRM_Core_BAO_ActionSchedule::add($params);
481 // we need to set this on the form so that hooks can identify the created entity
482 $this->set('id', $bao->id);
483 $bao->free();
6a488035
TO
484
485 $status = ts("Your new Reminder titled %1 has been saved.",
486 array(1 => "<strong>{$values['title']}</strong>")
487 );
488 if ($this->_action & CRM_Core_Action::UPDATE) {
489 $status = ts("Your Reminder titled %1 has been updated.",
490 array(1 => "<strong>{$values['title']}</strong>")
491 );
492
493 if ($this->_context == 'event' && $this->_eventId) {
494 $url = CRM_Utils_System::url('civicrm/event/manage/reminder',
495 "reset=1&action=update&id={$this->_eventId}"
496 );
497 $session = CRM_Core_Session::singleton();
498 $session->pushUserContext($url);
499 }
500 }
501 CRM_Core_Session::setStatus($status, ts('Saved'), 'success');
502 }
503}
504