Bug 18394: Remove useless code from item search
[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::Koha;
29
30 use Koha::AuthorisedValues;
31 use Koha::Item::Search::Field qw(GetItemSearchFields);
32 use Koha::ItemTypes;
33 use Koha::Libraries;
34
35 my $cgi = new CGI;
36 my %params = $cgi->Vars;
37
38 my $format = $cgi->param('format');
39 my $template_name = 'catalogue/itemsearch.tt';
40
41 if (defined $format and $format eq 'json') {
42     $template_name = 'catalogue/itemsearch_json.tt';
43
44     # Map DataTables parameters with 'regular' parameters
45     $cgi->param('rows', scalar $cgi->param('iDisplayLength'));
46     $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1);
47     my @columns = split /,/, scalar $cgi->param('sColumns');
48     $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]);
49     $cgi->param('sortorder', scalar $cgi->param('sSortDir_0'));
50
51     my @f = $cgi->multi_param('f');
52     my @q = $cgi->multi_param('q');
53     push @q, '' if @q == 0;
54     my @op = $cgi->multi_param('op');
55     my @c = $cgi->multi_param('c');
56     my $iColumns = $cgi->param('iColumns');
57     foreach my $i (0 .. ($iColumns - 1)) {
58         my $sSearch = $cgi->param("sSearch_$i");
59         if (defined $sSearch and $sSearch ne '') {
60             my @words = split /\s+/, $sSearch;
61             foreach my $word (@words) {
62                 push @f, $columns[$i];
63                 push @q, "%$word%";
64                 push @op, 'like';
65                 push @c, 'and';
66             }
67         }
68     }
69     $cgi->param('f', @f);
70     $cgi->param('q', @q);
71     $cgi->param('op', @op);
72     $cgi->param('c', @c);
73 } elsif (defined $format and $format eq 'csv') {
74     $template_name = 'catalogue/itemsearch_csv.tt';
75
76     # Retrieve all results
77     $cgi->param('rows', 0);
78 } elsif (defined $format) {
79     die "Unsupported format $format";
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 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } });
91 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
92
93 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => { not => undef } });
94 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
95
96 if (scalar keys %params > 0) {
97     # Parameters given, it's a search
98
99     my $filter = {
100         conjunction => 'AND',
101         filters => [],
102     };
103
104     foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
105         if (my @q = $cgi->multi_param($p)) {
106             if ($q[0] ne '') {
107                 my $f = {
108                     field => $p,
109                     query => \@q,
110                 };
111                 if (my $op = scalar $cgi->param($p . '_op')) {
112                     $f->{operator} = $op;
113                 }
114                 push @{ $filter->{filters} }, $f;
115             }
116         }
117     }
118
119     my @c = $cgi->multi_param('c');
120     my @fields = $cgi->multi_param('f');
121     my @q = $cgi->multi_param('q');
122     my @op = $cgi->multi_param('op');
123
124     my $f;
125     for (my $i = 0; $i < @fields; $i++) {
126         my $field = $fields[$i];
127         my $q = shift @q;
128         my $op = shift @op;
129         if (defined $q and $q ne '') {
130             if ($i == 0) {
131                 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
132                     $field = 'copyrightdate';
133                 }
134                 $f = {
135                     field => $field,
136                     query => $q,
137                     operator => $op,
138                 };
139             } else {
140                 my $c = shift @c;
141                 $f = {
142                     conjunction => $c,
143                     filters => [
144                         $f, {
145                             field => $field,
146                             query => $q,
147                             operator => $op,
148                         }
149                     ],
150                 };
151             }
152         }
153     }
154     push @{ $filter->{filters} }, $f;
155
156     # Yes/No parameters
157     foreach my $p (qw(damaged itemlost)) {
158         my $v = $cgi->param($p) // '';
159         my $f = {
160             field => $p,
161             query => 0,
162         };
163         if ($v eq 'yes') {
164             $f->{operator} = '!=';
165             push @{ $filter->{filters} }, $f;
166         } elsif ($v eq 'no') {
167             $f->{operator} = '=';
168             push @{ $filter->{filters} }, $f;
169         }
170     }
171
172     if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
173         push @{ $filter->{filters} }, {
174             field => 'itemcallnumber',
175             query => $itemcallnumber_from,
176             operator => '>=',
177         };
178     }
179     if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
180         push @{ $filter->{filters} }, {
181             field => 'itemcallnumber',
182             query => $itemcallnumber_to,
183             operator => '<=',
184         };
185     }
186
187     my $sortby = $cgi->param('sortby') || 'itemnumber';
188     if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
189         $sortby = 'copyrightdate';
190     }
191     my $search_params = {
192         rows => scalar $cgi->param('rows') // 20,
193         page => scalar $cgi->param('page') || 1,
194         sortby => $sortby,
195         sortorder => scalar $cgi->param('sortorder') || 'asc',
196     };
197
198     my ($results, $total_rows) = SearchItems($filter, $search_params);
199     if ($results) {
200         # Get notforloan labels
201         my $notforloan_map = {};
202         foreach my $nfl_value (@$notforloan_values) {
203             $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
204         }
205
206         # Get location labels
207         my $location_map = {};
208         foreach my $loc_value (@$location_values) {
209             $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
210         }
211
212         foreach my $item (@$results) {
213             $item->{biblio} = GetBiblio($item->{biblionumber});
214             ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
215             $item->{status} = $notforloan_map->{$item->{notforloan}};
216             if (defined $item->{location}) {
217                 $item->{location} = $location_map->{$item->{location}};
218             }
219         }
220     }
221
222     $template->param(
223         filter => $filter,
224         search_params => $search_params,
225         results => $results,
226         total_rows => $total_rows,
227     );
228
229     if ($format eq 'csv') {
230         print $cgi->header({
231             type => 'text/csv',
232             attachment => 'items.csv',
233         });
234
235         for my $line ( split '\n', $template->output ) {
236             print "$line\n" unless $line =~ m|^\s*$|;
237         }
238     } elsif ($format eq 'json') {
239         output_with_http_headers $cgi, $cookie, $template->output, 'json';
240     }
241
242     exit;
243 }
244
245 # Display the search form
246
247 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
248 my @locations;
249 foreach my $location (@$location_values) {
250     push @locations, {
251         value => $location->{authorised_value},
252         label => $location->{lib} // $location->{authorised_value},
253     };
254 }
255 my @itemtypes;
256 foreach my $itemtype ( Koha::ItemTypes->search ) {
257     push @itemtypes, {
258         value => $itemtype->itemtype,
259         label => $itemtype->translated_description,
260     };
261 }
262
263 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => { not => undef } });
264 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
265 my $ccodes = GetAuthorisedValues($ccode_avcode);
266 my @ccodes;
267 foreach my $ccode (@$ccodes) {
268     push @ccodes, {
269         value => $ccode->{authorised_value},
270         label => $ccode->{lib},
271     };
272 }
273
274 my @notforloans;
275 foreach my $value (@$notforloan_values) {
276     push @notforloans, {
277         value => $value->{authorised_value},
278         label => $value->{lib},
279     };
280 }
281
282 my @items_search_fields = GetItemSearchFields();
283
284 my $authorised_values = {};
285 foreach my $field (@items_search_fields) {
286     if (my $category = ($field->{authorised_values_category})) {
287         $authorised_values->{$category} = GetAuthorisedValues($category);
288     }
289 }
290
291 $template->param(
292     branches => \@branches,
293     locations => \@locations,
294     itemtypes => \@itemtypes,
295     ccodes => \@ccodes,
296     notforloans => \@notforloans,
297     items_search_fields => \@items_search_fields,
298     authorised_values_json => to_json($authorised_values),
299 );
300
301 output_html_with_http_headers $cgi, $cookie, $template->output;