From febefa163e6f43d7ddad059b32e63253d970571d Mon Sep 17 00:00:00 2001
From: Lari Taskula <lari.taskula@hypernova.fi>
Date: Thu, 3 Mar 2022 02:18:58 +0000
Subject: [PATCH] Bug 30200: Add custom tabs to View ILL requests page

Prereqs:
1: Make sure you have enabled an ILL backend and enabled system
   preferecnce ILLModule. For more information see
   https://koha-community.org/manual/21.11/en/html/ILL_requests.html

To test:
1: Note, the following branchcodes, statuses and backends depend
   on your test environment and which ILL backend you are using.
   This example is for Libris backend.

Set system preference ILLRequestsTabs:
- name: tab1
  status:
    - IN_REM
- name: tab2
  status:
    - IN_UTEL
    - IN_LEV
- name: tab3
  status:
    - IN_ANK

2. Add at least 4 ILL requests with the following SQL:
INSERT INTO `illrequests` (borrowernumber, branchcode, status, backend) VALUES (1, 'CPL', 'IN_REM', 'Libris');
INSERT INTO `illrequests` (borrowernumber, branchcode, status, backend) VALUES (1, 'CPL', 'IN_UTEL', 'Libris');
INSERT INTO `illrequests` (borrowernumber, branchcode, status, backend) VALUES (1, 'CPL', 'IN_LEV', 'Libris');
INSERT INTO `illrequests` (borrowernumber, branchcode, status, backend) VALUES (1, 'CPL', 'IN_ANK', 'Libris');

Note: Change values to whatever your test environment has set up.

3. Go to cgi-bin/koha/ill/ill-requests.pl
4. After the page loads, you should now see 4 different tabs:
   "All", "tab1", "tab2" and "tab3"
5. Confirm all ILL requests are present under "All" tab
6. Confirm only ILL requests with the statuses you defined in
   system preference ILLRequestsTabs are under tabs 1, 2 and 3
7. Go to cgi-bin/koha/members/ill-requests.pl?borrowernumber=1
8. Repeat steps 4-6
9. Clear system preference ILLRequestsTabs
10. Observe both ill/ill-requests.pl and members/ill-requests.pl
    now have only one tab "All" under which correct ILL requests
    are presented
11. Try some other status values in ILLRequestsTabs and observe
    expected behavior

Signed-off-by: Magnus Enger <magnus@libriotech.no>
Works as advertised.

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 ill/ill-requests.pl                           |  1 +
 .../prog/en/includes/ill-list-table.inc       |  2 +-
 .../prog/en/modules/ill/ill-requests.tt       | 15 ++++--
 .../prog/en/modules/members/ill-requests.tt   | 13 ++++-
 .../intranet-tmpl/prog/js/ill-list-table.js   | 50 +++++++++++++++++++
 members/ill-requests.pl                       |  1 +
 6 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/ill/ill-requests.pl b/ill/ill-requests.pl
index ea2305b3a4..298a619905 100755
--- a/ill/ill-requests.pl
+++ b/ill/ill-requests.pl
@@ -427,6 +427,7 @@ if ( $backends_available ) {
 
 $template->param(
     backends   => $backends,
+    illreq_tabs => C4::Context->yaml_preference('ILLRequestsTabs'),
     types      => [ "Book", "Article", "Journal" ],
     query_type => $op,
     branches   => Koha::Libraries->search,
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/ill-list-table.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/ill-list-table.inc
index 35a8538ec7..f7d6da0449 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/ill-list-table.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/ill-list-table.inc
@@ -1,4 +1,4 @@
-<div>
+<div id="ill-requests-results">
     [% IF prefilters.length > 0 %]
     <table id="ill-requests" data-prefilters="[% prefilters | html %]">
     [% ELSE %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
index d5774b7331..26e1217ee3 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
@@ -779,9 +779,18 @@
                 [% ELSIF query_type == 'illlist' %]
                     <!-- illlist -->
                     <h1>View ILL requests</h1>
-                    <div id="results">
-                        <h3>Details for all requests</h3>
-                        [% INCLUDE 'ill-list-table.inc' %]
+                    <div id="ill-requests-tabs" class="toptabs">
+                    <ul>
+                        <li>
+                            <a id="#ill-requests-tab-all" href="#ill-requests-results">All</a>
+                        </li>
+                        [% FOREACH illreq_tab IN illreq_tabs %]
+                        <li>
+                            <a href="#ill-requests-results" data-statuses="[% illreq_tab.status.join('|') %]">[% illreq_tab.name %]</a>
+                        </li>
+                        [% END %]
+                    </ul>
+                    [% INCLUDE 'ill-list-table.inc' %]
 
                     </div> <!-- /#results -->
                 [% ELSIF query_type == 'availability' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/ill-requests.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/ill-requests.tt
index fa95e58d57..c933a2a54d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/ill-requests.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/ill-requests.tt
@@ -39,7 +39,18 @@
             <main>
                 [% INCLUDE 'members-toolbar.inc' %]
                 <h1>ILL requests history</h1>
-                [% INCLUDE 'ill-list-table.inc' %]
+                <div id="ill-requests-tabs" class="toptabs">
+                    <ul>
+                        <li>
+                            <a id="#ill-requests-tab-all" href="#ill-requests-results">All</a>
+                        </li>
+                        [% FOREACH illreq_tab IN illreq_tabs %]
+                        <li>
+                            <a href="#ill-requests-results" data-statuses="[% illreq_tab.status.join('|') %]">[% illreq_tab.name %]</a>
+                        </li>
+                        [% END %]
+                    </ul>
+                    [% INCLUDE 'ill-list-table.inc' %]
             </main>
         </div> <!-- .col-sm-10.col-sm-push-2 -->
 
diff --git a/koha-tmpl/intranet-tmpl/prog/js/ill-list-table.js b/koha-tmpl/intranet-tmpl/prog/js/ill-list-table.js
index d6e92cc517..cda41455f6 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/ill-list-table.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/ill-list-table.js
@@ -317,6 +317,50 @@ $(document).ready(function() {
         }
     };
 
+    var tabsInit = function( ui, illtable_el ) {
+        localStorage.setItem(
+            "illrequests_activetab",
+            $( "#ill-requests-tabs" ).tabs( "option", "active" )
+        );
+
+        var ill_dt = illtable_el.DataTable();
+        $.fn.dataTable.ext.search.pop();
+        ill_dt.draw();
+
+        if ( $( "#ill-requests-tabs" ).tabs( "option", "active" ) > 0 ) {
+            // Initializing some other tab than "All"
+            var ill_tab_statuses = $("#ill-requests-tabs>ul>li.ui-tabs-active>a").attr('data-statuses');
+            ill_tab_statuses = ill_tab_statuses.split("|");
+
+            // Only show rows having the status defined in ILLRequestsTabs system
+            // preference in this tab
+            $.fn.dataTable.ext.search.push(
+                function(settings, data, dataIndex) {
+                    return ill_tab_statuses.indexOf(ill_dt.row(dataIndex).data().status) >= 0;
+                }
+            );
+            ill_dt.draw();
+        }
+    };
+
+    $('#ill-requests-tabs').tabs({
+        create: function(event,ui) {
+            if ( $.fn.dataTable.isDataTable( 'table#ill-requests' ) ) {
+                tabsInit(ui, $("table#ill-requests"));
+            }
+        },
+        activate: function(event,ui) {
+            if ( $.fn.dataTable.isDataTable( 'table#ill-requests' ) ) {
+                tabsInit(ui, $("table#ill-requests"));
+            }
+        }
+    });
+
+    var activeTab = localStorage.getItem("illrequests_activetab");
+    if( activeTab ){
+        $("#ill-requests-tabs").tabs("option","active", activeTab );
+    }
+
     // Display the modal containing request supplier metadata
     $('#ill-request-display-log').on('click', function(e) {
         e.preventDefault();
@@ -524,6 +568,12 @@ $(document).ready(function() {
 
             });
 
+            if( activeTab ){
+                if ( $.fn.dataTable.isDataTable( 'table#ill-requests' ) ) {
+                    tabsInit(null, $("table#ill-requests"));
+                }
+            }
+
         });
     } //END if window.location.search.length == 0
 
diff --git a/members/ill-requests.pl b/members/ill-requests.pl
index f2b23844c0..da348080fd 100755
--- a/members/ill-requests.pl
+++ b/members/ill-requests.pl
@@ -42,6 +42,7 @@ my $patron         = Koha::Patrons->find( $borrowernumber );
 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
 
 $template->param(
+    illreq_tabs => C4::Context->yaml_preference('ILLRequestsTabs'),
     prefilters => "borrowernumber=$borrowernumber",
     patron => $patron,
     illview  => 1,
-- 
2.30.2