Bug 19618: Add tests
[koha.git] / catalogue / itemsearch.pl
blob36e22b67f533512fccdb94b7018a37e2df2927c7
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::AuthorisedValues;
31 use Koha::Biblios;
32 use Koha::Item::Search::Field qw(GetItemSearchFields);
33 use Koha::ItemTypes;
34 use Koha::Libraries;
36 my $cgi = new CGI;
37 my %params = $cgi->Vars;
39 my $format = $cgi->param('format');
40 my $template_name = 'catalogue/itemsearch.tt';
42 if (defined $format and $format eq 'json') {
43 $template_name = 'catalogue/itemsearch_json.tt';
45 # Map DataTables parameters with 'regular' parameters
46 $cgi->param('rows', scalar $cgi->param('iDisplayLength'));
47 $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1);
48 my @columns = split /,/, scalar $cgi->param('sColumns');
49 $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]);
50 $cgi->param('sortorder', scalar $cgi->param('sSortDir_0'));
52 my @f = $cgi->multi_param('f');
53 my @q = $cgi->multi_param('q');
54 push @q, '' if @q == 0;
55 my @op = $cgi->multi_param('op');
56 my @c = $cgi->multi_param('c');
57 my $iColumns = $cgi->param('iColumns');
58 foreach my $i (0 .. ($iColumns - 1)) {
59 my $sSearch = $cgi->param("sSearch_$i");
60 if (defined $sSearch and $sSearch ne '') {
61 my @words = split /\s+/, $sSearch;
62 foreach my $word (@words) {
63 push @f, $columns[$i];
64 push @c, 'and';
66 if ( grep /^$columns[$i]$/, qw( ccode homebranch holdingbranch location itype notforloan itemlost ) ) {
67 push @q, "$word";
68 push @op, '=';
69 } else {
70 push @q, "%$word%";
71 push @op, 'like';
76 $cgi->param('f', @f);
77 $cgi->param('q', @q);
78 $cgi->param('op', @op);
79 $cgi->param('c', @c);
80 } elsif (defined $format and $format eq 'csv') {
81 $template_name = 'catalogue/itemsearch_csv.tt';
83 # Retrieve all results
84 $cgi->param('rows', 0);
85 } elsif (defined $format and $format eq 'barcodes') {
86 # Retrieve all results
87 $cgi->param('rows', 0);
88 } elsif (defined $format) {
89 die "Unsupported format $format";
92 my ($template, $borrowernumber, $cookie) = get_template_and_user({
93 template_name => $template_name,
94 query => $cgi,
95 type => 'intranet',
96 authnotrequired => 0,
97 flagsrequired => { catalogue => 1 },
98 });
100 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
101 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
103 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
104 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
106 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
107 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
109 if (scalar keys %params > 0) {
110 # Parameters given, it's a search
112 my $filter = {
113 conjunction => 'AND',
114 filters => [],
117 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost)) {
118 if (my @q = $cgi->multi_param($p)) {
119 if ($q[0] ne '') {
120 my $f = {
121 field => $p,
122 query => \@q,
124 if (my $op = scalar $cgi->param($p . '_op')) {
125 $f->{operator} = $op;
127 push @{ $filter->{filters} }, $f;
132 my @c = $cgi->multi_param('c');
133 my @fields = $cgi->multi_param('f');
134 my @q = $cgi->multi_param('q');
135 my @op = $cgi->multi_param('op');
137 my $f;
138 for (my $i = 0; $i < @fields; $i++) {
139 my $field = $fields[$i];
140 my $q = shift @q;
141 my $op = shift @op;
142 if (defined $q and $q ne '') {
143 if ($i == 0) {
144 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
145 $field = 'copyrightdate';
147 $f = {
148 field => $field,
149 query => $q,
150 operator => $op,
152 } else {
153 my $c = shift @c;
154 $f = {
155 conjunction => $c,
156 filters => [
157 $f, {
158 field => $field,
159 query => $q,
160 operator => $op,
167 push @{ $filter->{filters} }, $f;
169 # Yes/No parameters
170 foreach my $p (qw( damaged )) {
171 my $v = $cgi->param($p) // '';
172 my $f = {
173 field => $p,
174 query => 0,
176 if ($v eq 'yes') {
177 $f->{operator} = '!=';
178 push @{ $filter->{filters} }, $f;
179 } elsif ($v eq 'no') {
180 $f->{operator} = '=';
181 push @{ $filter->{filters} }, $f;
185 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
186 push @{ $filter->{filters} }, {
187 field => 'itemcallnumber',
188 query => $itemcallnumber_from,
189 operator => '>=',
192 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
193 push @{ $filter->{filters} }, {
194 field => 'itemcallnumber',
195 query => $itemcallnumber_to,
196 operator => '<=',
200 my $sortby = $cgi->param('sortby') || 'itemnumber';
201 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
202 $sortby = 'copyrightdate';
204 my $search_params = {
205 rows => scalar $cgi->param('rows') // 20,
206 page => scalar $cgi->param('page') || 1,
207 sortby => $sortby,
208 sortorder => scalar $cgi->param('sortorder') || 'asc',
211 my ($results, $total_rows) = SearchItems($filter, $search_params);
213 if ($format eq 'barcodes') {
214 print $cgi->header({
215 type => 'text/plain',
216 attachment => 'barcodes.txt',
219 foreach my $item (@$results) {
220 print $item->{barcode} . "\n";
222 exit;
225 if ($results) {
226 # Get notforloan labels
227 my $notforloan_map = {};
228 foreach my $nfl_value (@$notforloan_values) {
229 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
232 # Get location labels
233 my $location_map = {};
234 foreach my $loc_value (@$location_values) {
235 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
238 # Get itemlost labels
239 my $itemlost_map = {};
240 foreach my $il_value (@$itemlost_values) {
241 $itemlost_map->{$il_value->{authorised_value}} = $il_value->{lib};
244 foreach my $item (@$results) {
245 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
246 $item->{biblio} = $biblio;
247 $item->{biblioitem} = $biblio->biblioitem->unblessed;
248 $item->{status} = $notforloan_map->{$item->{notforloan}};
249 if (defined $item->{location}) {
250 $item->{location} = $location_map->{$item->{location}};
255 $template->param(
256 filter => $filter,
257 search_params => $search_params,
258 results => $results,
259 total_rows => $total_rows,
262 if ($format eq 'csv') {
263 print $cgi->header({
264 type => 'text/csv',
265 attachment => 'items.csv',
268 for my $line ( split '\n', $template->output ) {
269 print "$line\n" unless $line =~ m|^\s*$|;
271 } elsif ($format eq 'json') {
272 $template->param(sEcho => scalar $cgi->param('sEcho'));
273 output_with_http_headers $cgi, $cookie, $template->output, 'json';
276 exit;
279 # Display the search form
281 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
282 my @locations;
283 foreach my $location (@$location_values) {
284 push @locations, {
285 value => $location->{authorised_value},
286 label => $location->{lib} // $location->{authorised_value},
289 my @itemtypes;
290 foreach my $itemtype ( Koha::ItemTypes->search ) {
291 push @itemtypes, {
292 value => $itemtype->itemtype,
293 label => $itemtype->translated_description,
297 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
298 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
299 my $ccodes = GetAuthorisedValues($ccode_avcode);
300 my @ccodes;
301 foreach my $ccode (@$ccodes) {
302 push @ccodes, {
303 value => $ccode->{authorised_value},
304 label => $ccode->{lib},
308 my @notforloans;
309 foreach my $value (@$notforloan_values) {
310 push @notforloans, {
311 value => $value->{authorised_value},
312 label => $value->{lib},
316 my @itemlosts;
317 foreach my $value (@$itemlost_values) {
318 push @itemlosts, {
319 value => $value->{authorised_value},
320 label => $value->{lib},
324 my @items_search_fields = GetItemSearchFields();
326 my $authorised_values = {};
327 foreach my $field (@items_search_fields) {
328 if (my $category = ($field->{authorised_values_category})) {
329 $authorised_values->{$category} = GetAuthorisedValues($category);
333 $template->param(
334 branches => \@branches,
335 locations => \@locations,
336 itemtypes => \@itemtypes,
337 ccodes => \@ccodes,
338 notforloans => \@notforloans,
339 itemlosts => \@itemlosts,
340 items_search_fields => \@items_search_fields,
341 authorised_values_json => to_json($authorised_values),
344 output_html_with_http_headers $cgi, $cookie, $template->output;