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::Output qw( output_with_http_headers output_html_with_http_headers );
26 use C4::Items qw( SearchItems );
27 use C4::Koha qw( GetAuthorisedValues );
29 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];
65 if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) {
77 $cgi->param('op', @op);
79 } elsif (defined $format and $format eq 'csv') {
80 $template_name = 'catalogue/itemsearch_csv.tt';
82 # Retrieve all results
83 $cgi->param('rows', 0);
84 } elsif (defined $format and $format eq 'barcodes') {
85 # Retrieve all results
86 $cgi->param('rows', 0);
87 } elsif (defined $format) {
88 die "Unsupported format $format";
91 my ($template, $borrowernumber, $cookie) = get_template_and_user({
92 template_name => $template_name,
95 flagsrequired => { catalogue => 1 },
98 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
99 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
101 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
102 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
104 if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
105 $template->param( has_new_status => 1 );
108 if ( defined $format ) {
109 # Parameters given, it's a search
112 conjunction => 'AND',
116 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost withdrawn)) {
117 if (my @q = $cgi->multi_param($p)) {
123 if (my $op = scalar $cgi->param($p . '_op')) {
124 $f->{operator} = $op;
126 push @{ $filter->{filters} }, $f;
131 my @c = $cgi->multi_param('c');
132 my @fields = $cgi->multi_param('f');
133 my @q = $cgi->multi_param('q');
134 my @op = $cgi->multi_param('op');
137 for (my $i = 0; $i < @fields; $i++) {
138 my $field = $fields[$i];
141 if (defined $q and $q ne '') {
142 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
143 $field = 'copyrightdate';
167 push @{ $filter->{filters} }, $f;
170 foreach my $p (qw( damaged new_status )) {
171 my $v = $cgi->param($p) // '';
176 if ( $p eq 'new_status' ) {
180 $f->{operator} = '!=';
181 push @{ $filter->{filters} }, $f;
182 } elsif ($v eq 'no') {
183 $f->{operator} = '=';
184 push @{ $filter->{filters} }, $f;
188 # null/is not null parameters
189 foreach my $p (qw( onloan )) {
190 my $v = $cgi->param($p) // '';
195 if ( $v eq 'IS NOT NULL' ) {
196 $f->{query} = "not null";
197 } elsif ( $v eq 'IS NULL' ) {
198 $f->{query} = "null";
200 push @{ $filter->{filters} }, $f unless ( $v eq "" );
203 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
204 push @{ $filter->{filters} }, {
205 field => 'itemcallnumber',
206 query => $itemcallnumber_from,
210 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
211 push @{ $filter->{filters} }, {
212 field => 'itemcallnumber',
213 query => $itemcallnumber_to,
218 my $sortby = $cgi->param('sortby') || 'itemnumber';
219 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
220 $sortby = 'copyrightdate';
222 my $search_params = {
223 rows => scalar $cgi->param('rows') // 20,
224 page => scalar $cgi->param('page') || 1,
226 sortorder => scalar $cgi->param('sortorder') || 'asc',
229 my ($results, $total_rows) = SearchItems($filter, $search_params);
231 if ($format eq 'barcodes') {
233 type => 'text/plain',
234 attachment => 'barcodes.txt',
237 foreach my $item (@$results) {
238 print $item->{barcode} . "\n";
244 foreach my $item (@$results) {
245 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
246 $item->{biblio} = $biblio;
247 $item->{biblioitem} = $biblio->biblioitem->unblessed;
248 my $checkout = Koha::Checkouts->find({ itemnumber => $item->{itemnumber} });
249 $item->{checkout} = $checkout;
255 search_params => $search_params,
257 total_rows => $total_rows,
260 if ($format eq 'csv') {
263 attachment => 'items.csv',
266 for my $line ( split '\n', $template->output ) {
267 print "$line\n" unless $line =~ m|^\s*$|;
269 } elsif ($format eq 'json') {
270 $template->param(sEcho => scalar $cgi->param('sEcho'));
271 output_with_http_headers $cgi, $cookie, $template->output, 'json';
277 # Display the search form
279 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } )->as_list;
280 my @itemtypes = map { value => $_->itemtype, label => $_->translated_description }, Koha::ItemTypes->search_with_localization->as_list;
282 my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
283 foreach my $ccode (@ccodes) {
284 $ccode->{value} = $ccode->{authorised_value},
285 $ccode->{label} = $ccode->{lib},
289 foreach my $value (@$itemlost_values) {
291 value => $value->{authorised_value},
292 label => $value->{lib},
297 foreach my $value (@$withdrawn_values) {
299 value => $value->{authorised_value},
300 label => $value->{lib},
304 my @items_search_fields = GetItemSearchFields();
306 my $authorised_values = {};
307 foreach my $field (@items_search_fields) {
308 if (my $category = ($field->{authorised_values_category})) {
309 $authorised_values->{$category} = GetAuthorisedValues($category);
314 branches => \@branches,
315 itemtypes => \@itemtypes,
317 itemlosts => \@itemlosts,
318 withdrawns => \@withdrawns,
319 items_search_fields => \@items_search_fields,
320 authorised_values_json => to_json($authorised_values),
323 output_html_with_http_headers $cgi, $cookie, $template->output;