bug 2505: turn on warnings in labels/label-item-search.pl
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use HTML::Template::Pro;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Acquisition;
33 use C4::Search;
34 use C4::Dates;
35 use C4::Koha;    # XXX subfield_is_koha_internal_p
36 use C4::Debug;
37 use List::Util qw( max min );
38 use POSIX;
39
40 #use Smart::Comments;
41 #use Data::Dumper;
42
43 BEGIN {
44     $debug = $debug || $cgi_debug;
45     if ($debug) {
46         require Data::Dumper;
47         import Data::Dumper qw(Dumper);
48     }
49 }
50
51 # Creates a scrolling list with the associated default value.
52 # Using more than one scrolling list in a CGI assigns the same default value to all the
53 # scrolling lists on the page !?!? That's why this function was written.
54
55 my $query           = new CGI;
56
57 my $type      = $query->param('type');
58 my $op        = $query->param('op') || '';
59 my $batch_id  = $query->param('batch_id');
60 my $ccl_query = $query->param('ccl_query');
61
62 my $dbh = C4::Context->dbh;
63
64 my $startfrom = $query->param('startfrom') || 1;
65 my ( $template, $loggedinuser, $cookie );
66 my ($total_hits, $orderby, $results, $total, $error, $marcresults, $idx,        $datefrom, $dateto, $ccl_textbox);
67
68 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
69
70 my $show_results = 0;
71
72 if ($op eq "do_search") {
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 = C4::Dates->new($datefrom);
84         $ccl_query .= ' and ' if $ccl_textbox;
85                 $ccl_query .= "acqdate,st-date-normalized,ge=" .  $datefrom->output("iso");
86         }
87
88         if ($dateto) {
89                 $dateto = C4::Dates->new($dateto);
90         $ccl_query .= ' and ' if ($ccl_textbox || $datefrom) ;
91                 $ccl_query .= "acqdate,st-date-normalized,le=".  $dateto->output("iso");
92         }
93
94     my $offset =    $startfrom > 1 ? $startfrom - 1 : 0;
95         ($error, $marcresults, $total_hits) =
96           SimpleSearch($ccl_query, $offset, $resultsperpage);
97
98         if ($marcresults) {
99                 $show_results = scalar @$marcresults;
100         } else {
101                 $debug and warn "ERROR label-item-search: no results from SimpleSearch";
102
103                 # leave $show_results undef
104         }
105 }
106
107 if ($show_results) {
108         my $hits = $show_results;
109         my (@results, @items);
110
111         # This code needs to be refactored using these subs...
112         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
113         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
114         for (my $i = 0 ; $i < $hits ; $i++) {
115
116                 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
117                 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
118
119                 #DEBUG Notes: Transform it to Koha form...
120                 my $biblio = TransformMarcToKoha(C4::Context->dbh, $marcrecord, '');
121
122 # Begin building the hash for the template...
123 # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
124 #$biblio->{highlight}       = ($i % 2)?(1):(0);
125 #DEBUG Notes: Stuff the bib into @results...
126                 push @results, $biblio;
127                 my $biblionumber = $biblio->{'biblionumber'};
128
129                 #DEBUG Notes: Grab the item numbers associated with this MARC record...
130                 my $itemnums = get_itemnumbers_of($biblionumber);
131
132                 #DEBUG Notes: Retrieve the item data for each number...
133                 my $iii = $itemnums->{$biblionumber};
134                 if ($iii) {
135                         my $item_results = GetItemInfosOf(@$iii);
136                         foreach my $item (keys %$item_results) {
137
138 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
139                                 if ($item_results->{$item}->{'biblionumber'} eq
140                                         $results[$i]->{'biblionumber'}) {
141
142 # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
143 # This is a real hack, but I can't think of a better way right now. -fbcit
144 # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
145                                         push @{ $results[$i]->{'item'} }, { i_itemnumber1 =>
146                                                   $item_results->{$item}->{'itemnumber'} };
147                                         push @{ $results[$i]->{'item'} },
148                                           { i_itemcallnumber => (
149                                                           $item_results->{$item}->{'itemcallnumber'}
150                                                         ? $item_results->{$item}->{'itemcallnumber'} : 'NA'
151                                                 )
152                                           };
153                                         push @{ $results[$i]->{'item'} }, { i_dateaccessioned =>
154                                                   $item_results->{$item}->{'dateaccessioned'} };
155                                         push @{ $results[$i]->{'item'} },
156                                           { i_barcode => (
157                                                           $item_results->{$item}->{'barcode'}
158                                                         ? $item_results->{$item}->{'barcode'} : 'NA'
159                                                 )
160                                           };
161                                         push @{ $results[$i]->{'item'} }, { i_itemnumber2 =>
162                                                   $item_results->{$item}->{'itemnumber'} };
163                                 }
164                         }
165                 }
166         }
167         $debug and warn "**********\@results**********\n";
168         $debug and warn Dumper(@results);
169
170         ($template, $loggedinuser, $cookie) = get_template_and_user(
171                 {   template_name   => "labels/result.tmpl",
172                         query           => $query,
173                         type            => "intranet",
174                         authnotrequired => 0,
175                         flagsrequired   => { borrowers => 1 },
176                         flagsrequired   => { catalogue => 1 },
177                         debug           => 1,
178                 }
179         );
180
181         # build page nav stuff.
182         my (@field_data, @numbers);
183         $total = $total_hits;
184
185         my ($from, $to, $startfromnext, $startfromprev, $displaynext, $displayprev);
186
187         if ($total > $resultsperpage) {
188                 my $num_of_pages = ceil($total / $resultsperpage + 1);
189                 for (my $page = 1 ; $page < $num_of_pages ; $page++) {
190                         my $startfrm = (($page - 1) * $resultsperpage) + 1;
191                         push @numbers,
192                           { number    => $page,
193                                 startfrom => $startfrm
194                           };
195                 }
196
197                 $from          = $startfrom;
198                 $startfromprev = $startfrom - $resultsperpage;
199                 $startfromnext = $startfrom + $resultsperpage;
200
201                 $to =
202                     $startfrom + $resultsperpage > $total
203                   ? $total
204                   : $startfrom + $resultsperpage - 1;
205
206                 # multi page display
207                 $displaynext = 0;
208                 $displayprev = $startfrom > 1 ? $startfrom : 0;
209
210                 $displaynext = 1 if $to < $total_hits;
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                 result    => \@results,
232                 batch_id  => $batch_id,
233                 type      => $type,
234                 idx       => $idx,
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.tmpl",
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 =(value => $thisitemtype,
258                            description => $itemtypes->{$thisitemtype}->{'description'},
259                             );
260             push @itemtypeloop, \%row;
261     }
262     $template->param(
263     itemtypeloop =>\@itemtypeloop,
264     batch_id     => $batch_id,
265     type         => $type,
266     );
267
268 }
269 # Print the page
270 $template->param(
271     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
272 );
273 output_html_with_http_headers $query, $cookie, $template->output;