From ffb2ac59e8f6cc17ee6768181e5b572066223c70 Mon Sep 17 00:00:00 2001
From: Matthias Meusburger <matthias.meusburger@biblibre.com>
Date: Wed, 2 Oct 2019 14:21:00 +0200
Subject: [PATCH] Bug 23697: Add autocompletion search in patron module

 Test plan:

  - Apply the patch
  - Go to "Patrons" (members/members-home.pl)
  - Check that autocomplete is enabled for patron search
---
 .../prog/en/includes/patron-search.inc             | 12 ++++++-----
 koha-tmpl/intranet-tmpl/prog/js/members-menu.js    | 24 ++++++++++++++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
index bde60b4..7490849 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
@@ -8,12 +8,14 @@
     <div id="patron_search" class="residentsearch">
     <p class="tip">Enter patron card number or partial name:</p>
     <form action="/cgi-bin/koha/members/member.pl" method="post">
-    <input id="searchmember" data-toggle="tooltip" size="25" class="head-searchbox focus" name="searchmember" type="text" value="[% searchmember | html %]" autocomplete="off" />
-    <input type="hidden" name="quicksearch" value="1" />
-    <span class="filteraction" id="filteraction_off"> <a href="#">[-]</a></span>
-    <span class="filteraction" id="filteraction_on"> <a href="#">[+]</a></span>
+    <div class="autocomplete">
+        <input id="searchmember" data-toggle="tooltip" size="25" class="head-searchbox focus" name="searchmember" type="text" value="[% searchmember | html %]" autocomplete="off" />
+        <input type="hidden" name="quicksearch" value="1" />
+        <span class="filteraction" id="filteraction_off"> <a href="#">[-]</a></span>
+        <span class="filteraction" id="filteraction_on"> <a href="#">[+]</a></span>
+        <input value="Search" class="submit" type="submit" />
+    </div>
 
-    <input value="Search" class="submit" type="submit" />
 
     <div id="filters">
         <p><label for="searchfieldstype">Search fields:</label>
diff --git a/koha-tmpl/intranet-tmpl/prog/js/members-menu.js b/koha-tmpl/intranet-tmpl/prog/js/members-menu.js
index c6e751b..b08af98 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/members-menu.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/members-menu.js
@@ -95,8 +95,32 @@ $(document).ready(function(){
         $("#patronImageEdit .modal-title").text(modalTitle);
         $("#patronImageEdit").modal("show");
     });
+
+    var obj = $( "#searchmember" ).autocomplete({
+        source: "/cgi-bin/koha/circ/ysearch.pl",
+        minLength: 3,
+        select: function( event, ui ) {
+            window.location.href = ui.item.link;
+        }
+    }).data( "ui-autocomplete" );
+    if( obj ) {
+        obj._renderItem = function( ul, item ) {
+            item.link = "/cgi-bin/koha/circ/circulation.pl?borrowernumber=" + item.borrowernumber;
+            var cardnumber = "";
+            if( item.cardnumber != "" ){
+                // Display card number in parentheses if it exists
+                cardnumber = " (" + item.cardnumber + ") ";
+            }
+            return $( "<li></li>" )
+            .data( "ui-autocomplete-item", item )
+            .append( "<a href=\"" + item.link + "\">" + item.surname + ", " + item.firstname + cardnumber + " <small>" + item.dateofbirth + " " + item.address + " " + item.city + " " + item.zipcode + " " + item.country + "</small></a>" )
+            .appendTo( ul );
+        };
+    }
 });
 
+
+
 function searchfield_date_tooltip(filter) {
     var field = "#searchmember" + filter;
     var type = "#searchfieldstype" + filter;
-- 
2.7.4