labels item search: build item list correctly
[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 require Exporter;
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 qw( DHTMLcalendar );
35 use C4::Koha;    # XXX subfield_is_koha_internal_p
36
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 ### $query;
51
52 my $dbh = C4::Context->dbh;
53
54 my $startfrom = $query->param('startfrom');
55 $startfrom = 0 if ( !defined $startfrom );
56 my ( $template, $loggedinuser, $cookie );
57 my $resultsperpage;
58
59 if ( $op eq "do_search" ) {
60     my @marclist  = $query->param('marclist');
61     my @and_or    = $query->param('and_or');
62     my @excluding = $query->param('excluding');
63     my @operator  = $query->param('operator');
64     my @value     = $query->param('value');
65
66     $resultsperpage = $query->param('resultsperpage');
67     $resultsperpage = 19 if ( !defined $resultsperpage );
68     my $orderby = $query->param('orderby');
69
70     # builds tag and subfield arrays
71     my @tags;
72
73     my ( $results, $total ) ;
74       #catalogsearch( $dbh, \@tags, \@and_or, \@excluding, \@operator, \@value,
75       #  $startfrom * $resultsperpage,
76       #  $resultsperpage, $orderby );
77
78 #use Data::Dumper;
79         my $searchquery=$marclist[0];
80         my ($error, $marcresults) = SimpleSearch($searchquery);
81         my $hits = scalar @$marcresults;
82         my @results;
83
84         for(my $i=0;$i<$hits;$i++) {
85                 my %resultsloop;
86                 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
87                 my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
88                 #build the hash for the template.
89                 %resultsloop=%$biblio;
90                 $resultsloop{highlight}       = ($i % 2)?(1):(0);
91                 #warn $resultsloop{biblionumber};
92                 push @results, \%resultsloop;
93         }
94     my @results2;
95     my $i;
96     for ( $i = 0 ; $i <= ( $hits - 1 ) ; $i++ ) {
97         my $itemnums = get_itemnumbers_of($results[$i]->{'biblionumber'});
98
99         my $iii = $itemnums->{$results[$i]->{'biblionumber'} } ;
100         my $item_results;
101                 if ($iii ) {
102                         $item_results =  &GetItemInfosOf( @$iii );
103         }
104         foreach my $item (keys %$item_results) {
105                         for my $bibdata (keys %{$results[$i]}) {
106 #warn Dumper($bibdata);        
107 #warn Dumper($results[$i]->{$bibdata});
108                                 $item_results->{$item}{$bibdata} = $results[$i]->{$bibdata} unless exists $item_results->{$item}{$bibdata};
109                         }
110             push @results2, $item_results->{$item};
111         }
112
113     }
114    ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115         {
116             template_name   => "labels/result.tmpl",
117             query           => $query,
118             type            => "intranet",
119             authnotrequired => 0,
120             flagsrequired   => { borrowers => 1 },
121             flagsrequired   => { catalogue => 1 },
122             debug           => 1,
123         }
124     );
125
126     # multi page display gestion
127     my $displaynext = 0;
128     my $displayprev = $startfrom;
129     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
130         $displaynext = 1;
131     }
132
133     my @field_data = ();
134
135     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
136         push @field_data, { term => "marclist",  val => $marclist[$i] };
137         push @field_data, { term => "and_or",    val => $and_or[$i] };
138         push @field_data, { term => "excluding", val => $excluding[$i] };
139         push @field_data, { term => "operator",  val => $operator[$i] };
140         push @field_data, { term => "value",     val => $value[$i] };
141     }
142
143     my @numbers = ();
144
145     if ( $total > $resultsperpage ) {
146         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
147             if ( $i < 16 ) {
148                 my $highlight = 0;
149                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
150                 push @numbers,
151                   {
152                     number     => $i,
153                     highlight  => $highlight,
154                     searchdata => \@field_data,
155                     startfrom  => ( $i - 1 )
156                   };
157             }
158         }
159     }
160
161     my $from = $startfrom * $resultsperpage + 1;
162     my $to;
163
164     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
165         $to = $total;
166     }
167     else {
168         $to = ( ( $startfrom + 1 ) * $resultsperpage );
169     }
170
171     # this gets the results of the search (which are bibs)
172     # and then does a lookup on all items that exist for that bib
173     # then pushes the items onto a new array, as we really want the
174     # items attached to the bibs not thew bibs themselves
175
176    # my @results2;
177     for (my $i = 0 ; $i <= ( $total - 1 ) ; $i++ )
178     {    #total-1 coz the array starts at 0
179             #warn $i;
180             #warn Dumper $results->[$i]{'bibid'};
181         my $type = 'intra';
182         my @item_results =
183           &ItemInfo( 0, $results->[$i]{'biblionumber'}, $type );
184
185         foreach my $item (@item_results) {
186             #warn Dumper $item;
187             push @results2, $item;
188         }
189
190     }
191
192     $template->param(
193         result         => \@results2,
194         startfrom      => $startfrom,
195         displaynext    => $displaynext,
196         displayprev    => $displayprev,
197         resultsperpage => $resultsperpage,
198         startfromnext  => $startfrom + 1,
199         startfromprev  => $startfrom - 1,
200         searchdata     => \@field_data,
201         total          => $total,
202         from           => $from,
203         to             => $to,
204         numbers        => \@numbers,
205         batch_id       => $batch_id,
206     );
207 }
208
209 #
210 #
211 #   search section
212 #
213 #
214
215 else {
216     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
217         {
218             template_name   => "labels/search.tmpl",
219             query           => $query,
220             type            => "intranet",
221             authnotrequired => 0,
222             flagsrequired   => { catalogue => 1 },
223             debug           => 1,
224         }
225     );
226
227
228    #using old rel2.2 getitemtypes for testing!!!!, not devweek's GetItemTypes()
229
230     my $itemtypes = GetItemTypes;
231     my @itemtypeloop;
232     my ($thisitemtype );
233     foreach my $thisitemtype (keys %$itemtypes) {
234             my %row =(value => $thisitemtype,
235                            description => $itemtypes->{$thisitemtype}->{'description'},
236                             );  
237             push @itemtypeloop, \%row;
238     }  
239
240
241     $template->param(
242     itemtypeloop =>\@itemtypeloop,
243         batch_id     => $batch_id,
244     );
245
246 }
247 # Print the page
248 $template->param(
249     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
250 );
251 output_html_with_http_headers $query, $cookie, $template->output;
252
253 # Local Variables:
254 # tab-width: 4
255 # End: