2 # Copyright 2013 BibLibre
4 # This file is part of Koha
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
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.
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.
30 use Koha
::AuthorisedValues
;
31 use Koha
::Item
::Search
::Field
qw(GetItemSearchFields);
36 my %params = $cgi->Vars;
38 my $format = $cgi->param('format');
39 my $template_name = 'catalogue/itemsearch.tt';
41 if (defined $format and $format eq 'json') {
42 $template_name = 'catalogue/itemsearch_json.tt';
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'));
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];
71 $cgi->param('op', @op);
73 } elsif (defined $format and $format eq 'csv') {
74 $template_name = 'catalogue/itemsearch_csv.tt';
76 # Retrieve all results
77 $cgi->param('rows', 0);
78 } elsif (defined $format and $format eq 'barcodes') {
79 # Retrieve all results
80 $cgi->param('rows', 0);
81 } elsif (defined $format) {
82 die "Unsupported format $format";
85 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
86 template_name
=> $template_name,
90 flagsrequired
=> { catalogue
=> 1 },
93 my $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.notforloan', authorised_value
=> { not => undef } });
94 my $notforloan_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
96 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.location', authorised_value
=> { not => undef } });
97 my $location_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
99 if (scalar keys %params > 0) {
100 # Parameters given, it's a search
103 conjunction
=> 'AND',
107 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
108 if (my @q = $cgi->multi_param($p)) {
114 if (my $op = scalar $cgi->param($p . '_op')) {
115 $f->{operator
} = $op;
117 push @
{ $filter->{filters
} }, $f;
122 my @c = $cgi->multi_param('c');
123 my @fields = $cgi->multi_param('f');
124 my @q = $cgi->multi_param('q');
125 my @op = $cgi->multi_param('op');
128 for (my $i = 0; $i < @fields; $i++) {
129 my $field = $fields[$i];
132 if (defined $q and $q ne '') {
134 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
135 $field = 'copyrightdate';
157 push @
{ $filter->{filters
} }, $f;
160 foreach my $p (qw(damaged itemlost)) {
161 my $v = $cgi->param($p) // '';
167 $f->{operator
} = '!=';
168 push @
{ $filter->{filters
} }, $f;
169 } elsif ($v eq 'no') {
170 $f->{operator
} = '=';
171 push @
{ $filter->{filters
} }, $f;
175 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
176 push @
{ $filter->{filters
} }, {
177 field
=> 'itemcallnumber',
178 query
=> $itemcallnumber_from,
182 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
183 push @
{ $filter->{filters
} }, {
184 field
=> 'itemcallnumber',
185 query
=> $itemcallnumber_to,
190 my $sortby = $cgi->param('sortby') || 'itemnumber';
191 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
192 $sortby = 'copyrightdate';
194 my $search_params = {
195 rows
=> scalar $cgi->param('rows') // 20,
196 page
=> scalar $cgi->param('page') || 1,
198 sortorder
=> scalar $cgi->param('sortorder') || 'asc',
201 my ($results, $total_rows) = SearchItems
($filter, $search_params);
203 if ($format eq 'barcodes') {
205 type
=> 'text/plain',
206 attachment
=> 'barcodes.txt',
209 foreach my $item (@
$results) {
210 print $item->{barcode
} . "\n";
216 # Get notforloan labels
217 my $notforloan_map = {};
218 foreach my $nfl_value (@
$notforloan_values) {
219 $notforloan_map->{$nfl_value->{authorised_value
}} = $nfl_value->{lib
};
222 # Get location labels
223 my $location_map = {};
224 foreach my $loc_value (@
$location_values) {
225 $location_map->{$loc_value->{authorised_value
}} = $loc_value->{lib
};
228 foreach my $item (@
$results) {
229 $item->{biblio
} = GetBiblio
($item->{biblionumber
});
230 ($item->{biblioitem
}) = GetBiblioItemByBiblioNumber
($item->{biblionumber
});
231 $item->{status
} = $notforloan_map->{$item->{notforloan
}};
232 if (defined $item->{location
}) {
233 $item->{location
} = $location_map->{$item->{location
}};
240 search_params
=> $search_params,
242 total_rows
=> $total_rows,
245 if ($format eq 'csv') {
248 attachment
=> 'items.csv',
251 for my $line ( split '\n', $template->output ) {
252 print "$line\n" unless $line =~ m
|^\s
*$|;
254 } elsif ($format eq 'json') {
255 $template->param(sEcho
=> scalar $cgi->param('sEcho'));
256 output_with_http_headers
$cgi, $cookie, $template->output, 'json';
262 # Display the search form
264 my @branches = map { value
=> $_->branchcode, label
=> $_->branchname }, Koha
::Libraries
->search( {}, { order_by
=> 'branchname' } );
266 foreach my $location (@
$location_values) {
268 value
=> $location->{authorised_value
},
269 label
=> $location->{lib
} // $location->{authorised_value
},
273 foreach my $itemtype ( Koha
::ItemTypes
->search ) {
275 value
=> $itemtype->itemtype,
276 label
=> $itemtype->translated_description,
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);
284 foreach my $ccode (@
$ccodes) {
286 value
=> $ccode->{authorised_value
},
287 label
=> $ccode->{lib
},
292 foreach my $value (@
$notforloan_values) {
294 value
=> $value->{authorised_value
},
295 label
=> $value->{lib
},
299 my @items_search_fields = GetItemSearchFields
();
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);
309 branches
=> \
@branches,
310 locations
=> \
@locations,
311 itemtypes
=> \
@itemtypes,
313 notforloans
=> \
@notforloans,
314 items_search_fields
=> \
@items_search_fields,
315 authorised_values_json
=> to_json
($authorised_values),
318 output_html_with_http_headers
$cgi, $cookie, $template->output;