View | Details | Raw Unified | Return to bug 30673
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc (-1 / +1 lines)
Lines 5-11 Link Here
5
[% FILTER collapse %]
5
[% FILTER collapse %]
6
<script>
6
<script>
7
    var debug    = "[% debug | html %]";
7
    var debug    = "[% debug | html %]";
8
    var dateformat_pref = "[% Koha.Preference('dateformat ') | html %]";
8
    var dateformat_pref = "[% Koha.Preference('dateformat') | html %]";
9
    var flatpickr_dateformat_string = "";
9
    var flatpickr_dateformat_string = "";
10
    switch ( dateformat_pref ){
10
    switch ( dateformat_pref ){
11
        case "us":
11
        case "us":
(-)a/koha-tmpl/intranet-tmpl/prog/js/calendar.js (-30 / +29 lines)
Lines 1-74 Link Here
1
/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string bidi calendarFirstDayOfWeek */
1
/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string */
2
/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months */
2
/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months validate_date */
3
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
3
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
4
if (debug > 1) {
4
if (debug > 1) {
5
    alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
5
    alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
6
}
6
}
7
7
8
function is_valid_date(date) {
8
function is_valid_date( date, dateformat_string ) {
9
    // An empty string is considered as a valid date for convenient reasons.
9
    // An empty string is considered as a valid date for convenient reasons.
10
    if (date === '') return 1;
10
    if (date === '') return 1;
11
    var dateformat = flatpickr_dateformat_string;
11
    let dateformat;
12
    if (dateformat == 'us') {
12
    const ourformat = dateformat_string === undefined ? flatpickr_dateformat_string : dateformat_string;
13
    /* Check that the date string matches the general date format rules */
14
    if (ourformat == 'm/d/Y') {
15
        dateformat = "us";
13
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
16
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
14
        dateformat = 'm/d/Y';
17
    } else if (ourformat == 'd/m/Y') {
15
    } else if (dateformat == 'metric') {
18
        dateformat = "metric";
16
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
19
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
17
        dateformat = 'd/m/Y';
20
    } else if (ourformat == 'd.m.Y') {
18
    } else if (dateformat == 'iso') {
21
        dateformat = "dmydot";
19
        if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
20
        dateformat = 'Y-m-d';
21
    } else if (dateformat == 'dmydot') {
22
        if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
22
        if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
23
        dateformat = 'd.m.Y';
23
    } else if (ourformat == 'Y-m-d') {
24
    }
24
        dateformat = "iso";
25
    try {
25
        if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
26
        flatpickr.parseDate(date, dateformat);
27
    } catch (e) {
28
        return 0;
29
    }
26
    }
30
    return 1;
27
    /* Check that the date is a valid date */
28
    const dateobj = Date_from_syspref(date, dateformat);
29
    return ( dateobj instanceof Date && !isNaN( dateobj ) ) ? 1 : 0;
31
}
30
}
32
31
33
function get_dateformat_str(dateformat) {
32
function get_dateformat_str(dateformat) {
34
    var dateformat_str;
33
    var dateformat_str;
35
    if (dateformat == 'us') {
34
    if (dateformat == 'us') {
36
        dateformat_str = 'mm/dd/yyyy';
35
        dateformat_str = __("mm/dd/yyyy");
37
    } else if (dateformat == 'metric') {
36
    } else if (dateformat == 'metric') {
38
        dateformat_str = 'dd/mm/yyyy';
37
        dateformat_str = __("dd/mm/yyyy");
39
    } else if (dateformat == 'iso') {
38
    } else if (dateformat == 'iso') {
40
        dateformat_str = 'yyyy-mm-dd';
39
        dateformat_str = __("yyyy-mm-dd");
41
    } else if (dateformat == 'dmydot') {
40
    } else if (dateformat == 'dmydot') {
42
        dateformat_str = 'dd.mm.yyyy';
41
        dateformat_str = __("dd.mm.yyyy");
43
    }
42
    }
44
    return dateformat_str;
43
    return dateformat_str;
45
}
44
}
46
45
47
function validate_date(dateText, inst) {
46
function validate_date(dateText, inst) {
48
    if (!is_valid_date(dateText)) {
47
    if (!is_valid_date( dateText, inst.config.dateFormat )) {
49
        var dateformat_str = get_dateformat_str( dateformat_pref );
48
        var dateformat_str = get_dateformat_str( dateformat_pref );
50
        alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
49
        alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
51
        inst.clear();
50
        inst.clear();
52
    }
51
    }
53
}
52
}
54
53
55
function Date_from_syspref(dstring) {
54
function Date_from_syspref( dstring, dateformat = dateformat_pref ) {
56
    var dateX = dstring.split(/[-/.]/);
55
    var dateX = dstring.split(/[-/.]/);
57
    if (debug > 1 && sentmsg < 1) {
56
    if (debug > 1 && sentmsg < 1) {
58
        sentmsg++;
57
        sentmsg++;
59
        alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
58
        alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
60
    }
59
    }
61
    if (dateformat_pref === "iso") {
60
    if (dateformat === "iso") {
62
        return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
61
        return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
63
    } else if (dateformat_pref === "us") {
62
    } else if (dateformat === "us") {
64
        return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
63
        return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
65
    } else if (dateformat_pref === "metric") {
64
    } else if (dateformat === "metric") {
66
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
65
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
67
    } else if (dateformat_pref === "dmydot") {
66
    } else if (dateformat === "dmydot") {
68
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
67
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
69
    } else {
68
    } else {
70
        if (debug > 0) {
69
        if (debug > 0) {
71
            alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref);
70
            alert("KOHA ERROR - Unrecognized date format: " + dateformat);
72
        }
71
        }
73
        return 0;
72
        return 0;
74
    }
73
    }
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/calendar.inc (-22 / +56 lines)
Lines 12-18 Link Here
12
        longhand: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
12
        longhand: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
13
    };
13
    };
14
    var debug    = "[% debug | html %]";
14
    var debug    = "[% debug | html %]";
15
    var dateformat_pref = "[% Koha.Preference('dateformat ') | html %]";
15
    var dateformat_pref = "[% Koha.Preference('dateformat') | html %]";
16
    var sentmsg = 0;
16
    var sentmsg = 0;
17
    if (debug > 1) {alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");}
17
    if (debug > 1) {alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");}
18
    var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]';
18
    var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]';
Lines 84-114 Link Here
84
        },
84
        },
85
    });
85
    });
86
86
87
    var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
87
    var MSG_PLEASE_ENTER_A_VALID_DATE = ( _("Please enter a valid date (should match %s).") );
88
88
89
    function is_valid_date(date) {
89
    function is_valid_date( date, dateformat_string ) {
90
        // An empty string is considered as a valid date for convenient reasons.
90
        // An empty string is considered as a valid date for convenient reasons.
91
        if (date === '') return 1;
91
        if (date === '') return 1;
92
        var dateformat = flatpickr_dateformat_string;
92
        let dateformat;
93
        switch ( dateformat_pref ){
93
        const ourformat = dateformat_string === undefined ? flatpickr_dateformat_string : dateformat_string;
94
            case "us":
94
        /* Check that the date string matches the general date format rules */
95
                flatpickr_dateformat_string = "m/d/Y";
95
        if (ourformat == 'm/d/Y') {
96
                break;
96
            dateformat = "us";
97
            case "metric":
97
            if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
98
                flatpickr_dateformat_string = "d/m/Y";
98
        } else if (ourformat == 'd/m/Y') {
99
                break;
99
            dateformat = "metric";
100
            case "dmydot":
100
            if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
101
                flatpickr_dateformat_string = "d.m.Y";
101
        } else if (ourformat == 'd.m.Y') {
102
                break;
102
            dateformat = "dmydot";
103
            default:
103
            if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
104
                flatpickr_dateformat_string = "Y-m-d";
104
        } else if (ourformat == 'Y-m-d') {
105
            dateformat = "iso";
106
            if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
105
        }
107
        }
106
        try {
108
        /* Check that the date is a valid date */
107
            flatpickr.parseDate(date, dateformat);
109
        const dateobj = Date_from_syspref(date, dateformat);
108
        } catch (e) {
110
        return ( dateobj instanceof Date && !isNaN( dateobj ) ) ? 1 : 0;
109
            return 0;
111
    }
112
113
    function get_dateformat_str(dateformat) {
114
        var dateformat_str;
115
        if (dateformat == 'us') {
116
            dateformat_str = _("mm/dd/yyyy");
117
        } else if (dateformat == 'metric') {
118
            dateformat_str = _("dd/mm/yyyy");
119
        } else if (dateformat == 'iso') {
120
            dateformat_str = _("yyyy-mm-dd");
121
        } else if (dateformat == 'dmydot') {
122
            dateformat_str = _("dd.mm.yyyy");
110
        }
123
        }
111
        return 1;
124
        return dateformat_str;
112
    }
125
    }
113
126
114
    function validate_date(dateText, inst) {
127
    function validate_date(dateText, inst) {
Lines 119-124 Link Here
119
        }
132
        }
120
    }
133
    }
121
134
135
    function Date_from_syspref( dstring, dateformat = dateformat_pref ) {
136
        var dateX = dstring.split(/[-/.]/);
137
        if (debug > 1 && sentmsg < 1) {
138
            sentmsg++;
139
            alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
140
        }
141
        if (dateformat === "iso") {
142
            return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
143
        } else if (dateformat === "us") {
144
            return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
145
        } else if (dateformat === "metric") {
146
            return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
147
        } else if (dateformat === "dmydot") {
148
            return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
149
        } else {
150
            if (debug > 0) {
151
                alert("KOHA ERROR - Unrecognized date format: " + dateformat);
152
            }
153
            return 0;
154
        }
155
    }
156
122
        $(document).ready(function(){
157
        $(document).ready(function(){
123
            $(".flatpickr").flatpickr();
158
            $(".flatpickr").flatpickr();
124
159
125
- 

Return to bug 30673