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

(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc (-207 / +9 lines)
Lines 1-212 Link Here
1
[% USE Asset %]
1
[% USE Koha %]
2
[% USE Koha %]
3
[% USE raw %]
2
<!-- calendar.inc -->
4
<!-- calendar.inc -->
3
<script>
5
<script>
4
6
    var debug    = "[% debug | html %]";
5
var debug    = "[% debug | html %]";
7
    var dateformat_pref = "[% Koha.Preference('dateformat ') | html %]";
6
var dformat  = "[% dateformat | html %]";
8
    var dateformat_string = [% IF ( dateformat == "us" ) %]"mm/dd/yy"[% ELSIF ( dateformat == "metric" ) %]"dd/mm/yy"[% ELSIF ( dateformat == "dmydot" ) %]"dd.mm.yy"[% ELSE %]"yy-mm-dd"[% END %];
7
var sentmsg = 0;
9
    var sentmsg = 0;
8
if (debug > 1) {alert("dateformat: " + dformat + "\ndebug is on (level " + debug + ")");}
10
    var bidi = [% IF(bidi) %] true[% ELSE %] false[% END %];
9
var MSG_PLEASE_ENTER_A_VALID_DATE = (_("Please enter a valid date (should match %s)."));
11
    var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]';
10
11
function is_valid_date(date) {
12
    // An empty string is considered as a valid date for convenient reasons.
13
    if ( date === '' ) return 1;
14
15
    var dateformat = dateformat_str = '[% Koha.Preference('dateformat') | html %]';
16
    if ( dateformat == 'us' ) {
17
        if ( date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1 ) return 0;
18
        dateformat = 'mm/dd/yy';
19
    } else if ( dateformat == 'metric' ) {
20
        if ( date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1 ) return 0;
21
        dateformat = 'dd/mm/yy';
22
    } else if (dateformat == 'iso' ) {
23
        if ( date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1 ) return 0;
24
        dateformat = 'yy-mm-dd';
25
    } else if ( dateformat == 'dmydot' ) {
26
        if ( date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1 ) return 0;
27
        dateformat = 'dd.mm.yy';
28
    }
29
    try {
30
        $.datepicker.parseDate(dateformat, date);
31
    } catch (e) {
32
        return 0;
33
    };
34
    return 1;
35
}
36
37
function get_dateformat_str(dateformat) {
38
    var dateformat_str;
39
    if ( dateformat == 'us' ) {
40
        dateformat_str = 'mm/dd/yyyy';
41
    } else if ( dateformat == 'metric' ) {
42
        dateformat_str = 'dd/mm/yyyy';
43
    } else if (dateformat == 'iso' ) {
44
        dateformat_str = 'yyyy-mm-dd';
45
    } else if ( dateformat == 'dmydot' ) {
46
        dateformat_str = 'dd.mm.yyyy';
47
    }
48
    return dateformat_str;
49
}
50
51
function validate_date (dateText, inst) {
52
    if ( !is_valid_date(dateText) ) {
53
        var dateformat_str = get_dateformat_str( '[% Koha.Preference('dateformat') | html %]' );
54
        alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
55
        $('#'+inst.id).val('');
56
    }
57
}
58
59
function Date_from_syspref(dstring) {
60
        var dateX = dstring.split(/[-/.]/);
61
        if (debug > 1 && sentmsg < 1) {sentmsg++; alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));}
62
        if (dformat === "iso") {
63
                return new Date(dateX[0], (dateX[1] - 1), dateX[2]);  // YYYY-MM-DD to (YYYY,m(0-11),d)
64
        } else if (dformat === "us") {
65
                return new Date(dateX[2], (dateX[0] - 1), dateX[1]);  // MM/DD/YYYY to (YYYY,m(0-11),d)
66
        } else if (dformat === "metric") {
67
                return new Date(dateX[2], (dateX[1] - 1), dateX[0]);  // DD/MM/YYYY to (YYYY,m(0-11),d)
68
        } else if (dformat === "dmydot") {
69
                return new Date(dateX[2], (dateX[1] - 1), dateX[0]);  // DD.MM.YYYY to (YYYY,m(0-11),d)
70
        } else {
71
                if (debug > 0) {alert("KOHA ERROR - Unrecognized date format: " +dformat);}
72
                return 0;
73
        }
74
}
75
76
function DateTime_from_syspref(date_time) {
77
        var parts = date_time.split(" ");
78
        var date = parts[0];
79
        var time = parts[1];
80
        parts = time.split(":");
81
        var hour = parts[0];
82
        var minute = parts[1];
83
84
        if ( hour < 0 || hour > 23 ) {
85
            return 0;
86
        }
87
        if ( minute < 0 || minute > 59 ) {
88
            return 0;
89
        }
90
91
        var datetime = Date_from_syspref( date );
92
93
        if ( isNaN( datetime.getTime() ) ) {
94
            return 0;
95
        }
96
97
        datetime.setHours( hour );
98
        datetime.setMinutes( minute );
99
100
        return datetime;
101
}
102
103
104
/* Instead of including multiple localization files as you would normally see with
105
   jQueryUI we expose the localization strings in the default configuration */
106
jQuery(function($){
107
    $.datepicker.regional[''] = {
108
        closeText: _("Done"),
109
        prevText: _("Prev"),
110
        nextText: _("Next"),
111
        currentText: _("Today"),
112
        monthNames: [_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),
113
        _("July"),_("August"),_("September"),_("October"),_("November"),_("December")],
114
        monthNamesShort: [_("Jan"), _("Feb"), _("Mar"), _("Apr"), _("May"), _("Jun"),
115
        _("Jul"), _("Aug"), _("Sep"), _("Oct"), _("Nov"), _("Dec")],
116
        dayNames: [_("Sunday"), _("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday")],
117
        dayNamesShort: [_("Sun"), _("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat")],
118
        dayNamesMin: [_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa")],
119
        weekHeader: _("Wk"),
120
        dateFormat: "[% IF ( dateformat == "us" ) %]mm/dd/yy[% ELSIF ( dateformat == "metric" ) %]dd/mm/yy[% ELSIF ( dateformat == "dmydot" ) %]dd.mm.yy[% ELSE %]yy-mm-dd[% END %]",
121
        firstDay: '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]',
122
        isRTL: [% IF ( bidi ) %]true[% ELSE %]false[% END %],
123
        showMonthAfterYear: false,
124
        yearSuffix: ''};
125
    $.datepicker.setDefaults($.datepicker.regional['']);
126
});
127
128
/*  jQuery Validator plugin custom method
129
    This allows you to check that a given date falls after another.
130
    It is required that a message be defined.
131
132
   Syntax:
133
       $("#form_id").validate({
134
        rules: {
135
            input_name_of_later_date_field: {
136
                is_date_after: "#input_id_of_earlier_date_field"
137
            },
138
        },
139
        messages: {
140
            input_name_of_later_date_field: {
141
                is_date_after: _("Validation error to be shown, i.e. End date must come after start date")
142
            }
143
        }
144
    });
145
*/
146
147
jQuery.validator.addMethod("is_date_after",
148
function(value, element, params) {
149
    var from = Date_from_syspref( $(params).val() );
150
    var to = Date_from_syspref(value);
151
    return to > from;
152
});
153
154
jQuery.validator.addMethod("date_on_or_after",
155
function(value, element, params) {
156
    var from = Date_from_syspref( $(params).val() );
157
    var to = Date_from_syspref(value);
158
    return to >= from;
159
});
160
161
$(document).ready(function(){
162
163
$.datepicker.setDefaults({
164
        showOn: "both",
165
        buttonImage: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAT0lEQVQ4jWNgoAZYd/LVf3IwigGkAuwGLE4hDg9eA4il8RqADVdtLYVjZLVEuwDZAKJcgKxh+zkyXIBuI8lhgG4jOqZdLJACMAygKDNRAgBj9qOB+rWnhAAAAABJRU5ErkJggg==",
166
        buttonImageOnly: true,
167
        buttonText: _("Select date"),
168
        changeMonth: true,
169
        changeYear: true,
170
        showButtonPanel: true,
171
        showOtherMonths: true,
172
        selectOtherMonths: true,
173
        yearRange: "c-100:c+10"
174
    });
175
176
$("#dateofbirth").datepicker({
177
        yearRange: "c-100:c"
178
    });
179
180
    $(".futuredate").datepicker({
181
        minDate: 1, // require that hold suspended until date is after today
182
    });
183
184
    $( ".datepicker" ).datepicker({
185
        onClose: function(dateText, inst) {
186
            validate_date(dateText, inst);
187
        },
188
    }).on("change", function(e, value) {
189
        if ( ! is_valid_date( $(this).val() ) ) {$(this).val("");}
190
    });
191
    // http://jqueryui.com/demos/datepicker/#date-range
192
    var dates = $( ".datepickerfrom, .datepickerto" ).datepicker({
193
        changeMonth: true,
194
        numberOfMonths: 1,
195
        onSelect: function( selectedDate ) {
196
            var option = this.id == "from" ? "minDate" : "maxDate",
197
                instance = $( this ).data( "datepicker" );
198
                date = $.datepicker.parseDate(
199
                    instance.settings.dateFormat ||
200
                    $.datepicker._defaults.dateFormat,
201
                    selectedDate, instance.settings );
202
            dates.not( this ).datepicker( "option", option, date );
203
        },
204
        onClose: function(dateText, inst) {
205
            validate_date(dateText, inst);
206
        },
207
    }).on("change", function(e, value) {
208
        if ( ! is_valid_date( $(this).val() ) ) {$(this).val("");}
209
    });
210
});
211
</script>
12
</script>
212
<!-- / calendar.inc -->
13
<!-- / calendar.inc -->
14
[% Asset.js("js/calendar.js") | $raw %]
(-)a/koha-tmpl/intranet-tmpl/prog/js/calendar.js (-1 / +218 lines)
Line 0 Link Here
0
- 
1
/* global debug sentmsg __ dateformat_pref dateformat_string bidi calendarFirstDayOfWeek */
2
/* exported DateTime_from_syspref */
3
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
4
if (debug > 1) {
5
    alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
6
}
7
8
function is_valid_date(date) {
9
    // An empty string is considered as a valid date for convenient reasons.
10
    if (date === '') return 1;
11
12
    var dateformat = dateformat_string;
13
    if (dateformat == 'us') {
14
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
15
        dateformat = 'mm/dd/yy';
16
    } else if (dateformat == 'metric') {
17
        if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
18
        dateformat = 'dd/mm/yy';
19
    } else if (dateformat == 'iso') {
20
        if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
21
        dateformat = 'yy-mm-dd';
22
    } else if (dateformat == 'dmydot') {
23
        if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
24
        dateformat = 'dd.mm.yy';
25
    }
26
    try {
27
        $.datepicker.parseDate(dateformat, date);
28
    } catch (e) {
29
        return 0;
30
    }
31
    return 1;
32
}
33
34
function get_dateformat_str(dateformat) {
35
    var dateformat_str;
36
    if (dateformat == 'us') {
37
        dateformat_str = 'mm/dd/yyyy';
38
    } else if (dateformat == 'metric') {
39
        dateformat_str = 'dd/mm/yyyy';
40
    } else if (dateformat == 'iso') {
41
        dateformat_str = 'yyyy-mm-dd';
42
    } else if (dateformat == 'dmydot') {
43
        dateformat_str = 'dd.mm.yyyy';
44
    }
45
    return dateformat_str;
46
}
47
48
function validate_date(dateText, inst) {
49
    if (!is_valid_date(dateText)) {
50
        var dateformat_str = get_dateformat_str( dateformat_pref );
51
        alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
52
        $('#' + inst.id).val('');
53
    }
54
}
55
56
function Date_from_syspref(dstring) {
57
    var dateX = dstring.split(/[-/.]/);
58
    if (debug > 1 && sentmsg < 1) {
59
        sentmsg++;
60
        alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
61
    }
62
    if (dateformat_pref === "iso") {
63
        return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
64
    } else if (dateformat_pref === "us") {
65
        return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
66
    } else if (dateformat_pref === "metric") {
67
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
68
    } else if (dateformat_pref === "dmydot") {
69
        return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
70
    } else {
71
        if (debug > 0) {
72
            alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref);
73
        }
74
        return 0;
75
    }
76
}
77
78
function DateTime_from_syspref(date_time) {
79
    var parts = date_time.split(" ");
80
    var date = parts[0];
81
    var time = parts[1];
82
    parts = time.split(":");
83
    var hour = parts[0];
84
    var minute = parts[1];
85
86
    if (hour < 0 || hour > 23) {
87
        return 0;
88
    }
89
    if (minute < 0 || minute > 59) {
90
        return 0;
91
    }
92
93
    var datetime = Date_from_syspref(date);
94
95
    if (isNaN(datetime.getTime())) {
96
        return 0;
97
    }
98
99
    datetime.setHours(hour);
100
    datetime.setMinutes(minute);
101
102
    return datetime;
103
}
104
105
/* Instead of including multiple localization files as you would normally see with
106
   jQueryUI we expose the localization strings in the default configuration */
107
jQuery(function ($) {
108
    $.datepicker.regional[''] = {
109
        closeText: __("Done"),
110
        prevText: __("Prev"),
111
        nextText: __("Next"),
112
        currentText: __("Today"),
113
        monthNames: [__("January"), __("February"), __("March"), __("April"), __("May"), __("June"),
114
            __("July"), __("August"), __("September"), __("October"), __("November"), __("December")
115
        ],
116
        monthNamesShort: [__("Jan"), __("Feb"), __("Mar"), __("Apr"), __("May"), __("Jun"),
117
            __("Jul"), __("Aug"), __("Sep"), __("Oct"), __("Nov"), __("Dec")
118
        ],
119
        dayNames: [__("Sunday"), __("Monday"), __("Tuesday"), __("Wednesday"), __("Thursday"), __("Friday"), __("Saturday")],
120
        dayNamesShort: [__("Sun"), __("Mon"), __("Tue"), __("Wed"), __("Thu"), __("Fri"), __("Sat")],
121
        dayNamesMin: [__("Su"), __("Mo"), __("Tu"), __("We"), __("Th"), __("Fr"), __("Sa")],
122
        weekHeader: __("Wk"),
123
        dateFormat: dateformat_string,
124
        firstDay: calendarFirstDayOfWeek,
125
        isRTL: bidi,
126
        showMonthAfterYear: false,
127
        yearSuffix: ''
128
    };
129
    $.datepicker.setDefaults($.datepicker.regional['']);
130
});
131
132
/*  jQuery Validator plugin custom method
133
    This allows you to check that a given date falls after another.
134
    It is required that a message be defined.
135
136
   Syntax:
137
       $("#form_id").validate({
138
        rules: {
139
            input_name_of_later_date_field: {
140
                is_date_after: "#input_id_of_earlier_date_field"
141
            },
142
        },
143
        messages: {
144
            input_name_of_later_date_field: {
145
                is_date_after: _("Validation error to be shown, i.e. End date must come after start date")
146
            }
147
        }
148
    });
149
*/
150
151
jQuery.validator.addMethod("is_date_after",
152
    function (value, element, params) {
153
        var from = Date_from_syspref($(params).val());
154
        var to = Date_from_syspref(value);
155
        return to > from;
156
    });
157
158
jQuery.validator.addMethod("date_on_or_after",
159
    function (value, element, params) {
160
        var from = Date_from_syspref($(params).val());
161
        var to = Date_from_syspref(value);
162
        return to >= from;
163
    });
164
165
$(document).ready(function () {
166
167
    $.datepicker.setDefaults({
168
        showOn: "both",
169
        buttonImage: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAT0lEQVQ4jWNgoAZYd/LVf3IwigGkAuwGLE4hDg9eA4il8RqADVdtLYVjZLVEuwDZAKJcgKxh+zkyXIBuI8lhgG4jOqZdLJACMAygKDNRAgBj9qOB+rWnhAAAAABJRU5ErkJggg==",
170
        buttonImageOnly: true,
171
        buttonText: __("Select date"),
172
        changeMonth: true,
173
        changeYear: true,
174
        showButtonPanel: true,
175
        showOtherMonths: true,
176
        selectOtherMonths: true,
177
        yearRange: "c-100:c+10"
178
    });
179
180
    $("#dateofbirth").datepicker({
181
        yearRange: "c-100:c"
182
    });
183
184
    $(".futuredate").datepicker({
185
        minDate: 1, // require that hold suspended until date is after today
186
    });
187
188
    $(".datepicker").datepicker({
189
        onClose: function (dateText, inst) {
190
            validate_date(dateText, inst);
191
        },
192
    }).on("change", function () {
193
        if (!is_valid_date($(this).val())) {
194
            $(this).val("");
195
        }
196
    });
197
    // http://jqueryui.com/demos/datepicker/#date-range
198
    var dates = $(".datepickerfrom, .datepickerto").datepicker({
199
        changeMonth: true,
200
        numberOfMonths: 1,
201
        onSelect: function (selectedDate) {
202
            var option = this.id == "from" ? "minDate" : "maxDate",
203
                instance = $(this).data("datepicker");
204
            var date = $.datepicker.parseDate(
205
                instance.settings.dateFormat ||
206
                $.datepicker._defaults.dateFormat,
207
                selectedDate, instance.settings);
208
            dates.not(this).datepicker("option", option, date);
209
        },
210
        onClose: function (dateText, inst) {
211
            validate_date(dateText, inst);
212
        },
213
    }).on("change", function () {
214
        if (!is_valid_date($(this).val())) {
215
            $(this).val("");
216
        }
217
    });
218
});

Return to bug 26261