Bug 17829: Move GetMember to Koha::Patron
[koha.git] / svc / members / search
1 #!/usr/bin/perl
2
3 # Copyright 2013 BibLibre
4 #
5 # This file is part of Koha
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 use CGI;
22
23 use C4::Auth qw( get_template_and_user haspermission get_user_subpermissions );
24 use C4::Output qw( output_with_http_headers );
25 use C4::Utils::DataTables qw( dt_get_params );
26 use C4::Utils::DataTables::Members qw( search );
27 use Koha::DateUtils qw( output_pref dt_from_string );
28 use Koha::Patrons;
29
30 my $input = new CGI;
31
32 exit unless $input->param('template_path');
33
34 my ($template, $user, $cookie) = get_template_and_user({
35     template_name   => scalar $input->param('template_path'),
36     query           => $input,
37     type            => "intranet",
38     authnotrequired => 0,
39     flagsrequired   => { borrowers => 1 }
40 });
41
42 my $searchmember = $input->param('searchmember');
43 my $firstletter  = $input->param('firstletter');
44 my $categorycode = $input->param('categorycode');
45 my $branchcode = $input->param('branchcode');
46 my $searchtype = $input->param('searchtype');
47 my $searchfieldstype = $input->param('searchfieldstype') || 'standard';
48 my $has_permission = $input->param('has_permission');
49 my $selection_type = $input->param('selection_type');
50
51 # variable information for DataTables (id)
52 my $sEcho = $input->param('sEcho');
53
54 my %dt_params = dt_get_params($input);
55 foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
56     $dt_params{$_} =~ s/^dt_//;
57 }
58
59 my $results;
60 # If the user filled a term, maybe it's a cardnumber.
61 # This cannot be the case if a first letter is given.
62 if ( $searchmember
63     and not $firstletter
64     and $searchfieldstype
65     and $searchfieldstype eq 'standard' )
66 {
67     my $member = Koha::Patrons->find( { cardnumber => $searchmember } );
68     $results = {
69         iTotalRecords        => 1,
70         iTotalDisplayRecords => 1,
71         patrons              => [ $member->unblessed ],
72     } if $member;
73 }
74
75 # Perform the patrons search
76 $results = C4::Utils::DataTables::Members::search(
77     {
78         searchmember => $searchmember,
79         firstletter => $firstletter,
80         categorycode => $categorycode,
81         branchcode => $branchcode,
82         searchtype => $searchtype,
83         searchfieldstype => $searchfieldstype,
84         dt_params => \%dt_params,
85     }
86 ) unless $results;
87
88 # It is not recommanded to use the has_permission param if you use the pagination
89 # The filter is done AFTER requested the data
90 if ($has_permission) {
91     my ( $permission, $subpermission ) = split /\./, $has_permission;
92     my @patrons_with_permission;
93     for my $patron ( @{ $results->{patrons} } ) {
94         my $perms = haspermission( $patron->{userid} );
95         if (   $perms->{superlibrarian} == 1
96             or $perms->{$permission} == 1 )
97         {
98             push @patrons_with_permission, $patron;
99             next;
100         }
101
102         if ($subpermission) {
103             my $subperms = get_user_subpermissions( $patron->{userid} );
104             push @patrons_with_permission, $patron
105               if $subperms->{$permission}->{$subpermission};
106         }
107     }
108     $results->{patrons} = \@patrons_with_permission;
109     $results->{iTotalDisplayRecords} = scalar( @patrons_with_permission );
110 }
111
112 $template->param(
113     sEcho => $sEcho,
114     iTotalRecords => $results->{iTotalRecords},
115     iTotalDisplayRecords => $results->{iTotalDisplayRecords},
116     aaData => $results->{patrons},
117     selection_type => $selection_type,
118 );
119
120 output_with_http_headers $input, $cookie, $template->output, 'json';
121
122 __END__
123
124 =head1 NAME
125
126 search - a search script for finding patrons
127
128 =head1 SYNOPSIS
129
130 This script provides a service for template for patron search using DataTables
131
132 =head2 Performing a search
133
134 Call this script from a DataTables table my $searchmember = $input->param('searchmember');
135 All following params are optional:
136     searchmember => the search terms
137     firstletter => search patrons with surname begins with this pattern (currently only used for 1 letter)
138     categorycode and branchcode => search patrons belong to a given categorycode or a branchcode
139     searchtype: can be 'contain' or 'start_with'
140     searchfieldstype: Can be 'standard', 'email', 'borrowernumber', 'userid', 'phone' or 'address'
141
142 =cut
143
144 =back
145
146 =head1 LICENSE
147
148 Copyright 2013 BibLibre
149
150 This file is part of Koha.
151
152 Koha is free software; you can redistribute it and/or modify it under the
153 terms of the GNU General Public License as published by the Free Software
154 Foundation; either version 2 of the License, or (at your option) any later
155 version.
156
157 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
158 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
159 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
160
161 You should have received a copy of the GNU General Public License along
162 with Koha; if not, write to the Free Software Foundation, Inc.,
163 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.