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