3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2015
38 * This class generates form components for Scheduling Reminders
41 class CRM_Admin_Form_ScheduleReminders
extends CRM_Admin_Form
{
44 * Scheduled Reminder ID.
46 protected $_id = NULL;
51 * Build the form object.
55 public function buildQuickForm() {
56 parent
::buildQuickForm();
57 $this->_mappingID
= $mappingID = NULL;
58 $providersCount = CRM_SMS_BAO_Provider
::activeProviderCount();
59 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
61 //CRM-16777: Don't provide access to administer schedule reminder page, with user that does not have 'administer CiviCRM' permission
62 if (empty($this->_context
) && !CRM_Core_Permission
::check('administer CiviCRM')) {
63 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
65 //CRM-16777: When user have ACLs 'edit' permission for specific event, do not give access to add, delete & updtae
66 //schedule reminder for other events.
68 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
69 if (!CRM_Event_BAO_Event
::checkPermission($this->_compId
, CRM_Core_Permission
::EDIT
)) {
70 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
74 if ($this->_action
& (CRM_Core_Action
::DELETE
)) {
75 $reminderName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'title');
76 if ($this->_context
== 'event') {
77 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
79 $this->assign('reminderName', $reminderName);
82 elseif ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
83 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'mapping_id');
84 if ($this->_context
== 'event') {
85 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
88 elseif (!empty($this->_context
)) {
89 if ($this->_context
== 'event') {
90 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
91 $field = 'civicrm_event';
92 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_compId
, 'is_template');
94 $field = 'event_template';
96 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
97 if (!$this->_mappingID
) {
98 CRM_Core_Error
::fatal('Could not find mapping for event scheduled reminders.');
103 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context
)) {
104 $mappingID = $_POST['entity'][0];
106 elseif ($this->_mappingID
) {
107 $mappingID = $this->_mappingID
;
114 array('size' => 45, 'maxlength' => 128),
118 $selectionOptions = CRM_Core_BAO_ActionSchedule
::getSelection($mappingID);
119 extract($selectionOptions);
120 $this->assign('entityMapping', json_encode($entityMapping));
121 $this->assign('recipientMapping', json_encode($recipientMapping));
123 if (empty($this->_context
)) {
125 CRM_Core_Error
::fatal('Could not find mapping for scheduled reminders.');
133 'name' => 'entity[0]',
134 'style' => 'vertical-align: top;',
137 $sel->setOptions(array($sel1, $sel2, $sel3));
139 if (is_a($sel->_elements
[1], 'HTML_QuickForm_select')) {
140 // make second selector a multi-select -
141 $sel->_elements
[1]->setMultiple(TRUE);
142 $sel->_elements
[1]->setSize(5);
145 if (is_a($sel->_elements
[2], 'HTML_QuickForm_select')) {
146 // make third selector a multi-select -
147 $sel->_elements
[2]->setMultiple(TRUE);
148 $sel->_elements
[2]->setSize(5);
152 $options = $sel3[$this->_mappingID
][0];
153 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
155 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
156 $this->assign('context', $this->_context
);
159 //get the frequency units.
160 $this->_freqUnits
= CRM_Core_SelectValues
::getRecurringFrequencyUnits();
162 //pass the mapping ID in UPDATE mode
163 $mappings = CRM_Core_BAO_ActionSchedule
::getMapping($mappingID);
165 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(0, 30);
168 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
169 $isActive = ts('Send email');
170 $recordActivity = ts('Record activity for automated email');
171 if ($providersCount) {
172 $this->assign('sms', $providersCount);
173 $isActive = ts('Send email or SMS');
174 $recordActivity = ts('Record activity for automated email or SMS');
175 $options = CRM_Core_OptionGroup
::values('msg_mode');
176 $this->add('select', 'mode', ts('Send as'), $options);
178 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
180 $providerSelect = array();
181 foreach ($providers as $provider) {
182 $providerSelect[$provider['id']] = $provider['title'];
184 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
187 foreach ($this->_freqUnits
as $val => $label) {
188 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
191 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
194 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
197 'before' => ts('before'),
198 'after' => ts('after'),
201 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
203 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
205 $this->addElement('checkbox', 'record_activity', $recordActivity);
207 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
208 NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
211 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
212 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
213 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
214 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
215 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
216 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
218 $this->add('text', 'from_name', ts('From Name'));
219 $this->add('text', 'from_email', ts('From Email'));
221 $recipient = 'activity_contacts';
222 $recipientListingOptions = array();
225 $recipient = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping',
231 $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include'));
233 $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients'));
234 $this->assign('recipientLabels', $recipientLabels);
236 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);"));
238 $this->add('select', 'recipient', $recipientLabels['other'], $sel5[$recipient],
239 FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
242 if (!empty($this->_submitValues
['recipient_listing'])) {
243 if (!empty($this->_context
)) {
244 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_mappingID
, $this->_submitValues
['recipient']);
247 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
250 elseif (!empty($this->_values
['recipient_listing'])) {
251 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_values
['mapping_id'], $this->_values
['recipient']);
254 $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE,
255 array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE));
257 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
259 $this->add('select', 'group_id', ts('Group'),
260 CRM_Core_PseudoConstant
::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge')
263 CRM_Mailing_BAO_Mailing
::commonCompose($this);
265 $this->add('text', 'subject', ts('Subject'),
266 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
269 $this->add('checkbox', 'is_active', $isActive);
271 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
273 $this->setPageTitle(ts('Scheduled Reminder'));
279 * @param array $fields
280 * The input form values.
283 * true if no errors, else array of errors
285 public static function formRule($fields, $files, $self) {
287 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
288 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
290 $errors['entity'] = ts('Please select appropriate value');
293 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
294 if (count($fields['entity'][1]) > 1) {
295 $errors['entity'] = ts('You may only select one contact field per reminder');
297 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
298 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
302 if (!empty($fields['is_active']) &&
303 CRM_Utils_Array
::value('mode', $fields) == 'SMS' &&
304 CRM_Utils_System
::isNull($fields['subject'])
306 $errors['subject'] = ts('Subject is a required field.');
309 if (empty($self->_context
) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value(1, $fields['entity']))) {
310 $errors['entity'] = ts('Please select entity value');
313 if (!CRM_Utils_System
::isNull($fields['absolute_date'])) {
314 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date
::format(date('Ymd'))) {
315 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
319 $recipientKind = array(
320 'participant_role' => array(
321 'name' => 'participant role',
322 'target_id' => 'recipient_listing',
325 'name' => 'recipient',
326 'target_id' => 'recipient_manual_id',
329 if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
330 $errors[$recipientKind[$fields['recipient']]['target_id']] = ts('If "Also include" or "Limit to" are selected, you must specify at least one %1', array(1 => $recipientKind[$fields['recipient']]['name']));
333 if (!empty($errors)) {
337 return empty($errors) ?
TRUE : $errors;
343 public function setDefaultValues() {
344 if ($this->_action
& CRM_Core_Action
::ADD
) {
345 $defaults['is_active'] = 1;
346 $defaults['mode'] = 'Email';
347 $defaults['record_activity'] = 1;
350 $defaults = $this->_values
;
351 $entityValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_value', $defaults));
352 $entityStatus = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_status', $defaults));
353 if (empty($this->_context
)) {
354 $defaults['entity'][0] = CRM_Utils_Array
::value('mapping_id', $defaults);
355 $defaults['entity'][1] = $entityValue;
356 $defaults['entity'][2] = $entityStatus;
359 $defaults['entity'] = $entityStatus;
361 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $defaults)) {
362 list($date, $time) = CRM_Utils_Date
::setDateDefaults($absoluteDate);
363 $defaults['absolute_date'] = $date;
366 if ($recipientListing = CRM_Utils_Array
::value('recipient_listing', $defaults)) {
367 $defaults['recipient_listing'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
371 $defaults['text_message'] = CRM_Utils_Array
::value('body_text', $defaults);
372 $defaults['html_message'] = CRM_Utils_Array
::value('body_html', $defaults);
373 $defaults['sms_text_message'] = CRM_Utils_Array
::value('sms_body_text', $defaults);
374 $defaults['template'] = CRM_Utils_Array
::value('msg_template_id', $defaults);
375 $defaults['SMStemplate'] = CRM_Utils_Array
::value('sms_template_id', $defaults);
376 if (!empty($defaults['group_id'])) {
377 $defaults['recipient'] = 'group';
379 elseif (!empty($defaults['recipient_manual'])) {
380 $defaults['recipient'] = 'manual';
381 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
389 * Process the form submission.
394 public function postProcess() {
395 if ($this->_action
& CRM_Core_Action
::DELETE
) {
397 CRM_Core_BAO_ActionSchedule
::del($this->_id
);
398 CRM_Core_Session
::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
399 if ($this->_context
== 'event' && $this->_compId
) {
400 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
401 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
403 $session = CRM_Core_Session
::singleton();
404 $session->pushUserContext($url);
408 $values = $this->controller
->exportValues($this->getName());
422 foreach ($keys as $key) {
423 $params[$key] = CRM_Utils_Array
::value($key, $values);
426 $params['is_repeat'] = CRM_Utils_Array
::value('is_repeat', $values, 0);
429 'start_action_offset',
431 'start_action_condition',
433 'repetition_frequency_unit',
434 'repetition_frequency_interval',
435 'end_frequency_unit',
436 'end_frequency_interval',
441 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $params)) {
442 $params['absolute_date'] = CRM_Utils_Date
::processDate($absoluteDate);
443 $params['is_repeat'] = 0;
444 foreach ($moreKeys as $mkey) {
445 $params[$mkey] = 'null';
449 $params['absolute_date'] = 'null';
450 foreach ($moreKeys as $mkey) {
451 $params[$mkey] = CRM_Utils_Array
::value($mkey, $values);
455 $params['body_text'] = CRM_Utils_Array
::value('text_message', $values);
456 $params['sms_body_text'] = CRM_Utils_Array
::value('sms_text_message', $values);
457 $params['body_html'] = CRM_Utils_Array
::value('html_message', $values);
459 if (CRM_Utils_Array
::value('recipient', $values) == 'manual') {
460 $params['recipient_manual'] = CRM_Utils_Array
::value('recipient_manual_id', $values);
461 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
463 elseif (CRM_Utils_Array
::value('recipient', $values) == 'group') {
464 $params['group_id'] = $values['group_id'];
465 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
467 elseif (!CRM_Utils_System
::isNull($values['recipient_listing']) && !CRM_Utils_System
::isNull($values['limit_to'])) {
468 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
469 $params['recipient_listing'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
470 CRM_Utils_Array
::value('recipient_listing', $values)
472 $params['group_id'] = $params['recipient_manual'] = 'null';
475 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
476 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
479 if (!empty($this->_mappingID
) && !empty($this->_compId
)) {
480 $params['mapping_id'] = $this->_mappingID
;
481 $params['entity_value'] = $this->_compId
;
482 $params['entity_status'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values['entity']);
485 $params['mapping_id'] = $values['entity'][0];
486 $entity_value = $values['entity'][1];
487 $entity_status = $values['entity'][2];
488 if ($params['mapping_id'] == 1) {
489 $params['limit_to'] = 1;
492 foreach (array('entity_value', 'entity_status') as $key) {
493 $params[$key] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $
$key);
497 $params['is_active'] = CRM_Utils_Array
::value('is_active', $values, 0);
499 if (CRM_Utils_Array
::value('is_repeat', $values) == 0) {
500 $params['repetition_frequency_unit'] = 'null';
501 $params['repetition_frequency_interval'] = 'null';
502 $params['end_frequency_unit'] = 'null';
503 $params['end_frequency_interval'] = 'null';
504 $params['end_action'] = 'null';
505 $params['end_date'] = 'null';
508 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
509 $params['id'] = $this->_id
;
511 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
512 // we do this only once, so name never changes
513 $params['name'] = CRM_Utils_String
::munge($params['title'], '_', 64);
516 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
518 if ($params['mode'] == 'Email' ||
empty($params['sms_provider_id'])) {
519 unset($modePrefixes['SMS']);
521 elseif ($params['mode'] == 'SMS') {
522 unset($modePrefixes['Mail']);
525 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
527 $composeFields = array(
534 //mail template is composed
536 foreach ($modePrefixes as $prefix) {
537 $composeParams = array();
538 foreach ($composeFields as $key) {
539 $key = $prefix . $key;
540 if (!empty($values[$key])) {
541 $composeParams[$key] = $values[$key];
545 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
546 $templateParams = array('is_active' => TRUE);
547 if ($prefix == 'SMS') {
548 $templateParams +
= array(
549 'msg_text' => $params['sms_body_text'],
554 $templateParams +
= array(
555 'msg_text' => $params['body_text'],
556 'msg_html' => $params['body_html'],
557 'msg_subject' => $params['subject'],
560 $templateParams['id'] = $values[$prefix . 'template'];
562 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
565 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
566 $templateParams = array('is_active' => TRUE);
567 if ($prefix == 'SMS') {
568 $templateParams +
= array(
569 'msg_text' => $params['sms_body_text'],
574 $templateParams +
= array(
575 'msg_text' => $params['body_text'],
576 'msg_html' => $params['body_html'],
577 'msg_subject' => $params['subject'],
580 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
582 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
585 if ($prefix == 'SMS') {
586 if (isset($msgTemplate->id
)) {
587 $params['sms_template_id'] = $msgTemplate->id
;
590 $params['sms_template_id'] = CRM_Utils_Array
::value('SMStemplate', $values);
594 if (isset($msgTemplate->id
)) {
595 $params['msg_template_id'] = $msgTemplate->id
;
598 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $values);
603 $bao = CRM_Core_BAO_ActionSchedule
::add($params);
604 // we need to set this on the form so that hooks can identify the created entity
605 $this->set('id', $bao->id
);
608 $status = ts("Your new Reminder titled %1 has been saved.",
609 array(1 => "<strong>{$values['title']}</strong>")
612 if ($this->_action
) {
613 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
614 $status = ts("Your Reminder titled %1 has been updated.",
615 array(1 => "<strong>{$values['title']}</strong>")
619 if ($this->_context
== 'event' && $this->_compId
) {
620 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
621 $session = CRM_Core_Session
::singleton();
622 $session->pushUserContext($url);
625 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
629 * List available tokens for this form.
633 public function listTokens() {
634 $tokens = CRM_Core_SelectValues
::contactTokens();
635 $tokens = array_merge(CRM_Core_SelectValues
::activityTokens(), $tokens);
636 $tokens = array_merge(CRM_Core_SelectValues
::eventTokens(), $tokens);
637 $tokens = array_merge(CRM_Core_SelectValues
::membershipTokens(), $tokens);