followup to Mason's labels patch
[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 POSIX;
38
39 #use Smart::Comments;
40 #use Data::Dumper;
41
42 BEGIN {
43     $debug = $debug || $cgi_debug;
44     if ($debug) {
45         require Data::Dumper;
46         import Data::Dumper qw(Dumper);
47     }
48 }
49
50 # Creates a scrolling list with the associated default value.
51 # Using more than one scrolling list in a CGI assigns the same default value to all the
52 # scrolling lists on the page !?!? That's why this function was written.
53
54 my $query           = new CGI;
55
56 my $type      = $query->param('type');
57 my $op        = $query->param('op');
58 my $batch_id  = $query->param('batch_id');
59 my $ccl_query = $query->param('ccl_query');
60
61 my $dbh = C4::Context->dbh;
62
63 my $startfrom = $query->param('startfrom') || 1;
64 my ( $template, $loggedinuser, $cookie );
65 my ($total_hits, $orderby, $results, $total, $error, $marcresults, $idx,        $datefrom, $dateto, $ccl_textbox);
66
67 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
68
69 my $show_results = 0;
70
71 if ($op eq "do_search") {
72         $idx       = $query->param('idx');
73         $ccl_textbox = $query->param('ccl_textbox');
74      if ($ccl_textbox && $idx) {
75     $ccl_query = "$idx=$ccl_textbox" ;
76     }
77
78         $datefrom = $query->param('datefrom');
79         $dateto   = $query->param('dateto');
80
81         if ($datefrom) {
82                 $datefrom = C4::Dates->new($datefrom);
83         $ccl_query .= ' and ' if $ccl_textbox;
84                 $ccl_query .= "acqdate,st-date-normalized,ge=" .  $datefrom->output("iso");
85         }
86
87         if ($dateto) {
88                 $dateto = C4::Dates->new($dateto);
89         $ccl_query .= ' and ' if ($ccl_textbox || $datefrom) ;
90                 $ccl_query .= "acqdate,st-date-normalized,le=".  $dateto->output("iso");
91         }
92
93     my $offset =    $startfrom > 1 ? $startfrom - 1 : 0;
94         ($error, $marcresults, $total_hits) =
95           SimpleSearch($ccl_query, $offset, $resultsperpage);
96
97         if ($marcresults) {
98                 $show_results = scalar @$marcresults;
99         } else {
100                 warn "ERROR label-item-search: no results from SimpleSearch";
101
102                 # leave $show_results undef
103         }
104 }
105
106 if ($show_results) {
107         my $hits = $show_results;
108         my (@results, @items);
109
110         # This code needs to be refactored using these subs...
111         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
112         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
113         for (my $i = 0 ; $i < $hits ; $i++) {
114
115                 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
116                 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
117
118                 #DEBUG Notes: Transform it to Koha form...
119                 my $biblio = TransformMarcToKoha(C4::Context->dbh, $marcrecord, '');
120
121 # Begin building the hash for the template...
122 # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
123 #$biblio->{highlight}       = ($i % 2)?(1):(0);
124 #DEBUG Notes: Stuff the bib into @results...
125                 push @results, $biblio;
126                 my $biblionumber = $biblio->{'biblionumber'};
127
128                 #DEBUG Notes: Grab the item numbers associated with this MARC record...
129                 my $itemnums = get_itemnumbers_of($biblionumber);
130
131                 #DEBUG Notes: Retrieve the item data for each number...
132                 my $iii = $itemnums->{$biblionumber};
133                 if ($iii) {
134                         my $item_results = GetItemInfosOf(@$iii);
135                         foreach my $item (keys %$item_results) {
136
137 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
138                                 if ($item_results->{$item}->{'biblionumber'} eq
139                                         $results[$i]->{'biblionumber'}) {
140
141 # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
142 # This is a real hack, but I can't think of a better way right now. -fbcit
143 # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
144                                         push @{ $results[$i]->{'item'} }, { i_itemnumber1 =>
145                                                   $item_results->{$item}->{'itemnumber'} };
146                                         push @{ $results[$i]->{'item'} },
147                                           { i_itemcallnumber => (
148                                                           $item_results->{$item}->{'itemcallnumber'}
149                                                         ? $item_results->{$item}->{'itemcallnumber'} : 'NA'
150                                                 )
151                                           };
152                                         push @{ $results[$i]->{'item'} }, { i_dateaccessioned =>
153                                                   $item_results->{$item}->{'dateaccessioned'} };
154                                         push @{ $results[$i]->{'item'} },
155                                           { i_barcode => (
156                                                           $item_results->{$item}->{'barcode'}
157                                                         ? $item_results->{$item}->{'barcode'} : 'NA'
158                                                 )
159                                           };
160                                         push @{ $results[$i]->{'item'} }, { i_itemnumber2 =>
161                                                   $item_results->{$item}->{'itemnumber'} };
162                                 }
163                         }
164                 }
165         }
166         $debug and warn "**********\@results**********\n";
167         $debug and warn Dumper(@results);
168
169         ($template, $loggedinuser, $cookie) = get_template_and_user(
170                 {   template_name   => "labels/result.tmpl",
171                         query           => $query,
172                         type            => "intranet",
173                         authnotrequired => 0,
174                         flagsrequired   => { borrowers => 1 },
175                         flagsrequired   => { catalogue => 1 },
176                         debug           => 1,
177                 }
178         );
179
180         # build page nav stuff.
181         my (@field_data, @numbers);
182         $total = $total_hits;
183
184         my ($from, $to, $startfromnext, $startfromprev, $displaynext, $displayprev);
185
186         if ($total > $resultsperpage) {
187                 my $num_of_pages = ceil($total / $resultsperpage + 1);
188                 for (my $page = 1 ; $page < $num_of_pages ; $page++) {
189                         my $startfrm = (($page - 1) * $resultsperpage) + 1;
190                         push @numbers,
191                           { number    => $page,
192                                 startfrom => $startfrm
193                           };
194                 }
195
196                 $from          = $startfrom;
197                 $startfromprev = $startfrom - $resultsperpage;
198                 $startfromnext = $startfrom + $resultsperpage;
199
200                 $to =
201                     $startfrom + $resultsperpage > $total
202                   ? $total
203                   : $startfrom + $resultsperpage - 1;
204
205                 # multi page display
206                 $displaynext = 0;
207                 $displayprev = $startfrom > 1 ? $startfrom : 0;
208
209                 $displaynext = 1 if $to < $total_hits;
210
211         } else {
212         $displayprev = 0;
213         $displaynext = 0;
214     }
215
216         $template->param(
217                 total          => $total_hits,
218                 from           => $from,
219                 to             => $to,
220                 startfromnext  => $startfromnext,
221                 startfromprev  => $startfromprev,
222                 startfrom      => $startfrom,
223                 displaynext    => $displaynext,
224                 displayprev    => $displayprev,
225                 resultsperpage => $resultsperpage,
226                 numbers        => \@numbers,
227         );
228
229         $template->param(
230                 result    => \@results,
231                 batch_id  => $batch_id,
232                 type      => $type,
233                 idx       => $idx,
234                 ccl_query => $ccl_query,
235         );
236 }
237
238 #
239 #   search section
240 #
241
242 else {
243     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
244         {
245             template_name   => "labels/search.tmpl",
246             query           => $query,
247             type            => "intranet",
248             authnotrequired => 0,
249             flagsrequired   => { catalogue => 1 },
250             debug           => 1,
251         }
252     );
253     my $itemtypes = GetItemTypes;
254     my @itemtypeloop;
255     foreach my $thisitemtype (keys %$itemtypes) {
256             my %row =(value => $thisitemtype,
257                            description => $itemtypes->{$thisitemtype}->{'description'},
258                             );
259             push @itemtypeloop, \%row;
260     }
261     $template->param(
262     itemtypeloop =>\@itemtypeloop,
263     batch_id     => $batch_id,
264     type         => $type,
265     );
266
267 }
268 # Print the page
269 $template->param(
270     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
271 );
272 output_html_with_http_headers $query, $cookie, $template->output;
273
274 # Local Variables:
275 # tab-width: 4
276 # End:
277
278
279
280
281
282
283