From 3d8632cf7287d13a43d1e317f43593bd602ffe70 Mon Sep 17 00:00:00 2001
From: Brendan Lawlor <blawlor@clamsnet.org>
Date: Wed, 10 Jul 2024 20:23:38 +0000
Subject: [PATCH] Bug 14322: Add shareable link button to item search

This work in progress patch adds a button to copy a link to an item search. I fixed the 500 issue, so now if you paste the url it fills the first two field sets in the item search form.

Posting progress for feedback.

Test plan:
1. Apply patch
2. Do an item search using only the first two field sets
3. Include 'is and 'is not'
4. On the results page see a new 'Copy shareable link' button
5. Click the button to copy the url and paste in the browser
6. Notice the first two field sets are filled as you would expect
7. Needs more work in the template to make the other field sets work
---
 catalogue/itemsearch.pl                       |  7 ++++---
 .../prog/en/modules/catalogue/itemsearch.tt   | 21 ++++++++++++++-----
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/catalogue/itemsearch.pl b/catalogue/itemsearch.pl
index d32faa550d..a8134712f4 100755
--- a/catalogue/itemsearch.pl
+++ b/catalogue/itemsearch.pl
@@ -90,8 +90,8 @@ if (defined $format and $format eq 'json') {
 } elsif (defined $format and $format eq 'barcodes') {
     # Retrieve all results
     $cgi->param('rows', 0);
-} elsif (defined$format and $format eq 'shareable') {
-    # get the item search parameters from the url!?!?
+} elsif (defined $format and $format eq 'shareable') {
+    # get the item search parameters from the url and fill form
 } elsif (defined $format) {
     die "Unsupported format $format";
 }
@@ -121,7 +121,7 @@ if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => '
     $template->param( has_new_status => 1 );
 }
 
-if ( defined $format ) {
+if ( defined $format and $format ne 'shareable') {
     # Parameters given, it's a search
 
     my $filter = {
@@ -353,6 +353,7 @@ $template->param(
     damageds => \@damageds,
     items_search_fields => \@items_search_fields,
     authorised_values_json => to_json($authorised_values),
+    query => $cgi,
 );
 
 output_html_with_http_headers $cgi, $cookie, $template->output;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt
index 88e53477a7..046535dd4b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt
@@ -37,14 +37,25 @@
   <div class="form-field form-field-select">
     <label class="form-field-label" for="[% name | html %]">[% INCLUDE form_label label=name %]</label>
     <select id="[% name | html %]_op" name="[% name | html %]_op">
-      <option value="=">is</option>
-      <option value="!=" >is not</option>
+      [% IF query.param(name _ '_op') == '=' || !query.param(name _ '_op') %]
+        <option value="=" selected="selected">is</option>
+        <option value="!=" >is not</option>
+      [% ELSE %]
+        <option value="=">is</option>
+        <option value="!=" selected="selected">is not</option>
+      [% END %]
     </select>
     <select id="[% name | html %]" name="[% name | html %]" multiple="multiple" size="[% options.size < 4 ? options.size + 1 : 4 | html %]">
-      <option value="" selected="selected">
-        [% IF (empty_option) %][% INCLUDE form_label label=empty_option %][% ELSE %]<span>All</span>[% END %]
-      </option>
+      [% IF query.param(name).size == 0 %]
+        <option value="" selected="selected">
+            [% IF (empty_option) %][% INCLUDE form_label label=empty_option %][% ELSE %]<span>All</span>[% END %]
+        </option>
+      [% END %]
       [% FOREACH option IN options %]
+        [% IF query.param(name).grep(option.value).size %]
+            <option value="[% option.value | html %]" selected="selected">[% option.label | html %]</option>
+        [% END %]
+        [% query.param(option.value) %]
         <option value="[% option.value | html %]">[% option.label | html %]</option>
       [% END %]
     </select>
-- 
2.39.2