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
37 * This class generates form components for Scheduling Reminders
40 class CRM_Admin_Form_ScheduleReminders
extends CRM_Admin_Form
{
43 * Scheduled Reminder ID.
45 protected $_id = NULL;
50 * Build the form object.
54 public function buildQuickForm() {
55 parent
::buildQuickForm();
56 $this->_mappingID
= $mappingID = NULL;
57 $providersCount = CRM_SMS_BAO_Provider
::activeProviderCount();
58 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
60 //CRM-16777: Don't provide access to administer schedule reminder page, with user that does not have 'administer CiviCRM' permission
61 if (empty($this->_context
) && !CRM_Core_Permission
::check('administer CiviCRM')) {
62 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
64 //CRM-16777: When user have ACLs 'edit' permission for specific event, do not give access to add, delete & updtae
65 //schedule reminder for other events.
67 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
68 if (!CRM_Event_BAO_Event
::checkPermission($this->_compId
, CRM_Core_Permission
::EDIT
)) {
69 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
73 if ($this->_action
& (CRM_Core_Action
::DELETE
)) {
74 $reminderName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'title');
75 if ($this->_context
== 'event') {
76 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
78 $this->assign('reminderName', $reminderName);
81 elseif ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
82 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'mapping_id');
83 if ($this->_context
== 'event') {
84 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
87 elseif (!empty($this->_context
)) {
88 if ($this->_context
== 'event') {
89 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
90 $field = 'civicrm_event';
91 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_compId
, 'is_template');
93 $field = 'event_template';
95 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
96 if (!$this->_mappingID
) {
97 CRM_Core_Error
::fatal('Could not find mapping for event scheduled reminders.');
102 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context
)) {
103 $mappingID = $_POST['entity'][0];
105 elseif ($this->_mappingID
) {
106 $mappingID = $this->_mappingID
;
113 array('size' => 45, 'maxlength' => 128),
117 $selectionOptions = CRM_Core_BAO_ActionSchedule
::getSelection($mappingID);
118 extract($selectionOptions);
119 $this->assign('entityMapping', json_encode($entityMapping));
120 $this->assign('recipientMapping', json_encode($recipientMapping));
122 if (empty($this->_context
)) {
124 CRM_Core_Error
::fatal('Could not find mapping for scheduled reminders.');
132 'name' => 'entity[0]',
133 'style' => 'vertical-align: top;',
136 $sel->setOptions(array($sel1, $sel2, $sel3));
138 if (is_a($sel->_elements
[1], 'HTML_QuickForm_select')) {
139 // make second selector a multi-select -
140 $sel->_elements
[1]->setMultiple(TRUE);
141 $sel->_elements
[1]->setSize(5);
144 if (is_a($sel->_elements
[2], 'HTML_QuickForm_select')) {
145 // make third selector a multi-select -
146 $sel->_elements
[2]->setMultiple(TRUE);
147 $sel->_elements
[2]->setSize(5);
151 $options = $sel3[$this->_mappingID
][0];
152 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
154 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
155 $this->assign('context', $this->_context
);
158 //get the frequency units.
159 $this->_freqUnits
= CRM_Core_SelectValues
::getRecurringFrequencyUnits();
161 //pass the mapping ID in UPDATE mode
162 $mappings = CRM_Core_BAO_ActionSchedule
::getMapping($mappingID);
164 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(0, 30);
167 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
168 $isActive = ts('Send email');
169 $recordActivity = ts('Record activity for automated email');
170 if ($providersCount) {
171 $this->assign('sms', $providersCount);
172 $isActive = ts('Send email or SMS');
173 $recordActivity = ts('Record activity for automated email or SMS');
174 $options = CRM_Core_OptionGroup
::values('msg_mode');
175 $this->add('select', 'mode', ts('Send as'), $options);
177 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
179 $providerSelect = array();
180 foreach ($providers as $provider) {
181 $providerSelect[$provider['id']] = $provider['title'];
183 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
186 foreach ($this->_freqUnits
as $val => $label) {
187 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
190 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
193 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
196 'before' => ts('before'),
197 'after' => ts('after'),
200 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
202 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
204 $this->addElement('checkbox', 'record_activity', $recordActivity);
206 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
207 NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
210 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
211 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
212 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
213 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
214 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
215 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
217 $this->add('text', 'from_name', ts('From Name'));
218 $this->add('text', 'from_email', ts('From Email'));
220 $recipient = 'activity_contacts';
221 $recipientListingOptions = array();
224 $recipient = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping',
230 $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include'));
232 $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients'));
233 $this->assign('recipientLabels', $recipientLabels);
235 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);"));
237 $this->add('select', 'recipient', $recipientLabels['other'], $sel5[$recipient],
238 FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
241 if (!empty($this->_submitValues
['recipient_listing'])) {
242 if (!empty($this->_context
)) {
243 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_mappingID
, $this->_submitValues
['recipient']);
246 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
249 elseif (!empty($this->_values
['recipient_listing'])) {
250 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_values
['mapping_id'], $this->_values
['recipient']);
253 $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE,
254 array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE));
256 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
258 $this->add('select', 'group_id', ts('Group'),
259 CRM_Core_PseudoConstant
::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge')
262 CRM_Mailing_BAO_Mailing
::commonCompose($this);
264 $this->add('text', 'subject', ts('Subject'),
265 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
268 $this->add('checkbox', 'is_active', $isActive);
270 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
272 $this->setPageTitle(ts('Scheduled Reminder'));
278 * @param array $fields
279 * The input form values.
282 * true if no errors, else array of errors
284 public static function formRule($fields, $files, $self) {
286 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
287 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
289 $errors['entity'] = ts('Please select appropriate value');
292 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
293 if (count($fields['entity'][1]) > 1) {
294 $errors['entity'] = ts('You may only select one contact field per reminder');
296 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
297 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
301 if (!empty($fields['is_active']) &&
302 CRM_Utils_Array
::value('mode', $fields) == 'SMS' &&
303 CRM_Utils_System
::isNull($fields['subject'])
305 $errors['subject'] = ts('Subject is a required field.');
308 if (empty($self->_context
) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value(1, $fields['entity']))) {
309 $errors['entity'] = ts('Please select entity value');
312 if (!CRM_Utils_System
::isNull($fields['absolute_date'])) {
313 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date
::format(date('Ymd'))) {
314 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
318 $recipientKind = array(
319 'participant_role' => array(
320 'name' => 'participant role',
321 'target_id' => 'recipient_listing',
324 'name' => 'recipient',
325 'target_id' => 'recipient_manual_id',
328 if ($fields['limit_to'] != '' && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
329 $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']));
332 if (!empty($errors)) {
336 return empty($errors) ?
TRUE : $errors;
342 public function setDefaultValues() {
343 if ($this->_action
& CRM_Core_Action
::ADD
) {
344 $defaults['is_active'] = 1;
345 $defaults['mode'] = 'Email';
346 $defaults['record_activity'] = 1;
349 $defaults = $this->_values
;
350 $entityValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_value', $defaults));
351 $entityStatus = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_status', $defaults));
352 if (empty($this->_context
)) {
353 $defaults['entity'][0] = CRM_Utils_Array
::value('mapping_id', $defaults);
354 $defaults['entity'][1] = $entityValue;
355 $defaults['entity'][2] = $entityStatus;
358 $defaults['entity'] = $entityStatus;
360 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $defaults)) {
361 list($date, $time) = CRM_Utils_Date
::setDateDefaults($absoluteDate);
362 $defaults['absolute_date'] = $date;
365 if ($recipientListing = CRM_Utils_Array
::value('recipient_listing', $defaults)) {
366 $defaults['recipient_listing'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
370 $defaults['text_message'] = CRM_Utils_Array
::value('body_text', $defaults);
371 $defaults['html_message'] = CRM_Utils_Array
::value('body_html', $defaults);
372 $defaults['sms_text_message'] = CRM_Utils_Array
::value('sms_body_text', $defaults);
373 $defaults['template'] = CRM_Utils_Array
::value('msg_template_id', $defaults);
374 $defaults['SMStemplate'] = CRM_Utils_Array
::value('sms_template_id', $defaults);
375 if (!empty($defaults['group_id'])) {
376 $defaults['recipient'] = 'group';
378 elseif (!empty($defaults['recipient_manual'])) {
379 $defaults['recipient'] = 'manual';
380 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
388 * Process the form submission.
393 public function postProcess() {
394 if ($this->_action
& CRM_Core_Action
::DELETE
) {
396 CRM_Core_BAO_ActionSchedule
::del($this->_id
);
397 CRM_Core_Session
::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
398 if ($this->_context
== 'event' && $this->_compId
) {
399 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
400 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
402 $session = CRM_Core_Session
::singleton();
403 $session->pushUserContext($url);
407 $values = $this->controller
->exportValues($this->getName());
421 foreach ($keys as $key) {
422 $params[$key] = CRM_Utils_Array
::value($key, $values);
425 $params['is_repeat'] = CRM_Utils_Array
::value('is_repeat', $values, 0);
428 'start_action_offset',
430 'start_action_condition',
432 'repetition_frequency_unit',
433 'repetition_frequency_interval',
434 'end_frequency_unit',
435 'end_frequency_interval',
440 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $params)) {
441 $params['absolute_date'] = CRM_Utils_Date
::processDate($absoluteDate);
442 $params['is_repeat'] = 0;
443 foreach ($moreKeys as $mkey) {
444 $params[$mkey] = 'null';
448 $params['absolute_date'] = 'null';
449 foreach ($moreKeys as $mkey) {
450 $params[$mkey] = CRM_Utils_Array
::value($mkey, $values);
454 $params['body_text'] = CRM_Utils_Array
::value('text_message', $values);
455 $params['sms_body_text'] = CRM_Utils_Array
::value('sms_text_message', $values);
456 $params['body_html'] = CRM_Utils_Array
::value('html_message', $values);
458 if (CRM_Utils_Array
::value('recipient', $values) == 'manual') {
459 $params['recipient_manual'] = CRM_Utils_Array
::value('recipient_manual_id', $values);
460 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
462 elseif (CRM_Utils_Array
::value('recipient', $values) == 'group') {
463 $params['group_id'] = $values['group_id'];
464 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
466 elseif (!CRM_Utils_System
::isNull($values['recipient_listing']) && !CRM_Utils_System
::isNull($values['limit_to'])) {
467 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
468 $params['recipient_listing'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
469 CRM_Utils_Array
::value('recipient_listing', $values)
471 $params['group_id'] = $params['recipient_manual'] = 'null';
474 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
475 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
478 if (!empty($this->_mappingID
) && !empty($this->_compId
)) {
479 $params['mapping_id'] = $this->_mappingID
;
480 $params['entity_value'] = $this->_compId
;
481 $params['entity_status'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values['entity']);
484 $params['mapping_id'] = $values['entity'][0];
485 $entity_value = $values['entity'][1];
486 $entity_status = $values['entity'][2];
487 if ($params['mapping_id'] == 1) {
488 $params['limit_to'] = 1;
491 foreach (array('entity_value', 'entity_status') as $key) {
492 $params[$key] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $
$key);
496 $params['is_active'] = CRM_Utils_Array
::value('is_active', $values, 0);
498 if (CRM_Utils_Array
::value('is_repeat', $values) == 0) {
499 $params['repetition_frequency_unit'] = 'null';
500 $params['repetition_frequency_interval'] = 'null';
501 $params['end_frequency_unit'] = 'null';
502 $params['end_frequency_interval'] = 'null';
503 $params['end_action'] = 'null';
504 $params['end_date'] = 'null';
507 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
508 $params['id'] = $this->_id
;
510 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
511 // we do this only once, so name never changes
512 $params['name'] = CRM_Utils_String
::munge($params['title'], '_', 64);
515 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
517 if ($params['mode'] == 'Email' ||
empty($params['sms_provider_id'])) {
518 unset($modePrefixes['SMS']);
520 elseif ($params['mode'] == 'SMS') {
521 unset($modePrefixes['Mail']);
524 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
526 $composeFields = array(
533 //mail template is composed
535 foreach ($modePrefixes as $prefix) {
536 $composeParams = array();
537 foreach ($composeFields as $key) {
538 $key = $prefix . $key;
539 if (!empty($values[$key])) {
540 $composeParams[$key] = $values[$key];
544 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
545 $templateParams = array('is_active' => TRUE);
546 if ($prefix == 'SMS') {
547 $templateParams +
= array(
548 'msg_text' => $params['sms_body_text'],
553 $templateParams +
= array(
554 'msg_text' => $params['body_text'],
555 'msg_html' => $params['body_html'],
556 'msg_subject' => $params['subject'],
559 $templateParams['id'] = $values[$prefix . 'template'];
561 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
564 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
565 $templateParams = array('is_active' => TRUE);
566 if ($prefix == 'SMS') {
567 $templateParams +
= array(
568 'msg_text' => $params['sms_body_text'],
573 $templateParams +
= array(
574 'msg_text' => $params['body_text'],
575 'msg_html' => $params['body_html'],
576 'msg_subject' => $params['subject'],
579 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
581 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
584 if ($prefix == 'SMS') {
585 if (isset($msgTemplate->id
)) {
586 $params['sms_template_id'] = $msgTemplate->id
;
589 $params['sms_template_id'] = CRM_Utils_Array
::value('SMStemplate', $values);
593 if (isset($msgTemplate->id
)) {
594 $params['msg_template_id'] = $msgTemplate->id
;
597 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $values);
602 $bao = CRM_Core_BAO_ActionSchedule
::add($params);
603 // we need to set this on the form so that hooks can identify the created entity
604 $this->set('id', $bao->id
);
607 $status = ts("Your new Reminder titled %1 has been saved.",
608 array(1 => "<strong>{$values['title']}</strong>")
611 if ($this->_action
) {
612 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
613 $status = ts("Your Reminder titled %1 has been updated.",
614 array(1 => "<strong>{$values['title']}</strong>")
618 if ($this->_context
== 'event' && $this->_compId
) {
619 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
620 $session = CRM_Core_Session
::singleton();
621 $session->pushUserContext($url);
624 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
628 * List available tokens for this form.
632 public function listTokens() {
633 $tokens = CRM_Core_SelectValues
::contactTokens();
634 $tokens = array_merge(CRM_Core_SelectValues
::activityTokens(), $tokens);
635 $tokens = array_merge(CRM_Core_SelectValues
::eventTokens(), $tokens);
636 $tokens = array_merge(CRM_Core_SelectValues
::membershipTokens(), $tokens);