Bug 26854: Close STDERR when forking stage-marc-import.pl
[koha.git] / catalogue / itemsearch.pl
blobf5f66fe349f6a8f2e037ee575eb1829b0d1e7fe1
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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 = CGI->new;
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 { $_ eq $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 flagsrequired => { catalogue => 1 },
97 });
99 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
100 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
102 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
103 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
105 if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
106 $template->param( has_new_status => 1 );
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 withdrawn)) {
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 (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
144 $field = 'copyrightdate';
147 if ($i == 0) {
148 $f = {
149 field => $field,
150 query => $q,
151 operator => $op,
153 } else {
154 my $c = shift @c;
155 $f = {
156 conjunction => $c,
157 filters => [
158 $f, {
159 field => $field,
160 query => $q,
161 operator => $op,
168 push @{ $filter->{filters} }, $f;
170 # Yes/No parameters
171 foreach my $p (qw( damaged new_status )) {
172 my $v = $cgi->param($p) // '';
173 my $f = {
174 field => $p,
175 query => 0,
177 if ( $p eq 'new_status' ) {
178 $f->{ifnull} = 0;
180 if ($v eq 'yes') {
181 $f->{operator} = '!=';
182 push @{ $filter->{filters} }, $f;
183 } elsif ($v eq 'no') {
184 $f->{operator} = '=';
185 push @{ $filter->{filters} }, $f;
189 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
190 push @{ $filter->{filters} }, {
191 field => 'itemcallnumber',
192 query => $itemcallnumber_from,
193 operator => '>=',
196 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
197 push @{ $filter->{filters} }, {
198 field => 'itemcallnumber',
199 query => $itemcallnumber_to,
200 operator => '<=',
204 my $sortby = $cgi->param('sortby') || 'itemnumber';
205 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
206 $sortby = 'copyrightdate';
208 my $search_params = {
209 rows => scalar $cgi->param('rows') // 20,
210 page => scalar $cgi->param('page') || 1,
211 sortby => $sortby,
212 sortorder => scalar $cgi->param('sortorder') || 'asc',
215 my ($results, $total_rows) = SearchItems($filter, $search_params);
217 if ($format eq 'barcodes') {
218 print $cgi->header({
219 type => 'text/plain',
220 attachment => 'barcodes.txt',
223 foreach my $item (@$results) {
224 print $item->{barcode} . "\n";
226 exit;
229 if ($results) {
230 foreach my $item (@$results) {
231 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
232 $item->{biblio} = $biblio;
233 $item->{biblioitem} = $biblio->biblioitem->unblessed;
237 $template->param(
238 filter => $filter,
239 search_params => $search_params,
240 results => $results,
241 total_rows => $total_rows,
244 if ($format eq 'csv') {
245 print $cgi->header({
246 type => 'text/csv',
247 attachment => 'items.csv',
250 for my $line ( split '\n', $template->output ) {
251 print "$line\n" unless $line =~ m|^\s*$|;
253 } elsif ($format eq 'json') {
254 $template->param(sEcho => scalar $cgi->param('sEcho'));
255 output_with_http_headers $cgi, $cookie, $template->output, 'json';
258 exit;
261 # Display the search form
263 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
264 my @itemtypes;
265 foreach my $itemtype ( Koha::ItemTypes->search ) {
266 push @itemtypes, {
267 value => $itemtype->itemtype,
268 label => $itemtype->translated_description,
272 my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
273 foreach my $ccode (@ccodes) {
274 $ccode->{value} = $ccode->{authorised_value},
275 $ccode->{label} = $ccode->{lib},
278 my @itemlosts;
279 foreach my $value (@$itemlost_values) {
280 push @itemlosts, {
281 value => $value->{authorised_value},
282 label => $value->{lib},
286 my @withdrawns;
287 foreach my $value (@$withdrawn_values) {
288 push @withdrawns, {
289 value => $value->{authorised_value},
290 label => $value->{lib},
294 my @items_search_fields = GetItemSearchFields();
296 my $authorised_values = {};
297 foreach my $field (@items_search_fields) {
298 if (my $category = ($field->{authorised_values_category})) {
299 $authorised_values->{$category} = GetAuthorisedValues($category);
303 $template->param(
304 branches => \@branches,
305 itemtypes => \@itemtypes,
306 ccodes => \@ccodes,
307 itemlosts => \@itemlosts,
308 withdrawns => \@withdrawns,
309 items_search_fields => \@items_search_fields,
310 authorised_values_json => to_json($authorised_values),
313 output_html_with_http_headers $cgi, $cookie, $template->output;