Bug 12748: (QA followup) fix several tiny QA tools warnings
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use vars qw($debug $cgi_debug);
23
24 use CGI qw ( -utf8 );
25 use List::Util qw( max min );
26 use POSIX qw(ceil);
27
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Context;
31 use C4::Search qw(SimpleSearch);
32 use C4::Biblio qw(TransformMarcToKoha);
33 use C4::Items qw(GetItemInfosOf get_itemnumbers_of);
34 use C4::Koha qw(GetItemTypes);
35 use C4::Creators::Lib qw(html_table);
36 use C4::Debug;
37 use Koha::DateUtils;
38
39 use Koha::SearchEngine::Search;
40
41 BEGIN {
42     $debug = $debug || $cgi_debug;
43     if ($debug) {
44         require Data::Dumper;
45         import Data::Dumper qw(Dumper);
46     }
47 }
48
49 my $query = new CGI;
50
51 my $type      = $query->param('type');
52 my $op        = $query->param('op') || '';
53 my $batch_id  = $query->param('batch_id');
54 my $ccl_query = $query->param('ccl_query');
55 my $startfrom = $query->param('startfrom') || 1;
56 my ($template, $loggedinuser, $cookie) = (undef, undef, undef);
57 my (
58     $total_hits,  $total,  $error,
59     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
60 );
61 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
62 my $show_results = 0;
63 my $display_columns = [ {_add                   => {label => "Add Item", link_field => 1}},
64                         {_item_call_number      => {label => "Call Number", link_field => 0}},
65                         {_date_accessioned      => {label => "Accession Date", link_field => 0}},
66                         {_barcode               => {label => "Barcode", link_field => 0}},
67                         {select                 => {label => "Select", value => "_item_number"}},
68                       ];
69
70 if ( $op eq "do_search" ) {
71     my $QParser;
72     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
73     $idx         = $query->param('idx');
74     $ccl_textbox = $query->param('ccl_textbox');
75     if ( $ccl_textbox && $idx ) {
76         $ccl_query = "$idx:$ccl_textbox";
77     }
78
79     $datefrom = $query->param('datefrom');
80     $dateto   = $query->param('dateto');
81
82     if ($datefrom) {
83         $datefrom = eval { dt_from_string ( $datefrom ) };
84         if ($datefrom) {
85             $datefrom = output_pref( { dt => $datefrom, dateonly => 1, dateformat => 'iso' } );
86             if ($QParser) {
87                 $ccl_query .= ' && ' if $ccl_textbox;
88                 $ccl_query .=
89                     "acqdate(" . $datefrom . '-)';
90             } else {
91                 $ccl_query .= ' and ' if $ccl_textbox;
92                 $ccl_query .= "acqdate,st-date-normalized,ge=" . $datefrom;
93             }
94         }
95     }
96
97     if ($dateto) {
98         $dateto = eval { dt_from_string ( $dateto ) };
99         if ($dateto) {
100            $dateto = output_pref( { dt => $dateto, dateonly => 1, dateformat => 'iso' } );
101             if ($QParser) {
102                 $ccl_query .= ' && ' if ( $ccl_textbox || $datefrom );
103                 $ccl_query .= "acqdate(-" . $dateto . ')';
104             } else {
105                 $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
106                 $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto;
107             }
108         }
109     }
110
111     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
112     my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
113     ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($ccl_query, $offset, $resultsperpage);
114
115     if (!defined $error && @{$marcresults} ) {
116         $show_results = @{$marcresults};
117     }
118     else {
119         $debug and warn "ERROR label-item-search: no results from simple_search_compat";
120
121         # leave $show_results undef
122     }
123 }
124
125 if ($show_results) {
126     my $hits = $show_results;
127     my @results_set = ();
128     my @items =();
129     # This code needs to be refactored using these subs...
130     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
131     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
132     for ( my $i = 0 ; $i < $hits ; $i++ ) {
133         my @row_data= ();
134         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
135         my $marcrecord = C4::Search::new_record_from_zebra( 'biblioserver', $marcresults->[$i] );
136         #DEBUG Notes: Transform it to Koha form...
137         my $biblio = TransformMarcToKoha( $marcrecord, '' );
138         #DEBUG Notes: Stuff the bib into @biblio_data...
139         push (@results_set, $biblio);
140         my $biblionumber = $biblio->{'biblionumber'};
141         #DEBUG Notes: Grab the item numbers associated with this MARC record...
142         my $itemnums = get_itemnumbers_of($biblionumber);
143         #DEBUG Notes: Retrieve the item data for each number...
144         if (my $iii = $itemnums->{$biblionumber}) {
145             my $item_results = GetItemInfosOf(@$iii);
146             foreach my $item ( keys %$item_results ) {
147                 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
148                 if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) {
149                     my $item_data;
150                     $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'};
151                     $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA');
152                     $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'};
153                     $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA');
154                     $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'};
155                     unshift (@row_data, $item_data);    # item numbers are given to us in descending order by get_itemnumbers_of()...
156                 }
157             }
158             $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data);
159         }
160         else {
161             # FIXME: Some error trapping code needed
162             warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber);
163         }
164     }
165
166     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
167         {
168             template_name   => "labels/result.tt",
169             query           => $query,
170             type            => "intranet",
171             authnotrequired => 0,
172             flagsrequired   => { borrowers => 1 },
173             flagsrequired   => { catalogue => 1 },
174             debug           => 1,
175         }
176     );
177
178     # build page nav stuff.
179     my @numbers;
180     $total = $total_hits;
181
182     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
183         $displayprev );
184
185     if ( $total > $resultsperpage ) {
186         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
187         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
188             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
189             push @numbers,
190               {
191                 number    => $page,
192                 startfrom => $startfrm
193               };
194         }
195
196         $from          = $startfrom;
197         $startfromprev = $startfrom - $resultsperpage;
198         $startfromnext = $startfrom + $resultsperpage;
199
200         $to =
201             $startfrom + $resultsperpage > $total
202           ? $total
203           : $startfrom + $resultsperpage - 1;
204
205         # multi page display
206         $displaynext = 0;
207         $displayprev = $startfrom > 1 ? $startfrom : 0;
208
209         $displaynext = 1 if $to < $total_hits;
210
211     }
212     else {
213         $displayprev = 0;
214         $displaynext = 0;
215     }
216
217     $template->param(
218         total          => $total_hits,
219         from           => $from,
220         to             => $to,
221         startfromnext  => $startfromnext,
222         startfromprev  => $startfromprev,
223         startfrom      => $startfrom,
224         displaynext    => $displaynext,
225         displayprev    => $displayprev,
226         resultsperpage => $resultsperpage,
227         numbers        => \@numbers,
228     );
229
230     $template->param(
231         results   => ($show_results ? 1 : 0),
232         result_set=> \@results_set,
233         batch_id  => $batch_id,
234         type      => $type,
235         ccl_query => $ccl_query,
236     );
237 }
238
239 #
240 #   search section
241 #
242
243 else {
244     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
245         {
246             template_name   => "labels/search.tt",
247             query           => $query,
248             type            => "intranet",
249             authnotrequired => 0,
250             flagsrequired   => { catalogue => 1 },
251             debug           => 1,
252         }
253     );
254     my $itemtypes = GetItemTypes;
255     my @itemtypeloop;
256     foreach my $thisitemtype ( keys %$itemtypes ) {
257         my %row = (
258             value       => $thisitemtype,
259             description => $itemtypes->{$thisitemtype}->{'description'},
260         );
261         push @itemtypeloop, \%row;
262     }
263     $template->param(
264         itemtypeloop => \@itemtypeloop,
265         batch_id     => $batch_id,
266         type         => $type,
267     );
268
269 }
270
271 $template->param( idx => $idx );
272
273 # Print the page
274 output_html_with_http_headers $query, $cookie, $template->output;