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', 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];
71 $cgi->param('op', @op);
73 } elsif (defined $format and $format eq 'csv') {
74 $template_name = 'catalogue/itemsearch_csv.tt';
76 # Retrieve all results
77 $cgi->param('rows', 0);
80 $template_name = 'catalogue/itemsearch.tt';
81 $content_type = 'html';
84 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
85 template_name
=> $template_name,
89 flagsrequired
=> { catalogue
=> 1 },
92 my $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.notforloan', authorised_value
=> { not => undef } });
93 my $notforloan_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
95 $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.location', authorised_value
=> { not => undef } });
96 my $location_values = $mss->count ? GetAuthorisedValues
($mss->next->authorised_value) : [];
98 if (scalar keys %params > 0) {
99 # Parameters given, it's a search
102 conjunction
=> 'AND',
106 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
107 if (my @q = $cgi->multi_param($p)) {
113 if (my $op = scalar $cgi->param($p . '_op')) {
114 $f->{operator
} = $op;
116 push @
{ $filter->{filters
} }, $f;
121 my @c = $cgi->multi_param('c');
122 my @fields = $cgi->multi_param('f');
123 my @q = $cgi->multi_param('q');
124 my @op = $cgi->multi_param('op');
127 for (my $i = 0; $i < @fields; $i++) {
128 my $field = $fields[$i];
131 if (defined $q and $q ne '') {
133 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
134 $field = 'copyrightdate';
156 push @
{ $filter->{filters
} }, $f;
159 foreach my $p (qw(damaged itemlost)) {
160 my $v = $cgi->param($p) // '';
166 $f->{operator
} = '!=';
167 push @
{ $filter->{filters
} }, $f;
168 } elsif ($v eq 'no') {
169 $f->{operator
} = '=';
170 push @
{ $filter->{filters
} }, $f;
174 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
175 push @
{ $filter->{filters
} }, {
176 field
=> 'itemcallnumber',
177 query
=> $itemcallnumber_from,
181 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
182 push @
{ $filter->{filters
} }, {
183 field
=> 'itemcallnumber',
184 query
=> $itemcallnumber_to,
189 my $sortby = $cgi->param('sortby') || 'itemnumber';
190 if (C4
::Context
->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
191 $sortby = 'copyrightdate';
193 my $search_params = {
194 rows
=> scalar $cgi->param('rows') // 20,
195 page
=> scalar $cgi->param('page') || 1,
197 sortorder
=> scalar $cgi->param('sortorder') || 'asc',
200 my ($results, $total_rows) = SearchItems
($filter, $search_params);
202 # Get notforloan labels
203 my $notforloan_map = {};
204 foreach my $nfl_value (@
$notforloan_values) {
205 $notforloan_map->{$nfl_value->{authorised_value
}} = $nfl_value->{lib
};
208 # Get location labels
209 my $location_map = {};
210 foreach my $loc_value (@
$location_values) {
211 $location_map->{$loc_value->{authorised_value
}} = $loc_value->{lib
};
214 foreach my $item (@
$results) {
215 $item->{biblio
} = GetBiblio
($item->{biblionumber
});
216 ($item->{biblioitem
}) = GetBiblioItemByBiblioNumber
($item->{biblionumber
});
217 $item->{status
} = $notforloan_map->{$item->{notforloan
}};
218 if (defined $item->{location
}) {
219 $item->{location
} = $location_map->{$item->{location
}};
226 search_params
=> $search_params,
228 total_rows
=> $total_rows,
232 if ($format eq 'html') {
233 # Build pagination bar
234 my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
236 foreach my $p (keys %params) {
237 my @v = $cgi->multi_param($p);
238 push @params, map { "$p=" . $_ } @v;
240 $url .= '?' . join ('&', @params);
241 my $nb_pages = 1 + int($total_rows / $search_params->{rows
});
242 my $current_page = $search_params->{page
};
243 my $pagination_bar = pagination_bar
($url, $nb_pages, $current_page, 'page');
245 $template->param(pagination_bar
=> $pagination_bar);
249 if ($format eq 'html') {
250 # Retrieve data required for the form.
252 my @branches = map { value
=> $_->branchcode, label
=> $_->branchname }, Koha
::Libraries
->search( {}, { order_by
=> 'branchname' } );
254 foreach my $location (@
$location_values) {
256 value
=> $location->{authorised_value
},
257 label
=> $location->{lib
} // $location->{authorised_value
},
261 foreach my $itemtype ( Koha
::ItemTypes
->search ) {
263 value
=> $itemtype->itemtype,
264 label
=> $itemtype->translated_description,
268 my $mss = Koha
::MarcSubfieldStructures
->search({ frameworkcode
=> '', kohafield
=> 'items.ccode', authorised_value
=> { not => undef } });
269 my $ccode_avcode = $mss->count ?
$mss->next->authorised_value : 'CCODE';
270 my $ccodes = GetAuthorisedValues
($ccode_avcode);
272 foreach my $ccode (@
$ccodes) {
274 value
=> $ccode->{authorised_value
},
275 label
=> $ccode->{lib
},
280 foreach my $value (@
$notforloan_values) {
282 value
=> $value->{authorised_value
},
283 label
=> $value->{lib
},
287 my @items_search_fields = GetItemSearchFields
();
289 my $authorised_values = {};
290 foreach my $field (@items_search_fields) {
291 if (my $category = ($field->{authorised_values_category
})) {
292 $authorised_values->{$category} = GetAuthorisedValues
($category);
297 branches
=> \
@branches,
298 locations
=> \
@locations,
299 itemtypes
=> \
@itemtypes,
301 notforloans
=> \
@notforloans,
302 items_search_fields
=> \
@items_search_fields,
303 authorised_values_json
=> to_json
($authorised_values),
307 if ($format eq 'csv') {
310 attachment
=> 'items.csv',
313 for my $line ( split '\n', $template->output ) {
314 print "$line\n" unless $line =~ m
|^\s
*$|;
317 output_with_http_headers
$cgi, $cookie, $template->output, $content_type;