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