fix calls to get_template_and_user in label-item-search.pl
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use HTML::Template::Pro;
25 use C4::Context;
26 use C4::Search;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Acquisition;
32 use C4::Search;
33 use C4::Dates;
34 use C4::Koha;    # XXX subfield_is_koha_internal_p
35 use C4::Debug;
36 use List::Util qw( max min );
37 #use Smart::Comments;
38
39 BEGIN {
40     $debug = $debug || $cgi_debug;
41     if ($debug) {
42         require Data::Dumper;
43         import Data::Dumper qw(Dumper);
44     }
45 }
46
47 # Creates a scrolling list with the associated default value.
48 # Using more than one scrolling list in a CGI assigns the same default value to all the
49 # scrolling lists on the page !?!? That's why this function was written.
50
51 my $query           = new CGI;
52 my $type            = $query->param('type');
53 my $op              = $query->param('op');
54 my $batch_id        = $query->param('batch_id');
55 my $dateaccessioned = $query->param('dateaccessioned');
56
57 my $dbh = C4::Context->dbh;
58
59 my $startfrom = $query->param('startfrom') || 0;
60 my ( $template, $loggedinuser, $cookie );
61 my $total_hits;
62 my (@marclist,@and_or,@excluding,@operator,@value,$orderby,@tags,$results,$total,$error,$marcresults);
63 # XXX should this be maxItemsInSearchResults or numSearchResults preference instead of 19?
64 my $resultsperpage = $query->param('resultsperpage') || 19;
65
66 my $show_results = 0;
67 if ( $op eq "do_search" ) {
68     @marclist  = $query->param('marclist');
69     @and_or    = $query->param('and_or');
70     @excluding = $query->param('excluding');
71     @operator  = $query->param('operator');
72     @value     = $query->param('value');
73     $orderby   = $query->param('orderby');
74         if (scalar @marclist) {
75       #catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
76       #  $startfrom * $resultsperpage,
77       #  $resultsperpage, $orderby );
78                 ( $error, $marcresults, $total_hits ) = SimpleSearch( $marclist[0], $startfrom, $resultsperpage );
79                 if ($marcresults) {
80                         $show_results = scalar @$marcresults;
81                 } else {
82                         warn "ERROR label-item-search: no results from SimpleSearch";
83                         # leave $show_results undef
84                 }
85         }
86 }
87
88 if ( $show_results ) {
89   ( $template, $loggedinuser, $cookie ) = get_template_and_user(
90         {
91             template_name   => "labels/result.tmpl",
92             query           => $query,
93             type            => "intranet",
94             authnotrequired => 0,
95             flagsrequired   => { catalogue => 1 },
96             debug           => 1,
97         }
98     );
99
100         my $hits = $show_results;
101         my (@results, @items);
102         # This code needs to be refactored using these subs...
103         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
104         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
105         for(my $i=0; $i<$hits; $i++) {
106         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
107         my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
108         #DEBUG Notes: Transform it to Koha form...
109         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
110         # Begin building the hash for the template...
111         # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
112         #$biblio->{highlight}       = ($i % 2)?(1):(0);
113         #DEBUG Notes: Stuff the bib into @results...
114         push @results, $biblio;
115         my $biblionumber = $biblio->{'biblionumber'};
116         #DEBUG Notes: Grab the item numbers associated with this MARC record...
117         my $itemnums = get_itemnumbers_of($biblionumber);
118         #DEBUG Notes: Retrieve the item data for each number... 
119         my $iii = $itemnums->{$biblionumber};
120             if ($iii) {
121                 my $item_results =  GetItemInfosOf( @$iii );
122                 foreach my $item (keys %$item_results) {
123                     #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
124                     if ($item_results->{$item}->{'biblionumber'} eq $results[$i]->{'biblionumber'}) {
125                         # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
126                         # This is a real hack, but I can't think of a better way right now. -fbcit
127                         # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
128                         push @{$results[$i]->{'item'}}, { i_itemnumber1         => $item_results->{$item}->{'itemnumber'} };
129                         push @{$results[$i]->{'item'}}, { i_itemcallnumber      => ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA') };
130                         push @{$results[$i]->{'item'}}, { i_dateaccessioned     => $item_results->{$item}->{'dateaccessioned'} };
131                         push @{$results[$i]->{'item'}}, { i_barcode             => ($item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA')};
132                         push @{$results[$i]->{'item'}}, { i_itemnumber2         => $item_results->{$item}->{'itemnumber'} };
133                     }
134                 }
135             }
136         }
137         $debug and warn "**********\@results**********\n";
138         $debug and warn Dumper(@results);
139   
140     my @field_data = ();
141
142         # FIXME: this relies on symmetric order of CGI params that IS NOT GUARANTEED by spec.
143
144     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
145         push @field_data, { term => "marclist",  val => $marclist[$i] };
146         push @field_data, { term => "and_or",    val => $and_or[$i] };
147         push @field_data, { term => "excluding", val => $excluding[$i] };
148         push @field_data, { term => "operator",  val => $operator[$i] };
149         push @field_data, { term => "value",     val => $value[$i] };
150     }
151
152     my @numbers = ();
153     $total = $total_hits;
154     if ( $total > $resultsperpage ) {
155         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
156             if ( $i < 16 ) {
157                 my $highlight = 0;
158                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
159                 push @numbers,
160                   {
161                     number     => $i,
162                     highlight  => $highlight,
163                     searchdata => \@field_data,
164                     startfrom  => ( $i - 1 )
165                   };
166             }
167         }
168     }
169
170     # multi page display gestion
171     my $displaynext = 0;
172     my $displayprev = $startfrom;
173     # XXX Kludge. We show the "next" link if we retrieved the max number of results. There could be 0 more.
174     if ( scalar @results == $resultsperpage ) {
175         $displaynext = 1;
176     }
177
178     $template->param(
179         result         => \@results,
180         startfrom      => $startfrom,
181         displaynext    => $displaynext,
182         displayprev    => $displayprev,
183         resultsperpage => $resultsperpage,
184         startfromnext  => $startfrom + min( $resultsperpage, scalar @results ),
185         startfromprev  => max( $startfrom - $resultsperpage, 0 ),
186         searchdata     => \@field_data,
187         total          => $total_hits,
188         from           => $startfrom + 1,
189         to             => $startfrom + min( $resultsperpage, scalar @results ),
190         numbers        => \@numbers,
191         batch_id       => $batch_id,
192         type           => $type,
193     );
194 }
195
196 #
197 #   search section
198 #
199
200 else {
201     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
202         {
203             template_name   => "labels/search.tmpl",
204             query           => $query,
205             type            => "intranet",
206             authnotrequired => 0,
207             flagsrequired   => { catalogue => 1 },
208             debug           => 1,
209         }
210     );
211     my $itemtypes = GetItemTypes;
212     my @itemtypeloop;
213     foreach my $thisitemtype (keys %$itemtypes) {
214             my %row =(value => $thisitemtype,
215                            description => $itemtypes->{$thisitemtype}->{'description'},
216                             );  
217             push @itemtypeloop, \%row;
218     }  
219     $template->param(
220     itemtypeloop =>\@itemtypeloop,
221     batch_id     => $batch_id,
222     type         => $type,
223     );
224
225 }
226 # Print the page
227 $template->param(
228     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
229 );
230 output_html_with_http_headers $query, $cookie, $template->output;
231
232 # Local Variables:
233 # tab-width: 4
234 # End: