CRM-15932 - Much nicer widget for selecting exclude dates
authorColeman Watts <coleman@civicrm.org>
Sun, 1 Mar 2015 03:25:13 +0000 (22:25 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 2 Mar 2015 15:55:21 +0000 (10:55 -0500)
CRM/Core/Form/RecurringEntity.php
CRM/Core/Page/AJAX/RecurringEntity.php
templates/CRM/Core/Form/RecurringEntity.tpl

index 01995623c2446c8561a39fbc442e53ca05f712ea..8dc259190edb33f9feffa8836e976efd79615923 100644 (file)
@@ -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',
index f4fb03dfc926652ef21d0cd11b6760cd98d5c154..7ce02377a96a99d2508607eba1c6fbc9ee9d2f37 100644 (file)
@@ -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'];
index 422d74d3cb32bf338ef49f87c6ba16426a987bbb..1d4a5780867152111e7dd7b7d9e435b56810bc16 100644 (file)
         </td>\r
       </tr>\r
       <tr class="crm-core-form-recurringentity-block-exclude_date">\r
-        <td class="label">{$form.exclude_date.label} {help id="id-exclude-date" entityType=$entityType file="CRM/Core/Form/RecurringEntity.hlp"}</td>\r
-        <td>&nbsp;{include file="CRM/common/jcalendar.tpl" elementName=exclude_date}\r
-          &nbsp;{$form.add_to_exclude_list.html}&nbsp;{$form.remove_from_exclude_list.html}\r
-          {$form.exclude_date_list.html}\r
-        </td>\r
+        <td class="label">{$form.exclude_date_list.label} {help id="id-exclude-date" entityType=$entityType file="CRM/Core/Form/RecurringEntity.hlp"}</td>\r
+        <td>{$form.exclude_date_list.html}</td>\r
       </tr>\r
       <tr>\r
         <td class="label bold">{ts}Summary:{/ts}</td>\r
 \r
     //Select all options in selectbox before submitting\r
     $(this).submit(function() {\r
-      $('#exclude_date_list option').attr('selected',true);\r
-\r
       //Check form for values submitted\r
       if ($('input[name=ends]:checked').val() == 1) {\r
         if ($('#start_action_offset').val() == "") {\r
           return false;\r
         }\r
       }\r
-\r
     });\r
 \r
+    function getDisplayDate(date) {\r
+      return $.datepicker.formatDate(CRM.config.dateInputFormat, $.datepicker.parseDate('yy-mm-dd', date));\r
+    }\r
+\r
+    // Combine select2 and datepicker into a multi-select-date widget\r
+    $('#exclude_date_list').crmSelect2({\r
+      multiple: true,\r
+      data: [],\r
+      initSelection: function(element, callback) {\r
+        var values = [];\r
+        $.each($(element).val().split(','), function(k, v) {\r
+          values.push({\r
+            text: getDisplayDate(v),\r
+            id: v\r
+          });\r
+        });\r
+        callback(values);\r
+      }\r
+    })\r
+      // Prevent select2 from opening and show a datepicker instead\r
+      .on('select2-opening', function(e) {\r
+        var $el = $(this);\r
+        e.preventDefault();\r
+        $('.select2-search-field input', $el.select2('container'))\r
+          .datepicker({dateFormat: CRM.config.dateInputFormat})\r
+          .datepicker('show')\r
+          .off('.crmDate')\r
+          .on('change.crmDate', function() {\r
+            if ($(this).val()) {\r
+              var date = $(this).datepicker('getDate'),\r
+                data = $el.select2('data') || [];\r
+              data.push({\r
+                text: $.datepicker.formatDate(CRM.config.dateInputFormat, date),\r
+                id: $.datepicker.formatDate('yy-mm-dd', date)\r
+              });\r
+              $el.select2('data', data);\r
+            }\r
+          });\r
+      });\r
+\r
     //Detect changes in Repeat configuration field\r
     var unsavedChanges = false;\r
     $('div.crm-core-form-recurringentity-block').on('change', function() {\r
     //Dialog for preview repeat Configuration dates\r
     $('#preview-dialog').dialog({ autoOpen: false });\r
     function previewDialog() {\r
-      $('#exclude_date_list option').attr('selected', true);\r
-      //Copy exclude dates\r
-      var dateTxt=[];\r
-      $('#exclude_date_list option:selected').each(function() {\r
-        dateTxt.push($(this).text());\r
-      });\r
-      var completeDateText = dateTxt.join(',');\r
-      $('#copyExcludeDates').val(completeDateText);\r
-\r
       $('#generated_dates').html('').html('<div class="crm-loading-element"><span class="loading-text">{/literal}{ts escape='js'}Just a moment, generating dates{/ts}{literal}...</span></div>');\r
       $('#preview-dialog').dialog('open');\r
       $('#preview-dialog').dialog({\r
 \r
   });\r
 \r
-  //Exclude list function\r
-  function addToExcludeList(val) {\r
-    if (val !== "") {\r
-      var exists = false;\r
-      for(var i = 0, opts = document.getElementById('exclude_date_list').options; i < opts.length; ++i) {\r
-        if (opts[i].text == val) {\r
-          exists = true;\r
-          break;\r
-        }\r
-      }\r
-      if (exists == false) {\r
-        cj('#exclude_date_list').append('<option>'+val+'</option>');\r
-      }\r
-    }\r
-  }\r
-\r
-  function removeFromExcludeList(sourceID) {\r
-    var src = document.getElementById(sourceID);\r
-    for(var count= src.options.length-1; count >= 0; count--) {\r
-      if (src.options[count].selected == true) {\r
-        try{\r
-          src.remove(count, null);\r
-        }catch(error) {\r
-          src.remove(count);\r
-        }\r
-      }\r
-    }\r
-  }\r
 </script>\r
 {/literal}\r
 {*Hide Summary*}\r