Polish, English NZ and German updates
[koha.git] / labels / label-item-search.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use HTML::Template::Pro;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Acquisition;
33 use C4::Search;
34 use C4::Dates;
35 use C4::Koha;    # XXX subfield_is_koha_internal_p
36 use C4::Debug;
37 use List::Util qw( max min );
38 use POSIX;
39
40 #use Smart::Comments;
41 #use Data::Dumper;
42
43 BEGIN {
44     $debug = $debug || $cgi_debug;
45     if ($debug) {
46         require Data::Dumper;
47         import Data::Dumper qw(Dumper);
48     }
49 }
50
51 # Creates a scrolling list with the associated default value.
52 # Using more than one scrolling list in a CGI assigns the same default value to all the
53 # scrolling lists on the page !?!? That's why this function was written.
54
55 my $query = new CGI;
56
57 my $type      = $query->param('type');
58 my $op        = $query->param('op') || '';
59 my $batch_id  = $query->param('batch_id');
60 my $ccl_query = $query->param('ccl_query');
61
62 my $dbh = C4::Context->dbh;
63
64 my $startfrom = $query->param('startfrom') || 1;
65 my ( $template, $loggedinuser, $cookie );
66 my (
67     $total_hits,  $orderby, $results,  $total,  $error,
68     $marcresults, $idx,     $datefrom, $dateto, $ccl_textbox
69 );
70
71 my $resultsperpage = C4::Context->preference('numSearchResults') || '20';
72
73 my $show_results = 0;
74
75 if ($op eq "do_search") {
76         $idx       = $query->param('idx');
77         $ccl_textbox = $query->param('ccl_textbox');
78      if ($ccl_textbox && $idx) {
79     $ccl_query = "$idx=$ccl_textbox" ;
80     }
81
82         $datefrom = $query->param('datefrom');
83         $dateto   = $query->param('dateto');
84
85         if ($datefrom) {
86                 $datefrom = C4::Dates->new($datefrom);
87         $ccl_query .= ' and ' if $ccl_textbox;
88                 $ccl_query .= "acqdate,st-date-normalized,ge=" .  $datefrom->output("iso");
89         }
90
91         if ($dateto) {
92                 $dateto = C4::Dates->new($dateto);
93         $ccl_query .= ' and ' if ($ccl_textbox || $datefrom) ;
94                 $ccl_query .= "acqdate,st-date-normalized,le=".  $dateto->output("iso");
95         }
96
97     my $offset =    $startfrom > 1 ? $startfrom - 1 : 0;
98         ($error, $marcresults, $total_hits) =
99           SimpleSearch($ccl_query, $offset, $resultsperpage);
100
101         if ($marcresults) {
102                 $show_results = scalar @$marcresults;
103         } else {
104                 warn "ERROR label-item-search: no results from SimpleSearch";
105
106                 # leave $show_results undef
107         }
108 }
109
110 if ($show_results) {
111         my $hits = $show_results;
112         my (@results, @items);
113
114         # This code needs to be refactored using these subs...
115         #my @items = &GetItemsInfo( $biblio->{biblionumber}, 'intra' );
116         #my $dat = &GetBiblioData( $biblio->{biblionumber} );
117         for (my $i = 0 ; $i < $hits ; $i++) {
118
119                 #DEBUG Notes: Decode the MARC record from each resulting MARC record...
120                 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
121
122                 #DEBUG Notes: Transform it to Koha form...
123                 my $biblio = TransformMarcToKoha(C4::Context->dbh, $marcrecord, '');
124
125 # Begin building the hash for the template...
126 # I don't think we need this with the current template design, but I'm leaving it in place. -fbcit
127 #$biblio->{highlight}       = ($i % 2)?(1):(0);
128 #DEBUG Notes: Stuff the bib into @results...
129                 push @results, $biblio;
130                 my $biblionumber = $biblio->{'biblionumber'};
131
132                 #DEBUG Notes: Grab the item numbers associated with this MARC record...
133                 my $itemnums = get_itemnumbers_of($biblionumber);
134
135                 #DEBUG Notes: Retrieve the item data for each number...
136                 my $iii = $itemnums->{$biblionumber};
137                 if ($iii) {
138                         my $item_results = GetItemInfosOf(@$iii);
139                         foreach my $item (keys %$item_results) {
140
141 #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data...
142                                 if ($item_results->{$item}->{'biblionumber'} eq
143                                         $results[$i]->{'biblionumber'}) {
144
145 # NOTE: The order of the elements in this array must be preserved or the table dependent on it will be incorrectly rendered.
146 # This is a real hack, but I can't think of a better way right now. -fbcit
147 # It is conceivable that itemcallnumber and/or barcode fields might be empty so the trinaries cover this possibility.
148                                         push @{ $results[$i]->{'item'} }, { i_itemnumber1 =>
149                                                   $item_results->{$item}->{'itemnumber'} };
150                                         push @{ $results[$i]->{'item'} },
151                                           { i_itemcallnumber => (
152                                                           $item_results->{$item}->{'itemcallnumber'}
153                                                         ? $item_results->{$item}->{'itemcallnumber'} : 'NA'
154                                                 )
155                                           };
156                                         push @{ $results[$i]->{'item'} }, { i_dateaccessioned =>
157                                                   $item_results->{$item}->{'dateaccessioned'} };
158                                         push @{ $results[$i]->{'item'} },
159                                           { i_barcode => (
160                                                           $item_results->{$item}->{'barcode'}
161                                                         ? $item_results->{$item}->{'barcode'} : 'NA'
162                                                 )
163                                           };
164                                         push @{ $results[$i]->{'item'} }, { i_itemnumber2 =>
165                                                   $item_results->{$item}->{'itemnumber'} };
166                                 }
167                         }
168                 }
169         }
170         $debug and warn "**********\@results**********\n";
171         $debug and warn Dumper(@results);
172
173         ($template, $loggedinuser, $cookie) = get_template_and_user(
174                 {   template_name   => "labels/result.tmpl",
175                         query           => $query,
176                         type            => "intranet",
177                         authnotrequired => 0,
178                         flagsrequired   => { borrowers => 1 },
179                         flagsrequired   => { catalogue => 1 },
180                         debug           => 1,
181                 }
182         );
183
184         # build page nav stuff.
185         my (@field_data, @numbers);
186         $total = $total_hits;
187
188         my ($from, $to, $startfromnext, $startfromprev, $displaynext, $displayprev);
189
190         if ($total > $resultsperpage) {
191                 my $num_of_pages = ceil($total / $resultsperpage + 1);
192                 for (my $page = 1 ; $page < $num_of_pages ; $page++) {
193                         my $startfrm = (($page - 1) * $resultsperpage) + 1;
194                         push @numbers,
195                           { number    => $page,
196                                 startfrom => $startfrm
197                           };
198                 }
199
200                 $from          = $startfrom;
201                 $startfromprev = $startfrom - $resultsperpage;
202                 $startfromnext = $startfrom + $resultsperpage;
203
204                 $to =
205                     $startfrom + $resultsperpage > $total
206                   ? $total
207                   : $startfrom + $resultsperpage - 1;
208
209                 # multi page display
210                 $displaynext = 0;
211                 $displayprev = $startfrom > 1 ? $startfrom : 0;
212
213                 $displaynext = 1 if $to < $total_hits;
214
215         } else {
216         $displayprev = 0;
217         $displaynext = 0;
218     }
219
220         $template->param(
221                 total          => $total_hits,
222                 from           => $from,
223                 to             => $to,
224                 startfromnext  => $startfromnext,
225                 startfromprev  => $startfromprev,
226                 startfrom      => $startfrom,
227                 displaynext    => $displaynext,
228                 displayprev    => $displayprev,
229                 resultsperpage => $resultsperpage,
230                 numbers        => \@numbers,
231         );
232
233         $template->param(
234                 result    => \@results,
235                 batch_id  => $batch_id,
236                 type      => $type,
237                 idx       => $idx,
238                 ccl_query => $ccl_query,
239         );
240 }
241
242 #
243 #   search section
244 #
245
246 else {
247     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
248         {
249             template_name   => "labels/search.tmpl",
250             query           => $query,
251             type            => "intranet",
252             authnotrequired => 0,
253             flagsrequired   => { catalogue => 1 },
254             debug           => 1,
255         }
256     );
257     my $itemtypes = GetItemTypes;
258     my @itemtypeloop;
259     foreach my $thisitemtype ( keys %$itemtypes ) {
260         my %row = (
261             value       => $thisitemtype,
262             description => $itemtypes->{$thisitemtype}->{'description'},
263         );
264         push @itemtypeloop, \%row;
265     }
266     $template->param(
267         itemtypeloop => \@itemtypeloop,
268         batch_id     => $batch_id,
269         type         => $type,
270     );
271
272 }
273
274 # Print the page
275 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), );
276 output_html_with_http_headers $query, $cookie, $template->output;