From aecfaa5911e7ca99f73b28d3ab24bfa7f45fb661 Mon Sep 17 00:00:00 2001 From: CJ Lynce Date: Fri, 6 Sep 2024 20:51:38 +0000 Subject: [PATCH] Bug 36594: Library hours in koha/admin/branches.pl now adhere to selected time and calendar system preferences. -Newly created or edited libraries no longer display 'null' for undefined open and close times. -Libraries without any defined hours will state such instead of displaying the hours table. -The CalendarFirstDayOfWeek system preferences is now respected when viewing a library with defined hours. -Time displays and inputs now follow the TimeFormat system preference. -Times are no longer displayed with seconds. As part of this patch, a new Template Filter, KohaTimes, has been added to allow for proper formatting of time strings based on systems preferences. To Test: 1. Login to staff interface 2. Go to Koha administration > Basic parameters > Libraries 3. Edit any library or create a new library. Do not set hours. 4. Save the library. *Note that on the library list, in the hours column, all days are displayed with 'null'. 5. Edit or create a 2nd library. 6. Try to manually enter a time (not using the flatpickr modal) *Note that the mask wants a date format, not a time format. 7. Set some hours for the library, leaving at least one day without hours. Save. *Note that in the library list, in the hours column, defined times are in the format HH:MM:SS. 7. Edit the system preference CalendarFirstDayOfWeek to set a day other than Sunday as the first day of the week. 8. Return to Koha administration > Basic parameters > Libraries 9. Click on the name of a specific library with hours to view details. *Note that the order of the calendar weeek still starts with Sunday. 10. The order of the calendar week still starts with Sunday. 11. Edit the system preference TimeFormat and set to 12-hours. 12. Return to Koha administration > Basic parameters > Libraries. *Note that the time format on the branches list is not in 12-hour format. 13. Click on the name of a specific library with hours to view details. *Note that the time format in the branch details screen is not in 12-hour format. 14. Apply Patch. 15. Return to Koha administration > Basic parameters > Libraries 16. Verify that your library with all undefined times shows "Library hours not set". 17. Verify that your library with defined hours is showing times in HH:MM format, and days without defined hours are blank. 18. Verify that your library with defined hours is respecting the TimeFormat preferences. 19. Click on the name of a library with hours to view details. 20. Verify that the hours respect the CalendarFirstDayOfWeek and TimeFormat preferences. 21. Click on the name of another library without hours to view details. Verify these libraries show as "No opening hours have been set" in the branch detail view. 22. Edit a library's hours. When inputing, try to manually enter a time (not using the flatpickr modal). Verify that you can enter time in the proper (12 or 24 hours) format. Sponsored-by: Westlake Porter Public Library Signed-off-by: Olivier V Signed-off-by: Martin Renvoize --- Koha/Template/Plugin/KohaTimes.pm | 41 +++++++++++++++++++ .../prog/en/includes/calendar.inc | 8 +++- .../prog/en/includes/js-date-format.inc | 27 +++++++++++- .../prog/en/modules/admin/branches.tt | 32 ++++++++++++--- 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 Koha/Template/Plugin/KohaTimes.pm diff --git a/Koha/Template/Plugin/KohaTimes.pm b/Koha/Template/Plugin/KohaTimes.pm new file mode 100644 index 0000000000..c65c68eaf6 --- /dev/null +++ b/Koha/Template/Plugin/KohaTimes.pm @@ -0,0 +1,41 @@ +package Koha::Template::Plugin::KohaTimes; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Template::Plugin::Filter; +use base qw( Template::Plugin::Filter ); + +our $DYNAMIC = 1; + +sub filter { + my ( $self, $text, $args, $config ) = @_; + return "" unless $text; + + my ( $hours, $minutes, $seconds ) = split( ':', $text ); + if ( C4::Context->preference('TimeFormat') == "12hr" ) { + my $ampm = ( $hours >= 12 ) ? 'pm' : 'am'; + $hours = ( $hours == 0 ) ? "12" : $hours; + $hours = ( $hours > 12 ) ? ( $hours - 12 ) : $hours; + $hours = sprintf '%.2u', $hours; + + return "$hours:$minutes $ampm"; + } else { + return "$hours:$minutes"; + } +} + +1; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc index 9f1cdb6ceb..8280be9ba9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/calendar.inc @@ -85,11 +85,11 @@ instance.set('allowInput',false); } }); - /* When flatpickr instance is ready, add maskito input mask */ var thisInput = instance.input; let accepts_time = $(thisInput).data('flatpickr-enable-time'); let accepts_period = $(thisInput).data('flatpickr-period'); + let accepts_time_only = $(thisInput).data('flatpickr-time-only'); let maskitoOptions = {}; if ( !accepts_period ) { if ( accepts_time ) { @@ -99,6 +99,9 @@ dateSeparator: delimiter, overwiteMode: 'replace', }); + } else if ( accepts_time_only ) { + maskitoOptions = maskitoTimeOptionsGenerator({ + }); } else { maskitoOptions = maskitoDateOptionsGenerator({ mode: altinput_dateformat, @@ -112,6 +115,7 @@ onChange: function( selectedDates, dateText, instance) { var thisInput = instance.input; let accepts_time = $(thisInput).data('flatpickr-enable-time'); + let accepts_time_only = $(thisInput).data('flatpickr-time-only'); let accepts_period = $(thisInput).data('flatpickr-period'); if ( !accepts_period ) { if ( accepts_time ) { @@ -234,6 +238,8 @@ options['enableTime'] = true; options['noCalendar'] = true; options['dateFormat'] = "H:i"; + options['defaultHour'] = "0"; + options['defaultMinute'] = "0"; options['altFormat'] = flatpickr_timeformat_string; options['plugins'] = []; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc index bce0620c48..73bc40b695 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/js-date-format.inc @@ -34,7 +34,7 @@ }; /** - * A JS equivilent of the KohaDates TT Plugin. Passed an rfc3339 formatted date string, + * A JS equivalent of the KohaDates TT Plugin. Passed an rfc3339 formatted date string, * or JS Date, the function will return a date string formatted as per the koha instance config. * @param {String|Date} value rfc3339 formatted date string or a JS Date object. * @param {Object} [options] Optional set of switches for changing date handling @@ -95,6 +95,31 @@ return m.format("YYYY-MM-DD"); } + /** + * A JS equivalent of the KohaTimes TT Plugin. Passed a time in HH:MM:SS or HH:MM format + * the function will return a time string formatted as per the koha instance config. + */ + + window.$kohatime = function(value) { + if(!value) return 'invalid time'; + var intime = value.split(':'); + var hours = intime[0]; + var minutes = intime[1]; + + if ( hours < 0 || hours > 23 || minutes < 0 || minutes > 59 ) { return 'invalid time'; } + + if ( def_time_format == '12hr') { + var ampm = ( hours >= 12 ) ? 'pm' : 'am'; + hours = ( hours == 0 ) ? "12" : hours; + hours = ( hours > 12 ) ? ( hours - 12 ) : hours; + + return hours.toString().padStart(2,'0') + ':' + minutes.toString().padStart(2,'0') + " " + ampm; + } else { + return hours.toString().padStart(2,'0') + ':' + minutes.toString().padStart(2,'0'); + } + } + + })(); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt index af6062a9e9..748385be51 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt @@ -3,6 +3,7 @@ [% USE Asset %] [% USE TablesSettings %] [%- USE KohaSpan -%] +[% USE KohaTimes %] [% PROCESS 'i18n.inc' %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] @@ -556,7 +557,16 @@
  • Opening hours: - [% IF library.library_hours.count > 0 # Existing library %] + [% SET CalendarFirstDayOfWeek = Koha.Preference("CalendarFirstDayOfWeek") %] + [% SET set_hours = 0 %] + [% IF library.library_hours.count > 0 %] + [% FOR i IN [0..6] %] + [% IF library.library_hours.as_list.$i.open_time != null || library.library_hours.as_list.$i.close_time != null %] + [% set_hours = 1 %] + [% END %] + [% END %] + [% END %] + [% IF set_hours > 0 # Existing library %] [% SET library_hours = library.library_hours.as_list %] @@ -575,10 +585,10 @@ [% PROCESS dayname day=d %] [% END %] @@ -757,7 +767,15 @@ { "data": function( row, type, val, meta ) { let result = ''; + let set_hours = 0; if ( row.library_hours.length > 0 ) { + for (let check_counter = 0; check_counter < 7; check_counter++) { + if ( row.library_hours[check_counter].open_time != null || row.library_hours[check_counter].close_time != null ) { + set_hours = 1; + } + } + } + if ( set_hours > 0 ) { const daysOfWeek = [ _("Sunday"), _("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday") ]; result = '
    - [% hr.open_time | html %] + [% IF hr.open_time != null %][% hr.open_time | $KohaTimes %][% END %] - [% hr.close_time | html %] + [% IF hr.close_time != null %][% hr.close_time | $KohaTimes %][% END %]
    '; @@ -766,8 +784,12 @@ const day = i % 7; // Wrap around the day using modulo operator result += ''; result += ''; - result += ''; - result += ''; + result += ''; + result += ''; result += ''; counter++; } -- 2.39.5
    DayOpen timeClose time
    '+daysOfWeek[day]+''+row.library_hours[day].open_time+''+row.library_hours[day].close_time+''; + result += row.library_hours[day].open_time != null ? $kohatime(row.library_hours[day].open_time): ''; + result += ''; + result += row.library_hours[day].close_time != null ? $kohatime(row.library_hours[day].close_time): ''; + result += '