From 9b982f471f20b63b69f429d0ac6708f671d0d01e Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 11 Mar 2015 07:47:44 -0700
Subject: [PATCH] Bug 13822 - Patron autocomplete search is severly limited

The script that returns data for the patron autocomplete does not use C4::Members::Search. Instead it uses bespoke code that does not behave like the traditional search and is very limited in how it can search.

If, for example, I search for "Kyle Hall" in the standard search, I would get "Kyle Hall" back as a result. For the autocomplete search, I will not.

This script should use C4::Members::Search to provide better searching and to keep the code base DRY.

Test Plan:
1) Enable the system preference CircAutocompl
2) Create a user with the first name "Test" and the surname "User"
3) Perform a checkout autocomplete search for "Test User"
4) Note you do not get the user as a result
5) Apply this patch
6) Try different combinations of "Test" and "User" such as
   Test User
   User Test
   U Test
   Test U
   etc.
7) Note these searches now work
---
 circ/ysearch.pl |   27 +++++----------------------
 1 files changed, 5 insertions(+), 22 deletions(-)

diff --git a/circ/ysearch.pl b/circ/ysearch.pl
index 8973902..6ec08ec 100755
--- a/circ/ysearch.pl
+++ b/circ/ysearch.pl
@@ -33,7 +33,7 @@ use C4::Members;
 use C4::Auth qw/check_cookie_auth/;
 
 my $input   = new CGI;
-my $query   = $input->param('term');
+my $term    = $input->param('term');
 
 binmode STDOUT, ":encoding(UTF-8)";
 print $input->header(-type => 'text/plain', -charset => 'UTF-8');
@@ -43,30 +43,13 @@ if ($auth_status ne "ok") {
     exit 0;
 }
 
-my $dbh = C4::Context->dbh;
-my $sql = q(
-    SELECT borrowernumber, surname, firstname, cardnumber, address, city, zipcode, country
-    FROM borrowers
-    WHERE ( surname LIKE ?
-        OR firstname LIKE ?
-        OR cardnumber LIKE ? )
-);
-if (   C4::Context->preference("IndependentBranches")
-    && C4::Context->userenv
-    && !C4::Context->IsSuperLibrarian()
-    && C4::Context->userenv->{'branch'} )
-{
-    $sql .= " AND borrowers.branchcode ="
-      . $dbh->quote( C4::Context->userenv->{'branch'} );
-}
-
-$sql    .= q( ORDER BY surname, firstname LIMIT 10);
-my $sth = $dbh->prepare( $sql );
-$sth->execute("$query%", "$query%", "$query%");
+$term =~ s/,//g; #remove any commas from search string
+my $order_by = 'surname, firstname';
+my ($results) = Search( $term, $order_by, [ 10 ] );
 
 print "[";
 my $i = 0;
-while ( my $rec = $sth->fetchrow_hashref ) {
+foreach my $rec ( @$results ) {
     if($i > 0){ print ","; }
     print "{\"borrowernumber\":\"" . $rec->{borrowernumber} . "\",\"" .
           "surname\":\"".$rec->{surname} . "\",\"" .
-- 
1.7.2.5