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-2014
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 if ($this->_action
& (CRM_Core_Action
::DELETE
)) {
62 $reminderName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'title');
63 if ($this->_context
== 'event') {
64 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
66 $this->assign('reminderName', $reminderName);
69 elseif ($this->_action
& (CRM_Core_Action
::UPDATE
)) {
71 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionSchedule', $this->_id
, 'mapping_id');
72 if ($this->_context
== 'event') {
73 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
76 elseif (!empty($this->_context
)) {
77 if ($this->_context
== 'event') {
78 $this->_compId
= CRM_Utils_Request
::retrieve('compId', 'Integer', $this);
79 $field = 'civicrm_event';
80 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_compId
, 'is_template');
82 $field = 'event_template';
84 $this->_mappingID
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value');
85 if (!$this->_mappingID
) {
86 CRM_Core_Error
::fatal('Could not find mapping for event scheduled reminders.');
91 if (!empty($_POST) && !empty($_POST['entity']) && empty($this->_context
)) {
92 $mappingID = $_POST['entity'][0];
94 elseif ($this->_mappingID
) {
95 $mappingID = $this->_mappingID
;
102 array('size' => 45, 'maxlength' => 128),
106 $selectionOptions = CRM_Core_BAO_ActionSchedule
::getSelection($mappingID);
107 extract($selectionOptions);
108 $this->assign('entityMapping', json_encode($entityMapping));
109 $this->assign('recipientMapping', json_encode($recipientMapping));
111 if (empty($this->_context
)) {
113 CRM_Core_Error
::fatal('Could not find mapping for scheduled reminders.');
121 'name' => 'entity[0]',
122 'style' => 'vertical-align: top;',
126 $sel->setOptions(array($sel1, $sel2, $sel3));
128 if (is_a($sel->_elements
[1], 'HTML_QuickForm_select')) {
129 // make second selector a multi-select -
130 $sel->_elements
[1]->setMultiple(TRUE);
131 $sel->_elements
[1]->setSize(5);
134 if (is_a($sel->_elements
[2], 'HTML_QuickForm_select')) {
135 // make third selector a multi-select -
136 $sel->_elements
[2]->setMultiple(TRUE);
137 $sel->_elements
[2]->setSize(5);
141 $options = $sel3[$this->_mappingID
][0];
142 $attributes = array('multiple' => 'multiple', 'class' => 'crm-select2 huge', 'placeholder' => $options[0]);
144 $this->add('select', 'entity', ts('Recipient(s)'), $options, TRUE, $attributes);
145 $this->assign('context', $this->_context
);
148 //get the frequency units.
149 $this->_freqUnits
= CRM_Core_SelectValues
::getScheduleReminderFrequencyUnits();
151 //pass the mapping ID in UPDATE mode
152 $mappings = CRM_Core_BAO_ActionSchedule
::getMapping($mappingID);
154 $numericOptions = CRM_Core_SelectValues
::getNumericOptions(0, 30);
157 $this->add('select', 'start_action_offset', ts('When'), $numericOptions);
158 $isActive = ts('Send email');
159 $recordActivity = ts('Record activity for automated email');
160 if ($providersCount) {
161 $this->assign('sms', $providersCount);
162 $isActive = ts('Send email or SMS');
163 $recordActivity = ts('Record activity for automated email or SMS');
164 $options = CRM_Core_OptionGroup
::values('msg_mode');
165 $this->add('select', 'mode', ts('Send as'), $options);
167 $providers = CRM_SMS_BAO_Provider
::getProviders(NULL, NULL, TRUE, 'is_default desc');
169 $providerSelect = array();
170 foreach ($providers as $provider) {
171 $providerSelect[$provider['id']] = $provider['title'];
173 $this->add('select', 'sms_provider_id', ts('SMS Provider'), $providerSelect, TRUE);
176 foreach ($this->_freqUnits
as $val => $label) {
177 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
180 $this->addDate('absolute_date', ts('Start Date'), FALSE, array('formatType' => 'mailing'));
183 $this->add('select', 'start_action_unit', ts('Frequency'), $freqUnitsDisplay, TRUE);
186 'before' => ts('before'),
187 'after' => ts('after'),
190 $this->add('select', 'start_action_condition', ts('Action Condition'), $condition);
192 $this->add('select', 'start_action_date', ts('Date Field'), $sel4, TRUE);
194 $this->addElement('checkbox', 'record_activity', $recordActivity);
196 $this->addElement('checkbox', 'is_repeat', ts('Repeat'),
197 NULL, array('onchange' => "return showHideByValue('is_repeat',true,'repeatFields','table-row','radio',false);")
200 $this->add('select', 'repetition_frequency_unit', ts('every'), $freqUnitsDisplay);
201 $this->add('select', 'repetition_frequency_interval', ts('every'), $numericOptions);
202 $this->add('select', 'end_frequency_unit', ts('until'), $freqUnitsDisplay);
203 $this->add('select', 'end_frequency_interval', ts('until'), $numericOptions);
204 $this->add('select', 'end_action', ts('Repetition Condition'), $condition, TRUE);
205 $this->add('select', 'end_date', ts('Date Field'), $sel4, TRUE);
207 $this->add('text', 'from_name', ts('From Name'));
208 $this->add('text', 'from_email', ts('From Email'));
210 $recipient = 'activity_contacts';
211 $recipientListingOptions = array();
214 $recipient = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionMapping',
220 $limitOptions = array('' => '-neither-', 1 => ts('Limit to'), 0 => ts('Also include'));
222 $recipientLabels = array('activity' => ts('Recipients'), 'other' => ts('Limit or Add Recipients'));
223 $this->assign('recipientLabels', $recipientLabels);
225 $this->add('select', 'limit_to', ts('Limit Options'), $limitOptions, FALSE, array('onChange' => "showHideByValue('limit_to','','recipient', 'select','select',true);"));
227 $this->add('select', 'recipient', $recipientLabels['other'], $sel5[$recipient],
228 FALSE, array('onchange' => "showHideByValue('recipient','manual','recipientManual','table-row','select',false); showHideByValue('recipient','group','recipientGroup','table-row','select',false);")
231 if (!empty($this->_submitValues
['recipient_listing'])) {
232 if (!empty($this->_context
)) {
233 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_mappingID
, $this->_submitValues
['recipient']);
236 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($_POST['entity'][0], $_POST['recipient']);
239 elseif (!empty($this->_values
['recipient_listing'])) {
240 $recipientListingOptions = CRM_Core_BAO_ActionSchedule
::getRecipientListing($this->_values
['mapping_id'], $this->_values
['recipient']);
243 $this->add('select', 'recipient_listing', ts('Recipient Roles'), $recipientListingOptions, FALSE,
244 array('multiple' => TRUE, 'class' => 'crm-select2 huge', 'placeholder' => TRUE));
246 $this->addEntityRef('recipient_manual_id', ts('Manual Recipients'), array('multiple' => TRUE, 'create' => TRUE));
248 $this->add('select', 'group_id', ts('Group'),
249 CRM_Core_PseudoConstant
::nestedGroup('Mailing'), FALSE, array('class' => 'crm-select2 huge')
252 CRM_Mailing_BAO_Mailing
::commonCompose($this);
254 $this->add('text', 'subject', ts('Subject'),
255 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_ActionSchedule', 'subject')
258 $this->add('checkbox', 'is_active', $isActive);
260 $this->addFormRule(array('CRM_Admin_Form_ScheduleReminders', 'formRule'), $this);
262 $this->setPageTitle(ts('Scheduled Reminder'));
267 * @param array $fields
268 * The input form values.
270 * @return true if no errors, else array of errors
273 public static function formRule($fields, $files, $self) {
275 if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) ||
276 (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0)
278 $errors['entity'] = ts('Please select appropriate value');
281 if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) {
282 if (count($fields['entity'][1]) > 1) {
283 $errors['entity'] = ts('You may only select one contact field per reminder');
285 elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) {
286 $errors['entity'] = ts('Please select whether the reminder is sent each year.');
290 if (!empty($fields['is_active']) && $fields['mode'] != 'SMS' &&
291 CRM_Utils_System
::isNull($fields['subject'])
293 $errors['subject'] = ts('Subject is a required field.');
296 if (empty($self->_context
) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value(1, $fields['entity']))) {
297 $errors['entity'] = ts('Please select entity value');
300 if (!CRM_Utils_System
::isNull($fields['absolute_date'])) {
301 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($fields['absolute_date'], NULL)) < CRM_Utils_Date
::format(date('Ymd'))) {
302 $errors['absolute_date'] = ts('Absolute date cannot be earlier than the current time.');
306 $recipientKind = array(
307 'participant_role' => array(
308 'name' => 'participant role',
309 'target_id' => 'recipient_listing'
312 'name' => 'recipient',
313 'target_id' => 'recipient_manual_id'
316 if (!empty($fields['limit_to']) && array_key_exists($fields['recipient'], $recipientKind) && empty($fields[$recipientKind[$fields['recipient']]['target_id']])) {
317 $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']));
320 if (!empty($errors)) {
324 return empty($errors) ?
TRUE : $errors;
330 public function setDefaultValues() {
331 if ($this->_action
& CRM_Core_Action
::ADD
) {
332 $defaults['is_active'] = 1;
333 $defaults['mode'] = 'Email';
334 $defaults['record_activity'] = 1;
337 $defaults = $this->_values
;
338 $entityValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_value', $defaults));
339 $entityStatus = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, CRM_Utils_Array
::value('entity_status', $defaults));
340 if (empty($this->_context
)) {
341 $defaults['entity'][0] = CRM_Utils_Array
::value('mapping_id', $defaults);
342 $defaults['entity'][1] = $entityValue;
343 $defaults['entity'][2] = $entityStatus;
346 $defaults['entity'] = $entityStatus;
348 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $defaults)) {
349 list($date, $time) = CRM_Utils_Date
::setDateDefaults($absoluteDate);
350 $defaults['absolute_date'] = $date;
353 if ($recipientListing = CRM_Utils_Array
::value('recipient_listing', $defaults)) {
354 $defaults['recipient_listing'] = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
358 $defaults['text_message'] = CRM_Utils_Array
::value('body_text', $defaults);
359 $defaults['html_message'] = CRM_Utils_Array
::value('body_html', $defaults);
360 $defaults['sms_text_message'] = CRM_Utils_Array
::value('sms_body_text', $defaults);
361 $defaults['template'] = CRM_Utils_Array
::value('msg_template_id', $defaults);
362 $defaults['SMStemplate'] = CRM_Utils_Array
::value('sms_template_id', $defaults);
363 if (!empty($defaults['group_id'])) {
364 $defaults['recipient'] = 'group';
366 elseif (!empty($defaults['recipient_manual'])) {
367 $defaults['recipient'] = 'manual';
368 $defaults['recipient_manual_id'] = $defaults['recipient_manual'];
376 * Process the form submission
381 public function postProcess() {
382 if ($this->_action
& CRM_Core_Action
::DELETE
) {
384 CRM_Core_BAO_ActionSchedule
::del($this->_id
);
385 CRM_Core_Session
::setStatus(ts('Selected Reminder has been deleted.'), ts('Record Deleted'), 'success');
386 if ($this->_context
== 'event' && $this->_compId
) {
387 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder',
388 "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1"
390 $session = CRM_Core_Session
::singleton();
391 $session->pushUserContext($url);
395 $values = $this->controller
->exportValues($this->getName());
409 foreach ($keys as $key) {
410 $params[$key] = CRM_Utils_Array
::value($key, $values);
413 $params['is_repeat'] = CRM_Utils_Array
::value('is_repeat', $values, 0);
416 'start_action_offset',
418 'start_action_condition',
420 'repetition_frequency_unit',
421 'repetition_frequency_interval',
422 'end_frequency_unit',
423 'end_frequency_interval',
428 if ($absoluteDate = CRM_Utils_Array
::value('absolute_date', $params)) {
429 $params['absolute_date'] = CRM_Utils_Date
::processDate($absoluteDate);
430 $params['is_repeat'] = 0;
431 foreach ($moreKeys as $mkey) {
432 $params[$mkey] = 'null';
436 $params['absolute_date'] = 'null';
437 foreach ($moreKeys as $mkey) {
438 $params[$mkey] = CRM_Utils_Array
::value($mkey, $values);
442 $params['body_text'] = CRM_Utils_Array
::value('text_message', $values);
443 $params['sms_body_text'] = CRM_Utils_Array
::value('sms_text_message', $values);
444 $params['body_html'] = CRM_Utils_Array
::value('html_message', $values);
446 if (CRM_Utils_Array
::value('recipient', $values) == 'manual') {
447 $params['recipient_manual'] = CRM_Utils_Array
::value('recipient_manual_id', $values);
448 $params['group_id'] = $params['recipient'] = $params['recipient_listing'] = 'null';
450 elseif (CRM_Utils_Array
::value('recipient', $values) == 'group') {
451 $params['group_id'] = $values['group_id'];
452 $params['recipient_manual'] = $params['recipient'] = $params['recipient_listing'] = 'null';
454 elseif (!CRM_Utils_System
::isNull($values['recipient_listing']) && !CRM_Utils_System
::isNull($values['limit_to'])) {
455 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
456 $params['recipient_listing'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
457 CRM_Utils_Array
::value('recipient_listing', $values)
459 $params['group_id'] = $params['recipient_manual'] = 'null';
462 $params['recipient'] = CRM_Utils_Array
::value('recipient', $values);
463 $params['group_id'] = $params['recipient_manual'] = $params['recipient_listing'] = 'null';
466 if (!empty($this->_mappingID
) && !empty($this->_compId
)) {
467 $params['mapping_id'] = $this->_mappingID
;
468 $params['entity_value'] = $this->_compId
;
469 $params['entity_status'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values['entity']);
472 $params['mapping_id'] = $values['entity'][0];
473 $entity_value = $values['entity'][1];
474 $entity_status = $values['entity'][2];
475 if ($params['mapping_id'] == 1) {
476 $params['limit_to'] = 1;
479 foreach (array('entity_value', 'entity_status') as $key) {
480 $params[$key] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $
$key);
484 $params['is_active'] = CRM_Utils_Array
::value('is_active', $values, 0);
486 if (CRM_Utils_Array
::value('is_repeat', $values) == 0) {
487 $params['repetition_frequency_unit'] = 'null';
488 $params['repetition_frequency_interval'] = 'null';
489 $params['end_frequency_unit'] = 'null';
490 $params['end_frequency_interval'] = 'null';
491 $params['end_action'] = 'null';
492 $params['end_date'] = 'null';
495 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
496 $params['id'] = $this->_id
;
498 elseif ($this->_action
& CRM_Core_Action
::ADD
) {
499 // we do this only once, so name never changes
500 $params['name'] = CRM_Utils_String
::munge($params['title'], '_', 64);
503 $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
505 if ($params['mode'] == 'Email' ||
empty($params['sms_provider_id'])) {
506 unset($modePrefixes['SMS']);
508 elseif ($params['mode'] == 'SMS') {
509 unset($modePrefixes['Mail']);
512 //TODO: handle postprocessing of SMS and/or Email info based on $modePrefixes
514 $composeFields = array(
515 'template', 'saveTemplate',
516 'updateTemplate', 'saveTemplateName',
519 //mail template is composed
521 foreach ($modePrefixes as $prefix) {
522 $composeParams = array();
523 foreach ($composeFields as $key) {
524 $key = $prefix . $key;
525 if (!empty($values[$key])) {
526 $composeParams[$key] = $values[$key];
530 if (!empty($composeParams[$prefix . 'updateTemplate'])) {
531 $templateParams = array('is_active' => TRUE);
532 if ($prefix == 'SMS') {
533 $templateParams +
= array(
534 'msg_text' => $params['sms_body_text'],
539 $templateParams +
= array(
540 'msg_text' => $params['body_text'],
541 'msg_html' => $params['body_html'],
542 'msg_subject' => $params['subject'],
545 $templateParams['id'] = $values[$prefix . 'template'];
547 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
550 if (!empty($composeParams[$prefix . 'saveTemplate'])) {
551 $templateParams = array('is_active' => TRUE);
552 if ($prefix == 'SMS') {
553 $templateParams +
= array(
554 'msg_text' => $params['sms_body_text'],
559 $templateParams +
= array(
560 'msg_text' => $params['body_text'],
561 'msg_html' => $params['body_html'],
562 'msg_subject' => $params['subject'],
565 $templateParams['msg_title'] = $composeParams[$prefix . 'saveTemplateName'];
567 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
570 if ($prefix == 'SMS') {
571 if (isset($msgTemplate->id
)) {
572 $params['sms_template_id'] = $msgTemplate->id
;
575 $params['sms_template_id'] = CRM_Utils_Array
::value('SMStemplate', $values);
579 if (isset($msgTemplate->id
)) {
580 $params['msg_template_id'] = $msgTemplate->id
;
583 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $values);
588 $bao = CRM_Core_BAO_ActionSchedule
::add($params);
589 // we need to set this on the form so that hooks can identify the created entity
590 $this->set('id', $bao->id
);
593 $status = ts("Your new Reminder titled %1 has been saved.",
594 array(1 => "<strong>{$values['title']}</strong>")
597 if ($this->_action
) {
598 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
599 $status = ts("Your Reminder titled %1 has been updated.",
600 array(1 => "<strong>{$values['title']}</strong>")
604 if ($this->_context
== 'event' && $this->_compId
) {
605 $url = CRM_Utils_System
::url('civicrm/event/manage/reminder', "reset=1&action=browse&id={$this->_compId}&component={$this->_context}&setTab=1");
606 $session = CRM_Core_Session
::singleton();
607 $session->pushUserContext($url);
610 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');