bug 1980: changes to label-item-search.pl to allow pagination over large result sets
[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
36 use List::Util qw( max min );
37 #use Smart::Comments;
38 use Data::Dumper;
39
40 # Creates a scrolling list with the associated default value.
41 # Using more than one scrolling list in a CGI assigns the same default value to all the
42 # scrolling lists on the page !?!? That's why this function was written.
43
44 my $query           = new CGI;
45 my $type            = $query->param('type');
46 my $op              = $query->param('op');
47 my $batch_id        = $query->param('batch_id');
48 my $dateaccessioned = $query->param('dateaccessioned');
49
50 my $dbh = C4::Context->dbh;
51
52 my $startfrom = $query->param('startfrom') || 0;
53 my ( $template, $loggedinuser, $cookie );
54 my (@marclist,@and_or,@excluding,@operator,@value,$orderby,@tags,$results,$total,$error,$marcresults);
55 # XXX should this be maxItemsInSearchResults or numSearchResults preference instead of 19?
56 my $resultsperpage = $query->param('resultsperpage') || 19;
57
58 my $show_results = 0;
59 if ( $op eq "do_search" ) {
60     @marclist  = $query->param('marclist');
61     @and_or    = $query->param('and_or');
62     @excluding = $query->param('excluding');
63     @operator  = $query->param('operator');
64     @value     = $query->param('value');
65     $orderby   = $query->param('orderby');
66         if (scalar @marclist) {
67       #catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
68       #  $startfrom * $resultsperpage,
69       #  $resultsperpage, $orderby );
70                 ( $error, $marcresults ) = SimpleSearch( $marclist[0], $startfrom, $resultsperpage );
71                 if ($marcresults) {
72                         $show_results = scalar @$marcresults;
73                 } else {
74                         warn "ERROR label-item-search: no results from SimpleSearch";
75                         # leave $show_results undef
76                 }
77         }
78 }
79
80 if ( $show_results ) {
81         my $hits = $show_results;
82         my (@results,@results2);
83     # This code needs to be refactored using these subs...
84     #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
85     #my $dat = &GetBiblioData( $biblio->{biblionumber} );
86         for(my $i=0; $i<$hits; $i++) {
87         #DEBUG Notes: Decode the MARC record from each resulting MARC record...
88             my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
89         #DEBUG Notes: Transform it to Koha form...
90             my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
91             #build the hash for the template.
92             $biblio->{highlight}       = ($i % 2)?(1):(0);
93         #DEBUG Notes: Stuff it into @results... (used below to supply fields not existing in the item data)
94         push @results, $biblio;
95             my $biblionumber = $biblio->{'biblionumber'};
96         #DEBUG Notes: Grab the item numbers associated with this MARC record...
97         my $itemnums = get_itemnumbers_of($biblionumber);
98         #DEBUG Notes: Retrieve the item data for each number... 
99         my $iii = $itemnums->{$biblionumber};
100             if ($iii) {
101             my @titem_results = GetItemsInfo( $itemnums->{$biblionumber}, 'intra' );
102                 my $item_results =  GetItemInfosOf( @$iii );
103                 foreach my $item (keys %$item_results) {
104                 for my $bibdata (keys %{$results[$i]}) {
105                     if ( !$item_results->{$item}{$bibdata} ) {      #Only add the data from the bibliodata if the data does not already exit in itemdata.
106                         #Otherwise we just build duplicate records rather than unique records per item.
107                         $item_results->{$item}{$bibdata} = $results[$i]->{$bibdata};
108                     }
109                 }
110                 #DEBUG Notes: After merging the bib and item data, stuff the results into $results2...
111                 push @results2, $item_results->{$item};
112             }
113             #warn Dumper(@results2);
114             }
115     }
116   
117   ( $template, $loggedinuser, $cookie ) = get_template_and_user(
118         {
119             template_name   => "labels/result.tmpl",
120             query           => $query,
121             type            => "intranet",
122             authnotrequired => 0,
123             flagsrequired   => { borrowers => 1 },
124             flagsrequired   => { catalogue => 1 },
125             debug           => 1,
126         }
127     );
128
129     my @field_data = ();
130
131         # FIXME: this relies on symmetric order of CGI params that IS NOT GUARANTEED by spec.
132
133     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
134         push @field_data, { term => "marclist",  val => $marclist[$i] };
135         push @field_data, { term => "and_or",    val => $and_or[$i] };
136         push @field_data, { term => "excluding", val => $excluding[$i] };
137         push @field_data, { term => "operator",  val => $operator[$i] };
138         push @field_data, { term => "value",     val => $value[$i] };
139     }
140
141     my @numbers = ();
142     if ( $total > $resultsperpage ) {
143         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
144             if ( $i < 16 ) {
145                 my $highlight = 0;
146                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
147                 push @numbers,
148                   {
149                     number     => $i,
150                     highlight  => $highlight,
151                     searchdata => \@field_data,
152                     startfrom  => ( $i - 1 )
153                   };
154             }
155         }
156     }
157
158     # multi page display gestion
159     my $displaynext = 0;
160     my $displayprev = $startfrom;
161     # XXX Kludge. We show the "next" link if we retrieved the max number of results. There could be 0 more.
162     if ( scalar @results2 == $resultsperpage ) {
163         $displaynext = 1;
164     }
165
166     $template->param(
167         result         => \@results2,
168         startfrom      => $startfrom,
169         displaynext    => $displaynext,
170         displayprev    => $displayprev,
171         resultsperpage => $resultsperpage,
172         startfromnext  => $startfrom + min( $resultsperpage, scalar @results2 ),
173         startfromprev  => max( $startfrom - $resultsperpage, 0 ),
174         searchdata     => \@field_data,
175         total          => $total,
176         from           => $startfrom + 1,
177         to             => $startfrom + min( $resultsperpage, scalar @results2 ),
178         numbers        => \@numbers,
179         batch_id       => $batch_id,
180         type           => $type,
181     );
182 }
183
184 #
185 #   search section
186 #
187
188 else {
189     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
190         {
191             template_name   => "labels/search.tmpl",
192             query           => $query,
193             type            => "intranet",
194             authnotrequired => 0,
195             flagsrequired   => { catalogue => 1 },
196             debug           => 1,
197         }
198     );
199
200    #using old rel2.2 getitemtypes for testing!!!!, not devweek's GetItemTypes()
201
202     my $itemtypes = GetItemTypes;
203     my @itemtypeloop;
204     my ($thisitemtype );
205     foreach my $thisitemtype (keys %$itemtypes) {
206             my %row =(value => $thisitemtype,
207                            description => $itemtypes->{$thisitemtype}->{'description'},
208                             );  
209             push @itemtypeloop, \%row;
210     }  
211     $template->param(
212     itemtypeloop =>\@itemtypeloop,
213     batch_id     => $batch_id,
214     type         => $type,
215     );
216
217 }
218 # Print the page
219 $template->param(
220     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
221 );
222 output_html_with_http_headers $query, $cookie, $template->output;
223
224 # Local Variables:
225 # tab-width: 4
226 # End: