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