DBRev 17.05.00.000
[koha.git] / catalogue / itemsearch.pl
blob5f20dd3813b33ce6f8750ba46f01246fbc8d09b5
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::Item::Search::Field qw(GetItemSearchFields);
32 use Koha::ItemTypes;
33 use Koha::Libraries;
35 my $cgi = new CGI;
36 my %params = $cgi->Vars;
38 my $format = $cgi->param('format');
39 my $template_name = 'catalogue/itemsearch.tt';
41 if (defined $format and $format eq 'json') {
42 $template_name = 'catalogue/itemsearch_json.tt';
44 # Map DataTables parameters with 'regular' parameters
45 $cgi->param('rows', scalar $cgi->param('iDisplayLength'));
46 $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1);
47 my @columns = split /,/, scalar $cgi->param('sColumns');
48 $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]);
49 $cgi->param('sortorder', scalar $cgi->param('sSortDir_0'));
51 my @f = $cgi->multi_param('f');
52 my @q = $cgi->multi_param('q');
53 push @q, '' if @q == 0;
54 my @op = $cgi->multi_param('op');
55 my @c = $cgi->multi_param('c');
56 my $iColumns = $cgi->param('iColumns');
57 foreach my $i (0 .. ($iColumns - 1)) {
58 my $sSearch = $cgi->param("sSearch_$i");
59 if (defined $sSearch and $sSearch ne '') {
60 my @words = split /\s+/, $sSearch;
61 foreach my $word (@words) {
62 push @f, $columns[$i];
63 push @q, "%$word%";
64 push @op, 'like';
65 push @c, 'and';
69 $cgi->param('f', @f);
70 $cgi->param('q', @q);
71 $cgi->param('op', @op);
72 $cgi->param('c', @c);
73 } elsif (defined $format and $format eq 'csv') {
74 $template_name = 'catalogue/itemsearch_csv.tt';
76 # Retrieve all results
77 $cgi->param('rows', 0);
78 } elsif (defined $format and $format eq 'barcodes') {
79 # Retrieve all results
80 $cgi->param('rows', 0);
81 } elsif (defined $format) {
82 die "Unsupported format $format";
85 my ($template, $borrowernumber, $cookie) = get_template_and_user({
86 template_name => $template_name,
87 query => $cgi,
88 type => 'intranet',
89 authnotrequired => 0,
90 flagsrequired => { catalogue => 1 },
91 });
93 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } });
94 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
96 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => { not => undef } });
97 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
99 if (scalar keys %params > 0) {
100 # Parameters given, it's a search
102 my $filter = {
103 conjunction => 'AND',
104 filters => [],
107 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
108 if (my @q = $cgi->multi_param($p)) {
109 if ($q[0] ne '') {
110 my $f = {
111 field => $p,
112 query => \@q,
114 if (my $op = scalar $cgi->param($p . '_op')) {
115 $f->{operator} = $op;
117 push @{ $filter->{filters} }, $f;
122 my @c = $cgi->multi_param('c');
123 my @fields = $cgi->multi_param('f');
124 my @q = $cgi->multi_param('q');
125 my @op = $cgi->multi_param('op');
127 my $f;
128 for (my $i = 0; $i < @fields; $i++) {
129 my $field = $fields[$i];
130 my $q = shift @q;
131 my $op = shift @op;
132 if (defined $q and $q ne '') {
133 if ($i == 0) {
134 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
135 $field = 'copyrightdate';
137 $f = {
138 field => $field,
139 query => $q,
140 operator => $op,
142 } else {
143 my $c = shift @c;
144 $f = {
145 conjunction => $c,
146 filters => [
147 $f, {
148 field => $field,
149 query => $q,
150 operator => $op,
157 push @{ $filter->{filters} }, $f;
159 # Yes/No parameters
160 foreach my $p (qw(damaged itemlost)) {
161 my $v = $cgi->param($p) // '';
162 my $f = {
163 field => $p,
164 query => 0,
166 if ($v eq 'yes') {
167 $f->{operator} = '!=';
168 push @{ $filter->{filters} }, $f;
169 } elsif ($v eq 'no') {
170 $f->{operator} = '=';
171 push @{ $filter->{filters} }, $f;
175 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
176 push @{ $filter->{filters} }, {
177 field => 'itemcallnumber',
178 query => $itemcallnumber_from,
179 operator => '>=',
182 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
183 push @{ $filter->{filters} }, {
184 field => 'itemcallnumber',
185 query => $itemcallnumber_to,
186 operator => '<=',
190 my $sortby = $cgi->param('sortby') || 'itemnumber';
191 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
192 $sortby = 'copyrightdate';
194 my $search_params = {
195 rows => scalar $cgi->param('rows') // 20,
196 page => scalar $cgi->param('page') || 1,
197 sortby => $sortby,
198 sortorder => scalar $cgi->param('sortorder') || 'asc',
201 my ($results, $total_rows) = SearchItems($filter, $search_params);
203 if ($format eq 'barcodes') {
204 print $cgi->header({
205 type => 'text/plain',
206 attachment => 'barcodes.txt',
209 foreach my $item (@$results) {
210 print $item->{barcode} . "\n";
212 exit;
215 if ($results) {
216 # Get notforloan labels
217 my $notforloan_map = {};
218 foreach my $nfl_value (@$notforloan_values) {
219 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
222 # Get location labels
223 my $location_map = {};
224 foreach my $loc_value (@$location_values) {
225 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
228 foreach my $item (@$results) {
229 $item->{biblio} = GetBiblio($item->{biblionumber});
230 ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
231 $item->{status} = $notforloan_map->{$item->{notforloan}};
232 if (defined $item->{location}) {
233 $item->{location} = $location_map->{$item->{location}};
238 $template->param(
239 filter => $filter,
240 search_params => $search_params,
241 results => $results,
242 total_rows => $total_rows,
245 if ($format eq 'csv') {
246 print $cgi->header({
247 type => 'text/csv',
248 attachment => 'items.csv',
251 for my $line ( split '\n', $template->output ) {
252 print "$line\n" unless $line =~ m|^\s*$|;
254 } elsif ($format eq 'json') {
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 @locations;
265 foreach my $location (@$location_values) {
266 push @locations, {
267 value => $location->{authorised_value},
268 label => $location->{lib} // $location->{authorised_value},
271 my @itemtypes;
272 foreach my $itemtype ( Koha::ItemTypes->search ) {
273 push @itemtypes, {
274 value => $itemtype->itemtype,
275 label => $itemtype->translated_description,
279 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => { not => undef } });
280 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
281 my $ccodes = GetAuthorisedValues($ccode_avcode);
282 my @ccodes;
283 foreach my $ccode (@$ccodes) {
284 push @ccodes, {
285 value => $ccode->{authorised_value},
286 label => $ccode->{lib},
290 my @notforloans;
291 foreach my $value (@$notforloan_values) {
292 push @notforloans, {
293 value => $value->{authorised_value},
294 label => $value->{lib},
298 my @items_search_fields = GetItemSearchFields();
300 my $authorised_values = {};
301 foreach my $field (@items_search_fields) {
302 if (my $category = ($field->{authorised_values_category})) {
303 $authorised_values->{$category} = GetAuthorisedValues($category);
307 $template->param(
308 branches => \@branches,
309 locations => \@locations,
310 itemtypes => \@itemtypes,
311 ccodes => \@ccodes,
312 notforloans => \@notforloans,
313 items_search_fields => \@items_search_fields,
314 authorised_values_json => to_json($authorised_values),
317 output_html_with_http_headers $cgi, $cookie, $template->output;