Enabling SimpleSearch to return the total number of hits to caller
[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 qw( DHTMLcalendar );
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 (@marclist,@and_or,@excluding,@operator,@value,$orderby,@tags,$results,$total,$error,$marcresults);
62 # XXX should this be maxItemsInSearchResults or numSearchResults preference instead of 19?
63 my $resultsperpage = $query->param('resultsperpage') || 19;
64
65 my $show_results = 0;
66 if ( $op eq "do_search" ) {
67     @marclist  = $query->param('marclist');
68     @and_or    = $query->param('and_or');
69     @excluding = $query->param('excluding');
70     @operator  = $query->param('operator');
71     @value     = $query->param('value');
72     $orderby   = $query->param('orderby');
73         if (scalar @marclist) {
74       #catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
75       #  $startfrom * $resultsperpage,
76       #  $resultsperpage, $orderby );
77                 ( $error, $marcresults, $total ) = SimpleSearch( $marclist[0], $startfrom, $resultsperpage );
78                 warn "\$total=$total";
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         my $hits = $show_results;
90         my (@results, @items);
91         # This code needs to be refactored using these subs...
92         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
93         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
94         for(my $i=0; $i<$hits; $i++) {
95         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
96         my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
97         #DEBUG Notes: Transform it to Koha form...
98         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
99         # Begin building the hash for the template...
100         # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
101         #$biblio->{highlight}       = ($i % 2)?(1):(0);
102         #DEBUG Notes: Stuff the bib into @results...
103         push @results, $biblio;
104         my $biblionumber = $biblio->{'biblionumber'};
105         #DEBUG Notes: Grab the item numbers associated with this MARC record...
106         my $itemnums = get_itemnumbers_of($biblionumber);
107         #DEBUG Notes: Retrieve the item data for each number... 
108         my $iii = $itemnums->{$biblionumber};
109             if ($iii) {
110                 my $item_results =  GetItemInfosOf( @$iii );
111                 foreach my $item (keys %$item_results) {
112                     #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
113                     if ($item_results->{$item}->{'biblionumber'} eq $results[$i]->{'biblionumber'}) {
114                         # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
115                         # This is a real hack, but I can't think of a better way right now. -fbcit
116                         push @{$results[$i]->{'item'}}, { i_itemnumber1         => $item_results->{$item}->{'itemnumber'} };
117                         push @{$results[$i]->{'item'}}, { i_itemcallnumber      => $item_results->{$item}->{'itemcallnumber'} };
118                         push @{$results[$i]->{'item'}}, { i_dateaccessioned     => $item_results->{$item}->{'dateaccessioned'} };
119                         push @{$results[$i]->{'item'}}, { i_barcode             => $item_results->{$item}->{'barcode'} };
120                         push @{$results[$i]->{'item'}}, { i_itemnumber2         => $item_results->{$item}->{'itemnumber'} };
121                     }
122                 }
123             }
124         }
125         $debug and warn "**********\@results**********\n";
126         $debug and warn Dumper(@results);
127   
128   ( $template, $loggedinuser, $cookie ) = get_template_and_user(
129         {
130             template_name   => "labels/result.tmpl",
131             query           => $query,
132             type            => "intranet",
133             authnotrequired => 0,
134             flagsrequired   => { borrowers => 1 },
135             flagsrequired   => { catalogue => 1 },
136             debug           => 1,
137         }
138     );
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     if ( $total > $resultsperpage ) {
154         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
155             if ( $i < 16 ) {
156                 my $highlight = 0;
157                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
158                 push @numbers,
159                   {
160                     number     => $i,
161                     highlight  => $highlight,
162                     searchdata => \@field_data,
163                     startfrom  => ( $i - 1 )
164                   };
165             }
166         }
167     }
168
169     # multi page display gestion
170     my $displaynext = 0;
171     my $displayprev = $startfrom;
172     # XXX Kludge. We show the "next" link if we retrieved the max number of results. There could be 0 more.
173     if ( scalar @results == $resultsperpage ) {
174         $displaynext = 1;
175     }
176
177     $template->param(
178         result         => \@results,
179         startfrom      => $startfrom,
180         displaynext    => $displaynext,
181         displayprev    => $displayprev,
182         resultsperpage => $resultsperpage,
183         startfromnext  => $startfrom + min( $resultsperpage, scalar @results ),
184         startfromprev  => max( $startfrom - $resultsperpage, 0 ),
185         searchdata     => \@field_data,
186         total          => $total,
187         from           => $startfrom + 1,
188         to             => $startfrom + min( $resultsperpage, scalar @results ),
189         numbers        => \@numbers,
190         batch_id       => $batch_id,
191         type           => $type,
192     );
193 }
194
195 #
196 #   search section
197 #
198
199 else {
200     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
201         {
202             template_name   => "labels/search.tmpl",
203             query           => $query,
204             type            => "intranet",
205             authnotrequired => 0,
206             flagsrequired   => { catalogue => 1 },
207             debug           => 1,
208         }
209     );
210
211    #using old rel2.2 getitemtypes for testing!!!!, not devweek's GetItemTypes()
212
213     my $itemtypes = GetItemTypes;
214     my @itemtypeloop;
215     my ($thisitemtype );
216     foreach my $thisitemtype (keys %$itemtypes) {
217             my %row =(value => $thisitemtype,
218                            description => $itemtypes->{$thisitemtype}->{'description'},
219                             );  
220             push @itemtypeloop, \%row;
221     }  
222     $template->param(
223     itemtypeloop =>\@itemtypeloop,
224     batch_id     => $batch_id,
225     type         => $type,
226     );
227
228 }
229 # Print the page
230 $template->param(
231     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
232 );
233 output_html_with_http_headers $query, $cookie, $template->output;
234
235 # Local Variables:
236 # tab-width: 4
237 # End: