2 # Copyright 2013 BibLibre
4 # This file is part of Koha
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use JSON qw( to_json from_json );
24 use C4::Auth qw( get_template_and_user );
25 use C4::Circulation qw( barcodedecode );
26 use C4::Output qw( output_with_http_headers output_html_with_http_headers );
27 use C4::Items qw( SearchItems );
28 use C4::Koha qw( GetAuthorisedValues );
30 use Koha::AuthorisedValues;
32 use Koha::Item::Search::Field qw(GetItemSearchFields);
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('length'));
46 $cgi->param('page', (scalar $cgi->param('start') / scalar $cgi->param('length')) + 1);
47 my @columns = @{ from_json $cgi->param('columns') };
48 $cgi->param('sortby', $columns[ $cgi->param('order[0][column]') ]->{name});
49 $cgi->param('sortorder', scalar $cgi->param('order[0][dir]'));
51 my @f = $cgi->multi_param('f');
52 my @q = $cgi->multi_param('q');
54 # If index indicates the value is a barcode, we need to preproccess it before searching
55 for ( my $i = 0; $i < @q; $i++ ) {
56 $q[$i] = barcodedecode($q[$i]) if $f[$i] eq 'barcode';
59 push @q, '' if @q == 0;
60 my @op = $cgi->multi_param('op');
61 my @c = $cgi->multi_param('c');
62 for my $column (@columns) {
63 my $search = $column->{search}->{value};
64 my $column_name = $column->{name};
65 if (defined $search and $search ne '') {
66 my @words = split /\s+/, $search;
67 foreach my $word (@words) {
68 push @f, $column_name;
71 if ( grep { $_ eq $column_name } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) {
83 $cgi->param('op', @op);
85 } elsif (defined $format and $format eq 'csv') {
86 $template_name = 'catalogue/itemsearch_csv.tt';
88 # Retrieve all results
89 $cgi->param('rows', 0);
90 } elsif (defined $format and $format eq 'barcodes') {
91 # Retrieve all results
92 $cgi->param('rows', 0);
93 } elsif (defined $format) {
94 die "Unsupported format $format";
97 my ($template, $borrowernumber, $cookie) = get_template_and_user({
98 template_name => $template_name,
101 flagsrequired => { catalogue => 1 },
104 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
105 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
107 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
108 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
110 if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
111 $template->param( has_new_status => 1 );
114 if ( defined $format ) {
115 # Parameters given, it's a search
118 conjunction => 'AND',
123 my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost withdrawn))
126 @q = $cgi->multi_param( $p . "[]" );
127 if ( scalar @q == 0 ) {
128 @q = $cgi->multi_param($p);
136 if ( my $op = scalar $cgi->param( $p . '_op' ) ) {
137 $f->{operator} = $op;
139 push @{ $filter->{filters} }, $f;
144 my @c = $cgi->multi_param('c');
145 my @fields = $cgi->multi_param('f');
146 my @q = $cgi->multi_param('q');
147 my @op = $cgi->multi_param('op');
150 for (my $i = 0; $i < @fields; $i++) {
151 my $field = $fields[$i];
154 if (defined $q and $q ne '') {
155 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
156 $field = 'copyrightdate';
180 push @{ $filter->{filters} }, $f;
183 foreach my $p (qw( damaged new_status )) {
184 my $v = $cgi->param($p) // '';
189 if ( $p eq 'new_status' ) {
193 $f->{operator} = '!=';
194 push @{ $filter->{filters} }, $f;
195 } elsif ($v eq 'no') {
196 $f->{operator} = '=';
197 push @{ $filter->{filters} }, $f;
201 # null/is not null parameters
202 foreach my $p (qw( onloan )) {
203 my $v = $cgi->param($p) // '';
208 if ( $v eq 'IS NOT NULL' ) {
209 $f->{query} = "not null";
210 } elsif ( $v eq 'IS NULL' ) {
211 $f->{query} = "null";
213 push @{ $filter->{filters} }, $f unless ( $v eq "" );
216 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
217 push @{ $filter->{filters} }, {
218 field => 'itemcallnumber',
219 query => $itemcallnumber_from,
223 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
224 push @{ $filter->{filters} }, {
225 field => 'itemcallnumber',
226 query => $itemcallnumber_to,
231 my $sortby = $cgi->param('sortby') || 'itemnumber';
232 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
233 $sortby = 'copyrightdate';
235 my $search_params = {
236 rows => scalar $cgi->param('rows') // 20,
237 page => scalar $cgi->param('page') || 1,
239 sortorder => scalar $cgi->param('sortorder') || 'asc',
242 my ($results, $total_rows) = SearchItems($filter, $search_params);
244 if ($format eq 'barcodes') {
246 type => 'text/plain',
247 attachment => 'barcodes.txt',
250 foreach my $item (@$results) {
251 print $item->{barcode} . "\n";
257 foreach my $item (@$results) {
258 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
259 $item->{biblio} = $biblio;
260 $item->{biblioitem} = $biblio->biblioitem->unblessed;
261 my $checkout = Koha::Checkouts->find({ itemnumber => $item->{itemnumber} });
262 $item->{checkout} = $checkout;
268 search_params => $search_params,
270 total_rows => $total_rows,
271 user => Koha::Patrons->find( $borrowernumber ),
274 if ($format eq 'csv') {
277 attachment => 'items.csv',
280 for my $line ( split '\n', $template->output ) {
281 print "$line\n" unless $line =~ m|^\s*$|;
283 } elsif ($format eq 'json') {
284 $template->param(draw => scalar $cgi->param('draw'));
285 output_with_http_headers $cgi, $cookie, $template->output, 'json';
291 # Display the search form
293 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } )->as_list;
294 my @itemtypes = map { value => $_->itemtype, label => $_->translated_description }, Koha::ItemTypes->search_with_localization->as_list;
296 my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
297 foreach my $ccode (@ccodes) {
298 $ccode->{value} = $ccode->{authorised_value},
299 $ccode->{label} = $ccode->{lib},
303 foreach my $value (@$itemlost_values) {
305 value => $value->{authorised_value},
306 label => $value->{lib},
311 foreach my $value (@$withdrawn_values) {
313 value => $value->{authorised_value},
314 label => $value->{lib},
318 my @items_search_fields = GetItemSearchFields();
320 my $authorised_values = {};
321 foreach my $field (@items_search_fields) {
322 if (my $category = ($field->{authorised_values_category})) {
323 $authorised_values->{$category} = GetAuthorisedValues($category);
328 branches => \@branches,
329 itemtypes => \@itemtypes,
331 itemlosts => \@itemlosts,
332 withdrawns => \@withdrawns,
333 items_search_fields => \@items_search_fields,
334 authorised_values_json => to_json($authorised_values),
337 output_html_with_http_headers $cgi, $cookie, $template->output;