From 2edebfcf72b871c0b708eb53e570a0a07abf12d7 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Wed, 20 Jan 2010 20:30:23 +0100 Subject: [PATCH] Normalize free-form date and time fields before submitting, and warn user if there is a problem. --- kronolith/js/date.js | 2 +- kronolith/js/kronolith.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ kronolith/lib/Kronolith.php | 3 +++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/kronolith/js/date.js b/kronolith/js/date.js index 72bca4c20..9e8819765 100644 --- a/kronolith/js/date.js +++ b/kronolith/js/date.js @@ -2081,7 +2081,7 @@ var _ = $D.Parsing.Operators, g = $D.Grammar, t = $D.Translator, _fn; g.datePartDelimiter = _.rtoken(/^([\s\-\.\,\/\x27]+)/); - g.timePartDelimiter = _.stoken(":"); + g.timePartDelimiter = _.rtoken(/^([\.:])/); g.whiteSpace = _.rtoken(/^\s*/); g.generalDelimiter = _.rtoken(/^(([\s\,]|at|@|on)+)/); diff --git a/kronolith/js/kronolith.js b/kronolith/js/kronolith.js index 49b90be85..ba82f8b9d 100644 --- a/kronolith/js/kronolith.js +++ b/kronolith/js/kronolith.js @@ -30,6 +30,7 @@ KronolithCore = { tasktype: 'incomplete', growls: 0, alarms: [], + wrongFormat: $H(), mapMarker: null, map: null, mapInitialized: false, @@ -1945,6 +1946,11 @@ KronolithCore = { */ saveTask: function() { + if (this.wrongFormat.size()) { + this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.fix_form_values }]); + return; + } + var tasklist = $F('kronolithTaskList'), taskid = $F('kronolithTaskId'); this.startLoading('tasks:' + ($F('kronolithTaskCompleted') ? 'complete' : 'incomplete') + tasklist, this.tasktype); @@ -3229,6 +3235,11 @@ KronolithCore = { */ saveEvent: function() { + if (this.wrongFormat.size()) { + this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.fix_form_values }]); + return; + } + var cal = $F('kronolithEventTarget'), eventid = $F('kronolithEventId'), viewDates = this.viewDates(this.date, this.view), @@ -3529,6 +3540,34 @@ KronolithCore = { } }, + checkDate: function(e) { + var elm = e.element(); + if ($F(elm)) { + var date = Date.parseExact($F(elm), Kronolith.conf.date_format) || Date.parse($F(elm)); + if (date) { + elm.setValue(date.toString(Kronolith.conf.date_format)); + this.wrongFormat.unset(elm.id); + } else { + this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.wrong_date_format.interpolate({ wrong: $F(elm), right: new Date().toString(Kronolith.conf.date_format) }) }]); + this.wrongFormat.set(elm.id, true); + } + } + }, + + checkTime: function(e) { + var elm = e.element(); + if ($F(elm)) { + var time = Date.parseExact(new Date().toString(Kronolith.conf.date_format) + ' ' + $F(elm), Kronolith.conf.date_format + ' ' + Kronolith.conf.time_format) || Date.parse(new Date().toString('yyyy-MM-dd ') + $F(elm)); + if (time) { + elm.setValue(time.toString(Kronolith.conf.time_format)); + this.wrongFormat.unset(elm.id); + } else { + this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.wrong_time_format.interpolate({ wrong: $F(elm), right: new Date().toString(Kronolith.conf.time_format) }) }]); + this.wrongFormat.set(elm.id, true); + } + } + }, + _closeRedBox: function() { var content = RedBox.getWindowContents(); @@ -3724,6 +3763,13 @@ KronolithCore = { } }); + $('kronolithEventStartDate').observe('blur', this.checkDate.bind(this)); + $('kronolithEventEndDate').observe('blur', this.checkDate.bind(this)); + $('kronolithTaskDueDate').observe('blur', this.checkDate.bind(this)); + $('kronolithEventStartTime').observe('blur', this.checkTime.bind(this)); + $('kronolithEventEndTime').observe('blur', this.checkTime.bind(this)); + $('kronolithTaskDueTime').observe('blur', this.checkTime.bind(this)); + // Mouse wheel handler. [ 'kronolithEventStartDate', 'kronolithEventEndDate' ].each(function(field) { $(field).observe(Prototype.Browser.Gecko ? 'DOMMouseScroll' : 'mousewheel', function(e) { diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 7c69420fc..d7393b242 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -313,6 +313,9 @@ class Kronolith 'no_url' => _("You must specify a URL."), 'wrong_auth' => _("The authentication information you specified wasn't accepted."), 'geocode_error' => _("Unable to locate requested address"), + 'wrong_date_format' => sprintf(_("You used an unknown date format \"%s\". Please try something like \"%s\"."), '#{wrong}', '#{right}'), + 'wrong_time_format' => sprintf(_("You used an unknown time format \"%s\". Please try something like \"%s\"."), '#{wrong}', '#{right}'), + 'fix_form_values' => _("Please enter correct values in the form first."), ); for ($i = 1; $i <= 12; ++$i) { $code['text']['month'][$i - 1] = Horde_Nls::getLangInfo(constant('MON_' . $i)); -- 2.11.0