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.
32 use Koha
::Item
::Search
::Field
qw(GetItemSearchFields);
35 my %params = $cgi->Vars;
37 my $format = $cgi->param('format');
38 my ($template_name, $content_type);
39 if (defined $format and $format eq 'json') {
40 $template_name = 'catalogue/itemsearch.json.tt';
41 $content_type = 'json';
43 # Map DataTables parameters with 'regular' parameters
44 $cgi->param('rows', $cgi->param('iDisplayLength'));
45 $cgi->param('page', ($cgi->param('iDisplayStart') / $cgi->param('iDisplayLength')) + 1);
46 my @columns = split /,/, $cgi->param('sColumns');
47 $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
48 $cgi->param('sortorder', $cgi->param('sSortDir_0'));
50 my @f = $cgi->param('f');
51 my @q = $cgi->param('q');
52 push @q, '' if @q == 0;
53 my @op = $cgi->param('op');
54 my @c = $cgi->param('c');
55 foreach my $i (0 .. ($cgi->param('iColumns') - 1)) {
56 my $sSearch = $cgi->param("sSearch_$i");
57 if (defined $sSearch and $sSearch ne '') {
58 my @words = split /\s+/, $sSearch;
59 foreach my $word (@words) {
60 push @f, $columns[$i];
69 $cgi->param('op', @op);
71 } elsif (defined $format and $format eq 'csv') {
72 $template_name = 'catalogue/itemsearch.csv.tt';
74 # Retrieve all results
75 $cgi->param('rows', 0);
78 $template_name = 'catalogue/itemsearch.tt';
79 $content_type = 'html';
82 my ($template, $borrowernumber, $cookie) = get_template_and_user
({
83 template_name
=> $template_name,
87 flagsrequired
=> { catalogue
=> 1 },
90 my $notforloan_avcode = GetAuthValCode
('items.notforloan');
91 my $notforloan_values = GetAuthorisedValues
($notforloan_avcode);
93 my $location_avcode = GetAuthValCode
('items.location');
94 my $location_values = GetAuthorisedValues
($location_avcode);
96 if (scalar keys %params > 0) {
97 # Parameters given, it's a search
100 conjunction
=> 'AND',
104 foreach my $p (qw(homebranch location itype ccode issues datelastborrowed notforloan)) {
105 if (my @q = $cgi->param($p)) {
111 if (my $op = $cgi->param($p . '_op')) {
112 $f->{operator
} = $op;
114 push @
{ $filter->{filters
} }, $f;
119 my @c = $cgi->param('c');
120 my @fields = $cgi->param('f');
121 my @q = $cgi->param('q');
122 my @op = $cgi->param('op');
125 for (my $i = 0; $i < @fields; $i++) {
126 my $field = $fields[$i];
129 if (defined $q and $q ne '') {
151 push @
{ $filter->{filters
} }, $f;
154 foreach my $p (qw(damaged itemlost)) {
155 my $v = $cgi->param($p) // '';
161 $f->{operator
} = '!=';
162 push @
{ $filter->{filters
} }, $f;
163 } elsif ($v eq 'no') {
164 $f->{operator
} = '=';
165 push @
{ $filter->{filters
} }, $f;
169 if (my $itemcallnumber_from = $cgi->param('itemcallnumber_from')) {
170 push @
{ $filter->{filters
} }, {
171 field
=> 'itemcallnumber',
172 query
=> $itemcallnumber_from,
176 if (my $itemcallnumber_to = $cgi->param('itemcallnumber_to')) {
177 push @
{ $filter->{filters
} }, {
178 field
=> 'itemcallnumber',
179 query
=> $itemcallnumber_to,
184 my $search_params = {
185 rows
=> $cgi->param('rows') // 20,
186 page
=> $cgi->param('page') || 1,
187 sortby
=> $cgi->param('sortby') || 'itemnumber',
188 sortorder
=> $cgi->param('sortorder') || 'asc',
191 my ($results, $total_rows) = SearchItems
($filter, $search_params);
193 # Get notforloan labels
194 my $notforloan_map = {};
195 foreach my $nfl_value (@
$notforloan_values) {
196 $notforloan_map->{$nfl_value->{authorised_value
}} = $nfl_value->{lib
};
199 # Get location labels
200 my $location_map = {};
201 foreach my $loc_value (@
$location_values) {
202 $location_map->{$loc_value->{authorised_value
}} = $loc_value->{lib
};
205 foreach my $item (@
$results) {
206 $item->{biblio
} = GetBiblio
($item->{biblionumber
});
207 ($item->{biblioitem
}) = GetBiblioItemByBiblioNumber
($item->{biblionumber
});
208 $item->{status
} = $notforloan_map->{$item->{notforloan
}};
209 if (defined $item->{location
}) {
210 $item->{location
} = $location_map->{$item->{location
}};
217 search_params
=> $search_params,
219 total_rows
=> $total_rows,
223 if ($format eq 'html') {
224 # Build pagination bar
225 my $url = $cgi->url(-absolute
=> 1);
227 foreach my $p (keys %params) {
228 my @v = $cgi->param($p);
229 push @params, map { "$p=" . $_ } @v;
231 $url .= '?' . join ('&', @params);
232 my $nb_pages = 1 + int($total_rows / $search_params->{rows
});
233 my $current_page = $search_params->{page
};
234 my $pagination_bar = pagination_bar
($url, $nb_pages, $current_page, 'page');
236 $template->param(pagination_bar
=> $pagination_bar);
240 if ($format eq 'html') {
241 # Retrieve data required for the form.
243 my $branches = GetBranches
();
245 foreach my $branchcode (keys %$branches) {
247 value
=> $branchcode,
248 label
=> $branches->{$branchcode}->{branchname
},
252 foreach my $location (@
$location_values) {
254 value
=> $location->{authorised_value
},
255 label
=> $location->{lib
} // $location->{authorised_value
},
258 my @itemtypes = C4
::ItemType
->all();
259 foreach my $itemtype (@itemtypes) {
260 $itemtype->{value
} = $itemtype->{itemtype
};
261 $itemtype->{label
} = $itemtype->{description
};
263 my $ccode_avcode = GetAuthValCode
('items.ccode') || 'CCODE';
264 my $ccodes = GetAuthorisedValues
($ccode_avcode);
266 foreach my $ccode (@
$ccodes) {
268 value
=> $ccode->{authorised_value
},
269 label
=> $ccode->{lib
},
274 foreach my $value (@
$notforloan_values) {
276 value
=> $value->{authorised_value
},
277 label
=> $value->{lib
},
281 my @items_search_fields = GetItemSearchFields
();
283 my $authorised_values = {};
284 foreach my $field (@items_search_fields) {
285 if (my $category = ($field->{authorised_values_category
})) {
286 $authorised_values->{$category} = GetAuthorisedValues
($category);
291 branches
=> \
@branches,
292 locations
=> \
@locations,
293 itemtypes
=> \
@itemtypes,
295 notforloans
=> \
@notforloans,
296 items_search_fields
=> \
@items_search_fields,
297 authorised_values_json
=> to_json
($authorised_values),
301 if ($format eq 'csv') {
304 attachment
=> 'items.csv',
306 print $template->output;
308 output_with_http_headers
$cgi, $cookie, $template->output, $content_type;