Adding labels generator.
[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::Koha;
25 use C4::Auth;
26 use HTML::Template;
27 use C4::Context;
28 use C4::Search;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Biblio;
32 use C4::Acquisition;
33 use C4::Search;
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
81 my $hits = scalar @$marcresults;
82 my @results;
83 my $results;
84
85
86 for(my $i=0;$i<$hits;$i++) {
87     my %resultsloop;
88     my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
89     my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
90     #build the hash for the template.
91     %resultsloop=%$biblio;
92     $resultsloop{highlight}       = ($i % 2)?(1):(0);
93 #warn $resultsloop{biblionumber};
94
95     push @results, \%resultsloop;
96 }
97     my @results2;
98     my $i;
99     for ( $i = 0 ; $i <= ( $hits - 1 ) ; $i++ ) {
100
101         my $itemnums = get_itemnumbers_of($results[$i]->{'biblionumber'});
102
103         my $iii = $itemnums->{$results[$i]->{'biblionumber'} } ;
104         my $item_results;
105                 if ($iii ) {
106                         $item_results =  &GetItemInfosOf( @$iii );
107         }
108         foreach my $item (keys %$item_results) {
109                         for my $bibdata (keys %{$results[$i]}) {
110 #warn Dumper($bibdata);        
111 #warn Dumper($results[$i]->{$bibdata});
112                                 $item_results->{$item}{$bibdata} = $results[$i]->{$bibdata};
113                         }
114             push @results2, $item_results->{$item};
115         }
116
117     }
118    ( $template, $loggedinuser, $cookie ) = get_template_and_user(
119         {
120             template_name   => "labels/result.tmpl",
121             query           => $query,
122             type            => "intranet",
123             authnotrequired => 0,
124             flagsrequired   => { borrowers => 1 },
125             flagsrequired   => { catalogue => 1 },
126             debug           => 1,
127         }
128     );
129
130     # multi page display gestion
131     my $displaynext = 0;
132     my $displayprev = $startfrom;
133     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
134         $displaynext = 1;
135     }
136
137     my @field_data = ();
138
139     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
140         push @field_data, { term => "marclist",  val => $marclist[$i] };
141         push @field_data, { term => "and_or",    val => $and_or[$i] };
142         push @field_data, { term => "excluding", val => $excluding[$i] };
143         push @field_data, { term => "operator",  val => $operator[$i] };
144         push @field_data, { term => "value",     val => $value[$i] };
145     }
146
147     my @numbers = ();
148
149     if ( $total > $resultsperpage ) {
150         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
151             if ( $i < 16 ) {
152                 my $highlight = 0;
153                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
154                 push @numbers,
155                   {
156                     number     => $i,
157                     highlight  => $highlight,
158                     searchdata => \@field_data,
159                     startfrom  => ( $i - 1 )
160                   };
161             }
162         }
163     }
164
165     my $from = $startfrom * $resultsperpage + 1;
166     my $to;
167
168     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
169         $to = $total;
170     }
171     else {
172         $to = ( ( $startfrom + 1 ) * $resultsperpage );
173     }
174
175     # this gets the results of the search (which are bibs)
176     # and then does a lookup on all items that exist for that bib
177     # then pushes the items onto a new array, as we really want the
178     # items attached to the bibs not thew bibs themselves
179
180    # my @results2;
181     my $i;
182     for ( $i = 0 ; $i <= ( $total - 1 ) ; $i++ )
183     {    #total-1 coz the array starts at 0
184             #warn $i;
185             #warn Dumper $results->[$i]{'bibid'};
186         my $type = 'intra';
187         my @item_results =
188           &ItemInfo( 0, $results->[$i]{'biblionumber'}, $type );
189
190         foreach my $item (@item_results) {
191
192             #warn Dumper $item;
193             push @results2, $item;
194         }
195
196     }
197
198     $template->param(
199         result         => \@results2,
200         startfrom      => $startfrom,
201         displaynext    => $displaynext,
202         displayprev    => $displayprev,
203         resultsperpage => $resultsperpage,
204         startfromnext  => $startfrom + 1,
205         startfromprev  => $startfrom - 1,
206         searchdata     => \@field_data,
207         total          => $total,
208         from           => $from,
209         to             => $to,
210         numbers        => \@numbers,
211         batch_id       => $batch_id,
212     );
213 }
214
215 #
216 #
217 #   search section
218 #
219 #
220
221 else {
222     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
223         {
224             template_name   => "labels/search.tmpl",
225             query           => $query,
226             type            => "intranet",
227             authnotrequired => 0,
228             flagsrequired   => { catalogue => 1 },
229             debug           => 1,
230         }
231     );
232
233
234    #using old rel2.2 getitemtypes for testing!!!!, not devweek's GetItemTypes()
235
236     my $itemtypes = GetItemTypes;
237     my @itemtypeloop;
238     my ($thisitemtype );
239     foreach my $thisitemtype (keys %$itemtypes) {
240             my %row =(value => $thisitemtype,
241                            description => $itemtypes->{$thisitemtype}->{'description'},
242                             );  
243             push @itemtypeloop, \%row;
244     }  
245
246
247     $template->param(
248     itemtypeloop =>\@itemtypeloop,
249         batch_id     => $batch_id,
250     );
251
252     # Print the page
253     $template->param(
254         intranetcolorstylesheet =>
255           C4::Context->preference("intranetcolorstylesheet"),
256         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
257         IntranetNav        => C4::Context->preference("IntranetNav"),
258     );
259 }
260 output_html_with_http_headers $query, $cookie, $template->output;
261
262 # Local Variables:
263 # tab-width: 4
264 # End: