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];
66 if ( grep /^$columns[$i]$/, qw( ccode homebranch holdingbranch location notforloan ) ) {
78 $cgi->param('op', @op);
80 } elsif (defined $format and $format eq 'csv') {
81 $template_name = 'catalogue/itemsearch_csv.tt';
83 # Retrieve all results
84 $cgi->param('rows', 0);
85 } elsif (defined $format and $format eq 'barcodes') {
86 # Retrieve all results
87 $cgi->param('rows', 0);
88 } elsif (defined $format) {
89 die "Unsupported format $format";
92 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
93 template_name
=> $template_name,
97 flagsrequired
=> { catalogue
=> 1 },
100 my $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.notforloan', authorised_value
=> [ -and => {'!=' => undef }, {'!=' => ''}] });
101 my $notforloan_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
103 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.location', authorised_value
=> [ -and => {'!=' => undef }, {'!=' => ''}] });
104 my $location_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
106 if (scalar keys %params > 0) {
107 # Parameters given, it's a search
110 conjunction
=> 'AND',
114 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
115 if (my @q = $cgi->multi_param($p)) {
121 if (my $op = scalar $cgi->param($p . '_op')) {
122 $f->{operator
} = $op;
124 push @
{ $filter->{filters
} }, $f;
129 my @c = $cgi->multi_param('c');
130 my @fields = $cgi->multi_param('f');
131 my @q = $cgi->multi_param('q');
132 my @op = $cgi->multi_param('op');
135 for (my $i = 0; $i < @fields; $i++) {
136 my $field = $fields[$i];
139 if (defined $q and $q ne '') {
141 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
142 $field = 'copyrightdate';
164 push @
{ $filter->{filters
} }, $f;
167 foreach my $p (qw(damaged itemlost)) {
168 my $v = $cgi->param($p) // '';
174 $f->{operator
} = '!=';
175 push @
{ $filter->{filters
} }, $f;
176 } elsif ($v eq 'no') {
177 $f->{operator
} = '=';
178 push @
{ $filter->{filters
} }, $f;
182 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
183 push @
{ $filter->{filters
} }, {
184 field
=> 'itemcallnumber',
185 query
=> $itemcallnumber_from,
189 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
190 push @
{ $filter->{filters
} }, {
191 field
=> 'itemcallnumber',
192 query
=> $itemcallnumber_to,
197 my $sortby = $cgi->param('sortby') || 'itemnumber';
198 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
199 $sortby = 'copyrightdate';
201 my $search_params = {
202 rows
=> scalar $cgi->param('rows') // 20,
203 page
=> scalar $cgi->param('page') || 1,
205 sortorder
=> scalar $cgi->param('sortorder') || 'asc',
208 my ($results, $total_rows) = SearchItems
($filter, $search_params);
210 if ($format eq 'barcodes') {
212 type
=> 'text/plain',
213 attachment
=> 'barcodes.txt',
216 foreach my $item (@
$results) {
217 print $item->{barcode
} . "\n";
223 # Get notforloan labels
224 my $notforloan_map = {};
225 foreach my $nfl_value (@
$notforloan_values) {
226 $notforloan_map->{$nfl_value->{authorised_value
}} = $nfl_value->{lib
};
229 # Get location labels
230 my $location_map = {};
231 foreach my $loc_value (@
$location_values) {
232 $location_map->{$loc_value->{authorised_value
}} = $loc_value->{lib
};
235 foreach my $item (@
$results) {
236 my $biblio = Koha
::Biblios
->find( $item->{biblionumber
} );
237 $item->{biblio
} = $biblio;
238 $item->{biblioitem
} = $biblio->biblioitem->unblessed;
239 $item->{status
} = $notforloan_map->{$item->{notforloan
}};
240 if (defined $item->{location
}) {
241 $item->{location
} = $location_map->{$item->{location
}};
248 search_params
=> $search_params,
250 total_rows
=> $total_rows,
253 if ($format eq 'csv') {
256 attachment
=> 'items.csv',
259 for my $line ( split '\n', $template->output ) {
260 print "$line\n" unless $line =~ m
|^\s
*$|;
262 } elsif ($format eq 'json') {
263 $template->param(sEcho
=> scalar $cgi->param('sEcho'));
264 output_with_http_headers
$cgi, $cookie, $template->output, 'json';
270 # Display the search form
272 my @branches = map { value
=> $_->branchcode, label
=> $_->branchname }, Koha
::Libraries
->search( {}, { order_by
=> 'branchname' } );
274 foreach my $location (@
$location_values) {
276 value
=> $location->{authorised_value
},
277 label
=> $location->{lib
} // $location->{authorised_value
},
281 foreach my $itemtype ( Koha
::ItemTypes
->search ) {
283 value
=> $itemtype->itemtype,
284 label
=> $itemtype->translated_description,
288 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.ccode', authorised_value
=> [ -and => {'!=' => undef }, {'!=' => ''}] });
289 my $ccode_avcode = $mss->count ?
$mss->next->authorised_value : 'CCODE';
290 my $ccodes = GetAuthorisedValues
($ccode_avcode);
292 foreach my $ccode (@
$ccodes) {
294 value
=> $ccode->{authorised_value
},
295 label
=> $ccode->{lib
},
300 foreach my $value (@
$notforloan_values) {
302 value
=> $value->{authorised_value
},
303 label
=> $value->{lib
},
307 my @items_search_fields = GetItemSearchFields
();
309 my $authorised_values = {};
310 foreach my $field (@items_search_fields) {
311 if (my $category = ($field->{authorised_values_category
})) {
312 $authorised_values->{$category} = GetAuthorisedValues
($category);
317 branches
=> \
@branches,
318 locations
=> \
@locations,
319 itemtypes
=> \
@itemtypes,
321 notforloans
=> \
@notforloans,
322 items_search_fields
=> \
@items_search_fields,
323 authorised_values_json
=> to_json
($authorised_values),
326 output_html_with_http_headers
$cgi, $cookie, $template->output;