Bug 13892: Make ysearch.pl not using C4::Members::Search

To get rid of SQLHelper, we should not use this C4::Members::Search anymore.

Test plan:
0/ Enable the CircAutocompl pref
1/ Go on the circulation home page, or the reserve page
2/ Search for a patron and wait for the autocompletion box
3/ Confirm there is no regression

Works as expected.
Signed-off-by: Marc Veron <veron@veron.ch>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
This commit is contained in:
Jonathan Druart 2015-03-23 13:13:38 +01:00 committed by Tomas Cohen Arazi
parent 92dd223c3c
commit bcb58e1817
2 changed files with 44 additions and 42 deletions

View file

@ -96,17 +96,17 @@ my @objects = Koha::Objects->search($params);
=cut =cut
sub search { sub search {
my ( $self, $params ) = @_; my ( $self, $params, $attributes ) = @_;
if (wantarray) { if (wantarray) {
my @dbic_rows = $self->_resultset()->search($params); my @dbic_rows = $self->_resultset()->search($params, $attributes);
return $self->_wrap(@dbic_rows); return $self->_wrap(@dbic_rows);
} }
else { else {
my $class = ref($self) ? ref($self) : $self; my $class = ref($self) ? ref($self) : $self;
my $rs = $self->_resultset()->search($params); my $rs = $self->_resultset()->search($params, $attributes);
return $class->_new_from_dbic($rs); return $class->_new_from_dbic($rs);
} }

View file

@ -22,61 +22,63 @@
=head1 ysearch.pl =head1 ysearch.pl
=cut =cut
use strict; use Modern::Perl;
#use warnings; FIXME - Bug 2505
use CGI qw ( -utf8 ); use CGI qw ( -utf8 );
use C4::Context; use C4::Context;
use C4::Members;
use C4::Auth qw/check_cookie_auth/; use C4::Auth qw/check_cookie_auth/;
use Koha::Borrowers;
my $input = new CGI; use JSON qw( to_json );
my $query = $input->param('term');
my $input = new CGI;
my $query = $input->param('term');
binmode STDOUT, ":encoding(UTF-8)"; binmode STDOUT, ":encoding(UTF-8)";
print $input->header(-type => 'text/plain', -charset => 'UTF-8'); print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
my ($auth_status, $sessionID) = check_cookie_auth($input->cookie('CGISESSID'), { circulate => '*' }); my ( $auth_status, $sessionID ) = check_cookie_auth( $input->cookie('CGISESSID'), { circulate => '*' } );
if ($auth_status ne "ok") { if ( $auth_status ne "ok" ) {
exit 0; exit 0;
} }
my $dbh = C4::Context->dbh; my $limit_on_branch;
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") if ( C4::Context->preference("IndependentBranches")
&& C4::Context->userenv && C4::Context->userenv
&& !C4::Context->IsSuperLibrarian() && !C4::Context->IsSuperLibrarian()
&& C4::Context->userenv->{'branch'} ) && C4::Context->userenv->{'branch'} ) {
{ $limit_on_branch = 1;
$sql .= " AND borrowers.branchcode ="
. $dbh->quote( C4::Context->userenv->{'branch'} );
} }
$sql .= q( ORDER BY surname, firstname LIMIT 10); my $borrowers_rs = Koha::Borrowers->search(
my $sth = $dbh->prepare( $sql ); { -or => {
$sth->execute("$query%", "$query%", "$query%"); surname => { -like => "$query%" },
firstname => { -like => "$query%" },
cardnumber => { -like => "$query%" },
( $limit_on_branch ? { branchcode => C4::Context->userenv->{branch} } : () ),
},
},
{
# Get the first 10 results
page => 1,
rows => 10,
order_by => [ 'surname', 'firstname' ],
},
);
print "["; my @borrowers;
my $i = 0; while ( my $b = $borrowers_rs->next ) {
while ( my $rec = $sth->fetchrow_hashref ) { push @borrowers,
if($i > 0){ print ","; } { borrowernumber => $b->borrowernumber,
print "{\"borrowernumber\":\"" . $rec->{borrowernumber} . "\",\"" . surname => $b->surname,
"surname\":\"".$rec->{surname} . "\",\"" . firstname => $b->firstname,
"firstname\":\"".$rec->{firstname} . "\",\"" . cardnumber => $b->cardnumber,
"cardnumber\":\"".$rec->{cardnumber} . "\",\"" . address => $b->address,
"address\":\"".$rec->{address} . "\",\"" . city => $b->city,
"city\":\"".$rec->{city} . "\",\"" . zipcode => $b->zipcode,
"zipcode\":\"".$rec->{zipcode} . "\",\"" . country => $b->country
"country\":\"".$rec->{country} . "\"" . };
"}";
$i++;
} }
print "]";
print to_json( \@borrowers );