From 530dd28be7d8ef038b12178796e81eb449a10a2c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 28 Feb 2015 22:25:13 -0500 Subject: [PATCH] CRM-15932 - Much nicer widget for selecting exclude dates --- CRM/Core/Form/RecurringEntity.php | 22 +++--- CRM/Core/Page/AJAX/RecurringEntity.php | 2 +- templates/CRM/Core/Form/RecurringEntity.tpl | 87 ++++++++++----------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index 01995623c2..8dc259190e 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -101,7 +101,7 @@ class CRM_Core_Form_RecurringEntity { $excludeOptionValues = array(); if (!empty($optionValue)) { foreach ($optionValue as $key => $val) { - $excludeOptionValues[$val['value']] = date('m/d/Y', strtotime($val['value'])); + $excludeOptionValues[$val['value']] = CRM_Utils_Date::customFormat($val['value']); } self::$_excludeDateInfo = $excludeOptionValues; } @@ -146,6 +146,9 @@ class CRM_Core_Form_RecurringEntity { if (self::$_scheduleReminderDetails->entity_status) { $defaults['repeats_by'] = 2; } + if (self::$_excludeDateInfo) { + $defaults['exclude_date_list'] = implode(',', self::$_excludeDateInfo); + } } return $defaults; } @@ -153,7 +156,7 @@ class CRM_Core_Form_RecurringEntity { /** * Build form. * - * @param $form + * @param CRM_Core_Form $form */ public static function buildQuickForm(&$form) { if (self::$_entityTable) { @@ -216,15 +219,9 @@ class CRM_Core_Form_RecurringEntity { $form->add('text', 'start_action_offset', ts(''), array('size' => 3, 'maxlength' => 2)); $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule')); $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing')); - $form->addDate('exclude_date', ts('Exclude Date(s)'), FALSE); - $select = $form->add('select', 'exclude_date_list', ts(''), self::$_excludeDateInfo, FALSE, array( - 'style' => 'width:150px;', - 'size' => 4, + $form->add('text', 'exclude_date_list', ts('Exclude Dates'), array( + 'class' => 'twenty', )); - $select->setMultiple(TRUE); - $form->addElement('button', 'add_to_exclude_list', '>>', 'onClick="addToExcludeList(document.getElementById(\'exclude_date\').value);"'); - $form->addElement('button', 'remove_from_exclude_list', '<<', 'onClick="removeFromExcludeList(\'exclude_date_list\')"'); - $form->addElement('hidden', 'copyExcludeDates', '', array('id' => 'copyExcludeDates')); $form->addElement('hidden', 'allowRepeatConfigToSubmit', '', array('id' => 'allowRepeatConfigToSubmit')); $form->addButtons(array( array( @@ -363,10 +360,9 @@ class CRM_Core_Form_RecurringEntity { //exclude dates $excludeDateList = array(); - if (CRM_Utils_Array::value('copyExcludeDates', $params) && CRM_Utils_Array::value('parent_entity_id', $params) && $actionScheduleObj->entity_value) { + if (CRM_Utils_Array::value('exclude_date_list', $params) && CRM_Utils_Array::value('parent_entity_id', $params) && $actionScheduleObj->entity_value) { //Since we get comma separated values lets get them in array - $excludeDates = array(); - $excludeDates = explode(",", $params['copyExcludeDates']); + $excludeDates = explode(",", $params['exclude_date_list']); //Check if there exists any values for this option group $optionGroupIdExists = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', diff --git a/CRM/Core/Page/AJAX/RecurringEntity.php b/CRM/Core/Page/AJAX/RecurringEntity.php index f4fb03dfc9..7ce02377a9 100644 --- a/CRM/Core/Page/AJAX/RecurringEntity.php +++ b/CRM/Core/Page/AJAX/RecurringEntity.php @@ -63,7 +63,7 @@ class CRM_Core_Page_AJAX_RecurringEntity { } $recursion->scheduleFormValues = $formValues; if (!empty($formValues['exclude_date_list'])) { - $recursion->excludeDates = $formValues['exclude_date_list']; + $recursion->excludeDates = explode(',', $formValues['exclude_date_list']); } if (CRM_Utils_Array::value('excludeDateRangeColumns', CRM_Core_BAO_RecurringEntity::$_dateColumns[$formValues['entity_table']])) { $recursion->excludeDateRangeColumns = CRM_Core_BAO_RecurringEntity::$_dateColumns[$formValues['entity_table']]['excludeDateRangeColumns']; diff --git a/templates/CRM/Core/Form/RecurringEntity.tpl b/templates/CRM/Core/Form/RecurringEntity.tpl index 422d74d3cb..1d4a578086 100644 --- a/templates/CRM/Core/Form/RecurringEntity.tpl +++ b/templates/CRM/Core/Form/RecurringEntity.tpl @@ -74,11 +74,8 @@ - {$form.exclude_date.label} {help id="id-exclude-date" entityType=$entityType file="CRM/Core/Form/RecurringEntity.hlp"} -  {include file="CRM/common/jcalendar.tpl" elementName=exclude_date} -  {$form.add_to_exclude_list.html} {$form.remove_from_exclude_list.html} - {$form.exclude_date_list.html} - + {$form.exclude_date_list.label} {help id="id-exclude-date" entityType=$entityType file="CRM/Core/Form/RecurringEntity.hlp"} + {$form.exclude_date_list.html} {ts}Summary:{/ts} @@ -203,8 +200,6 @@ //Select all options in selectbox before submitting $(this).submit(function() { - $('#exclude_date_list option').attr('selected',true); - //Check form for values submitted if ($('input[name=ends]:checked').val() == 1) { if ($('#start_action_offset').val() == "") { @@ -225,9 +220,48 @@ return false; } } - }); + function getDisplayDate(date) { + return $.datepicker.formatDate(CRM.config.dateInputFormat, $.datepicker.parseDate('yy-mm-dd', date)); + } + + // Combine select2 and datepicker into a multi-select-date widget + $('#exclude_date_list').crmSelect2({ + multiple: true, + data: [], + initSelection: function(element, callback) { + var values = []; + $.each($(element).val().split(','), function(k, v) { + values.push({ + text: getDisplayDate(v), + id: v + }); + }); + callback(values); + } + }) + // Prevent select2 from opening and show a datepicker instead + .on('select2-opening', function(e) { + var $el = $(this); + e.preventDefault(); + $('.select2-search-field input', $el.select2('container')) + .datepicker({dateFormat: CRM.config.dateInputFormat}) + .datepicker('show') + .off('.crmDate') + .on('change.crmDate', function() { + if ($(this).val()) { + var date = $(this).datepicker('getDate'), + data = $el.select2('data') || []; + data.push({ + text: $.datepicker.formatDate(CRM.config.dateInputFormat, date), + id: $.datepicker.formatDate('yy-mm-dd', date) + }); + $el.select2('data', data); + } + }); + }); + //Detect changes in Repeat configuration field var unsavedChanges = false; $('div.crm-core-form-recurringentity-block').on('change', function() { @@ -238,15 +272,6 @@ //Dialog for preview repeat Configuration dates $('#preview-dialog').dialog({ autoOpen: false }); function previewDialog() { - $('#exclude_date_list option').attr('selected', true); - //Copy exclude dates - var dateTxt=[]; - $('#exclude_date_list option:selected').each(function() { - dateTxt.push($(this).text()); - }); - var completeDateText = dateTxt.join(','); - $('#copyExcludeDates').val(completeDateText); - $('#generated_dates').html('').html('
{/literal}{ts escape='js'}Just a moment, generating dates{/ts}{literal}...
'); $('#preview-dialog').dialog('open'); $('#preview-dialog').dialog({ @@ -436,34 +461,6 @@ }); - //Exclude list function - function addToExcludeList(val) { - if (val !== "") { - var exists = false; - for(var i = 0, opts = document.getElementById('exclude_date_list').options; i < opts.length; ++i) { - if (opts[i].text == val) { - exists = true; - break; - } - } - if (exists == false) { - cj('#exclude_date_list').append(''); - } - } - } - - function removeFromExcludeList(sourceID) { - var src = document.getElementById(sourceID); - for(var count= src.options.length-1; count >= 0; count--) { - if (src.options[count].selected == true) { - try{ - src.remove(count, null); - }catch(error) { - src.remove(count); - } - } - } - } {/literal} {*Hide Summary*} -- 2.25.1