3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class generates form components for Scheduling Reminders.
37 class CRM_Admin_Form_ScheduleReminders
extends CRM_Admin_Form
{
40 * Scheduled Reminder ID.
42 protected $_id = NULL;
47 * Build the form object.
49 public function buildQuickForm() {
50 parent
::buildQuickForm();
51 $this->_mappingID
= $mappingID = NULL;
52 $providersCount = CRM_SMS_BAO_Provider
::activeProviderCount();
53 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
55 //CRM-16777: Don't provide access to administer schedule reminder page, with user that does not have 'administer CiviCRM' permission
56 if (empty($this->_context
) && !CRM_Core_Permission
::check('administer CiviCRM')) {
57 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
59 //CRM-16777: When user have ACLs 'edit' permission for specific event, do not give access to add, delete & updtae
60 //schedule reminder for other events.
62 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
63 if (!CRM_Event_BAO_Event
::checkPermission($this->_compId
, CRM_Core_Permission
::EDIT
)) {
64 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
68 if ($this->_action
& (CRM_Core_Action
::DELETE
)) {
69 $reminderName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'title');
70 if ($this->_context
== 'event') {
71 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
73 $this->assign('reminderName', $reminderName);
76 elseif ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
77 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'mapping_id');
78 if ($this->_context
== 'event') {
79 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
82 elseif (!empty($this->_context
)) {
83 if ($this->_context
== 'event') {
84 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
85 $field = 'civicrm_event';
86 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_compId
, 'is_template');
88 $field = 'event_template';
90 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
91 if (!$this->_mappingID
) {
92 CRM_Core_Error
::fatal('Could not find mapping for event scheduled reminders.');
97 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context
)) {
98 $mappingID = $_POST['entity'][0];
100 elseif ($this->_mappingID
) {
101 $mappingID = $this->_mappingID
;
108 array('size' => 45, 'maxlength' => 128),
112 $selectionOptions = CRM_Core_BAO_ActionSchedule
::getSelection($mappingID);
113 extract($selectionOptions);
114 $this->assign('entityMapping', json_encode($entityMapping));
115 $this->assign('recipientMapping', json_encode($recipientMapping));
117 if (empty($this->_context
)) {
119 CRM_Core_Error
::fatal('Could not find mapping for scheduled reminders.');
127 'name' => 'entity[0]',
128 'style' => 'vertical-align: top;',
131 $sel->setOptions(array($sel1, $sel2, $sel3));
133 if (is_a($sel->_elements
[1], 'HTML_QuickForm_select')) {
134 // make second selector a multi-select -
135 $sel->_elements
[1]->setMultiple(TRUE);
136 $sel->_elements
[1]->setSize(5);
139 if (is_a($sel->_elements
[2], 'HTML_QuickForm_select')) {
140 // make third selector a multi-select -
141 $sel->_elements
[2]->setMultiple(TRUE);
142 $sel->_elements
[2]->setSize(5);
146 $options = $sel3[$this->_mappingID
][0];
147 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
149 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
150 $this->assign('context', $this->_context
);
153 //get the frequency units.
154 $this->_freqUnits
= CRM_Core_SelectValues
::getRecurringFrequencyUnits();
156 //pass the mapping ID in UPDATE mode
157 $mappings = CRM_Core_BAO_ActionSchedule
::getMapping($mappingID);
159 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(0, 30);
162 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
163 $isActive = ts('Send email');
164 $recordActivity = ts('Record activity for automated email');
165 if ($providersCount) {
166 $this->assign('sms', $providersCount);
167 $isActive = ts('Send email or SMS');
168 $recordActivity = ts('Record activity for automated email or SMS');
169 $options = CRM_Core_OptionGroup
::values('msg_mode');
170 $this->add('select', 'mode', ts('Send as'), $options);
172 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
174 $providerSelect = array();
175 foreach ($providers as $provider) {
176 $providerSelect[$provider['id']] = $provider['title'];
178 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
181 foreach ($this->_freqUnits
as $val => $label) {
182 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
185 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
188 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
191 'before' => ts('before'),
192 'after' => ts('after'),
195 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
197 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
199 $this->addElement('checkbox', 'record_activity', $recordActivity);
201 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
202 NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
205 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
206 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
207 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
208 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
209 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
210 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
212 $this->add('text', 'from_name', ts('From Name'));
213 $this->add('text', 'from_email', ts('From Email'));
215 $recipient = 'activity_contacts';
216 $recipientListingOptions = array();
219 $recipient = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping',
225 $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include'));
227 $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients'));
228 $this->assign('recipientLabels', $recipientLabels);
230 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);"));
232 $this->add('select', 'recipient', $recipientLabels['other'], $sel5[$recipient],
233 FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
236 if (!empty($this->_submitValues
['recipient_listing'])) {
237 if (!empty($this->_context
)) {
238 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_mappingID
, $this->_submitValues
['recipient']);
241 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
244 elseif (!empty($this->_values
['recipient_listing'])) {
245 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_values
['mapping_id'], $this->_values
['recipient']);
248 $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE,
249 array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE));
251 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
253 $this->add('select', 'group_id', ts('Group'),
254 CRM_Core_PseudoConstant
::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge')
257 CRM_Mailing_BAO_Mailing
::commonCompose($this);
259 $this->add('text', 'subject', ts('Subject'),
260 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
263 $this->add('checkbox', 'is_active', $isActive);
265 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
267 $this->setPageTitle(ts('Scheduled Reminder'));
273 * @param array $fields
274 * The input form values.
277 * true if no errors, else array of errors
279 public static function formRule($fields, $files, $self) {
281 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
282 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
284 $errors['entity'] = ts('Please select appropriate value');
287 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
288 if (count($fields['entity'][1]) > 1) {
289 $errors['entity'] = ts('You may only select one contact field per reminder');
291 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
292 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
296 if (!empty($fields['is_active']) &&
297 CRM_Utils_Array
::value('mode', $fields) == 'SMS' &&
298 CRM_Utils_System
::isNull($fields['subject'])
300 $errors['subject'] = ts('Subject is a required field.');
303 if (empty($self->_context
) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value(1, $fields['entity']))) {
304 $errors['entity'] = ts('Please select entity value');
307 if (!CRM_Utils_System
::isNull($fields['absolute_date'])) {
308 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date
::format(date('Ymd'))) {
309 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
313 $recipientKind = array(
314 'participant_role' => array(
315 'name' => 'participant role',
316 'target_id' => 'recipient_listing',
319 'name' => 'recipient',
320 'target_id' => 'recipient_manual_id',
323 if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
324 $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']));
327 if (!empty($errors)) {
331 return empty($errors) ?
TRUE : $errors;
337 public function setDefaultValues() {
338 if ($this->_action
& CRM_Core_Action
::ADD
) {
339 $defaults['is_active'] = 1;
340 $defaults['mode'] = 'Email';
341 $defaults['record_activity'] = 1;
344 $defaults = $this->_values
;
345 $entityValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_value', $defaults));
346 $entityStatus = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_status', $defaults));
347 if (empty($this->_context
)) {
348 $defaults['entity'][0] = CRM_Utils_Array
::value('mapping_id', $defaults);
349 $defaults['entity'][1] = $entityValue;
350 $defaults['entity'][2] = $entityStatus;
353 $defaults['entity'] = $entityStatus;
355 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $defaults)) {
356 list($date, $time) = CRM_Utils_Date
::setDateDefaults($absoluteDate);
357 $defaults['absolute_date'] = $date;
360 if ($recipientListing = CRM_Utils_Array
::value('recipient_listing', $defaults)) {
361 $defaults['recipient_listing'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
365 $defaults['text_message'] = CRM_Utils_Array
::value('body_text', $defaults);
366 $defaults['html_message'] = CRM_Utils_Array
::value('body_html', $defaults);
367 $defaults['sms_text_message'] = CRM_Utils_Array
::value('sms_body_text', $defaults);
368 $defaults['template'] = CRM_Utils_Array
::value('msg_template_id', $defaults);
369 $defaults['SMStemplate'] = CRM_Utils_Array
::value('sms_template_id', $defaults);
370 if (!empty($defaults['group_id'])) {
371 $defaults['recipient'] = 'group';
373 elseif (!empty($defaults['recipient_manual'])) {
374 $defaults['recipient'] = 'manual';
375 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
383 * Process the form submission.
385 public function postProcess() {
386 if ($this->_action
& CRM_Core_Action
::DELETE
) {
388 CRM_Core_BAO_ActionSchedule
::del($this->_id
);
389 CRM_Core_Session
::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
390 if ($this->_context
== 'event' && $this->_compId
) {
391 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
392 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
394 $session = CRM_Core_Session
::singleton();
395 $session->pushUserContext($url);
399 $values = $this->controller
->exportValues($this->getName());
413 foreach ($keys as $key) {
414 $params[$key] = CRM_Utils_Array
::value($key, $values);
417 $params['is_repeat'] = CRM_Utils_Array
::value('is_repeat', $values, 0);
420 'start_action_offset',
422 'start_action_condition',
424 'repetition_frequency_unit',
425 'repetition_frequency_interval',
426 'end_frequency_unit',
427 'end_frequency_interval',
432 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $params)) {
433 $params['absolute_date'] = CRM_Utils_Date
::processDate($absoluteDate);
434 $params['is_repeat'] = 0;
435 foreach ($moreKeys as $mkey) {
436 $params[$mkey] = 'null';
440 $params['absolute_date'] = 'null';
441 foreach ($moreKeys as $mkey) {
442 $params[$mkey] = CRM_Utils_Array
::value($mkey, $values);
446 $params['body_text'] = CRM_Utils_Array
::value('text_message', $values);
447 $params['sms_body_text'] = CRM_Utils_Array
::value('sms_text_message', $values);
448 $params['body_html'] = CRM_Utils_Array
::value('html_message', $values);
450 if (CRM_Utils_Array
::value('recipient', $values) == 'manual') {
451 $params['recipient_manual'] = CRM_Utils_Array
::value('recipient_manual_id', $values);
452 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
454 elseif (CRM_Utils_Array
::value('recipient', $values) == 'group') {
455 $params['group_id'] = $values['group_id'];
456 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
458 elseif (!CRM_Utils_System
::isNull($values['recipient_listing']) && !CRM_Utils_System
::isNull($values['limit_to'])) {
459 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
460 $params['recipient_listing'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
461 CRM_Utils_Array
::value('recipient_listing', $values)
463 $params['group_id'] = $params['recipient_manual'] = 'null';
466 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
467 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
470 if (!empty($this->_mappingID
) && !empty($this->_compId
)) {
471 $params['mapping_id'] = $this->_mappingID
;
472 $params['entity_value'] = $this->_compId
;
473 $params['entity_status'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values['entity']);
476 $params['mapping_id'] = $values['entity'][0];
477 $entity_value = $values['entity'][1];
478 $entity_status = $values['entity'][2];
479 if ($params['mapping_id'] == 1) {
480 $params['limit_to'] = 1;
483 foreach (array('entity_value', 'entity_status') as $key) {
484 $params[$key] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $
$key);
488 $params['is_active'] = CRM_Utils_Array
::value('is_active', $values, 0);
490 if (CRM_Utils_Array
::value('is_repeat', $values) == 0) {
491 $params['repetition_frequency_unit'] = 'null';
492 $params['repetition_frequency_interval'] = 'null';
493 $params['end_frequency_unit'] = 'null';
494 $params['end_frequency_interval'] = 'null';
495 $params['end_action'] = 'null';
496 $params['end_date'] = 'null';
499 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
500 $params['id'] = $this->_id
;
502 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
503 // we do this only once, so name never changes
504 $params['name'] = CRM_Utils_String
::munge($params['title'], '_', 64);
507 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
509 if ($params['mode'] == 'Email' ||
empty($params['sms_provider_id'])) {
510 unset($modePrefixes['SMS']);
512 elseif ($params['mode'] == 'SMS') {
513 unset($modePrefixes['Mail']);
516 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
518 $composeFields = array(
525 //mail template is composed
527 foreach ($modePrefixes as $prefix) {
528 $composeParams = array();
529 foreach ($composeFields as $key) {
530 $key = $prefix . $key;
531 if (!empty($values[$key])) {
532 $composeParams[$key] = $values[$key];
536 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
537 $templateParams = array('is_active' => TRUE);
538 if ($prefix == 'SMS') {
539 $templateParams +
= array(
540 'msg_text' => $params['sms_body_text'],
545 $templateParams +
= array(
546 'msg_text' => $params['body_text'],
547 'msg_html' => $params['body_html'],
548 'msg_subject' => $params['subject'],
551 $templateParams['id'] = $values[$prefix . 'template'];
553 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
556 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
557 $templateParams = array('is_active' => TRUE);
558 if ($prefix == 'SMS') {
559 $templateParams +
= array(
560 'msg_text' => $params['sms_body_text'],
565 $templateParams +
= array(
566 'msg_text' => $params['body_text'],
567 'msg_html' => $params['body_html'],
568 'msg_subject' => $params['subject'],
571 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
573 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
576 if ($prefix == 'SMS') {
577 if (isset($msgTemplate->id
)) {
578 $params['sms_template_id'] = $msgTemplate->id
;
581 $params['sms_template_id'] = CRM_Utils_Array
::value('SMStemplate', $values);
585 if (isset($msgTemplate->id
)) {
586 $params['msg_template_id'] = $msgTemplate->id
;
589 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $values);
594 $bao = CRM_Core_BAO_ActionSchedule
::add($params);
595 // we need to set this on the form so that hooks can identify the created entity
596 $this->set('id', $bao->id
);
599 $status = ts("Your new Reminder titled %1 has been saved.",
600 array(1 => "<strong>{$values['title']}</strong>")
603 if ($this->_action
) {
604 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
605 $status = ts("Your Reminder titled %1 has been updated.",
606 array(1 => "<strong>{$values['title']}</strong>")
610 if ($this->_context
== 'event' && $this->_compId
) {
611 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
612 $session = CRM_Core_Session
::singleton();
613 $session->pushUserContext($url);
616 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
620 * List available tokens for this form.
624 public function listTokens() {
625 $tokens = CRM_Core_SelectValues
::contactTokens();
626 $tokens = array_merge(CRM_Core_SelectValues
::activityTokens(), $tokens);
627 $tokens = array_merge(CRM_Core_SelectValues
::eventTokens(), $tokens);
628 $tokens = array_merge(CRM_Core_SelectValues
::membershipTokens(), $tokens);