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