Bug 10584 - Hide OPAC biblio details if all items are hidden
[koha.git] / opac / opac-detail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2011 KohaAloha, NZ
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22
23 use strict;
24 use warnings;
25
26 use CGI;
27 use C4::Auth qw(:DEFAULT get_session);
28 use C4::Branch;
29 use C4::Koha;
30 use C4::Serials;    #uses getsubscriptionfrom biblionumber
31 use C4::Output;
32 use C4::Biblio;
33 use C4::Items;
34 use C4::Circulation;
35 use C4::Tags qw(get_tags);
36 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
37 use C4::External::Amazon;
38 use C4::External::Syndetics qw(get_syndetics_index get_syndetics_summary get_syndetics_toc get_syndetics_excerpt get_syndetics_reviews get_syndetics_anotes );
39 use C4::Review;
40 use C4::Ratings;
41 use C4::Members;
42 use C4::VirtualShelves;
43 use C4::XSLT;
44 use C4::ShelfBrowser;
45 use C4::Reserves;
46 use C4::Charset;
47 use MARC::Record;
48 use MARC::Field;
49 use List::MoreUtils qw/any none/;
50 use C4::Images;
51 use Koha::DateUtils;
52 use C4::HTML5Media;
53 use C4::CourseReserves qw(GetItemCourseReservesInfo);
54
55 BEGIN {
56         if (C4::Context->preference('BakerTaylorEnabled')) {
57                 require C4::External::BakerTaylor;
58                 import C4::External::BakerTaylor qw(&image_url &link_url);
59         }
60 }
61
62 my $query = new CGI;
63 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
64     {
65         template_name   => "opac-detail.tmpl",
66         query           => $query,
67         type            => "opac",
68         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
69         flagsrequired   => { borrow => 1 },
70     }
71 );
72
73 my $biblionumber = $query->param('biblionumber') || $query->param('bib') || 0;
74 $biblionumber = int($biblionumber);
75
76 my @all_items = GetItemsInfo($biblionumber);
77 my @hiddenitems;
78 if (scalar @all_items >= 1) {
79     push @hiddenitems, GetHiddenItemnumbers(@all_items);
80
81     if (scalar @hiddenitems == scalar @all_items ) {
82         print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
83         exit;
84     }
85 }
86
87 my $record       = GetMarcBiblio($biblionumber);
88 if ( ! $record ) {
89     print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
90     exit;
91 }
92 $template->param( biblionumber => $biblionumber );
93
94 # get biblionumbers stored in the cart
95 my @cart_list;
96
97 if($query->cookie("bib_list")){
98     my $cart_list = $query->cookie("bib_list");
99     @cart_list = split(/\//, $cart_list);
100     if ( grep {$_ eq $biblionumber} @cart_list) {
101         $template->param( incart => 1 );
102     }
103 }
104
105
106 SetUTF8Flag($record);
107 my $marcflavour      = C4::Context->preference("marcflavour");
108 my $ean = GetNormalizedEAN( $record, $marcflavour );
109
110 # XSLT processing of some stuff
111 if (C4::Context->preference("OPACXSLTDetailsDisplay") ) {
112     $template->param( 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "OPACXSLTDetailsDisplay" ) );
113 }
114
115 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
116 $template->{VARS}->{'OpacBrowseResults'} = $OpacBrowseResults;
117
118 # We look for the busc param to build the simple paging from the search
119 if ($OpacBrowseResults) {
120 my $session = get_session($query->cookie("CGISESSID"));
121 my %paging = (previous => {}, next => {});
122 if ($session->param('busc')) {
123     use C4::Search;
124
125     # Rebuild the string to store on session
126     sub rebuildBuscParam
127     {
128         my $arrParamsBusc = shift;
129
130         my $pasarParams = '';
131         my $j = 0;
132         for (keys %$arrParamsBusc) {
133             if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
134                 if (defined($arrParamsBusc->{$_})) {
135                     $pasarParams .= '&' if ($j);
136                     $pasarParams .= $_ . '=' . $arrParamsBusc->{$_};
137                     $j++;
138                 }
139             } else {
140                 for my $value (@{$arrParamsBusc->{$_}}) {
141                     $pasarParams .= '&' if ($j);
142                     $pasarParams .= $_ . '=' . $value;
143                     $j++;
144                 }
145             }
146         }
147         return $pasarParams;
148     }#rebuildBuscParam
149
150     # Search given the current values from the busc param
151     sub searchAgain
152     {
153         my ($arrParamsBusc, $offset, $results_per_page) = @_;
154
155         my $expanded_facet = $arrParamsBusc->{'expand'};
156         my $branches = GetBranches();
157         my @servers;
158         @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
159         @servers = ("biblioserver") unless (@servers);
160
161         my ($default_sort_by, @sort_by);
162         $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
163         @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
164         $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
165         my ($error, $results_hashref, $facets);
166         eval {
167             ($error, $results_hashref, $facets) = getRecords($arrParamsBusc->{'query'},$arrParamsBusc->{'simple_query'},\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$arrParamsBusc->{'query_type'},$arrParamsBusc->{'scan'});
168         };
169         my $hits;
170         my @newresults;
171         for (my $i=0;$i<@servers;$i++) {
172             my $server = $servers[$i];
173             $hits = $results_hashref->{$server}->{"hits"};
174             @newresults = searchResults('opac', '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
175         }
176         return \@newresults;
177     }#searchAgain
178
179     # Build the current list of biblionumbers in this search
180     sub buildListBiblios
181     {
182         my ($newresultsRef, $results_per_page) = @_;
183
184         my $listBiblios = '';
185         my $j = 0;
186         foreach (@$newresultsRef) {
187             my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
188             $listBiblios .= $bibnum . ',';
189             $j++;
190             last if ($j == $results_per_page);
191         }
192         chop $listBiblios if ($listBiblios =~ /,$/);
193         return $listBiblios;
194     }#buildListBiblios
195
196     my $busc = $session->param("busc");
197     my @arrBusc = split(/\&(?:amp;)?/, $busc);
198     my ($key, $value);
199     my %arrParamsBusc = ();
200     for (@arrBusc) {
201         ($key, $value) = split(/=/, $_, 2);
202         if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
203             $arrParamsBusc{$key} = $value;
204         } else {
205             unless (exists($arrParamsBusc{$key})) {
206                 $arrParamsBusc{$key} = [];
207             }
208             push @{$arrParamsBusc{$key}}, $value;
209         }
210     }
211     my $searchAgain = 0;
212     my $count = C4::Context->preference('OPACnumSearchResults') || 20;
213     my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
214     $arrParamsBusc{'count'} = $results_per_page;
215     my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
216     # The value OPACnumSearchResults has changed and the search has to be rebuild
217     if ($count != $results_per_page) {
218         if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
219             my $indexBiblio = 0;
220             my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
221             for (@arrBibliosAux) {
222                 last if ($_ == $biblionumber);
223                 $indexBiblio++;
224             }
225             $indexBiblio += $offset;
226             $offset = int($indexBiblio / $count) * $count;
227             $arrParamsBusc{'offset'} = $offset;
228         }
229         $arrParamsBusc{'count'} = $count;
230         $results_per_page = $count;
231         my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page);
232         $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
233         delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
234         delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
235         delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
236         delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
237         my $newbusc = rebuildBuscParam(\%arrParamsBusc);
238         $session->param("busc" => $newbusc);
239         @arrBusc = split(/\&(?:amp;)?/, $newbusc);
240     } else {
241         my $modifyListBiblios = 0;
242         # We come from a previous click
243         if (exists($arrParamsBusc{'previous'})) {
244             $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
245             delete $arrParamsBusc{'previous'};
246         } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
247             $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
248             delete $arrParamsBusc{'next'};
249         }
250         if ($modifyListBiblios) {
251             if (exists($arrParamsBusc{'newlistBiblios'})) {
252                 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
253                 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
254                 my @arrAux = split(',', $listBibliosAux);
255                 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
256                 if ($modifyListBiblios == 1) {
257                     $arrParamsBusc{'next'} = $arrAux[0];
258                     $paging{'next'}->{biblionumber} = $arrAux[0];
259                 }else {
260                     $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
261                     $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
262                 }
263             } else {
264                 delete $arrParamsBusc{'listBiblios'};
265             }
266             my $offsetAux = $arrParamsBusc{'offset'};
267             $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
268             $arrParamsBusc{'offsetSearch'} = $offsetAux;
269             $offset = $arrParamsBusc{'offset'};
270             my $newbusc = rebuildBuscParam(\%arrParamsBusc);
271             $session->param("busc" => $newbusc);
272             @arrBusc = split(/\&(?:amp;)?/, $newbusc);
273         }
274     }
275     my $buscParam = '';
276     my $j = 0;
277     # Rebuild the query for the button "back to results"
278     for (@arrBusc) {
279         unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
280             $buscParam .= '&amp;' unless ($j == 0);
281             $buscParam .= $_;
282             $j++;
283         }
284     }
285     $template->param('busc' => $buscParam);
286     my $offsetSearch;
287     my @arrBiblios;
288     # We are inside the list of biblios and we don't have to search
289     if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
290         @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
291         if (@arrBiblios) {
292             # We are at the first item of the list
293             if ($arrBiblios[0] == $biblionumber) {
294                 if (@arrBiblios > 1) {
295                     for (my $j = 1; $j < @arrBiblios; $j++) {
296                         next unless ($arrBiblios[$j]);
297                         $paging{'next'}->{biblionumber} = $arrBiblios[$j];
298                         last;
299                     }
300                 }
301                 # search again if we are not at the first searching list
302                 if ($offset && !$arrParamsBusc{'previous'}) {
303                     $searchAgain = 1;
304                     $offsetSearch = $offset - $results_per_page;
305                 }
306             # we are at the last item of the list
307             } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
308                 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
309                     next unless ($arrBiblios[$j]);
310                     $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
311                     last;
312                 }
313                 if (!$offset) {
314                     # search again if we are at the first list and there is more results
315                     $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
316                 } else {
317                     # search again if we aren't at the first list and there is more results
318                     $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
319                 }
320                 $offsetSearch = $offset + $results_per_page if ($searchAgain);
321             } else {
322                 for (my $j = 1; $j < $#arrBiblios; $j++) {
323                     if ($arrBiblios[$j] == $biblionumber) {
324                         for (my $z = $j - 1; $z >= 0; $z--) {
325                             next unless ($arrBiblios[$z]);
326                             $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
327                             last;
328                         }
329                         for (my $z = $j + 1; $z < @arrBiblios; $z++) {
330                             next unless ($arrBiblios[$z]);
331                             $paging{'next'}->{biblionumber} = $arrBiblios[$z];
332                             last;
333                         }
334                         last;
335                     }
336                 }
337             }
338         }
339         $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
340     }
341     if ($searchAgain) {
342         my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page);
343         my @newresults = @$newresultsRef;
344         # build the new listBiblios
345         my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
346         unless (exists($arrParamsBusc{'listBiblios'})) {
347             $arrParamsBusc{'listBiblios'} = $listBiblios;
348             @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
349         } else {
350             $arrParamsBusc{'newlistBiblios'} = $listBiblios;
351         }
352         # From the new list we build again the next and previous result
353         if (@arrBiblios) {
354             if ($arrBiblios[0] == $biblionumber) {
355                 for (my $j = $#newresults; $j >= 0; $j--) {
356                     next unless ($newresults[$j]);
357                     $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
358                     $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
359                     $arrParamsBusc{'offsetSearch'} = $offsetSearch;
360                    last;
361                 }
362             } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
363                 for (my $j = 0; $j < @newresults; $j++) {
364                     next unless ($newresults[$j]);
365                     $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
366                     $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
367                     $arrParamsBusc{'offsetSearch'} = $offsetSearch;
368                     last;
369                 }
370             }
371         }
372         # build new busc param
373         my $newbusc = rebuildBuscParam(\%arrParamsBusc);
374         $session->param("busc" => $newbusc);
375     }
376     my ($previous, $next, $dataBiblioPaging);
377     # Previous biblio
378     if ($paging{'previous'}->{biblionumber}) {
379         $previous = 'opac-detail.pl?biblionumber=' . $paging{'previous'}->{biblionumber}  . '&query_desc=' . $query->param('query_desc');
380         $dataBiblioPaging = GetBiblioData($paging{'previous'}->{biblionumber});
381         $template->param('previousTitle' => $dataBiblioPaging->{'title'}) if ($dataBiblioPaging);
382     }
383     # Next biblio
384     if ($paging{'next'}->{biblionumber}) {
385         $next = 'opac-detail.pl?biblionumber=' . $paging{'next'}->{biblionumber} . '&query_desc=' . $query->param('query_desc');
386         $dataBiblioPaging = GetBiblioData($paging{'next'}->{biblionumber});
387         $template->param('nextTitle' => $dataBiblioPaging->{'title'}) if ($dataBiblioPaging);
388     }
389     $template->param('previous' => $previous, 'next' => $next);
390     # Partial list of biblio results
391     my @listResults;
392     for (my $j = 0; $j < @arrBiblios; $j++) {
393         next unless ($arrBiblios[$j]);
394         $dataBiblioPaging = GetBiblioData($arrBiblios[$j]) if ($arrBiblios[$j] != $biblionumber);
395         push @listResults, {index => $j + 1 + $offset, biblionumber => $arrBiblios[$j], title => ($arrBiblios[$j] == $biblionumber)?'':$dataBiblioPaging->{title}, author => ($arrBiblios[$j] != $biblionumber && $dataBiblioPaging->{author})?$dataBiblioPaging->{author}:'', url => ($arrBiblios[$j] == $biblionumber)?'':'opac-detail.pl?biblionumber=' . $arrBiblios[$j]};
396     }
397     $template->param('listResults' => \@listResults) if (@listResults);
398     $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
399 }
400 }
401
402
403
404 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
405 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
406
407
408
409 $template->param('OPACShowCheckoutName' => C4::Context->preference("OPACShowCheckoutName") );
410 $template->param('OPACShowBarcode' => C4::Context->preference("OPACShowBarcode") );
411
412 # adding items linked via host biblios
413
414 my $analyticfield = '773';
415 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
416     $analyticfield = '773';
417 } elsif ($marcflavour eq 'UNIMARC') {
418     $analyticfield = '461';
419 }
420 foreach my $hostfield ( $record->field($analyticfield)) {
421     my $hostbiblionumber = $hostfield->subfield("0");
422     my $linkeditemnumber = $hostfield->subfield("9");
423     my @hostitemInfos = GetItemsInfo($hostbiblionumber);
424     foreach my $hostitemInfo (@hostitemInfos){
425         if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
426             push(@all_items, $hostitemInfo);
427         }
428     }
429 }
430
431 my @items;
432
433 # Are there items to hide?
434 my $hideitems;
435 $hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0;
436
437 # Hide items
438 if ($hideitems) {
439     for my $itm (@all_items) {
440         if  ( C4::Context->preference('hidelostitems') ) {
441             push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
442         } else {
443             push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
444     }
445 }
446 } else {
447     # Or not
448     @items = @all_items;
449 }
450
451 my $branches = GetBranches();
452 my $branch = '';
453 if (C4::Context->userenv){
454     $branch = C4::Context->userenv->{branch};
455 }
456 if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) {
457     if (
458         ( ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) && $branch )
459         ||
460         C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch'
461     ) {
462         my $branchname;
463         if ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) {
464             $branchname = $branches->{$branch}->{'branchname'};
465         }
466         elsif (  C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch' ) {
467             $branchname = $branches->{ $ENV{'BRANCHCODE'} }->{'branchname'};
468         }
469
470         my @our_items;
471         my @other_items;
472
473         foreach my $item ( @items ) {
474            if ( $item->{'branchname'} eq $branchname ) {
475                $item->{'this_branch'} = 1;
476                push( @our_items, $item );
477            } else {
478                push( @other_items, $item );
479            }
480         }
481
482         @items = ( @our_items, @other_items );
483     }
484 }
485
486 my $dat = &GetBiblioData($biblionumber);
487
488 my $itemtypes = GetItemTypes();
489 # imageurl:
490 my $itemtype = $dat->{'itemtype'};
491 if ( $itemtype ) {
492     $dat->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
493     $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
494 }
495 my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'}, 'opac');
496 my $collections =  GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'}, 'opac');
497 my $copynumbers = GetKohaAuthorisedValues('items.copynumber',$dat->{'frameworkcode'}, 'opac');
498
499 #coping with subscriptions
500 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
501 my @subscriptions       = GetSubscriptions($dat->{'title'}, $dat->{'issn'}, $ean, $biblionumber );
502
503 my @subs;
504 $dat->{'serial'}=1 if $subscriptionsnumber;
505 foreach my $subscription (@subscriptions) {
506     my $serials_to_display;
507     my %cell;
508     $cell{subscriptionid}    = $subscription->{subscriptionid};
509     $cell{subscriptionnotes} = $subscription->{notes};
510     $cell{missinglist}       = $subscription->{missinglist};
511     $cell{opacnote}          = $subscription->{opacnote};
512     $cell{histstartdate}     = $subscription->{histstartdate};
513     $cell{histenddate}       = $subscription->{histenddate};
514     $cell{branchcode}        = $subscription->{branchcode};
515     $cell{branchname}        = GetBranchName($subscription->{branchcode});
516     $cell{hasalert}          = $subscription->{hasalert};
517     $cell{callnumber}        = $subscription->{callnumber};
518     $cell{closed}            = $subscription->{closed};
519     #get the three latest serials.
520     $serials_to_display = $subscription->{opacdisplaycount};
521     $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
522         $cell{opacdisplaycount} = $serials_to_display;
523     $cell{latestserials} =
524       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
525     push @subs, \%cell;
526 }
527
528 $dat->{'count'} = scalar(@items);
529
530
531 my $biblio_authorised_value_images = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $biblionumber, $record ) );
532
533 my (%item_reserves, %priority);
534 my ($show_holds_count, $show_priority);
535 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
536     m/holds/o and $show_holds_count = 1;
537     m/priority/ and $show_priority = 1;
538 }
539 my $has_hold;
540 if ( $show_holds_count || $show_priority) {
541     my ($reserve_count,$reserves) = GetReservesFromBiblionumber($biblionumber);
542     $template->param( holds_count  => $reserve_count ) if $show_holds_count;
543     foreach (@$reserves) {
544         $item_reserves{ $_->{itemnumber} }++ if $_->{itemnumber};
545         if ($show_priority && $_->{borrowernumber} == $borrowernumber) {
546             $has_hold = 1;
547             $_->{itemnumber}
548                 ? ($priority{ $_->{itemnumber} } = $_->{priority})
549                 : ($template->param( priority => $_->{priority} ));
550         }
551     }
552 }
553 $template->param( show_priority => $has_hold ) ;
554
555 my $norequests = 1;
556 my %itemfields;
557 my (@itemloop, @otheritemloop);
558 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
559 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
560     $template->param(SeparateHoldings => 1);
561 }
562 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
563 for my $itm (@items) {
564     $itm->{holds_count} = $item_reserves{ $itm->{itemnumber} };
565     $itm->{priority} = $priority{ $itm->{itemnumber} };
566     $norequests = 0
567        if ( (not $itm->{'withdrawn'} )
568          && (not $itm->{'itemlost'} )
569          && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'} )
570                  && (not $itemtypes->{$itm->{'itype'}}->{notforloan} )
571          && ($itm->{'itemnumber'} ) );
572
573     # get collection code description, too
574     my $ccode = $itm->{'ccode'};
575     $itm->{'ccode'} = $collections->{$ccode} if ( defined($collections) && exists( $collections->{$ccode} ) );
576     my $copynumber = $itm->{'copynumber'};
577     $itm->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumbers) && defined($copynumber) && exists( $copynumbers->{$copynumber} ) );
578     if ( defined $itm->{'location'} ) {
579         $itm->{'location_description'} = $shelflocations->{ $itm->{'location'} };
580     }
581     if (exists $itm->{itype} && defined($itm->{itype}) && exists $itemtypes->{ $itm->{itype} }) {
582         $itm->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
583         $itm->{'description'} = $itemtypes->{ $itm->{itype} }->{'description'};
584     }
585     foreach (qw(ccode enumchron copynumber itemnotes uri)) {
586         $itemfields{$_} = 1 if ($itm->{$_});
587     }
588
589      # walk through the item-level authorised values and populate some images
590      my $item_authorised_value_images = C4::Items::get_authorised_value_images( C4::Items::get_item_authorised_values( $itm->{'itemnumber'} ) );
591      # warn( Data::Dumper->Dump( [ $item_authorised_value_images ], [ 'item_authorised_value_images' ] ) );
592
593      if ( $itm->{'itemlost'} ) {
594          my $lostimageinfo = List::Util::first { $_->{'category'} eq 'LOST' } @$item_authorised_value_images;
595          $itm->{'lostimageurl'}   = $lostimageinfo->{ 'imageurl' };
596          $itm->{'lostimagelabel'} = $lostimageinfo->{ 'label' };
597      }
598      my $reserve_status = C4::Reserves::GetReserveStatus($itm->{itemnumber});
599       if( $reserve_status eq "Waiting"){ $itm->{'waiting'} = 1; }
600       if( $reserve_status eq "Reserved"){ $itm->{'onhold'} = 1; }
601     
602      my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
603      if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
604         $itm->{transfertwhen} = $transfertwhen;
605         $itm->{transfertfrom} = $branches->{$transfertfrom}{branchname};
606         $itm->{transfertto}   = $branches->{$transfertto}{branchname};
607      }
608     my $itembranch = $itm->{$separatebranch};
609     if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
610         if ($itembranch and $itembranch eq $currentbranch) {
611             push @itemloop, $itm;
612         } else {
613             push @otheritemloop, $itm;
614         }
615     } else {
616         push @itemloop, $itm;
617     }
618 }
619
620 # Display only one tab if one items list is empty
621 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
622     $template->param(SeparateHoldings => 0);
623     if (scalar(@itemloop) == 0) {
624         @itemloop = @otheritemloop;
625     }
626 }
627
628 # If there is a lot of items, and the user has not decided
629 # to view them all yet, we first warn him
630 # TODO: The limit of 50 could be a syspref
631 my $viewallitems = $query->param('viewallitems');
632 if (scalar(@itemloop) >= 50 && !$viewallitems) {
633     $template->param('lotsofitems' => 1);
634 }
635
636 ## get notes and subjects from MARC record
637 my $dbh              = C4::Context->dbh;
638 my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
639 my $marcisbnsarray   = GetMarcISBN    ($record,$marcflavour);
640 my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
641 my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
642 my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
643 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
644 my $marchostsarray  = GetMarcHosts($record,$marcflavour);
645 my $subtitle         = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber));
646
647     $template->param(
648                      MARCNOTES               => $marcnotesarray,
649                      MARCSUBJCTS             => $marcsubjctsarray,
650                      MARCAUTHORS             => $marcauthorsarray,
651                      MARCSERIES              => $marcseriesarray,
652                      MARCURLS                => $marcurlsarray,
653                      MARCISBNS               => $marcisbnsarray,
654                      MARCHOSTS               => $marchostsarray,
655                      norequests              => $norequests,
656                      RequestOnOpac           => C4::Context->preference("RequestOnOpac"),
657                      itemdata_ccode          => $itemfields{ccode},
658                      itemdata_enumchron      => $itemfields{enumchron},
659                      itemdata_uri            => $itemfields{uri},
660                      itemdata_copynumber     => $itemfields{copynumber},
661                      itemdata_itemnotes          => $itemfields{itemnotes},
662                      authorised_value_images => $biblio_authorised_value_images,
663                      subtitle                => $subtitle,
664                      OpacStarRatings         => C4::Context->preference("OpacStarRatings"),
665     );
666
667 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
668     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
669     my $subfields = substr $fieldspec, 3;
670     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
671     my @alternateholdingsinfo = ();
672     my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
673
674     for my $field (@holdingsfields) {
675         my %holding = ( holding => '' );
676         my $havesubfield = 0;
677         for my $subfield ($field->subfields()) {
678             if ((index $subfields, $$subfield[0]) >= 0) {
679                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
680                 $holding{'holding'} .= $$subfield[1];
681                 $havesubfield++;
682             }
683         }
684         if ($havesubfield) {
685             push(@alternateholdingsinfo, \%holding);
686         }
687     }
688
689     $template->param(
690         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
691         );
692 }
693
694 foreach ( keys %{$dat} ) {
695     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
696 }
697
698 # some useful variables for enhanced content;
699 # in each case, we're grabbing the first value we find in
700 # the record and normalizing it
701 my $upc = GetNormalizedUPC($record,$marcflavour);
702 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
703 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
704 my $content_identifier_exists;
705 if ( $isbn or $ean or $oclc or $upc ) {
706     $content_identifier_exists = 1;
707 }
708 $template->param(
709         normalized_upc => $upc,
710         normalized_ean => $ean,
711         normalized_oclc => $oclc,
712         normalized_isbn => $isbn,
713         content_identifier_exists =>  $content_identifier_exists,
714 );
715
716 # COinS format FIXME: for books Only
717 $template->param(
718     ocoins => GetCOinSBiblio($record),
719 );
720
721 my $libravatar_enabled = 0;
722 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto')) {
723     eval {
724         require Libravatar::URL;
725         Libravatar::URL->import();
726     };
727     if (!$@ ) {
728         $libravatar_enabled = 1;
729     }
730 }
731
732 my $reviews = getreviews( $biblionumber, 1 );
733 my $loggedincommenter;
734
735
736
737
738 foreach ( @$reviews ) {
739     my $borrowerData   = GetMember('borrowernumber' => $_->{borrowernumber});
740     # setting some borrower info into this hash
741     $_->{title}     = $borrowerData->{'title'};
742     $_->{surname}   = $borrowerData->{'surname'};
743     $_->{firstname} = $borrowerData->{'firstname'};
744     if ($libravatar_enabled and $borrowerData->{'email'}) {
745         $_->{avatarurl} = libravatar_url(email => $borrowerData->{'email'}, https => $ENV{HTTPS});
746     }
747     $_->{userid}    = $borrowerData->{'userid'};
748     $_->{cardnumber}    = $borrowerData->{'cardnumber'};
749
750     if ($borrowerData->{'borrowernumber'} eq $borrowernumber) {
751                 $_->{your_comment} = 1;
752                 $loggedincommenter = 1;
753         }
754 }
755
756
757 if(C4::Context->preference("ISBD")) {
758         $template->param(ISBD => 1);
759 }
760
761 $template->param(
762     itemloop            => \@itemloop,
763     otheritemloop       => \@otheritemloop,
764     subscriptionsnumber => $subscriptionsnumber,
765     biblionumber        => $biblionumber,
766     subscriptions       => \@subs,
767     subscriptionsnumber => $subscriptionsnumber,
768     reviews             => $reviews,
769     loggedincommenter   => $loggedincommenter
770 );
771
772 # Lists
773
774 if (C4::Context->preference("virtualshelves") ) {
775    $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
776 }
777
778
779 # XISBN Stuff
780 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
781     eval {
782         $template->param(
783             XISBNS => get_xisbns($isbn)
784         );
785     };
786     if ($@) { warn "XISBN Failed $@"; }
787 }
788
789 # Serial Collection
790 my @sc_fields = $record->field(955);
791 my @lc_fields = $marcflavour eq 'UNIMARC'
792     ? $record->field(930)
793     : $record->field(852);
794 my @serialcollections = ();
795
796 foreach my $sc_field (@sc_fields) {
797     my %row_data;
798
799     $row_data{text}    = $sc_field->subfield('r');
800     $row_data{branch}  = $sc_field->subfield('9');
801     foreach my $lc_field (@lc_fields) {
802         $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
803             ? $lc_field->subfield('a') # 930$a
804             : $lc_field->subfield('h') # 852$h
805             if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
806     }
807
808     if ($row_data{text} && $row_data{branch}) { 
809         push (@serialcollections, \%row_data);
810     }
811 }
812
813 if (scalar(@serialcollections) > 0) {
814     $template->param(
815         serialcollection  => 1,
816         serialcollections => \@serialcollections);
817 }
818
819 # Local cover Images stuff
820 if (C4::Context->preference("OPACLocalCoverImages")){
821                 $template->param(OPACLocalCoverImages => 1);
822 }
823
824 # HTML5 Media
825 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
826     $template->param( C4::HTML5Media->gethtml5media($record));
827 }
828
829 my $syndetics_elements;
830
831 if ( C4::Context->preference("SyndeticsEnabled") ) {
832     $template->param("SyndeticsEnabled" => 1);
833     $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
834         eval {
835             $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
836             for my $element (values %$syndetics_elements) {
837                 $template->param("Syndetics$element"."Exists" => 1 );
838                 #warn "Exists: "."Syndetics$element"."Exists";
839         }
840     };
841     warn $@ if $@;
842 }
843
844 if ( C4::Context->preference("SyndeticsEnabled")
845         && C4::Context->preference("SyndeticsSummary")
846         && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
847         eval {
848             my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
849             $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
850         };
851         warn $@ if $@;
852
853 }
854
855 if ( C4::Context->preference("SyndeticsEnabled")
856         && C4::Context->preference("SyndeticsTOC")
857         && exists($syndetics_elements->{'TOC'}) ) {
858         eval {
859     my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
860     $template->param( SYNDETICS_TOC => $syndetics_toc );
861         };
862         warn $@ if $@;
863 }
864
865 if ( C4::Context->preference("SyndeticsEnabled")
866     && C4::Context->preference("SyndeticsExcerpt")
867     && exists($syndetics_elements->{'DBCHAPTER'}) ) {
868     eval {
869     my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
870     $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
871     };
872         warn $@ if $@;
873 }
874
875 if ( C4::Context->preference("SyndeticsEnabled")
876     && C4::Context->preference("SyndeticsReviews")) {
877     eval {
878     my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
879     $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
880     };
881         warn $@ if $@;
882 }
883
884 if ( C4::Context->preference("SyndeticsEnabled")
885     && C4::Context->preference("SyndeticsAuthorNotes")
886         && exists($syndetics_elements->{'ANOTES'}) ) {
887     eval {
888     my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
889     $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
890     };
891     warn $@ if $@;
892 }
893
894 # LibraryThingForLibraries ID Code and Tabbed View Option
895 if( C4::Context->preference('LibraryThingForLibrariesEnabled') ) 
896
897 $template->param(LibraryThingForLibrariesID =>
898 C4::Context->preference('LibraryThingForLibrariesID') ); 
899 $template->param(LibraryThingForLibrariesTabbedView =>
900 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
901
902
903 # Novelist Select
904 if( C4::Context->preference('NovelistSelectEnabled') ) 
905
906 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') ); 
907 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') ); 
908 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') ); 
909
910
911
912 # Babelthèque
913 if ( C4::Context->preference("Babeltheque") ) {
914     $template->param( 
915         Babeltheque => 1,
916         Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
917     );
918 }
919
920 # Social Networks
921 if ( C4::Context->preference( "SocialNetworks" ) ) {
922     $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
923     $template->param( SocialNetworks => 1 );
924 }
925
926 # Shelf Browser Stuff
927 if (C4::Context->preference("OPACShelfBrowser")) {
928     my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
929     if (defined($starting_itemnumber)) {
930         $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
931         my $nearby = GetNearbyItems($starting_itemnumber,3);
932
933         $template->param(
934             starting_homebranch => $nearby->{starting_homebranch}->{description},
935             starting_location => $nearby->{starting_location}->{description},
936             starting_ccode => $nearby->{starting_ccode}->{description},
937             starting_itemnumber => $nearby->{starting_itemnumber},
938             shelfbrowser_prev_itemnumber => $nearby->{prev_itemnumber},
939             shelfbrowser_next_itemnumber => $nearby->{next_itemnumber},
940             shelfbrowser_prev_biblionumber => $nearby->{prev_biblionumber},
941             shelfbrowser_next_biblionumber => $nearby->{next_biblionumber},
942             PREVIOUS_SHELF_BROWSE => $nearby->{prev},
943             NEXT_SHELF_BROWSE => $nearby->{next},
944         );
945
946         # in which tab shelf browser should open ?
947         if (grep { $starting_itemnumber == $_->{itemnumber} } @itemloop) {
948             $template->param(shelfbrowser_tab => 'holdings');
949         } else {
950             $template->param(shelfbrowser_tab => 'otherholdings');
951         }
952     }
953 }
954
955 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
956
957 if (C4::Context->preference("BakerTaylorEnabled")) {
958         $template->param(
959                 BakerTaylorEnabled  => 1,
960                 BakerTaylorImageURL => &image_url(),
961                 BakerTaylorLinkURL  => &link_url(),
962                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
963         );
964         my ($bt_user, $bt_pass);
965         if ($isbn and
966                 $bt_user = C4::Context->preference('BakerTaylorUsername') and
967                 $bt_pass = C4::Context->preference('BakerTaylorPassword')    )
968         {
969                 $template->param(
970                 BakerTaylorContentURL   =>
971                 sprintf("http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
972                                 $bt_user,$bt_pass,$isbn)
973                 );
974         }
975 }
976
977 my $tag_quantity;
978 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
979         $template->param(
980                 TagsEnabled => 1,
981                 TagsShowOnDetail => $tag_quantity,
982                 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
983         );
984         $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
985                                                                 'sort'=>'-weight', limit=>$tag_quantity}));
986 }
987
988 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
989     # These values are going to be read by Javascript, at least in the case
990     # of the google covers
991     $template->param(covernewwindow => 'true');
992 } else {
993     $template->param(covernewwindow => 'false');
994 }
995
996 #Export options
997 my $OpacExportOptions=C4::Context->preference("OpacExportOptions");
998 my @export_options = split(/\|/,$OpacExportOptions);
999 $template->{VARS}->{'export_options'} = \@export_options;
1000
1001 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1002     my $rating = GetRating( $biblionumber, $borrowernumber );
1003     $template->param(
1004         rating_value   => $rating->{'rating_value'},
1005         rating_total   => $rating->{'rating_total'},
1006         rating_avg     => $rating->{'rating_avg'},
1007         rating_avg_int => $rating->{'rating_avg_int'},
1008         borrowernumber => $borrowernumber
1009     );
1010 }
1011
1012 #Search for title in links
1013 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
1014 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1015 my $issn = $marcissns->[0] || '';
1016
1017 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1018     $dat->{title} =~ s/\/+$//; # remove trailing slash
1019     $dat->{title} =~ s/\s+$//; # remove trailing space
1020     $search_for_title = parametrized_url(
1021         $search_for_title,
1022         {
1023             TITLE         => $dat->{title},
1024             AUTHOR        => $dat->{author},
1025             ISBN          => $isbn,
1026             ISSN          => $issn,
1027             CONTROLNUMBER => $marccontrolnumber,
1028             BIBLIONUMBER  => $biblionumber,
1029         }
1030     );
1031     $template->param('OPACSearchForTitleIn' => $search_for_title);
1032 }
1033
1034 # We try to select the best default tab to show, according to what
1035 # the user wants, and what's available for display
1036 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1037 my $defaulttab = 
1038     $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1039         ? 'subscriptions' :
1040     $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1041         ? 'serialcollection' :
1042     $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1043         ? 'holdings' :
1044     $subscriptionsnumber
1045         ? 'subscriptions' :
1046     @serialcollections > 0 
1047         ? 'serialcollection' : 'subscriptions';
1048 $template->param('defaulttab' => $defaulttab);
1049
1050 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1051     my @images = ListImagesForBiblio($biblionumber);
1052     $template->{VARS}->{localimages} = \@images;
1053 }
1054
1055 $template->{VARS}->{IDreamBooksReviews} = C4::Context->preference('IDreamBooksReviews');
1056 $template->{VARS}->{IDreamBooksReadometer} = C4::Context->preference('IDreamBooksReadometer');
1057 $template->{VARS}->{IDreamBooksResults} = C4::Context->preference('IDreamBooksResults');
1058 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1059
1060 if (C4::Context->preference('OpacHighlightedWords')) {
1061     $template->{VARS}->{query_desc} = $query->param('query_desc');
1062 }
1063 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1064
1065 if ( C4::Context->preference('UseCourseReserves') ) {
1066     foreach my $i ( @items ) {
1067         $i->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $i->{'itemnumber'} );
1068     }
1069 }
1070
1071 output_html_with_http_headers $query, $cookie, $template->output;