From 0f2f4098ddd64037a3e56e592f080c09e25e59af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tadeusz=20=E2=80=9Etadzik=E2=80=9D=20So=C5=9Bnierz?=
 <tadeusz@sosnierz.com>
Date: Fri, 7 Mar 2025 12:12:45 +0100
Subject: [PATCH] Bug 39610: Make ILL request metadata fields shown in OPAC
 table configurable

Test plan:

1. In KTD, go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=master+switch and enable the ILL module
2. Run the following script to populate the request list for user `koha`:

    use strict;
    use warnings;

    use Koha::ILL::Request;

    my $backend = 'Standard';
    my $branchcode = 'CPL';

    for my $n (1..100) {
        my $req = Koha::ILL::Request->new->load_backend($backend);
        my $fields = $req->_backend->_get_core_fields();
        my @extended_attributes = map { +{ type => $_, value => "$_ for $n" } } keys %$fields;
        $req->_backend->create_api({
            backend => $backend,
            branchcode => $branchcode,
            borrowernumber => 51,
            type => 'book',
            extended_attributes => \@extended_attributes,
        }, $req);
    }

3. Navigate to http://localhost:8080/cgi-bin/koha/opac-illrequests.pl, log in as `koha` as needed
4. Observe only the default Author and Title being displayed
5. Go to http://localhost:8081/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ILLOpacMetadataFields
   and set `author|article_author(Article Author)` as the new value
6. On http://localhost:8080/cgi-bin/koha/opac-illrequests.pl, observe how the configured metadata values are now shown, with configured names

Sponsored-by: Wiko (https://www.wiko-berlin.de/)
---
 .../admin/preferences/interlibrary_loans.pref  |  8 +++++++-
 .../bootstrap/en/modules/opac-illrequests.tt   | 18 +++++++-----------
 opac/opac-illrequests.pl                       |  9 ++++++++-
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/interlibrary_loans.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/interlibrary_loans.pref
index 858ee77554..b2d45c0084 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/interlibrary_loans.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/interlibrary_loans.pref
@@ -55,6 +55,12 @@ Interlibrary loans:
                   0: "Don't allow"
                   1: Allow
             - OPAC users to place ILL requests without having to be logged in.
+        -
+            - "ILL request metadata displayed in the OPAC request table:"
+            - pref: ILLOpacMetadataFields
+              class: multi
+            - (separated with |). Defaults to "author|title".
+            - 'Customize the displayed name by putting its display name in brackets, like: "part_title(Chapter/Article title)".'
     Workflow:
         -
             - pref: ILLCheckAvailability
@@ -96,4 +102,4 @@ Interlibrary loans:
             - "Select which backends will be queried for availability and in what order of priority. The first available backend will be suggested to place the request with.<br>"
             - "If no backend is selected, automatic backend selection will not take place."
             - pref: AutoILLBackendPriority
-              type: ill-backends
\ No newline at end of file
+              type: ill-backends
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-illrequests.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-illrequests.tt
index 179794eb79..ec80593328 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-illrequests.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-illrequests.tt
@@ -141,8 +141,9 @@
                                 <thead>
                                     <tr>
                                         <th>Request ID</th>
-                                        <th>Author</th>
-                                        <th>Title</th>
+                                        [% FOR field IN metadata_fields %]
+                                            <th>[% field.name | html %] </th>
+                                        [% END %]
                                         <th>Requested from</th>
                                         <th>Request type</th>
                                         <th>Status</th>
@@ -438,20 +439,15 @@
                     data: 'ill_request_id',
                     sortable: true
                 },
+                [% FOR field IN metadata_fields %]
                 {
-                    data: 'author',
+                    data: '[% field.key | html %]',
                     sortable: false,
                     render: (data, type, row, meta) => {
-                        return display_extended_attribute(row, 'author');
-                    },
-                },
-                {
-                    data: 'title',
-                    sortable: false,
-                    render: (data, type, row, meta) => {
-                        return display_extended_attribute(row, 'title');
+                        return display_extended_attribute(row, '[% field.key | html %]');
                     },
                 },
+                [% END %]
                 {
                     data: 'ill_backend_id',
                     sortable: true
diff --git a/opac/opac-illrequests.pl b/opac/opac-illrequests.pl
index 27b853a64f..ea66187906 100755
--- a/opac/opac-illrequests.pl
+++ b/opac/opac-illrequests.pl
@@ -95,7 +95,14 @@ if ( ( $op eq 'cud-create' || $op eq 'cancreq' || $op eq 'cud-update' ) && !$can
 }
 
 if ( $op eq 'list' ) {
-    $template->param( backends => $backends );
+    my @metadata_fields =
+        map { / ^ ( [^\(]+ ) \( ([^\)]+) \) $ /x ? { key => $1, name => $2 } : { key => $_, name => ucfirst $_ } }
+        split /\|/, ( C4::Context->preference('ILLOpacMetadataFields') || 'author|title' );
+
+    $template->param(
+        backends        => $backends,
+        metadata_fields => \@metadata_fields,
+    );
 } elsif ( $op eq 'view' ) {
     $template->param( request => $request );
 } elsif ( $op eq 'cud-update' ) {
-- 
2.47.2