From 581b6b41fd51549fe05afede9da647ccf6297660 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 27 Aug 2014 08:46:57 -0400
Subject: [PATCH] Make possible to set vacation start/end date and time

---
 plugins/managesieve/Changelog                              |    1 
 plugins/managesieve/managesieve.js                         |  125 ++++++++++++++++++++++++-------
 plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php |   77 ++++++++++++++----
 3 files changed, 156 insertions(+), 47 deletions(-)

diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog
index 334699c..01afe69 100644
--- a/plugins/managesieve/Changelog
+++ b/plugins/managesieve/Changelog
@@ -1,5 +1,6 @@
 - Fix missing css/js scripts in filter form in mail task
 - Fix default vacation status (#1490019)
+- Make possible to set vacation start/end date and time
 
 * version 8.0 [2014-07-16]
 -----------------------------------------------------------
diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
index 1c31817..af028e0 100644
--- a/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
+++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
@@ -90,6 +90,14 @@
         $date_extension  = in_array('date', $this->exts);
         $regex_extension = in_array('regex', $this->exts);
 
+        // set user's timezone
+        try {
+            $timezone = new DateTimeZone($this->rc->config->get('timezone', 'GMT'));
+        }
+        catch (Exception $e) {
+            $timezone = new DateTimeZone('GMT');
+        }
+
         $status        = rcube_utils::get_input_value('vacation_status', rcube_utils::INPUT_POST);
         $subject       = rcube_utils::get_input_value('vacation_subject', rcube_utils::INPUT_POST, true);
         $reason        = rcube_utils::get_input_value('vacation_reason', rcube_utils::INPUT_POST, true);
@@ -98,6 +106,8 @@
         $interval_type = rcube_utils::get_input_value('vacation_interval_type', rcube_utils::INPUT_POST);
         $date_from     = rcube_utils::get_input_value('vacation_datefrom', rcube_utils::INPUT_POST);
         $date_to       = rcube_utils::get_input_value('vacation_dateto', rcube_utils::INPUT_POST);
+        $time_from     = rcube_utils::get_input_value('vacation_timefrom', rcube_utils::INPUT_POST);
+        $time_to       = rcube_utils::get_input_value('vacation_timeto', rcube_utils::INPUT_POST);
         $after         = rcube_utils::get_input_value('vacation_after', rcube_utils::INPUT_POST);
         $action        = rcube_utils::get_input_value('vacation_action', rcube_utils::INPUT_POST);
         $target        = rcube_utils::get_input_value('action_target', rcube_utils::INPUT_POST, true);
@@ -133,9 +143,8 @@
 
         // find and remove existing date/regex/true rules
         foreach ((array) $vacation_tests as $idx => $t) {
-            if (($t['test'] == 'currentdate' && $t['part'] == 'date' && $t['type'] == $type)
+            if ($t['test'] == 'currentdate' || $t['test'] == 'true'
                 || ($t['test'] == 'header' && $t['type'] == 'regex' && $t['arg1'] == 'received')
-                || ($t['test'] == 'true')
             ) {
                 unset($vacation_tests[$idx]);
             }
@@ -143,17 +152,26 @@
 
         if ($date_extension) {
             foreach (array('date_from', 'date_to') as $var) {
-                $date = $$var;
-                if ($date && ($dt = rcube_utils::anytodatetime($date))) {
-                    $type = 'value-' . ($var == 'date_from' ? 'ge' : 'le');
-                    $test = array(
+                $time = ${str_replace('date', 'time', $var)};
+                $date = trim($$var . ' ' . $time);
+
+                if ($date && ($dt = rcube_utils::anytodatetime($date, $timezone))) {
+                    $vacation_tests[] = array(
                         'test' => 'currentdate',
                         'part' => 'date',
-                        'type' => $type,
+                        'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'),
+                        'zone' => $dt->format('O'),
                         'arg'  => $dt->format('Y-m-d'),
                     );
-
-                    $vacation_tests[] = $test;
+                    if ($time) {
+                        $vacation_tests[] = array(
+                            'test' => 'currentdate',
+                            'part' => 'time',
+                            'type' => 'value-' . ($var == 'date_from' ? 'ge' : 'le'),
+                            'zone' => $dt->format('O'),
+                            'arg'  => $dt->format('H:i:s'),
+                        );
+                    }
                 }
             }
         }
@@ -304,15 +322,35 @@
 
         if ($date_extension || $regex_extension) {
             $date_from   = new html_inputfield(array('name' => 'vacation_datefrom', 'id' => 'vacation_datefrom', 'class' => 'datepicker', 'size' => 12));
-            $date_to     = new html_inputfield(array('name' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12));
+            $date_to     = new html_inputfield(array('name' => 'vacation_dateto', 'id' => 'vacation_dateto', 'class' => 'datepicker', 'size' => 12));
             $date_format = $this->rc->config->get('date_format', 'Y-m-d');
         }
 
         if ($date_extension) {
+            $time_from   = new html_inputfield(array('name' => 'vacation_timefrom', 'id' => 'vacation_timefrom', 'size' => 6));
+            $time_to     = new html_inputfield(array('name' => 'vacation_timeto', 'id' => 'vacation_timeto', 'size' => 6));
+            $time_format = $this->rc->config->get('time_format', 'H:i');
+            $date_value  = array();
+
             foreach ((array) $this->vacation['tests'] as $test) {
-                if ($test['test'] == 'currentdate' && $test['part'] == 'date') {
-                    $date = $this->rc->format_date($test['arg'], $date_format, false);
-                    $date_value[$test['type'] == 'value-ge' ? 'from' : 'to'] = $date;
+                if ($test['test'] == 'currentdate' && ($test['part'] == 'date' || $test['part'] == 'time')) {
+                    $idx = $test['type'] == 'value-ge' ? 'from' : 'to';
+                    $date_value[$idx][$test['part']] = $test['arg'];
+                    if ($test['zone']) {
+                        $date_value[$idx]['zone'] = $test['zone'];
+                    }
+                }
+            }
+
+            foreach ($date_value as $idx => $value) {
+                $date = $value['date'] . ' '
+                    . ($value['time'] ? $value['time'] : ($idx == 'from' ? '00:00:00' : '23:59:59'))
+                    . ($value['zone'] ? ' ' . $value['zone'] : '');
+
+                $date_value[$idx] = $this->rc->format_date($date, $date_format, !empty($value['time']) && !empty($value['zone']));
+
+                if (!empty($value['time'])) {
+                    $date_value['time_' . $idx] = $this->rc->format_date($date, $time_format, !empty($value['zone']));
                 }
             }
         }
@@ -360,11 +398,10 @@
         $table->add(null, $reason->show($this->vacation['reason']));
 
         if ($date_extension || $regex_extension) {
-            $table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.dates')));
-            $table->add(null,
-                $this->plugin->gettext('vacation.from'). ' ' . $date_from->show($date_value['from'])
-                . ' ' . $this->plugin->gettext('vacation.to'). ' ' . $date_to->show($date_value['to'])
-            );
+            $table->add('title', html::label('vacation_datefrom', $this->plugin->gettext('vacation.start')));
+            $table->add(null, $date_from->show($date_value['from']) . ($time_from ? ' ' . $time_from->show($date_value['time_from']) : ''));
+            $table->add('title', html::label('vacation_dateto', $this->plugin->gettext('vacation.end')));
+            $table->add(null, $date_to->show($date_value['to']) . ($time_to ? ' ' . $time_to->show($date_value['time_to']) : ''));
         }
 
         $table->add('title', html::label('vacation_status', $this->plugin->gettext('vacation.status')));
@@ -394,6 +431,10 @@
 
         $this->rc->output->add_gui_object('sieveform', $form_id);
 
+        if ($time_format) {
+            $this->rc->output->set_env('time_format', $time_format);
+        }
+
         return $out;
     }
 
diff --git a/plugins/managesieve/managesieve.js b/plugins/managesieve/managesieve.js
index 5e14431..cd0d5f3 100644
--- a/plugins/managesieve/managesieve.js
+++ b/plugins/managesieve/managesieve.js
@@ -48,35 +48,7 @@
     if (rcmail.env.action.startsWith('plugin.managesieve')) {
       if (rcmail.gui_objects.sieveform) {
         rcmail.enable_command('plugin.managesieve-save', true);
-
-        // small resize for header element
-        $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
-          if (this.value == '...') this.style.width = '40px';
-        });
-
-        // resize dialog window
-        if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
-          parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
-        }
-
-        $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
-
-        // initialize smart list inputs
-        $('textarea[data-type="list"]', rcmail.gui_objects.sieveform).each(function() {
-          smart_field_init(this);
-        });
-
-        // enable date pickers on date fields
-        if ($.datepicker && rcmail.env.date_format) {
-          $.datepicker.setDefaults({
-            dateFormat: rcmail.env.date_format,
-            changeMonth: true,
-            showOtherMonths: true,
-            selectOtherMonths: true,
-            onSelect: function(dateText) { $(this).focus().val(dateText); }
-          });
-          $('input.datepicker').datepicker();
-        }
+        sieve_form_init();
       }
       else {
         rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror);
@@ -857,6 +829,101 @@
   }
 };
 
+// format time string
+function sieve_formattime(hour, minutes)
+{
+  var i, c, h, time = '', format = rcmail.env.time_format || 'H:i';
+
+  for (i=0; i<format.length; i++) {
+    c = format.charAt(i);
+    switch (c) {
+      case 'a': time += hour > 12 ? 'am' : 'pm'; break;
+      case 'A': time += hour > 12 ? 'AM' : 'PM'; break;
+      case 'g':
+      case 'h':
+        h = hour == 0 ? 12 : hour > 12 ? hour - 12 : hour;
+        time += (c == 'h' && hour < 10 ? '0' : '') + hour;
+        break;
+      case 'G': time += hour; break;
+      case 'H': time += (hour < 10 ? '0' : '') + hour; break;
+      case 'i': time += (minutes < 10 ? '0' : '') + minutes; break;
+      case 's': time += '00';
+      default: time += c;
+    }
+  }
+
+  return time;
+}
+
+function sieve_form_init()
+{
+  // small resize for header element
+  $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
+    if (this.value == '...') this.style.width = '40px';
+  });
+
+  // resize dialog window
+  if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
+    parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
+  }
+
+  $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
+
+  // initialize smart list inputs
+  $('textarea[data-type="list"]', rcmail.gui_objects.sieveform).each(function() {
+    smart_field_init(this);
+  });
+
+  // enable date pickers on date fields
+  if ($.datepicker && rcmail.env.date_format) {
+    $.datepicker.setDefaults({
+      dateFormat: rcmail.env.date_format,
+      changeMonth: true,
+      showOtherMonths: true,
+      selectOtherMonths: true,
+      onSelect: function(dateText) { $(this).focus().val(dateText); }
+    });
+    $('input.datepicker').datepicker();
+  }
+
+  // configure drop-down menu on time input fields based on jquery UI autocomplete
+  $('#vacation_timefrom, #vacation_timeto')
+    .attr('autocomplete', "off")
+    .autocomplete({
+      delay: 100,
+      minLength: 1,
+      source: function(p, callback) {
+        var h, result = [];
+        for (h = 0; h < 24; h++)
+          result.push(sieve_formattime(h, 0));
+        result.push(sieve_formattime(23, 59));
+
+        return callback(result);
+      },
+      open: function(event, ui) {
+        // scroll to current time
+        var $this = $(this), val = $this.val(),
+          widget = $this.autocomplete('widget').css('width', '10em'),
+          menu = $this.data('ui-autocomplete').menu;
+
+        if (val && val.length)
+          widget.children().each(function() {
+            var li = $(this);
+            if (li.text().indexOf(val) == 0)
+              menu._scrollIntoView(li);
+          });
+      },
+      select: function(event, ui) {
+        $(this).val(ui.item.value);
+        return false;
+      }
+    })
+    .click(function() {  // show drop-down upon clicks
+      $(this).autocomplete('search', $(this).val() || ' ');
+    })
+}
+
+
 /*********************************************************/
 /*********           Mail UI methods             *********/
 /*********************************************************/

--
Gitblit v1.9.1