Koha/koha-tmpl/intranet-tmpl/prog/js/calendar.js
Owen Leonard bda0b1fd03 Bug 30514: Error in date format check following datepicker removal
This patch corrects the is_valid_date function in the OPAC and staff
interface so that it work correctly with Flatpickr.

To test, apply the patch and clear your browser cache if necessary.

- In the staff client, locate a bibliographic record and edit its items.
- Test that the "Date acquired" date picker works correctly and that
  there are no errors in the browser console.
- Test other date input fields to check that they still work, e.g.
  specify due date during checkout; Catalog statistics wizard; Patron
  entry/modification, etc.
- Perform similar tests in the OPAC: Hold suspension; Update your
  personal details.

Signed-off-by: David Cook <dcook@prosentient.com.au>

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-05-06 10:33:10 -10:00

145 lines
4.8 KiB
JavaScript

/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string bidi calendarFirstDayOfWeek */
/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months */
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
if (debug > 1) {
alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
}
function is_valid_date(date) {
// An empty string is considered as a valid date for convenient reasons.
if (date === '') return 1;
var dateformat = flatpickr_dateformat_string;
if (dateformat == 'us') {
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
dateformat = 'm/d/Y';
} else if (dateformat == 'metric') {
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
dateformat = 'd/m/Y';
} else if (dateformat == 'iso') {
if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
dateformat = 'Y-m-d';
} else if (dateformat == 'dmydot') {
if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
dateformat = 'd.m.Y';
}
try {
flatpickr.parseDate(date, dateformat);
} catch (e) {
return 0;
}
return 1;
}
function get_dateformat_str(dateformat) {
var dateformat_str;
if (dateformat == 'us') {
dateformat_str = 'mm/dd/yyyy';
} else if (dateformat == 'metric') {
dateformat_str = 'dd/mm/yyyy';
} else if (dateformat == 'iso') {
dateformat_str = 'yyyy-mm-dd';
} else if (dateformat == 'dmydot') {
dateformat_str = 'dd.mm.yyyy';
}
return dateformat_str;
}
function validate_date(dateText, inst) {
if (!is_valid_date(dateText)) {
var dateformat_str = get_dateformat_str( dateformat_pref );
alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
inst.clear();
}
}
function Date_from_syspref(dstring) {
var dateX = dstring.split(/[-/.]/);
if (debug > 1 && sentmsg < 1) {
sentmsg++;
alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
}
if (dateformat_pref === "iso") {
return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
} else if (dateformat_pref === "us") {
return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
} else if (dateformat_pref === "metric") {
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
} else if (dateformat_pref === "dmydot") {
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
} else {
if (debug > 0) {
alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref);
}
return 0;
}
}
function DateTime_from_syspref(date_time) {
var parts = date_time.split(" ");
var date = parts[0];
var time = parts[1];
parts = time.split(":");
var hour = parts[0];
var minute = parts[1];
if (hour < 0 || hour > 23) {
return 0;
}
if (minute < 0 || minute > 59) {
return 0;
}
var datetime = Date_from_syspref(date);
if (isNaN(datetime.getTime())) {
return 0;
}
datetime.setHours(hour);
datetime.setMinutes(minute);
return datetime;
}
/* jQuery Validator plugin custom method
This allows you to check that a given date falls after another.
It is required that a message be defined.
Syntax:
$("#form_id").validate({
rules: {
input_name_of_later_date_field: {
is_date_after: "#input_id_of_earlier_date_field"
},
},
messages: {
input_name_of_later_date_field: {
is_date_after: _("Validation error to be shown, i.e. End date must come after start date")
}
}
});
*/
jQuery.validator.addMethod("is_date_after",
function (value, element, params) {
var from = Date_from_syspref($(params).val());
var to = Date_from_syspref(value);
return to > from;
});
jQuery.validator.addMethod("date_on_or_after",
function (value, element, params) {
var from = Date_from_syspref($(params).val());
var to = Date_from_syspref(value);
return to >= from;
});
var flatpickr_weekdays = {
shorthand: [ __("Sun"), __("Mon"), __("Tue"), __("Wed"), __("Thu"), __("Fri"), __("Sat")],
longhand: [ __("Sunday"), __("Monday"), __("Tuesday"), __("Wednesday"), __("Thursday"), __("Friday"), __("Saturday") ]
};
var flatpickr_months = {
shorthand: [ __("Jan"), __("Feb"), __("Mar"), __("Apr"), __("May"), __("Jun"), __("Jul"), __("Aug"), __("Sep"), __("Oct"), __("Nov"), __("Dec")],
longhand: [ __("January"), __("February"), __("March"), __("April"), __("May"), __("June"), __("July"), __("August"), __("September"), __("October"), __("November"), __("December")]
};