Bug 12648: Refactoring to prepare user search for reuse
[koha.git] / acqui / add_user_search.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Output;
25 use C4::Members;
26
27 my $input = new CGI;
28
29 my $dbh = C4::Context->dbh;
30
31 my ( $template, $loggedinuser, $cookie, $staff_flags ) = get_template_and_user(
32     {   template_name   => "acqui/add_user_search.tt",
33         query           => $input,
34         type            => "intranet",
35         authnotrequired => 0,
36         flagsrequired   => { acquisition => 'order_manage' },
37     }
38 );
39
40 my $q = $input->param('q') || '';
41 my $op = $input->param('op') || '';
42
43 my $referer = $input->referer();
44
45 # If this script is called by acqui/basket.pl
46 # the patrons to return should be superlibrarian or have the order_manage
47 # acquisition flag.
48 my $search_patrons_with_acq_perm_only =
49     ( $referer =~ m|acqui/basket.pl| )
50         ? 1 : 0;
51
52 if( $op eq "do_search" ) {
53     my $results = C4::Members::Search( $q, "surname");
54
55     my @users_loop;
56     my $nresults = 0;
57     foreach my $res (@$results) {
58         my $should_be_returned = 1;
59
60         if ( $search_patrons_with_acq_perm_only ) {
61             $should_be_returned = 0;
62             my $perms = haspermission( $res->{userid} );
63             my $subperms = get_user_subpermissions( $res->{userid} );
64
65             if( $perms->{superlibrarian} == 1
66              || $perms->{acquisition} == 1
67              || $subperms->{acquisition}->{'order_manage'} ) {
68                 $should_be_returned = 1;
69             }
70         }
71         if ( $should_be_returned ) {
72             my %row = (
73                 borrowernumber  => $res->{borrowernumber},
74                 cardnumber      => $res->{cardnumber},
75                 surname         => $res->{surname},
76                 firstname       => $res->{firstname},
77                 categorycode    => $res->{categorycode},
78                 branchcode      => $res->{branchcode},
79             );
80             push( @users_loop, \%row );
81             $nresults ++;
82         }
83     }
84
85     $template->param(
86         q           => $q,
87         nresults    => $nresults,
88         users_loop  => \@users_loop,
89     );
90 }
91
92 $template->param(
93     patrons_with_acq_perm_only => $search_patrons_with_acq_perm_only,
94 );
95 output_html_with_http_headers( $input, $cookie, $template->output );