Bug 17268: (follow-up) Fix licenses
[koha.git] / svc / members / search
blob76c5749f22367942c746389908679c4834c134cc
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 );
28 use Koha::Patrons;
30 my $input = new CGI;
32 exit unless $input->param('template_path');
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 => 'edit_borrowers' }
40 });
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');
51 # variable information for DataTables (id)
52 my $sEcho = $input->param('sEcho');
54 my %dt_params = dt_get_params($input);
55 foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
56 $dt_params{$_} =~ s/^dt_//;
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' )
67 my $member = Koha::Patrons->find( { cardnumber => $searchmember } );
68 $results = {
69 iTotalRecords => 1,
70 iTotalDisplayRecords => 1,
71 patrons => [ $member->unblessed ],
72 } if $member;
75 if ($has_permission) {
76 my ( $permission, $subpermission ) = split /\./, $has_permission;
77 $has_permission = {permission => $permission, subpermission => $subpermission};
80 # Perform the patrons search
81 $results = C4::Utils::DataTables::Members::search(
83 searchmember => $searchmember,
84 firstletter => $firstletter,
85 categorycode => $categorycode,
86 branchcode => $branchcode,
87 searchtype => $searchtype,
88 searchfieldstype => $searchfieldstype,
89 dt_params => \%dt_params,
90 ( $has_permission ? ( has_permission => $has_permission ) : () ),
92 ) unless $results;
94 $template->param(
95 sEcho => $sEcho,
96 iTotalRecords => $results->{iTotalRecords},
97 iTotalDisplayRecords => $results->{iTotalDisplayRecords},
98 aaData => $results->{patrons},
99 selection_type => $selection_type,
102 output_with_http_headers $input, $cookie, $template->output, 'json';
104 __END__
106 =head1 NAME
108 search - a search script for finding patrons
110 =head1 SYNOPSIS
112 This script provides a service for template for patron search using DataTables
114 =head2 Performing a search
116 Call this script from a DataTables table my $searchmember = $input->param('searchmember');
117 All following params are optional:
118 searchmember => the search terms
119 firstletter => search patrons with surname begins with this pattern (currently only used for 1 letter)
120 categorycode and branchcode => search patrons belong to a given categorycode or a branchcode
121 searchtype: can be 'contain' or 'start_with'
122 searchfieldstype: Can be 'standard', 'email', 'borrowernumber', 'userid', 'phone' or 'address'
124 =cut
126 =back
128 =head1 LICENSE
130 Copyright 2013 BibLibre
132 This file is part of Koha.
134 Koha is free software; you can redistribute it and/or modify it under the
135 terms of the GNU General Public License as published by the Free Software
136 Foundation; either version 2 of the License, or (at your option) any later
137 version.
139 Koha is free software; you can redistribute it and/or modify it
140 under the terms of the GNU General Public License as published by
141 the Free Software Foundation; either version 3 of the License, or
142 (at your option) any later version.
144 Koha is distributed in the hope that it will be useful, but
145 WITHOUT ANY WARRANTY; without even the implied warranty of
146 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147 GNU General Public License for more details.
149 You should have received a copy of the GNU General Public License
150 along with Koha; if not, see <http://www.gnu.org/licenses>.