From 2b8eefb82067b1448c68b97d2ee23fc8bed8136c Mon Sep 17 00:00:00 2001
From: Fridolin Somers <fridolin.somers@biblibre.com>
Date: Thu, 25 Jan 2024 09:50:28 +0100
Subject: [PATCH] Bug 35903: In cataloguing authority plugin using autocomplete
 must set operator exact

When cataloguing and using authority plugin, there is auto-completion on inputs and default operator is "contains".
When using auto-completion and selecting a result it would be logical to set operator "exact".

See doc https://api.jqueryui.com/autocomplete/#event-select

This patch also adds auto-completion missing on "Search all headings".

Test plan:
1) Create a new authority Topical Term with heading "Cart"
2) Create a new authority Topical Term with heading "Carthage"
3) Create a new biblio record
4) Use authority plugin on field 650
5) You see current operator are "contains"
6) Enter "Car" in "Search main heading ($a only):"
7) You see auto-completion showing "Cart" and "Carthage"
8) Click on "Cart"
9) You see operator changes to "is exactly"
10) Submit form to see the results
11) Clear form and repeat 6-9 for the three other inputs

Signed-off-by: Michelle Spinney <mspinney@clamsnet.org>
---
 .../prog/en/includes/auth-finder-search.inc   |  3 +-
 .../prog/js/auth-finder-search.js             | 42 ++++++++++++++++++-
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/auth-finder-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/auth-finder-search.inc
index 28775c9977..b7a6297239 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/auth-finder-search.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/auth-finder-search.inc
@@ -89,7 +89,8 @@
                         <option value="exact">is exactly</option>
                         [% END %]
                     </select>
-                    <input type="text" name="value_match" value="[% value_match | html %]" />
+                    <input id="value_match" style="width:400px;" type="text" name="value_match" value="[% value_match | html %]" />
+                    <div id="yvaluecontainermarclistheading"></div>
                 </li>
                 <li>
                     <label for="marclistanywhere">Search entire record: </label>
diff --git a/koha-tmpl/intranet-tmpl/prog/js/auth-finder-search.js b/koha-tmpl/intranet-tmpl/prog/js/auth-finder-search.js
index d986723664..5bdf7db81a 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/auth-finder-search.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/auth-finder-search.js
@@ -9,7 +9,7 @@ $(document).ready(function(){
         e.preventDefault();
         finderjump('authorities.pl?index=' + index + '&authtypecode=' + authtypecode, 'full' );
     });
-    // marclist
+    // marclistanywhere
     $( "#value_any" ).autocomplete({
         source: function(request, response) {
             $.ajax({
@@ -35,6 +35,40 @@ $(document).ready(function(){
                 }
             });
         },
+        select: function( event, ui ) {
+            $("#marclistanywhere").val("exact");
+        },
+        minLength: 3,
+    });
+    // marclistheading
+    $( "#value_match" ).autocomplete({
+        source: function(request, response) {
+            $.ajax({
+                url: "/cgi-bin/koha/authorities/ysearch.pl",
+                dataType: "json",
+                data: {
+                    authtypecode : authtypecode,
+                    term: request.term,
+                    op: "do_search",
+                    type: "intranet",
+                    and_or: "and",
+                    operator: "start",
+                    orderby: "HeadingAsc",
+                    querytype: "match"
+                },
+                success: function(data) {
+                    response( $.map( data, function( item ) {
+                        return {
+                            label: item.summary,
+                            value: item.summary
+                        };
+                    }));
+                }
+            });
+        },
+        select: function( event, ui ) {
+            $("#marclistheading").val("exact");
+        },
         minLength: 3,
     });
     // mainentry
@@ -63,6 +97,9 @@ $(document).ready(function(){
                 }
             });
         },
+        select: function( event, ui ) {
+            $("#mainentry").val("exact");
+        },
         minLength: 3,
     });
     // mainmainentry
@@ -91,6 +128,9 @@ $(document).ready(function(){
                 }
             });
         },
+        select: function( event, ui ) {
+            $("#mainmainentry").val("exact");
+        },
         minLength: 3,
     });
     $("#clear-form").click(function(){
-- 
2.30.2