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