Fixes to enable display and selection of individual items rather than bibs.
[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         # This code needs to be refactored using these subs...
82         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
83         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
84         for(my $i=0; $i<$hits; $i++) {
85             #DEBUG Notes: Decode the MARC record from each resulting MARC record...
86             my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
87             #DEBUG Notes: Transform it to Koha form...
88             my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
89             #build the hash for the template.
90             $biblio->{highlight}       = ($i % 2)?(1):(0);
91             #DEBUG Notes: Stuff it into @results... (used below to supply fields not existing in the item data)
92             push @results, $biblio;
93             my $biblionumber = $biblio->{'biblionumber'};
94             #DEBUG Notes: Grab the item numbers associated with this MARC record...
95             my $itemnums = get_itemnumbers_of($biblionumber);
96             #DEBUG Notes: Retrieve the item data for each number... 
97             my $iii = $itemnums->{$biblionumber};
98             if ($iii) {
99                 my @titem_results = GetItemsInfo( $itemnums->{$biblionumber}, 'intra' );
100                 my $item_results =  GetItemInfosOf( @$iii );
101                 foreach my $item (keys %$item_results) {
102                     for my $bibdata (keys %{$results[$i]}) {
103                         if ( !$item_results->{$item}{$bibdata} ) {      #Only add the data from the bibliodata if the data does not already exit in itemdata.
104                                                                         #Otherwise we just build duplicate records rather than unique records per item.
105                             $item_results->{$item}{$bibdata} = $results[$i]->{$bibdata};
106                         }
107                     }
108                     #DEBUG Notes: After merging the bib and item data, stuff the results into $results2...
109                     push @results2, $item_results->{$item};
110                 }
111                 #warn Dumper(@results2);
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         # FIXME: this relies on symmetric order of CGI params that IS NOT GUARANTEED by spec.
136
137     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
138         push @field_data, { term => "marclist",  val => $marclist[$i] };
139         push @field_data, { term => "and_or",    val => $and_or[$i] };
140         push @field_data, { term => "excluding", val => $excluding[$i] };
141         push @field_data, { term => "operator",  val => $operator[$i] };
142         push @field_data, { term => "value",     val => $value[$i] };
143     }
144
145     my @numbers = ();
146     if ( $total > $resultsperpage ) {
147         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
148             if ( $i < 16 ) {
149                 my $highlight = 0;
150                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
151                 push @numbers,
152                   {
153                     number     => $i,
154                     highlight  => $highlight,
155                     searchdata => \@field_data,
156                     startfrom  => ( $i - 1 )
157                   };
158             }
159         }
160     }
161
162     my $from = $startfrom * $resultsperpage + 1;
163         my $temp = ( $startfrom + 1 ) * $resultsperpage;
164     my $to   = ($total < $temp) ? $total : $temp;
165
166     # this gets the results of the search (which are bibs)
167     # and then does a lookup on all items that exist for that bib
168     # then pushes the items onto a new array, as we really want the
169     # items attached to the bibs not thew bibs themselves
170
171    # my @results2;
172     # for (my $i = 0 ; $i < $total ; $i++ )
173     # {
174         #warn $i;
175         #warn Dumper $results->[$i]{'bibid'};
176     #     my $type = 'intra';
177     #     my @item_results = &ItemInfo( 0, $results->[$i]{'biblionumber'}, $type );
178                         # FIXME: ItemInfo doesn't exist !!
179 #               push @results2,\@item_results;
180         # foreach my $item (@item_results) {
181         #       warn Dumper $item;
182         #       push @results2, $item;
183         # }
184 #     }
185
186     $template->param(
187         result         => \@results2,
188         startfrom      => $startfrom,
189         displaynext    => $displaynext,
190         displayprev    => $displayprev,
191         resultsperpage => $resultsperpage,
192         startfromnext  => $startfrom + 1,
193         startfromprev  => $startfrom - 1,
194         searchdata     => \@field_data,
195         total          => $total,
196         from           => $from,
197         to             => $to,
198         numbers        => \@numbers,
199         batch_id       => $batch_id,
200     );
201 }
202
203 #
204 #   search section
205 #
206
207 else {
208     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
209         {
210             template_name   => "labels/search.tmpl",
211             query           => $query,
212             type            => "intranet",
213             authnotrequired => 0,
214             flagsrequired   => { catalogue => 1 },
215             debug           => 1,
216         }
217     );
218
219    #using old rel2.2 getitemtypes for testing!!!!, not devweek's GetItemTypes()
220
221     my $itemtypes = GetItemTypes;
222     my @itemtypeloop;
223     my ($thisitemtype );
224     foreach my $thisitemtype (keys %$itemtypes) {
225             my %row =(value => $thisitemtype,
226                            description => $itemtypes->{$thisitemtype}->{'description'},
227                             );  
228             push @itemtypeloop, \%row;
229     }  
230     $template->param(
231     itemtypeloop =>\@itemtypeloop,
232         batch_id     => $batch_id,
233     );
234
235 }
236 # Print the page
237 $template->param(
238     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
239 );
240 output_html_with_http_headers $query, $cookie, $template->output;
241
242 # Local Variables:
243 # tab-width: 4
244 # End: