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