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 );
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);
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');
55 # If index indicates the value is a barcode, we need to preproccess it before searching
56 for ( my $i = 0; $i < @q; $i++ ) {
57 $q[$i] = barcodedecode($q[$i]) if $f[$i] eq 'barcode';
60 push @q, '' if @q == 0;
61 my @op = $cgi->multi_param('op');
62 my @c = $cgi->multi_param('c');
63 my $iColumns = $cgi->param('iColumns');
64 foreach my $i (0 .. ($iColumns - 1)) {
65 my $sSearch = $cgi->param("sSearch_$i");
66 if (defined $sSearch and $sSearch ne '') {
67 my @words = split /\s+/, $sSearch;
68 foreach my $word (@words) {
69 push @f, $columns[$i];
72 if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) {
84 $cgi->param('op', @op);
86 } elsif (defined $format and $format eq 'csv') {
87 $template_name = 'catalogue/itemsearch_csv.tt';
89 # Retrieve all results
90 $cgi->param('rows', 0);
91 } elsif (defined $format and $format eq 'barcodes') {
92 # Retrieve all results
93 $cgi->param('rows', 0);
94 } elsif (defined $format) {
95 die "Unsupported format $format";
98 my ($template, $borrowernumber, $cookie) = get_template_and_user({
99 template_name => $template_name,
102 flagsrequired => { catalogue => 1 },
105 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
106 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
108 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
109 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
111 if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
112 $template->param( has_new_status => 1 );
115 if ( defined $format ) {
116 # Parameters given, it's a search
119 conjunction => 'AND',
123 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost withdrawn)) {
124 if (my @q = $cgi->multi_param($p)) {
130 if (my $op = scalar $cgi->param($p . '_op')) {
131 $f->{operator} = $op;
133 push @{ $filter->{filters} }, $f;
138 my @c = $cgi->multi_param('c');
139 my @fields = $cgi->multi_param('f');
140 my @q = $cgi->multi_param('q');
141 my @op = $cgi->multi_param('op');
144 for (my $i = 0; $i < @fields; $i++) {
145 my $field = $fields[$i];
148 if (defined $q and $q ne '') {
149 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
150 $field = 'copyrightdate';
174 push @{ $filter->{filters} }, $f;
177 foreach my $p (qw( damaged new_status )) {
178 my $v = $cgi->param($p) // '';
183 if ( $p eq 'new_status' ) {
187 $f->{operator} = '!=';
188 push @{ $filter->{filters} }, $f;
189 } elsif ($v eq 'no') {
190 $f->{operator} = '=';
191 push @{ $filter->{filters} }, $f;
195 # null/is not null parameters
196 foreach my $p (qw( onloan )) {
197 my $v = $cgi->param($p) // '';
202 if ( $v eq 'IS NOT NULL' ) {
203 $f->{query} = "not null";
204 } elsif ( $v eq 'IS NULL' ) {
205 $f->{query} = "null";
207 push @{ $filter->{filters} }, $f unless ( $v eq "" );
210 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
211 push @{ $filter->{filters} }, {
212 field => 'itemcallnumber',
213 query => $itemcallnumber_from,
217 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
218 push @{ $filter->{filters} }, {
219 field => 'itemcallnumber',
220 query => $itemcallnumber_to,
225 my $sortby = $cgi->param('sortby') || 'itemnumber';
226 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
227 $sortby = 'copyrightdate';
229 my $search_params = {
230 rows => scalar $cgi->param('rows') // 20,
231 page => scalar $cgi->param('page') || 1,
233 sortorder => scalar $cgi->param('sortorder') || 'asc',
236 my ($results, $total_rows) = SearchItems($filter, $search_params);
238 if ($format eq 'barcodes') {
240 type => 'text/plain',
241 attachment => 'barcodes.txt',
244 foreach my $item (@$results) {
245 print $item->{barcode} . "\n";
251 foreach my $item (@$results) {
252 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
253 $item->{biblio} = $biblio;
254 $item->{biblioitem} = $biblio->biblioitem->unblessed;
255 my $checkout = Koha::Checkouts->find({ itemnumber => $item->{itemnumber} });
256 $item->{checkout} = $checkout;
262 search_params => $search_params,
264 total_rows => $total_rows,
265 user => Koha::Patrons->find( $borrowernumber ),
268 if ($format eq 'csv') {
271 attachment => 'items.csv',
274 for my $line ( split '\n', $template->output ) {
275 print "$line\n" unless $line =~ m|^\s*$|;
277 } elsif ($format eq 'json') {
278 $template->param(sEcho => scalar $cgi->param('sEcho'));
279 output_with_http_headers $cgi, $cookie, $template->output, 'json';
285 # Display the search form
287 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } )->as_list;
288 my @itemtypes = map { value => $_->itemtype, label => $_->translated_description }, Koha::ItemTypes->search_with_localization->as_list;
290 my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
291 foreach my $ccode (@ccodes) {
292 $ccode->{value} = $ccode->{authorised_value},
293 $ccode->{label} = $ccode->{lib},
297 foreach my $value (@$itemlost_values) {
299 value => $value->{authorised_value},
300 label => $value->{lib},
305 foreach my $value (@$withdrawn_values) {
307 value => $value->{authorised_value},
308 label => $value->{lib},
312 my @items_search_fields = GetItemSearchFields();
314 my $authorised_values = {};
315 foreach my $field (@items_search_fields) {
316 if (my $category = ($field->{authorised_values_category})) {
317 $authorised_values->{$category} = GetAuthorisedValues($category);
322 branches => \@branches,
323 itemtypes => \@itemtypes,
325 itemlosts => \@itemlosts,
326 withdrawns => \@withdrawns,
327 items_search_fields => \@items_search_fields,
328 authorised_values_json => to_json($authorised_values),
331 output_html_with_http_headers $cgi, $cookie, $template->output;