Bug 13636: DBRev 3.21.00.036
[koha.git] / svc / members / search
blob1c2d6449ca7ea52f81631808bbc14b3b031360c1
1 #!/usr/bin/perl
3 # Copyright 2013 BibLibre
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
21 use CGI;
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 );
29 my $input = new CGI;
31 exit unless $input->param('template_path');
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 });
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 my $selection_type = $input->param('selection_type');
50 if ( $searchfieldstype eq "dateofbirth" ) {
51 $searchmember = output_pref({dt => dt_from_string($searchmember), dateformat => 'iso', dateonly => 1});
54 # variable information for DataTables (id)
55 my $sEcho = $input->param('sEcho');
57 my %dt_params = dt_get_params($input);
58 foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
59 $dt_params{$_} =~ s/^dt_//;
62 my $results;
63 # If the user filled a term, maybe it's a cardnumber.
64 # This cannot be the case if a first letter is given.
65 if ( $searchmember
66 and not $firstletter
67 and $searchfieldstype
68 and $searchfieldstype eq 'standard' )
70 my $member = C4::Members::GetMember( cardnumber => $searchmember );
71 $results = {
72 iTotalRecords => 1,
73 iTotalDisplayRecords => 1,
74 patrons => [ $member ],
75 } if $member;
78 # Perform the patrons search
79 $results = C4::Utils::DataTables::Members::search(
81 searchmember => $searchmember,
82 firstletter => $firstletter,
83 categorycode => $categorycode,
84 branchcode => $branchcode,
85 searchtype => $searchtype,
86 searchfieldstype => $searchfieldstype,
87 dt_params => \%dt_params,
89 ) unless $results;
91 # It is not recommanded to use the has_permission param if you use the pagination
92 # The filter is done AFTER requested the data
93 if ($has_permission) {
94 my ( $permission, $subpermission ) = split /\./, $has_permission;
95 my @patrons_with_permission;
96 for my $patron ( @{ $results->{patrons} } ) {
97 my $perms = haspermission( $patron->{userid} );
98 if ( $perms->{superlibrarian} == 1
99 or $perms->{$permission} == 1 )
101 push @patrons_with_permission, $patron;
102 next;
105 if ($subpermission) {
106 my $subperms = get_user_subpermissions( $patron->{userid} );
107 push @patrons_with_permission, $patron
108 if $subperms->{$permission}->{$subpermission};
111 $results->{patrons} = \@patrons_with_permission;
112 $results->{iTotalDisplayRecords} = scalar( @patrons_with_permission );
115 $template->param(
116 sEcho => $sEcho,
117 iTotalRecords => $results->{iTotalRecords},
118 iTotalDisplayRecords => $results->{iTotalDisplayRecords},
119 aaData => $results->{patrons},
120 selection_type => $selection_type,
123 output_with_http_headers $input, $cookie, $template->output, 'json';
125 __END__
127 =head1 NAME
129 search - a search script for finding patrons
131 =head1 SYNOPSIS
133 This script provides a service for template for patron search using DataTables
135 =head2 Performing a search
137 Call this script from a DataTables table my $searchmember = $input->param('searchmember');
138 All following params are optional:
139 searchmember => the search terms
140 firstletter => search patrons with surname begins with this pattern (currently only used for 1 letter)
141 categorycode and branchcode => search patrons belong to a given categorycode or a branchcode
142 searchtype: can be 'contain' or 'start_with'
143 searchfieldstype: Can be 'standard', 'email', 'borrowernumber', 'userid', 'phone' or 'address'
145 =cut
147 =back
149 =head1 LICENSE
151 Copyright 2013 BibLibre
153 This file is part of Koha.
155 Koha is free software; you can redistribute it and/or modify it under the
156 terms of the GNU General Public License as published by the Free Software
157 Foundation; either version 2 of the License, or (at your option) any later
158 version.
160 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
161 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
162 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
164 You should have received a copy of the GNU General Public License along
165 with Koha; if not, write to the Free Software Foundation, Inc.,
166 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.