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