From 01c6a75d164d0e7f7273dc01c707acc4af5223dc Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Mon, 27 Jan 2014 11:54:45 +0100 Subject: [PATCH] Consider the configured date format when parsing date/time values for tasks (#2801) + reduce code-duplication --- plugins/tasklist/tasklist.php | 47 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/plugins/tasklist/tasklist.php b/plugins/tasklist/tasklist.php index 325c5bd4..53597d58 100644 --- a/plugins/tasklist/tasklist.php +++ b/plugins/tasklist/tasklist.php @@ -381,27 +381,11 @@ class tasklist extends rcube_plugin } if (!empty($rec['date'])) { - try { - $date = new DateTime($rec['date'] . ' ' . $rec['time'], $this->timezone); - $rec['date'] = $date->format('Y-m-d'); - if (!empty($rec['time'])) - $rec['time'] = $date->format('H:i'); - } - catch (Exception $e) { - $rec['date'] = $rec['time'] = null; - } + $this->normalize_dates($rec, 'date', 'time'); } if (!empty($rec['startdate'])) { - try { - $date = new DateTime($rec['startdate'] . ' ' . $rec['starttime'], $this->timezone); - $rec['startdate'] = $date->format('Y-m-d'); - if (!empty($rec['starttime'])) - $rec['starttime'] = $date->format('H:i'); - } - catch (Exception $e) { - $rec['startdate'] = $rec['starttime'] = null; - } + $this->normalize_dates($rec, 'startdate', 'starttime'); } // convert tags to array, filter out empty entries @@ -434,6 +418,33 @@ class tasklist extends rcube_plugin return $rec; } + /** + * Utility method to convert a tasks date/time values into a normalized format + */ + private function normalize_dates(&$rec, $date_key, $time_key) + { + try { + // parse date from user format (#2801) + $date_format = $this->rc->config->get(empty($rec[$time_key]) ? 'date_format' : 'date_long', 'Y-m-d'); + $date = DateTime::createFromFormat($date_format, trim($rec[$date_key] . ' ' . $rec[$time_key]), $this->timezone); + + // fall back to default strtotime logic + if (empty($date)) { + $date = new DateTime($rec[$date_key] . ' ' . $rec[$time_key], $this->timezone); + } + + $rec[$date_key] = $date->format('Y-m-d'); + if (!empty($rec[$time_key])) + $rec[$time_key] = $date->format('H:i'); + + return true; + } + catch (Exception $e) { + $rec[$date_key] = $rec[$time_key] = null; + } + + return false; + } /** * Releases some resources after successful save