Bug 17469: Add missing sample notices to fr-CA web installer
[koha.git] / catalogue / itemsearch.pl
blobfa6b357b9f76504480c681ed2be1e0c6afa0d760
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, $content_type);
40 if (defined $format and $format eq 'json') {
41 $template_name = 'catalogue/itemsearch_json.tt';
42 $content_type = 'json';
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 } else {
79 $format = 'html';
80 $template_name = 'catalogue/itemsearch.tt';
81 $content_type = 'html';
84 my ($template, $borrowernumber, $cookie) = get_template_and_user({
85 template_name => $template_name,
86 query => $cgi,
87 type => 'intranet',
88 authnotrequired => 0,
89 flagsrequired => { catalogue => 1 },
90 });
92 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } });
93 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
95 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => { not => undef } });
96 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
98 if (scalar keys %params > 0) {
99 # Parameters given, it's a search
101 my $filter = {
102 conjunction => 'AND',
103 filters => [],
106 foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan)) {
107 if (my @q = $cgi->multi_param($p)) {
108 if ($q[0] ne '') {
109 my $f = {
110 field => $p,
111 query => \@q,
113 if (my $op = scalar $cgi->param($p . '_op')) {
114 $f->{operator} = $op;
116 push @{ $filter->{filters} }, $f;
121 my @c = $cgi->multi_param('c');
122 my @fields = $cgi->multi_param('f');
123 my @q = $cgi->multi_param('q');
124 my @op = $cgi->multi_param('op');
126 my $f;
127 for (my $i = 0; $i < @fields; $i++) {
128 my $field = $fields[$i];
129 my $q = shift @q;
130 my $op = shift @op;
131 if (defined $q and $q ne '') {
132 if ($i == 0) {
133 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
134 $field = 'copyrightdate';
136 $f = {
137 field => $field,
138 query => $q,
139 operator => $op,
141 } else {
142 my $c = shift @c;
143 $f = {
144 conjunction => $c,
145 filters => [
146 $f, {
147 field => $field,
148 query => $q,
149 operator => $op,
156 push @{ $filter->{filters} }, $f;
158 # Yes/No parameters
159 foreach my $p (qw(damaged itemlost)) {
160 my $v = $cgi->param($p) // '';
161 my $f = {
162 field => $p,
163 query => 0,
165 if ($v eq 'yes') {
166 $f->{operator} = '!=';
167 push @{ $filter->{filters} }, $f;
168 } elsif ($v eq 'no') {
169 $f->{operator} = '=';
170 push @{ $filter->{filters} }, $f;
174 if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
175 push @{ $filter->{filters} }, {
176 field => 'itemcallnumber',
177 query => $itemcallnumber_from,
178 operator => '>=',
181 if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
182 push @{ $filter->{filters} }, {
183 field => 'itemcallnumber',
184 query => $itemcallnumber_to,
185 operator => '<=',
189 my $sortby = $cgi->param('sortby') || 'itemnumber';
190 if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
191 $sortby = 'copyrightdate';
193 my $search_params = {
194 rows => scalar $cgi->param('rows') // 20,
195 page => scalar $cgi->param('page') || 1,
196 sortby => $sortby,
197 sortorder => scalar $cgi->param('sortorder') || 'asc',
200 my ($results, $total_rows) = SearchItems($filter, $search_params);
201 if ($results) {
202 # Get notforloan labels
203 my $notforloan_map = {};
204 foreach my $nfl_value (@$notforloan_values) {
205 $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
208 # Get location labels
209 my $location_map = {};
210 foreach my $loc_value (@$location_values) {
211 $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
214 foreach my $item (@$results) {
215 $item->{biblio} = GetBiblio($item->{biblionumber});
216 ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
217 $item->{status} = $notforloan_map->{$item->{notforloan}};
218 if (defined $item->{location}) {
219 $item->{location} = $location_map->{$item->{location}};
224 $template->param(
225 filter => $filter,
226 search_params => $search_params,
227 results => $results,
228 total_rows => $total_rows,
229 search_done => 1,
232 if ($format eq 'html') {
233 # Build pagination bar
234 my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
235 my @params;
236 foreach my $p (keys %params) {
237 my @v = $cgi->multi_param($p);
238 push @params, map { "$p=" . $_ } @v;
240 $url .= '?' . join ('&', @params);
241 my $nb_pages = 1 + int($total_rows / $search_params->{rows});
242 my $current_page = $search_params->{page};
243 my $pagination_bar = pagination_bar($url, $nb_pages, $current_page, 'page');
245 $template->param(pagination_bar => $pagination_bar);
249 if ($format eq 'html') {
250 # Retrieve data required for the form.
252 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
253 my @locations;
254 foreach my $location (@$location_values) {
255 push @locations, {
256 value => $location->{authorised_value},
257 label => $location->{lib} // $location->{authorised_value},
260 my @itemtypes;
261 foreach my $itemtype ( Koha::ItemTypes->search ) {
262 push @itemtypes, {
263 value => $itemtype->itemtype,
264 label => $itemtype->translated_description,
268 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => { not => undef } });
269 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
270 my $ccodes = GetAuthorisedValues($ccode_avcode);
271 my @ccodes;
272 foreach my $ccode (@$ccodes) {
273 push @ccodes, {
274 value => $ccode->{authorised_value},
275 label => $ccode->{lib},
279 my @notforloans;
280 foreach my $value (@$notforloan_values) {
281 push @notforloans, {
282 value => $value->{authorised_value},
283 label => $value->{lib},
287 my @items_search_fields = GetItemSearchFields();
289 my $authorised_values = {};
290 foreach my $field (@items_search_fields) {
291 if (my $category = ($field->{authorised_values_category})) {
292 $authorised_values->{$category} = GetAuthorisedValues($category);
296 $template->param(
297 branches => \@branches,
298 locations => \@locations,
299 itemtypes => \@itemtypes,
300 ccodes => \@ccodes,
301 notforloans => \@notforloans,
302 items_search_fields => \@items_search_fields,
303 authorised_values_json => to_json($authorised_values),
307 if ($format eq 'csv') {
308 print $cgi->header({
309 type => 'text/csv',
310 attachment => 'items.csv',
313 for my $line ( split '\n', $template->output ) {
314 print "$line\n" unless $line =~ m|^\s*$|;
316 } else {
317 output_with_http_headers $cgi, $cookie, $template->output, $content_type;