Bug 19228: Trigger confirm delete when removing item from course
[koha.git] / catalogue / itemsearch.pl
blobc48f2f1aba43cf730ea34ca303d48bf73b5679ec
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 @q, "%$word%";
65 push @op, 'like';
66 push @c, 'and';
70 $cgi->param('f', @f);
71 $cgi->param('q', @q);
72 $cgi->param('op', @op);
73 $cgi->param('c', @c);
74 } elsif (defined $format and $format eq 'csv') {
75 $template_name = 'catalogue/itemsearch_csv.tt';
77 # Retrieve all results
78 $cgi->param('rows', 0);
79 } elsif (defined $format and $format eq 'barcodes') {
80 # Retrieve all results
81 $cgi->param('rows', 0);
82 } elsif (defined $format) {
83 die "Unsupported format $format";
86 my ($template, $borrowernumber, $cookie) = get_template_and_user({
87 template_name => $template_name,
88 query => $cgi,
89 type => 'intranet',
90 authnotrequired => 0,
91 flagsrequired => { catalogue => 1 },
92 });
94 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } });
95 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
97 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => { not => undef } });
98 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
100 if (scalar keys %params > 0) {
101 # Parameters given, it's a search
103 my $filter = {
104 conjunction => 'AND',
105 filters => [],
108 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
109 if (my @q = $cgi->multi_param($p)) {
110 if ($q[0] ne '') {
111 my $f = {
112 field => $p,
113 query => \@q,
115 if (my $op = scalar $cgi->param($p . '_op')) {
116 $f->{operator} = $op;
118 push @{ $filter->{filters} }, $f;
123 my @c = $cgi->multi_param('c');
124 my @fields = $cgi->multi_param('f');
125 my @q = $cgi->multi_param('q');
126 my @op = $cgi->multi_param('op');
128 my $f;
129 for (my $i = 0; $i < @fields; $i++) {
130 my $field = $fields[$i];
131 my $q = shift @q;
132 my $op = shift @op;
133 if (defined $q and $q ne '') {
134 if ($i == 0) {
135 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
136 $field = 'copyrightdate';
138 $f = {
139 field => $field,
140 query => $q,
141 operator => $op,
143 } else {
144 my $c = shift @c;
145 $f = {
146 conjunction => $c,
147 filters => [
148 $f, {
149 field => $field,
150 query => $q,
151 operator => $op,
158 push @{ $filter->{filters} }, $f;
160 # Yes/No parameters
161 foreach my $p (qw(damaged itemlost)) {
162 my $v = $cgi->param($p) // '';
163 my $f = {
164 field => $p,
165 query => 0,
167 if ($v eq 'yes') {
168 $f->{operator} = '!=';
169 push @{ $filter->{filters} }, $f;
170 } elsif ($v eq 'no') {
171 $f->{operator} = '=';
172 push @{ $filter->{filters} }, $f;
176 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
177 push @{ $filter->{filters} }, {
178 field => 'itemcallnumber',
179 query => $itemcallnumber_from,
180 operator => '>=',
183 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
184 push @{ $filter->{filters} }, {
185 field => 'itemcallnumber',
186 query => $itemcallnumber_to,
187 operator => '<=',
191 my $sortby = $cgi->param('sortby') || 'itemnumber';
192 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
193 $sortby = 'copyrightdate';
195 my $search_params = {
196 rows => scalar $cgi->param('rows') // 20,
197 page => scalar $cgi->param('page') || 1,
198 sortby => $sortby,
199 sortorder => scalar $cgi->param('sortorder') || 'asc',
202 my ($results, $total_rows) = SearchItems($filter, $search_params);
204 if ($format eq 'barcodes') {
205 print $cgi->header({
206 type => 'text/plain',
207 attachment => 'barcodes.txt',
210 foreach my $item (@$results) {
211 print $item->{barcode} . "\n";
213 exit;
216 if ($results) {
217 # Get notforloan labels
218 my $notforloan_map = {};
219 foreach my $nfl_value (@$notforloan_values) {
220 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
223 # Get location labels
224 my $location_map = {};
225 foreach my $loc_value (@$location_values) {
226 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
229 foreach my $item (@$results) {
230 $item->{biblio} = Koha::Biblios->find( $item->{biblionumber} );
231 ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
232 $item->{status} = $notforloan_map->{$item->{notforloan}};
233 if (defined $item->{location}) {
234 $item->{location} = $location_map->{$item->{location}};
239 $template->param(
240 filter => $filter,
241 search_params => $search_params,
242 results => $results,
243 total_rows => $total_rows,
246 if ($format eq 'csv') {
247 print $cgi->header({
248 type => 'text/csv',
249 attachment => 'items.csv',
252 for my $line ( split '\n', $template->output ) {
253 print "$line\n" unless $line =~ m|^\s*$|;
255 } elsif ($format eq 'json') {
256 $template->param(sEcho => scalar $cgi->param('sEcho'));
257 output_with_http_headers $cgi, $cookie, $template->output, 'json';
260 exit;
263 # Display the search form
265 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
266 my @locations;
267 foreach my $location (@$location_values) {
268 push @locations, {
269 value => $location->{authorised_value},
270 label => $location->{lib} // $location->{authorised_value},
273 my @itemtypes;
274 foreach my $itemtype ( Koha::ItemTypes->search ) {
275 push @itemtypes, {
276 value => $itemtype->itemtype,
277 label => $itemtype->translated_description,
281 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => { not => undef } });
282 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
283 my $ccodes = GetAuthorisedValues($ccode_avcode);
284 my @ccodes;
285 foreach my $ccode (@$ccodes) {
286 push @ccodes, {
287 value => $ccode->{authorised_value},
288 label => $ccode->{lib},
292 my @notforloans;
293 foreach my $value (@$notforloan_values) {
294 push @notforloans, {
295 value => $value->{authorised_value},
296 label => $value->{lib},
300 my @items_search_fields = GetItemSearchFields();
302 my $authorised_values = {};
303 foreach my $field (@items_search_fields) {
304 if (my $category = ($field->{authorised_values_category})) {
305 $authorised_values->{$category} = GetAuthorisedValues($category);
309 $template->param(
310 branches => \@branches,
311 locations => \@locations,
312 itemtypes => \@itemtypes,
313 ccodes => \@ccodes,
314 notforloans => \@notforloans,
315 items_search_fields => \@items_search_fields,
316 authorised_values_json => to_json($authorised_values),
319 output_html_with_http_headers $cgi, $cookie, $template->output;