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;
31 use Koha::Item::Search::Field qw(GetItemSearchFields);
36 my %params = $cgi->Vars;
38 my $format = $cgi->param('format');
39 my ($template_name, $content_type);
40 if (defined $format and $format eq 'json') {
41 $template_name = 'catalogue/itemsearch_json.tt';
42 $content_type = 'json';
44 # Map DataTables parameters with 'regular' parameters
45 $cgi->param('rows', $cgi->param('iDisplayLength'));
46 $cgi->param('page', ($cgi->param('iDisplayStart') / $cgi->param('iDisplayLength')) + 1);
47 my @columns = split /,/, $cgi->multi_param('sColumns');
48 $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
49 $cgi->param('sortorder', $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 foreach my $i (0 .. ($cgi->param('iColumns') - 1)) {
57 my $sSearch = $cgi->param("sSearch_$i");
58 if (defined $sSearch and $sSearch ne '') {
59 my @words = split /\s+/, $sSearch;
60 foreach my $word (@words) {
61 push @f, $columns[$i];
70 $cgi->param('op', @op);
72 } elsif (defined $format and $format eq 'csv') {
73 $template_name = 'catalogue/itemsearch_csv.tt';
75 # Retrieve all results
76 $cgi->param('rows', 0);
79 $template_name = 'catalogue/itemsearch.tt';
80 $content_type = 'html';
83 my ($template, $borrowernumber, $cookie) = get_template_and_user({
84 template_name => $template_name,
88 flagsrequired => { catalogue => 1 },
91 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan' });
92 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
94 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location' });
95 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
97 if (scalar keys %params > 0) {
98 # Parameters given, it's a search
101 conjunction => 'AND',
105 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
106 if (my @q = $cgi->multi_param($p)) {
112 if (my $op = $cgi->param($p . '_op')) {
113 $f->{operator} = $op;
115 push @{ $filter->{filters} }, $f;
120 my @c = $cgi->multi_param('c');
121 my @fields = $cgi->multi_param('f');
122 my @q = $cgi->multi_param('q');
123 my @op = $cgi->multi_param('op');
126 for (my $i = 0; $i < @fields; $i++) {
127 my $field = $fields[$i];
130 if (defined $q and $q ne '') {
132 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
133 $field = 'copyrightdate';
155 push @{ $filter->{filters} }, $f;
158 foreach my $p (qw(damaged itemlost)) {
159 my $v = $cgi->param($p) // '';
165 $f->{operator} = '!=';
166 push @{ $filter->{filters} }, $f;
167 } elsif ($v eq 'no') {
168 $f->{operator} = '=';
169 push @{ $filter->{filters} }, $f;
173 if (my $itemcallnumber_from = $cgi->param('itemcallnumber_from')) {
174 push @{ $filter->{filters} }, {
175 field => 'itemcallnumber',
176 query => $itemcallnumber_from,
180 if (my $itemcallnumber_to = $cgi->param('itemcallnumber_to')) {
181 push @{ $filter->{filters} }, {
182 field => 'itemcallnumber',
183 query => $itemcallnumber_to,
188 my $sortby = $cgi->param('sortby') || 'itemnumber';
189 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
190 $sortby = 'copyrightdate';
192 my $search_params = {
193 rows => scalar $cgi->param('rows') // 20,
194 page => scalar $cgi->param('page') || 1,
196 sortorder => scalar $cgi->param('sortorder') || 'asc',
199 my ($results, $total_rows) = SearchItems($filter, $search_params);
201 # Get notforloan labels
202 my $notforloan_map = {};
203 foreach my $nfl_value (@$notforloan_values) {
204 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
207 # Get location labels
208 my $location_map = {};
209 foreach my $loc_value (@$location_values) {
210 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
213 foreach my $item (@$results) {
214 $item->{biblio} = GetBiblio($item->{biblionumber});
215 ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
216 $item->{status} = $notforloan_map->{$item->{notforloan}};
217 if (defined $item->{location}) {
218 $item->{location} = $location_map->{$item->{location}};
225 search_params => $search_params,
227 total_rows => $total_rows,
231 if ($format eq 'html') {
232 # Build pagination bar
233 my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
235 foreach my $p (keys %params) {
236 my @v = $cgi->multi_param($p);
237 push @params, map { "$p=" . $_ } @v;
239 $url .= '?' . join ('&', @params);
240 my $nb_pages = 1 + int($total_rows / $search_params->{rows});
241 my $current_page = $search_params->{page};
242 my $pagination_bar = pagination_bar($url, $nb_pages, $current_page, 'page');
244 $template->param(pagination_bar => $pagination_bar);
248 if ($format eq 'html') {
249 # Retrieve data required for the form.
251 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
253 foreach my $location (@$location_values) {
255 value => $location->{authorised_value},
256 label => $location->{lib} // $location->{authorised_value},
260 foreach my $itemtype ( Koha::ItemTypes->search ) {
262 value => $itemtype->itemtype,
263 label => $itemtype->translated_description,
267 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode' });
268 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
269 my $ccodes = GetAuthorisedValues($ccode_avcode);
271 foreach my $ccode (@$ccodes) {
273 value => $ccode->{authorised_value},
274 label => $ccode->{lib},
279 foreach my $value (@$notforloan_values) {
281 value => $value->{authorised_value},
282 label => $value->{lib},
286 my @items_search_fields = GetItemSearchFields();
288 my $authorised_values = {};
289 foreach my $field (@items_search_fields) {
290 if (my $category = ($field->{authorised_values_category})) {
291 $authorised_values->{$category} = GetAuthorisedValues($category);
296 branches => \@branches,
297 locations => \@locations,
298 itemtypes => \@itemtypes,
300 notforloans => \@notforloans,
301 items_search_fields => \@items_search_fields,
302 authorised_values_json => to_json($authorised_values),
306 if ($format eq 'csv') {
309 attachment => 'items.csv',
312 for my $line ( split '\n', $template->output ) {
313 print "$line\n" unless $line =~ m|^\s*$|;
316 output_with_http_headers $cgi, $cookie, $template->output, $content_type;