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