From 4caedf3911b652271b8512b8129ed751b6505567 Mon Sep 17 00:00:00 2001
From: Maryse Simard <maryse.simard@inlibro.com>
Date: Thu, 20 Oct 2022 16:52:19 -0400
Subject: [PATCH] Bug 17015: Add an option to view past holiday details in
 table

---
 Koha/DiscreteCalendar.pm                      | 10 ++--
 .../en/modules/tools/discrete_calendar.tt     | 59 ++++++++++++++++---
 tools/discrete_calendar.pl                    |  4 +-
 3 files changed, 59 insertions(+), 14 deletions(-)

diff --git a/Koha/DiscreteCalendar.pm b/Koha/DiscreteCalendar.pm
index fd80e1dd34..22b865434f 100644
--- a/Koha/DiscreteCalendar.pm
+++ b/Koha/DiscreteCalendar.pm
@@ -337,7 +337,7 @@ Returns an array of all the date objects that are unique holidays.
 
 sub get_unique_holidays {
     my $self = shift;
-    my $exclude_past = shift || 1;
+    my $exclude_past = shift // 1;
     my $branchcode = $self->{branchcode};
     my @unique_holidays;
     my $schema = Koha::Database->new->schema;
@@ -377,7 +377,7 @@ Returns an array of all the date objects that are float holidays.
 
 sub get_float_holidays {
     my $self = shift;
-    my $exclude_past = shift || 1;
+    my $exclude_past = shift // 1;
     my $branchcode = $self->{branchcode};
     my @float_holidays;
     my $schema = Koha::Database->new->schema;
@@ -417,7 +417,7 @@ Returns an array of all the date objects that are float holidays in need of vali
 
 sub get_need_validation_holidays {
     my $self = shift;
-    my $exclude_past = shift || 1;
+    my $exclude_past = shift // 1;
     my $branchcode = $self->{branchcode};
     my @need_validation_holidays;
     my $schema = Koha::Database->new->schema;
@@ -457,7 +457,7 @@ Returns an array of all the date objects that are repeatable holidays.
 
 sub get_repeatable_holidays {
     my $self = shift;
-    my $exclude_past = shift || 1;
+    my $exclude_past = shift // 1;
     my $branchcode = $self->{branchcode};
     my @repeatable_holidays;
     my $schema = Koha::Database->new->schema;
@@ -497,7 +497,7 @@ Returns an array of all the date objects that are weekly holidays.
 
 sub get_week_days_holidays {
     my $self = shift;
-    my $exclude_past = shift || 1;
+    my $exclude_past = shift // 1;
     my $branchcode = $self->{branchcode};
     my @week_days;
     my $schema = Koha::Database->new->schema;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/discrete_calendar.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/discrete_calendar.tt
index 863d39c0cf..cfd6e4f359 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/discrete_calendar.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/discrete_calendar.tt
@@ -225,7 +225,7 @@
                         <div id="holiday-list">
                             [% IF ( NEED_VALIDATION_HOLIDAYS ) %]
                             <h3>Need validation holidays</h3>
-                            <table id="holidaysvalidation">
+                            <table id="holidaysvalidation" class="dataTable no-footer">
                                 <thead>
                                     <tr>
                                         <th class="validation">Date</th>
@@ -247,7 +247,7 @@
 
                             [% IF ( WEEKLY_HOLIDAYS ) %]
                             <h3>Weekly - Repeatable holidays</h3>
-                            <table id="holidayweeklyrepeatable">
+                            <table id="holidayweeklyrepeatable" class="dataTable no-footer">
                                 <thead>
                                     <tr>
                                         <th class="repeatableweekly">Day of week</th>
@@ -269,7 +269,7 @@
 
                             [% IF ( REPEATABLE_HOLIDAYS ) %]
                             <h3>Yearly - Repeatable holidays</h3>
-                            <table id="holidaysyearlyrepeatable">
+                            <table id="holidaysyearlyrepeatable" class="dataTable no-footer">
                                 <thead>
                                     <tr>
                                         [% IF ( dateformat == "metric" ) %]
@@ -299,7 +299,11 @@
 
                             [% IF ( UNIQUE_HOLIDAYS ) %]
                             <h3>Unique holidays</h3>
-                            <table id="holidaysunique">
+                            <label class="controls">
+                                <input type="checkbox" name="show_past" id="show_past_holidaysunique" class="show_past" />
+                                Show past entries
+                            </label>
+                            <table id="holidaysunique" class="dataTable no-footer">
                                 <thead>
                                     <tr>
                                         <th class="holiday">Date</th>
@@ -309,7 +313,7 @@
                                 </thead>
                                 <tbody>
                                     [% FOREACH HOLIDAYS_LOO IN UNIQUE_HOLIDAYS %]
-                                    <tr>
+                                    <tr data-date="[% HOLIDAYS_LOO.date | html %]">
                                         <td><a href="#main" onclick="go_to_date('[% HOLIDAYS_LOO.date | html %]')"><span title="[% HOLIDAYS_LOO.DATE_SORT | html %]">[% HOLIDAYS_LOO.outputdate | html %]</span></a></td>
                                         <td>[% HOLIDAYS_LOO.note | html %]</td>
                                         <td>[% HOLIDAYS_LOO.description.replace('\\\r\\\n', '<br />') | html %]</td>
@@ -321,7 +325,11 @@
 
                             [% IF ( FLOAT_HOLIDAYS ) %]
                             <h3>Floating holidays</h3>
-                            <table id="holidaysfloat">
+                            <label class="controls">
+                                <input type="checkbox" name="show_past" id="show_past_holidaysfloat" class="show_past" />
+                                Show past entries
+                            </label>
+                            <table id="holidaysfloat" class="dataTable no-footer">
                                 <thead>
                                     <tr>
                                         <th class="float">Date</th>
@@ -331,7 +339,7 @@
                                 </thead>
                                 <tbody>
                                     [% FOREACH float_holiday IN FLOAT_HOLIDAYS %]
-                                    <tr>
+                                    <tr data-date="[% float_holiday.date | html %]">
                                         <td><a href="#main" onclick="go_to_date('[% float_holiday.date | html %]')"><span title="[% float_holiday.DATE_SORT | html %]">[% float_holiday.outputdate | html %]</span></a></td>
                                         <td>[% float_holiday.note | html %]</td>
                                         <td>[% float_holiday.description.replace('\\\r\\\n', '<br />') | html %]</td>
@@ -601,6 +609,25 @@
             return true;
         }
 
+        /* Custom table search configuration: If a table row
+            has an "expired" class, hide it UNLESS the
+            show_expired checkbox is checked */
+        $.fn.dataTable.ext.search.push(
+            function( settings, searchData, index, rowData, counter ) {
+                var table = settings.nTable.id;
+                var row = $(settings.aoData[index].nTr);
+                if( row.hasClass("date_past") && !$("#show_past_" + table ).prop("checked") ){
+                    return false;
+                } else {
+                    return true;
+                }
+            }
+        );
+
+        // Create current date variable
+        var date = new Date();
+        var datestring = date.toISOString().substring(0, 10);
+
         $(document).ready(function() {
             $(".hint").hide();
             $("#days_of_week").hide();
@@ -616,6 +643,24 @@
                 var first_td = $(this).find('td').first();
                 first_td.html(weekdays[first_td.html()]);
             });
+            $("#holidayweeklyrepeatable").dataTable($.extend(true, {}, dataTablesDefaults, {
+                "sDom": 't',
+                "bPaginate": false
+            }));
+            var tables = $("#holidaysyearlyrepeatable, #holidaysunique, #holidaysfloat").DataTable($.extend(true, {}, dataTablesDefaults, {
+                "sDom": 't',
+                "bPaginate": false,
+                "createdRow": function( row, data, dataIndex ) {
+                    var holiday = $(row).data("date");
+                    if( holiday < datestring ){
+                        $(row).addClass("date_past");
+                    }
+                }
+            }));
+
+            $(".show_past").on("change", function(){
+                tables.draw();
+            });
 
             $("a.helptext").click(function () {
                 $(this).parent().find(".hint").toggle();
diff --git a/tools/discrete_calendar.pl b/tools/discrete_calendar.pl
index f7c041520f..96dd7daa31 100755
--- a/tools/discrete_calendar.pl
+++ b/tools/discrete_calendar.pl
@@ -146,9 +146,9 @@ my @week_days = $calendar->get_week_days_holidays();
 my @repeatable_holidays = $calendar->get_repeatable_holidays();
 
 #discrete_calendar unique holidays
-my @unique_holidays =$calendar->get_unique_holidays();
+my @unique_holidays =$calendar->get_unique_holidays(0);
 #discrete_calendar floating holidays
-my @float_holidays =$calendar->get_float_holidays();
+my @float_holidays =$calendar->get_float_holidays(0);
 #discrete_caledar need validation holidays
 my @need_validation_holidays =$calendar->get_need_validation_holidays();
 
-- 
2.25.1