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