Bug 16554: More i18n changes - en, es, nb and pl files
[koha.git] / catalogue / itemsearch.pl
blob3a5f0d95e3a6e309ba7f4a822de4fb76d8bfef81
1 #!/usr/bin/perl
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
9 # version.
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.
19 use Modern::Perl;
20 use CGI;
22 use JSON;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Koha;
30 use Koha::Item::Search::Field qw(GetItemSearchFields);
31 use Koha::ItemTypes;
32 use Koha::Libraries;
34 my $cgi = new CGI;
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->multi_param('sColumns');
47 $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
48 $cgi->param('sortorder', $cgi->param('sSortDir_0'));
50 my @f = $cgi->multi_param('f');
51 my @q = $cgi->multi_param('q');
52 push @q, '' if @q == 0;
53 my @op = $cgi->multi_param('op');
54 my @c = $cgi->multi_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];
61 push @q, "%$word%";
62 push @op, 'like';
63 push @c, 'and';
67 $cgi->param('f', @f);
68 $cgi->param('q', @q);
69 $cgi->param('op', @op);
70 $cgi->param('c', @c);
71 } elsif (defined $format and $format eq 'csv') {
72 $template_name = 'catalogue/itemsearch_csv.tt';
74 # Retrieve all results
75 $cgi->param('rows', 0);
76 } else {
77 $format = 'html';
78 $template_name = 'catalogue/itemsearch.tt';
79 $content_type = 'html';
82 my ($template, $borrowernumber, $cookie) = get_template_and_user({
83 template_name => $template_name,
84 query => $cgi,
85 type => 'intranet',
86 authnotrequired => 0,
87 flagsrequired => { catalogue => 1 },
88 });
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
99 my $filter = {
100 conjunction => 'AND',
101 filters => [],
104 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
105 if (my @q = $cgi->multi_param($p)) {
106 if ($q[0] ne '') {
107 my $f = {
108 field => $p,
109 query => \@q,
111 if (my $op = $cgi->param($p . '_op')) {
112 $f->{operator} = $op;
114 push @{ $filter->{filters} }, $f;
119 my @c = $cgi->multi_param('c');
120 my @fields = $cgi->multi_param('f');
121 my @q = $cgi->multi_param('q');
122 my @op = $cgi->multi_param('op');
124 my $f;
125 for (my $i = 0; $i < @fields; $i++) {
126 my $field = $fields[$i];
127 my $q = shift @q;
128 my $op = shift @op;
129 if (defined $q and $q ne '') {
130 if ($i == 0) {
131 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
132 $field = 'copyrightdate';
134 $f = {
135 field => $field,
136 query => $q,
137 operator => $op,
139 } else {
140 my $c = shift @c;
141 $f = {
142 conjunction => $c,
143 filters => [
144 $f, {
145 field => $field,
146 query => $q,
147 operator => $op,
154 push @{ $filter->{filters} }, $f;
156 # Yes/No parameters
157 foreach my $p (qw(damaged itemlost)) {
158 my $v = $cgi->param($p) // '';
159 my $f = {
160 field => $p,
161 query => 0,
163 if ($v eq 'yes') {
164 $f->{operator} = '!=';
165 push @{ $filter->{filters} }, $f;
166 } elsif ($v eq 'no') {
167 $f->{operator} = '=';
168 push @{ $filter->{filters} }, $f;
172 if (my $itemcallnumber_from = $cgi->param('itemcallnumber_from')) {
173 push @{ $filter->{filters} }, {
174 field => 'itemcallnumber',
175 query => $itemcallnumber_from,
176 operator => '>=',
179 if (my $itemcallnumber_to = $cgi->param('itemcallnumber_to')) {
180 push @{ $filter->{filters} }, {
181 field => 'itemcallnumber',
182 query => $itemcallnumber_to,
183 operator => '<=',
187 my $sortby = $cgi->param('sortby') || 'itemnumber';
188 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
189 $sortby = 'copyrightdate';
191 my $search_params = {
192 rows => scalar $cgi->param('rows') // 20,
193 page => scalar $cgi->param('page') || 1,
194 sortby => $sortby,
195 sortorder => scalar $cgi->param('sortorder') || 'asc',
198 my ($results, $total_rows) = SearchItems($filter, $search_params);
199 if ($results) {
200 # Get notforloan labels
201 my $notforloan_map = {};
202 foreach my $nfl_value (@$notforloan_values) {
203 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
206 # Get location labels
207 my $location_map = {};
208 foreach my $loc_value (@$location_values) {
209 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
212 foreach my $item (@$results) {
213 $item->{biblio} = GetBiblio($item->{biblionumber});
214 ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
215 $item->{status} = $notforloan_map->{$item->{notforloan}};
216 if (defined $item->{location}) {
217 $item->{location} = $location_map->{$item->{location}};
222 $template->param(
223 filter => $filter,
224 search_params => $search_params,
225 results => $results,
226 total_rows => $total_rows,
227 search_done => 1,
230 if ($format eq 'html') {
231 # Build pagination bar
232 my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
233 my @params;
234 foreach my $p (keys %params) {
235 my @v = $cgi->multi_param($p);
236 push @params, map { "$p=" . $_ } @v;
238 $url .= '?' . join ('&', @params);
239 my $nb_pages = 1 + int($total_rows / $search_params->{rows});
240 my $current_page = $search_params->{page};
241 my $pagination_bar = pagination_bar($url, $nb_pages, $current_page, 'page');
243 $template->param(pagination_bar => $pagination_bar);
247 if ($format eq 'html') {
248 # Retrieve data required for the form.
250 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
251 my @locations;
252 foreach my $location (@$location_values) {
253 push @locations, {
254 value => $location->{authorised_value},
255 label => $location->{lib} // $location->{authorised_value},
258 my @itemtypes;
259 foreach my $itemtype ( Koha::ItemTypes->search ) {
260 push @itemtypes, {
261 value => $itemtype->itemtype,
262 label => $itemtype->translated_description,
265 my $ccode_avcode = GetAuthValCode('items.ccode') || 'CCODE';
266 my $ccodes = GetAuthorisedValues($ccode_avcode);
267 my @ccodes;
268 foreach my $ccode (@$ccodes) {
269 push @ccodes, {
270 value => $ccode->{authorised_value},
271 label => $ccode->{lib},
275 my @notforloans;
276 foreach my $value (@$notforloan_values) {
277 push @notforloans, {
278 value => $value->{authorised_value},
279 label => $value->{lib},
283 my @items_search_fields = GetItemSearchFields();
285 my $authorised_values = {};
286 foreach my $field (@items_search_fields) {
287 if (my $category = ($field->{authorised_values_category})) {
288 $authorised_values->{$category} = GetAuthorisedValues($category);
292 $template->param(
293 branches => \@branches,
294 locations => \@locations,
295 itemtypes => \@itemtypes,
296 ccodes => \@ccodes,
297 notforloans => \@notforloans,
298 items_search_fields => \@items_search_fields,
299 authorised_values_json => to_json($authorised_values),
303 if ($format eq 'csv') {
304 print $cgi->header({
305 type => 'text/csv',
306 attachment => 'items.csv',
309 for my $line ( split '\n', $template->output ) {
310 print "$line\n" unless $line =~ m|^\s*$|;
312 } else {
313 output_with_http_headers $cgi, $cookie, $template->output, $content_type;