Bug 13232: Search patrons by a first letter should not redirect to a patron detail...
[koha.git] / svc / members / search
blob735925822d2c43b697f162c561ed2b5050b81cd8
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 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.
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.
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.
20 use Modern::Perl;
21 use CGI;
23 use C4::Auth qw( get_template_and_user );
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');
48 if ( $searchfieldstype eq "dateofbirth" ) {
49 $searchmember = output_pref({dt => dt_from_string($searchmember), dateformat => 'iso', dateonly => 1});
52 # variable information for DataTables (id)
53 my $sEcho = $input->param('sEcho');
55 my %dt_params = dt_get_params($input);
56 foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
57 $dt_params{$_} =~ s/^dt_//;
60 my $results;
61 # If the user filled a term, maybe it's a cardnumber.
62 # This cannot be the case if a first letter is given.
63 if ( not $firstletter
64 and $searchfieldstype
65 and $searchfieldstype eq 'standard' )
67 my $member = C4::Members::GetMember( cardnumber => $searchmember );
68 $results = {
69 iTotalRecords => 1,
70 iTotalDisplayRecords => 1,
71 patrons => [ $member ],
72 } if $member;
75 # Perform the patrons search
76 $results = C4::Utils::DataTables::Members::search(
78 searchmember => $searchmember,
79 firstletter => $firstletter,
80 categorycode => $categorycode,
81 branchcode => $branchcode,
82 searchtype => $searchtype,
83 searchfieldstype => $searchfieldstype,
84 dt_params => \%dt_params,
87 ) unless $results;
89 $template->param(
90 sEcho => $sEcho,
91 iTotalRecords => $results->{iTotalRecords},
92 iTotalDisplayRecords => $results->{iTotalDisplayRecords},
93 aaData => $results->{patrons}
96 output_with_http_headers $input, $cookie, $template->output, 'json';
98 __END__
100 =head1 NAME
102 search - a search script for finding patrons
104 =head1 SYNOPSIS
106 This script provides a service for template for patron search using DataTables
108 =head2 Performing a search
110 Call this script from a DataTables table my $searchmember = $input->param('searchmember');
111 All following params are optional:
112 searchmember => the search terms
113 firstletter => search patrons with surname begins with this pattern (currently only used for 1 letter)
114 categorycode and branchcode => search patrons belong to a given categorycode or a branchcode
115 searchtype: can be 'contain' or 'start_with'
116 searchfieldstype: Can be 'standard', 'email', 'borrowernumber', 'phone' or 'address'
118 =cut
120 =back
122 =head1 LICENSE
124 Copyright 2013 BibLibre
126 This file is part of Koha.
128 Koha is free software; you can redistribute it and/or modify it under the
129 terms of the GNU General Public License as published by the Free Software
130 Foundation; either version 2 of the License, or (at your option) any later
131 version.
133 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
134 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
135 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
137 You should have received a copy of the GNU General Public License along
138 with Koha; if not, write to the Free Software Foundation, Inc.,
139 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.