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