Bug 11425: Add item search form in staff interface
[koha.git] / catalogue / itemsearch.pl
1 #!/usr/bin/perl
2 # Copyright 2013 BibLibre
3 #
4 # This file is part of Koha
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 3 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 use Modern::Perl;
20 use CGI;
21
22 use JSON;
23
24 use C4::Auth;
25 use C4::Output;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Branch;
29 use C4::Koha;
30 use C4::ItemType;
31
32 use Koha::Item::Search::Field qw(GetItemSearchFields);
33
34 my $cgi = new CGI;
35 my %params = $cgi->Vars;
36
37 my $format = $cgi->param('format');
38 my ($template_name, $content_type);
39 if (defined $format and $format eq 'json') {
40     $template_name = 'catalogue/itemsearch.json.tt';
41     $content_type = 'json';
42
43     # Map DataTables parameters with 'regular' parameters
44     $cgi->param('rows', $cgi->param('iDisplayLength'));
45     $cgi->param('page', ($cgi->param('iDisplayStart') / $cgi->param('iDisplayLength')) + 1);
46     my @columns = split /,/, $cgi->param('sColumns');
47     $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
48     $cgi->param('sortorder', $cgi->param('sSortDir_0'));
49
50     my @f = $cgi->param('f');
51     my @q = $cgi->param('q');
52     push @q, '' if @q == 0;
53     my @op = $cgi->param('op');
54     my @c = $cgi->param('c');
55     foreach my $i (0 .. ($cgi->param('iColumns') - 1)) {
56         my $sSearch = $cgi->param("sSearch_$i");
57         if ($sSearch) {
58             my @words = split /\s+/, $sSearch;
59             foreach my $word (@words) {
60                 push @f, $columns[$i];
61                 push @q, "%$word%";
62                 push @op, 'like';
63                 push @c, 'and';
64             }
65         }
66     }
67     $cgi->param('f', @f);
68     $cgi->param('q', @q);
69     $cgi->param('op', @op);
70     $cgi->param('c', @c);
71 } elsif (defined $format and $format eq 'csv') {
72     $template_name = 'catalogue/itemsearch.csv.tt';
73
74     # Retrieve all results
75     $cgi->param('rows', 0);
76 } else {
77     $format = 'html';
78     $template_name = 'catalogue/itemsearch.tt';
79     $content_type = 'html';
80 }
81
82 my ($template, $borrowernumber, $cookie) = get_template_and_user({
83     template_name => $template_name,
84     query => $cgi,
85     type => 'intranet',
86     authnotrequired => 0,
87     flagsrequired   => { catalogue => 1 },
88 });
89
90 my $notforloan_avcode = GetAuthValCode('items.notforloan');
91 my $notforloan_values = GetAuthorisedValues($notforloan_avcode);
92
93 if (scalar keys %params > 0) {
94     # Parameters given, it's a search
95
96     my $filter = {
97         conjunction => 'AND',
98         filters => [],
99     };
100
101     foreach my $p (qw(homebranch location itype ccode issues datelastborrowed)) {
102         if (my @q = $cgi->param($p)) {
103             if ($q[0] ne '') {
104                 my $f = {
105                     field => $p,
106                     query => \@q,
107                 };
108                 if (my $op = $cgi->param($p . '_op')) {
109                     $f->{operator} = $op;
110                 }
111                 push @{ $filter->{filters} }, $f;
112             }
113         }
114     }
115
116     my @c = $cgi->param('c');
117     my @fields = $cgi->param('f');
118     my @q = $cgi->param('q');
119     my @op = $cgi->param('op');
120
121     my $f;
122     for (my $i = 0; $i < @fields; $i++) {
123         my $field = $fields[$i];
124         my $q = shift @q;
125         my $op = shift @op;
126         if (defined $q and $q ne '') {
127             if ($i == 0) {
128                 $f = {
129                     field => $field,
130                     query => $q,
131                     operator => $op,
132                 };
133             } else {
134                 my $c = shift @c;
135                 $f = {
136                     conjunction => $c,
137                     filters => [
138                         $f, {
139                             field => $field,
140                             query => $q,
141                             operator => $op,
142                         }
143                     ],
144                 };
145             }
146         }
147     }
148     push @{ $filter->{filters} }, $f;
149
150     # Yes/No parameters
151     foreach my $p (qw(damaged itemlost)) {
152         my $v = $cgi->param($p) // '';
153         my $f = {
154             field => $p,
155             query => 0,
156         };
157         if ($v eq 'yes') {
158             $f->{operator} = '!=';
159             push @{ $filter->{filters} }, $f;
160         } elsif ($v eq 'no') {
161             $f->{operator} = '=';
162             push @{ $filter->{filters} }, $f;
163         }
164     }
165
166     if (my $itemcallnumber_from = $cgi->param('itemcallnumber_from')) {
167         push @{ $filter->{filters} }, {
168             field => 'itemcallnumber',
169             query => $itemcallnumber_from,
170             operator => '>=',
171         };
172     }
173     if (my $itemcallnumber_to = $cgi->param('itemcallnumber_to')) {
174         push @{ $filter->{filters} }, {
175             field => 'itemcallnumber',
176             query => $itemcallnumber_to,
177             operator => '<=',
178         };
179     }
180
181     my $search_params = {
182         rows => $cgi->param('rows') // 20,
183         page => $cgi->param('page') || 1,
184         sortby => $cgi->param('sortby') || 'itemnumber',
185         sortorder => $cgi->param('sortorder') || 'asc',
186     };
187
188     my ($results, $total_rows) = SearchItems($filter, $search_params);
189     if ($results) {
190         # Get notforloan labels
191         my $notforloan_map = {};
192         foreach my $nfl_value (@$notforloan_values) {
193             $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
194         }
195
196         foreach my $item (@$results) {
197             $item->{biblio} = GetBiblio($item->{biblionumber});
198             ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
199             $item->{status} = $notforloan_map->{$item->{notforloan}};
200         }
201     }
202
203     $template->param(
204         filter => $filter,
205         search_params => $search_params,
206         results => $results,
207         total_rows => $total_rows,
208         search_done => 1,
209     );
210
211     if ($format eq 'html') {
212         # Build pagination bar
213         my $url = $cgi->url(-absolute => 1);
214         my @params;
215         foreach my $p (keys %params) {
216             my @v = $cgi->param($p);
217             push @params, map { "$p=" . $_ } @v;
218         }
219         $url .= '?' . join ('&', @params);
220         my $nb_pages = 1 + int($total_rows / $search_params->{rows});
221         my $current_page = $search_params->{page};
222         my $pagination_bar = pagination_bar($url, $nb_pages, $current_page, 'page');
223
224         $template->param(pagination_bar => $pagination_bar);
225     }
226 }
227
228 if ($format eq 'html') {
229     # Retrieve data required for the form.
230
231     my $branches = GetBranches();
232     my @branches;
233     foreach my $branchcode (keys %$branches) {
234         push @branches, {
235             value => $branchcode,
236             label => $branches->{$branchcode}->{branchname},
237         };
238     }
239     my $locations = GetAuthorisedValues('LOC');
240     my @locations;
241     foreach my $location (@$locations) {
242         push @locations, {
243             value => $location->{authorised_value},
244             label => $location->{lib},
245         };
246     }
247     my @itemtypes = C4::ItemType->all();
248     foreach my $itemtype (@itemtypes) {
249         $itemtype->{value} = $itemtype->{itemtype};
250         $itemtype->{label} = $itemtype->{description};
251     }
252     my $ccode_avcode = GetAuthValCode('items.ccode') || 'CCODE';
253     my $ccodes = GetAuthorisedValues($ccode_avcode);
254     my @ccodes;
255     foreach my $ccode (@$ccodes) {
256         push @ccodes, {
257             value => $ccode->{authorised_value},
258             label => $ccode->{lib},
259         };
260     }
261
262     my @notforloans;
263     foreach my $value (@$notforloan_values) {
264         push @notforloans, {
265             value => $value->{authorised_value},
266             label => $value->{lib},
267         };
268     }
269
270     my @items_search_fields = GetItemSearchFields();
271
272     my $authorised_values = {};
273     foreach my $field (@items_search_fields) {
274         if (my $category = ($field->{authorised_values_category})) {
275             $authorised_values->{$category} = GetAuthorisedValues($category);
276         }
277     }
278
279     $template->param(
280         branches => \@branches,
281         locations => \@locations,
282         itemtypes => \@itemtypes,
283         ccodes => \@ccodes,
284         notforloans => \@notforloans,
285         items_search_fields => \@items_search_fields,
286         authorised_values_json => to_json($authorised_values),
287     );
288 }
289
290 if ($format eq 'csv') {
291     print $cgi->header({
292         type => 'text/csv',
293         attachment => 'items.csv',
294     });
295     print $template->output;
296 } else {
297     output_with_http_headers $cgi, $cookie, $template->output, $content_type;
298 }