Bug 28854: Update circulation functionality for bundles
[koha.git] / circ / ysearch.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2007 Tamil s.a.r.l.
6 # Parts copyright 2010-2012 Athens County Public Libraries
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 =head1 ysearch.pl
24
25 =cut
26
27 use Modern::Perl;
28 use CGI qw ( -utf8 );
29 use C4::Context;
30 use C4::Auth qw( check_cookie_auth );
31 use Koha::Patrons;
32 use Koha::DateUtils qw( format_sqldatetime );
33 use Koha::Libraries;
34
35 use JSON qw( to_json );
36
37 my $input = CGI->new;
38 my $query = $input->param('term');
39
40 binmode STDOUT, ":encoding(UTF-8)";
41 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
42
43 my ( $auth_status ) = check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => '1' } );
44 if ( $auth_status ne "ok" ) {
45     exit 0;
46 }
47
48 my $limit_on_branch;
49 if (   C4::Context->preference("IndependentBranches")
50     && C4::Context->userenv
51     && !C4::Context->IsSuperLibrarian()
52     && C4::Context->userenv->{'branch'} ) {
53     $limit_on_branch = 1;
54 }
55
56 my @parts = split( /,\s|\s/, $query );
57 my @params;
58 foreach my $p (@parts) {
59     push(
60         @params,
61         -or => [
62             surname     => { -like => "%$p%" },
63             firstname   => { -like => "%$p%" },
64             middle_name => { -like => "%$p%" },
65             cardnumber  => { -like => "$p%" },
66         ]
67     );
68 }
69
70 push( @params, { 'me.branchcode' => C4::Context->userenv->{branch} } ) if $limit_on_branch;
71
72 my $borrowers_rs = Koha::Patrons->search_limited(
73     { -and => \@params },
74     {
75         # Get the first 10 results
76         page     => 1,
77         rows     => 10,
78         order_by => [ 'surname', 'firstname', 'middle_name' ],
79         prefetch => 'branchcode',
80     },
81 );
82
83 my @borrowers;
84 while ( my $b = $borrowers_rs->next ) {
85     push @borrowers,
86       {
87         borrowernumber => $b->borrowernumber,
88         surname        => $b->surname     // '',
89         firstname      => $b->firstname   // '',
90         middle_name    => $b->middle_name // '',
91         cardnumber     => $b->cardnumber  // '',
92         dateofbirth    => format_sqldatetime( $b->dateofbirth, undef, undef, 1 ) // '',
93         age        => $b->get_age             // '',
94         address    => $b->address             // '',
95         city       => $b->city                // '',
96         zipcode    => $b->zipcode             // '',
97         country    => $b->country             // '',
98         branchcode => $b->branchcode          // '',
99         branchname => $b->library->branchname // '',
100       };
101 }
102
103 print to_json( \@borrowers );