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