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