Bug 3252: Fixes XHTML on catalog statistic form page.
[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 (
67     $total_hits,  $orderby, $results,  $total,  $error,
68     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
69 );
70
71 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
72
73 my $show_results = 0;
74
75 if ( $op eq "do_search" ) {
76     $idx         = $query->param('idx');
77     $ccl_textbox = $query->param('ccl_textbox');
78     if ( $ccl_textbox && $idx ) {
79         $ccl_query = "$idx=$ccl_textbox";
80     }
81
82     $datefrom = $query->param('datefrom');
83     $dateto   = $query->param('dateto');
84
85     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
86         {
87             template_name   => "labels/result.tmpl",
88             query           => $query,
89             type            => "intranet",
90             authnotrequired => 0,
91             flagsrequired   => { borrowers => 1 },
92             flagsrequired   => { catalogue => 1 },
93             debug           => 1,
94         }
95     );
96
97     if ($datefrom) {
98         $datefrom = C4::Dates->new($datefrom);
99         $ccl_query .= ' and ' if $ccl_textbox;
100         $ccl_query .=
101           "acqdate,st-date-normalized,ge=" . $datefrom->output("iso");
102     }
103
104     if ($dateto) {
105         $dateto = C4::Dates->new($dateto);
106         $ccl_query .= ' and ' if ( $ccl_textbox || $datefrom );
107         $ccl_query .= "acqdate,st-date-normalized,le=" . $dateto->output("iso");
108     }
109
110     my $offset = $startfrom > 1 ? $startfrom - 1 : 0;
111     ( $error, $marcresults, $total_hits ) =
112       SimpleSearch( $ccl_query, $offset, $resultsperpage );
113
114     if ($marcresults) {
115         $show_results = scalar @$marcresults;
116     }
117     else {
118         $debug and warn "ERROR label-item-search: no results from SimpleSearch";
119
120         # leave $show_results undef
121     }
122 }
123
124 if ($show_results) {
125     my $hits = $show_results;
126     my ( @results, @items );
127
128     # This code needs to be refactored using these subs...
129     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
130     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
131     for ( my $i = 0 ; $i < $hits ; $i++ ) {
132
133         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
134         my $marcrecord = MARC::File::USMARC::decode( $marcresults->[$i] );
135
136         #DEBUG Notes: Transform it to Koha form...
137         my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
138
139 # Begin building the hash for the template...
140 # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
141 #$biblio->{highlight}       = ($i % 2)?(1):(0);
142 #DEBUG Notes: Stuff the bib into @results...
143         push @results, $biblio;
144         my $biblionumber = $biblio->{'biblionumber'};
145
146         #DEBUG Notes: Grab the item numbers associated with this MARC record...
147         my $itemnums = get_itemnumbers_of($biblionumber);
148
149         #DEBUG Notes: Retrieve the item data for each number...
150         my $iii = $itemnums->{$biblionumber};
151         if ($iii) {
152             my $item_results = GetItemInfosOf(@$iii);
153             foreach my $item ( keys %$item_results ) {
154
155 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
156                 if ( $item_results->{$item}->{'biblionumber'} eq
157                     $results[$i]->{'biblionumber'} )
158                 {
159
160 # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
161 # This is a real hack, but I can't think of a better way right now. -fbcit
162 # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
163                     push @{ $results[$i]->{'item'} }, { i_itemnumber1 =>
164                           $item_results->{$item}->{'itemnumber'} };
165                     push @{ $results[$i]->{'item'} },
166                       {
167                         i_itemcallnumber => (
168                               $item_results->{$item}->{'itemcallnumber'}
169                             ? $item_results->{$item}->{'itemcallnumber'}
170                             : 'NA'
171                         )
172                       };
173                     push @{ $results[$i]->{'item'} }, { i_dateaccessioned =>
174                           $item_results->{$item}->{'dateaccessioned'} };
175                     push @{ $results[$i]->{'item'} },
176                       {
177                         i_barcode => (
178                               $item_results->{$item}->{'barcode'}
179                             ? $item_results->{$item}->{'barcode'}
180                             : 'NA'
181                         )
182                       };
183                     push @{ $results[$i]->{'item'} }, { i_itemnumber2 =>
184                           $item_results->{$item}->{'itemnumber'} };
185                 }
186             }
187         }
188     }
189     $debug and warn "**********\@results**********\n";
190     $debug and warn Dumper(@results);
191
192     # build page nav stuff.
193     my ( @field_data, @numbers );
194     $total = $total_hits;
195
196     my ( $from, $to, $startfromnext, $startfromprev, $displaynext,
197         $displayprev );
198
199     if ( $total > $resultsperpage ) {
200         my $num_of_pages = ceil( $total / $resultsperpage + 1 );
201         for ( my $page = 1 ; $page < $num_of_pages ; $page++ ) {
202             my $startfrm = ( ( $page - 1 ) * $resultsperpage ) + 1;
203             push @numbers,
204               {
205                 number    => $page,
206                 startfrom => $startfrm
207               };
208         }
209
210         $from          = $startfrom;
211         $startfromprev = $startfrom - $resultsperpage;
212         $startfromnext = $startfrom + $resultsperpage;
213
214         $to =
215             $startfrom + $resultsperpage > $total
216           ? $total
217           : $startfrom + $resultsperpage - 1;
218
219         # multi page display
220         $displaynext = 0;
221         $displayprev = $startfrom > 1 ? $startfrom : 0;
222
223         $displaynext = 1 if $to < $total_hits;
224
225     }
226     else {
227         $displayprev = 0;
228         $displaynext = 0;
229     }
230
231     $template->param(
232         total          => $total_hits,
233         from           => $from,
234         to             => $to,
235         startfromnext  => $startfromnext,
236         startfromprev  => $startfromprev,
237         startfrom      => $startfrom,
238         displaynext    => $displaynext,
239         displayprev    => $displayprev,
240         resultsperpage => $resultsperpage,
241         numbers        => \@numbers,
242     );
243
244     $template->param(
245         result    => \@results,
246         batch_id  => $batch_id,
247         type      => $type,
248         idx       => $idx,
249         ccl_query => $ccl_query,
250     );
251 }
252
253 #
254 #   search section
255 #
256
257 else {
258     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
259         {
260             template_name   => "labels/search.tmpl",
261             query           => $query,
262             type            => "intranet",
263             authnotrequired => 0,
264             flagsrequired   => { catalogue => 1 },
265             debug           => 1,
266         }
267     );
268     my $itemtypes = GetItemTypes;
269     my @itemtypeloop;
270     foreach my $thisitemtype ( keys %$itemtypes ) {
271         my %row = (
272             value       => $thisitemtype,
273             description => $itemtypes->{$thisitemtype}->{'description'},
274         );
275         push @itemtypeloop, \%row;
276     }
277     $template->param(
278         itemtypeloop => \@itemtypeloop,
279         batch_id     => $batch_id,
280         type         => $type,
281     );
282
283 }
284
285 # Print the page
286 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
287 output_html_with_http_headers $query, $cookie, $template->output;