From 9192007ec074d84cc698bdbfb09922aa916a47fe Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Fri, 9 Feb 2024 16:59:45 +0000
Subject: [PATCH] Bug 6796: Take CalenderFirstDayOfWeek into account

This patch adds accounting for the CalendarFirstDayOfWeek preference in
the libraries summary display table
---
 .../prog/en/modules/admin/branches.tt         | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

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 7da93b93234..1e4e26c0dca 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tt
@@ -638,6 +638,7 @@
         var table_settings = [% TablesSettings.GetTableSettings( 'admin', 'libraries', 'libraries', 'json' ) | $raw %];
         var saved_table = localStorage.getItem("DataTables_libraries_/cgi-bin/koha/admin/branches.pl");
         var updated_settings = get_columns_saved_state(saved_table, table_settings);
+        var calendarFirstDayOfWeek = '[% Koha.Preference('CalendarFirstDayOfWeek') | html %]';
 
         $(document).ready(function() {
 
@@ -747,16 +748,18 @@
                     },
                     {
                         "data": function( row, type, val, meta ) {
-                            const daynames = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
-                            var daycount = 0;
+                            const daysOfWeek = [ _("Sunday"), _("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday") ];
+
                             var result = '<table id="library_hours_table"><thead><tr><th>Day</th><th>Open time</th><th>Close time</th></tr></thead><tbody>';
-                            while ( row.library_hours[daycount] ) {
-                                result += '<tr id="hours_'+daycount+'">';
-                                result += '<td>'+daynames[daycount]+'</td>';
-                                result += '<td><span>'+row.library_hours[daycount].open_time+'</span></td>';
-                                result += '<td><span>'+row.library_hours[daycount].close_time+'</span></td>';
+                            let counter = 0;
+                            for (let i = calendarFirstDayOfWeek; counter < 7; i++) {
+                                const index = i % 7; // Wrap around the index using modulo operator
+                                result += '<tr id="hours_'+index+'">';
+                                result += '<td>'+daysOfWeek[index]+'</td>';
+                                result += '<td><span>'+row.library_hours[index].open_time+'</span></td>';
+                                result += '<td><span>'+row.library_hours[index].close_time+'</span></td>';
                                 result += '</tr>';
-                                daycount++;
+                                counter++;
                             }
                             result += '</tbody></table>';
                             return result;
-- 
2.43.0