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
;
32 use Koha
::Item
::Search
::Field
qw(GetItemSearchFields);
37 my %params = $cgi->Vars;
39 my $format = $cgi->param('format');
40 my $template_name = 'catalogue/itemsearch.tt';
42 if (defined $format and $format eq 'json') {
43 $template_name = 'catalogue/itemsearch_json.tt';
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'));
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];
72 $cgi->param('op', @op);
74 } elsif (defined $format and $format eq 'csv') {
75 $template_name = 'catalogue/itemsearch_csv.tt';
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";
86 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
87 template_name
=> $template_name,
91 flagsrequired
=> { catalogue
=> 1 },
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) : [];
97 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.location', authorised_value
=> { not => undef } });
98 my $location_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
100 if (scalar keys %params > 0) {
101 # Parameters given, it's a search
104 conjunction
=> 'AND',
108 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
109 if (my @q = $cgi->multi_param($p)) {
115 if (my $op = scalar $cgi->param($p . '_op')) {
116 $f->{operator
} = $op;
118 push @
{ $filter->{filters
} }, $f;
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');
129 for (my $i = 0; $i < @fields; $i++) {
130 my $field = $fields[$i];
133 if (defined $q and $q ne '') {
135 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
136 $field = 'copyrightdate';
158 push @
{ $filter->{filters
} }, $f;
161 foreach my $p (qw(damaged itemlost)) {
162 my $v = $cgi->param($p) // '';
168 $f->{operator
} = '!=';
169 push @
{ $filter->{filters
} }, $f;
170 } elsif ($v eq 'no') {
171 $f->{operator
} = '=';
172 push @
{ $filter->{filters
} }, $f;
176 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
177 push @
{ $filter->{filters
} }, {
178 field
=> 'itemcallnumber',
179 query
=> $itemcallnumber_from,
183 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
184 push @
{ $filter->{filters
} }, {
185 field
=> 'itemcallnumber',
186 query
=> $itemcallnumber_to,
191 my $sortby = $cgi->param('sortby') || 'itemnumber';
192 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
193 $sortby = 'copyrightdate';
195 my $search_params = {
196 rows
=> scalar $cgi->param('rows') // 20,
197 page
=> scalar $cgi->param('page') || 1,
199 sortorder
=> scalar $cgi->param('sortorder') || 'asc',
202 my ($results, $total_rows) = SearchItems
($filter, $search_params);
204 if ($format eq 'barcodes') {
206 type
=> 'text/plain',
207 attachment
=> 'barcodes.txt',
210 foreach my $item (@
$results) {
211 print $item->{barcode
} . "\n";
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
};
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
};
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
}};
241 search_params
=> $search_params,
243 total_rows
=> $total_rows,
246 if ($format eq 'csv') {
249 attachment
=> 'items.csv',
252 for my $line ( split '\n', $template->output ) {
253 print "$line\n" unless $line =~ m
|^\s
*$|;
255 } elsif ($format eq 'json') {
256 $template->param(sEcho
=> scalar $cgi->param('sEcho'));
257 output_with_http_headers
$cgi, $cookie, $template->output, 'json';
263 # Display the search form
265 my @branches = map { value
=> $_->branchcode, label
=> $_->branchname }, Koha
::Libraries
->search( {}, { order_by
=> 'branchname' } );
267 foreach my $location (@
$location_values) {
269 value
=> $location->{authorised_value
},
270 label
=> $location->{lib
} // $location->{authorised_value
},
274 foreach my $itemtype ( Koha
::ItemTypes
->search ) {
276 value
=> $itemtype->itemtype,
277 label
=> $itemtype->translated_description,
281 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.ccode', authorised_value
=> { not => undef } });
282 my $ccode_avcode = $mss->count ?
$mss->next->authorised_value : 'CCODE';
283 my $ccodes = GetAuthorisedValues
($ccode_avcode);
285 foreach my $ccode (@
$ccodes) {
287 value
=> $ccode->{authorised_value
},
288 label
=> $ccode->{lib
},
293 foreach my $value (@
$notforloan_values) {
295 value
=> $value->{authorised_value
},
296 label
=> $value->{lib
},
300 my @items_search_fields = GetItemSearchFields
();
302 my $authorised_values = {};
303 foreach my $field (@items_search_fields) {
304 if (my $category = ($field->{authorised_values_category
})) {
305 $authorised_values->{$category} = GetAuthorisedValues
($category);
310 branches
=> \
@branches,
311 locations
=> \
@locations,
312 itemtypes
=> \
@itemtypes,
314 notforloans
=> \
@notforloans,
315 items_search_fields
=> \
@items_search_fields,
316 authorised_values_json
=> to_json
($authorised_values),
319 output_html_with_http_headers
$cgi, $cookie, $template->output;