Bug 29939: Use the REST API for ratings
[srvgit] / 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 $reserve_status =
733           C4::Reserves::GetReserveStatus( $item->itemnumber );
734         if ( $reserve_status eq "Waiting"  ) { $item_info->{'waiting'} = 1; }
735         if ( $reserve_status eq "Reserved" ) { $item_info->{'onhold'}  = 1; }
736
737         my ( $transfertwhen, $transfertfrom, $transfertto ) =
738           GetTransfers( $item->itemnumber );
739         if ( defined($transfertwhen) && $transfertwhen ne '' ) {
740             $item_info->{transfertwhen} = $transfertwhen;
741             $item_info->{transfertfrom} = $transfertfrom;
742             $item_info->{transfertto}   = $transfertto;
743         }
744
745         if ( C4::Context->preference('OPACAcquisitionDetails') ) {
746             $item_info->{on_order} = 1
747               if grep { $_ eq $item->itemnumber } @itemnumbers_on_order;
748         }
749
750         if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
751             $item_info->{cover_images} = $item->cover_images;
752         }
753
754
755         if ( $item->in_bundle ) {
756             $item_info->{bundle_host} = $item->bundle_host;
757         }
758
759         if ( C4::Context->preference('UseCourseReserves') ) {
760             $item_info->{course_reserves} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
761         }
762
763         $item_info->{holding_branch} = $item->holding_branch;
764         $item_info->{home_branch}    = $item->home_branch;
765
766         my $itembranch = $item->$separatebranch;
767         if ( $currentbranch
768             and C4::Context->preference('OpacSeparateHoldings') )
769         {
770             if ( $itembranch and $itembranch eq $currentbranch ) {
771                 push @itemloop, $item_info;
772                 $itemloop_has_images++ if $item->cover_images->count;
773             }
774             else {
775                 push @otheritemloop, $item_info;
776                 $otheritemloop_has_images++ if $item->cover_images->count;
777             }
778         }
779         else {
780             push @itemloop, $item_info;
781             $itemloop_has_images++ if $item->cover_images->count;
782         }
783     }
784 }
785
786 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
787     $template->param( ReservableItems => 1 );
788 }
789
790 $template->param(
791     itemloop_has_images      => $itemloop_has_images,
792     otheritemloop_has_images => $otheritemloop_has_images,
793 );
794
795 # Display only one tab if one items list is empty
796 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
797     $template->param(SeparateHoldings => 0);
798     if (scalar(@itemloop) == 0) {
799         @itemloop = @otheritemloop;
800     }
801 }
802
803 my $marcnotesarray = $biblio->get_marc_notes({ opac => 1, record => $record });
804
805 if( C4::Context->preference('ArticleRequests') ) {
806     my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
807     my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
808     my $artreqpossible = $patron
809         ? $biblio->can_article_request( $patron )
810         : $itemtype
811         ? $itemtype->may_article_request
812         : q{};
813     $template->param( artreqpossible => $artreqpossible );
814 }
815
816 my $norequests = ! $biblio->items->filter_by_for_hold->count;
817     $template->param(
818                      MARCNOTES               => $marcnotesarray,
819                      norequests              => $norequests,
820                      itemdata_ccode          => $itemfields{ccode},
821                      itemdata_materials      => $itemfields{materials},
822                      itemdata_enumchron      => $itemfields{enumchron},
823                      itemdata_uri            => $itemfields{uri},
824                      itemdata_copynumber     => $itemfields{copynumber},
825                      itemdata_itemnotes      => $itemfields{itemnotes},
826                      itemdata_location       => $itemfields{location_description},
827                      OpacStarRatings         => C4::Context->preference("OpacStarRatings"),
828     );
829
830 if (C4::Context->preference("AlternateHoldingsField") && $items->count == 0) {
831     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
832     my $subfields = substr $fieldspec, 3;
833     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
834     my @alternateholdingsinfo = ();
835     my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
836
837     for my $field (@holdingsfields) {
838         my %holding = ( holding => '' );
839         my $havesubfield = 0;
840         for my $subfield ($field->subfields()) {
841             if ((index $subfields, $$subfield[0]) >= 0) {
842                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
843                 $holding{'holding'} .= $$subfield[1];
844                 $havesubfield++;
845             }
846         }
847         if ($havesubfield) {
848             push(@alternateholdingsinfo, \%holding);
849         }
850     }
851
852     $template->param(
853         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
854         );
855 }
856
857 # FIXME: The template uses this hash directly. Need to filter.
858 foreach ( keys %{$dat} ) {
859     next if ( $HideMARC->{$_} );
860     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
861 }
862
863 # some useful variables for enhanced content;
864 # in each case, we're grabbing the first value we find in
865 # the record and normalizing it
866 my $upc = GetNormalizedUPC($record,$marcflavour);
867 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
868 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
869 my $content_identifier_exists;
870 if ( $isbn or $ean or $oclc or $upc ) {
871     $content_identifier_exists = 1;
872 }
873 $template->param(
874         normalized_upc => $upc,
875         normalized_ean => $ean,
876         normalized_oclc => $oclc,
877         normalized_isbn => $isbn,
878         content_identifier_exists =>  $content_identifier_exists,
879 );
880
881 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
882 # COinS format FIXME: for books Only
883 my $coins = eval { $biblio->get_coins };
884 $template->param( ocoins => $coins );
885
886 my ( $loggedincommenter, $reviews );
887 if ( C4::Context->preference('OPACComments') ) {
888     $reviews = Koha::Reviews->search(
889         {
890             biblionumber => $biblionumber,
891             -or => { approved => 1, borrowernumber => $borrowernumber }
892         },
893         {
894             order_by => { -desc => 'datereviewed' }
895         }
896     )->unblessed;
897     my $libravatar_enabled = 0;
898     if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
899         eval {
900             require Libravatar::URL;
901             Libravatar::URL->import();
902         };
903         if ( !$@ ) {
904             $libravatar_enabled = 1;
905         }
906     }
907     for my $review (@$reviews) {
908         my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
909
910         # setting some borrower info into this hash
911         if ( $review_patron ) {
912             $review->{patron} = $review_patron;
913             if ( $libravatar_enabled and $review_patron->email ) {
914                 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
915             }
916
917             if ( $review_patron->borrowernumber eq $borrowernumber ) {
918                 $loggedincommenter = 1;
919             }
920         }
921     }
922 }
923
924 if ( C4::Context->preference("OPACISBD") ) {
925     $template->param( ISBD => 1 );
926 }
927
928 $template->param(
929     itemloop            => \@itemloop,
930     otheritemloop       => \@otheritemloop,
931     biblionumber        => $biblionumber,
932     subscriptions       => \@subs,
933     subscriptionsnumber => $subscriptionsnumber,
934     reviews             => $reviews,
935     loggedincommenter   => $loggedincommenter
936 );
937
938 # Lists
939 if (C4::Context->preference("virtualshelves") ) {
940     my $shelves = Koha::Virtualshelves->search(
941         {
942             biblionumber => $biblionumber,
943             public       => 1,
944         },
945         {
946             join => 'virtualshelfcontents',
947         }
948     );
949     $template->param( shelves => $shelves );
950 }
951
952 # XISBN Stuff
953 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
954     eval {
955         $template->param(
956             XISBNS => scalar get_xisbns($isbn, $biblionumber)
957         );
958     };
959     if ($@) { warn "XISBN Failed $@"; }
960 }
961
962 # Serial Collection
963 my @sc_fields = $record->field(955);
964 my @lc_fields = $marcflavour eq 'UNIMARC'
965     ? $record->field(930)
966     : $record->field(852);
967 my @serialcollections = ();
968
969 foreach my $sc_field (@sc_fields) {
970     my %row_data;
971
972     $row_data{text}    = $sc_field->subfield('r');
973     $row_data{branch}  = $sc_field->subfield('9');
974     foreach my $lc_field (@lc_fields) {
975         $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
976             ? $lc_field->subfield('a') # 930$a
977             : $lc_field->subfield('h') # 852$h
978             if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
979     }
980
981     if ($row_data{text} && $row_data{branch}) { 
982         push (@serialcollections, \%row_data);
983     }
984 }
985
986 if (scalar(@serialcollections) > 0) {
987     $template->param(
988         serialcollection  => 1,
989         serialcollections => \@serialcollections);
990 }
991
992 # Local cover Images stuff
993 if (C4::Context->preference("OPACLocalCoverImages")){
994                 $template->param(OPACLocalCoverImages => 1);
995 }
996
997 # HTML5 Media
998 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
999     $template->param( C4::HTML5Media->gethtml5media($record));
1000 }
1001
1002 my $syndetics_elements;
1003
1004 if ( C4::Context->preference("SyndeticsEnabled") ) {
1005     $template->param("SyndeticsEnabled" => 1);
1006     $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
1007         eval {
1008             $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
1009             for my $element (values %$syndetics_elements) {
1010                 $template->param("Syndetics$element"."Exists" => 1 );
1011                 #warn "Exists: "."Syndetics$element"."Exists";
1012         }
1013     };
1014     warn $@ if $@;
1015 }
1016
1017 if ( C4::Context->preference("SyndeticsEnabled")
1018         && C4::Context->preference("SyndeticsSummary")
1019         && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1020         eval {
1021             my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1022             $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1023         };
1024         warn $@ if $@;
1025
1026 }
1027
1028 if ( C4::Context->preference("SyndeticsEnabled")
1029         && C4::Context->preference("SyndeticsTOC")
1030         && exists($syndetics_elements->{'TOC'}) ) {
1031         eval {
1032     my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1033     $template->param( SYNDETICS_TOC => $syndetics_toc );
1034         };
1035         warn $@ if $@;
1036 }
1037
1038 if ( C4::Context->preference("SyndeticsEnabled")
1039     && C4::Context->preference("SyndeticsExcerpt")
1040     && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1041     eval {
1042     my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1043     $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1044     };
1045         warn $@ if $@;
1046 }
1047
1048 if ( C4::Context->preference("SyndeticsEnabled")
1049     && C4::Context->preference("SyndeticsReviews")) {
1050     eval {
1051     my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1052     $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1053     };
1054         warn $@ if $@;
1055 }
1056
1057 if ( C4::Context->preference("SyndeticsEnabled")
1058     && C4::Context->preference("SyndeticsAuthorNotes")
1059         && exists($syndetics_elements->{'ANOTES'}) ) {
1060     eval {
1061     my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1062     $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1063     };
1064     warn $@ if $@;
1065 }
1066
1067 # LibraryThingForLibraries ID Code and Tabbed View Option
1068 if( C4::Context->preference('LibraryThingForLibrariesEnabled') ) 
1069
1070 $template->param(LibraryThingForLibrariesID =>
1071 C4::Context->preference('LibraryThingForLibrariesID') ); 
1072 $template->param(LibraryThingForLibrariesTabbedView =>
1073 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1074
1075
1076 # Novelist Select
1077 if( C4::Context->preference('NovelistSelectEnabled') ) 
1078
1079 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') ); 
1080 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') ); 
1081 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') ); 
1082
1083
1084
1085 # Babelthèque
1086 if ( C4::Context->preference("Babeltheque") ) {
1087     $template->param( 
1088         Babeltheque => 1,
1089         Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1090     );
1091 }
1092
1093 # Social Networks
1094 if ( C4::Context->preference( "SocialNetworks" ) ) {
1095     $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1096     $template->param( SocialNetworks => 1 );
1097 }
1098
1099 # Shelf Browser Stuff
1100 if (C4::Context->preference("OPACShelfBrowser")) {
1101     my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1102     if (defined($starting_itemnumber)) {
1103         $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1104         my $nearby = GetNearbyItems($starting_itemnumber);
1105
1106         $template->param(
1107             starting_itemnumber => $starting_itemnumber,
1108             starting_homebranch => $nearby->{starting_homebranch}->{description},
1109             starting_location => $nearby->{starting_location}->{description},
1110             starting_ccode => $nearby->{starting_ccode}->{description},
1111             shelfbrowser_prev_item => $nearby->{prev_item},
1112             shelfbrowser_next_item => $nearby->{next_item},
1113             shelfbrowser_items => $nearby->{items},
1114         );
1115
1116         # in which tab shelf browser should open ?
1117         if (grep { $starting_itemnumber == $_->itemnumber } @itemloop) {
1118             $template->param(shelfbrowser_tab => 'holdings');
1119         } else {
1120             $template->param(shelfbrowser_tab => 'otherholdings');
1121         }
1122     }
1123 }
1124
1125 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1126
1127 if (C4::Context->preference("BakerTaylorEnabled")) {
1128         $template->param(
1129                 BakerTaylorEnabled  => 1,
1130                 BakerTaylorImageURL => &image_url(),
1131                 BakerTaylorLinkURL  => &link_url(),
1132                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1133         );
1134         my ($bt_user, $bt_pass);
1135         if ($isbn and
1136                 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1137                 $bt_pass = C4::Context->preference('BakerTaylorPassword')    )
1138         {
1139                 $template->param(
1140                 BakerTaylorContentURL   =>
1141         sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1142                                 $bt_user,$bt_pass,$isbn)
1143                 );
1144         }
1145 }
1146
1147 my $tag_quantity;
1148 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1149         $template->param(
1150                 TagsEnabled => 1,
1151                 TagsShowOnDetail => $tag_quantity,
1152                 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1153         );
1154         $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1155                                                                 'sort'=>'-weight', limit=>$tag_quantity}));
1156 }
1157
1158 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1159     # These values are going to be read by Javascript, at least in the case
1160     # of the google covers
1161     $template->param(covernewwindow => 'true');
1162 } else {
1163     $template->param(covernewwindow => 'false');
1164 }
1165
1166 $template->param(borrowernumber => $borrowernumber);
1167
1168 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1169     my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1170     my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1171     $template->param(
1172         ratings => $ratings,
1173         my_rating => $my_rating,
1174     );
1175 }
1176
1177 #Search for title in links
1178 my $marccontrolnumber   = GetMarcControlnumber ($record, $marcflavour);
1179 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1180 my $issn = $marcissns->[0] || '';
1181
1182 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1183     $dat->{title} =~ s/\/+$//; # remove trailing slash
1184     $dat->{title} =~ s/\s+$//; # remove trailing space
1185     my $oclc_no = Koha::Util::MARC::oclc_number( $record );
1186     $search_for_title = parametrized_url(
1187         $search_for_title,
1188         {
1189             TITLE         => $dat->{title},
1190             AUTHOR        => $dat->{author},
1191             ISBN          => $isbn,
1192             ISSN          => $issn,
1193             CONTROLNUMBER => $marccontrolnumber,
1194             BIBLIONUMBER  => $biblionumber,
1195             OCLC_NO       => $oclc_no,
1196         }
1197     );
1198     $template->param('OPACSearchForTitleIn' => $search_for_title);
1199 }
1200
1201 #IDREF
1202 if ( C4::Context->preference("IDREF") ) {
1203     # If the record comes from the SUDOC
1204     if ( $record->field('009') ) {
1205         my $unimarc3 = $record->field("009")->data;
1206         if ( $unimarc3 =~ /^\d+$/ ) {
1207             $template->param(
1208                 IDREF => 1,
1209             );
1210         }
1211     }
1212 }
1213
1214 # We try to select the best default tab to show, according to what
1215 # the user wants, and what's available for display
1216 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1217 my $defaulttab = 
1218     $viewallitems
1219         ? 'holdings' :
1220     $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1221         ? 'subscriptions' :
1222     $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1223         ? 'serialcollection' :
1224     $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1225         ? 'holdings' :
1226     ( $showcomp eq 'both' || $showcomp eq 'opac' ) && scalar (@itemloop) == 0 && $parts
1227         ? 'components' :
1228     scalar (@itemloop) == 0
1229         ? 'media' :
1230     $subscriptionsnumber
1231         ? 'subscriptions' :
1232     @serialcollections > 0 
1233         ? 'serialcollection' : 'subscriptions';
1234 $template->param('defaulttab' => $defaulttab);
1235
1236 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1237     $template->param( localimages => $biblio->cover_images );
1238 }
1239
1240 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1241
1242 if (C4::Context->preference('OpacHighlightedWords')) {
1243     $template->{VARS}->{query_desc} = $query->param('query_desc');
1244 }
1245 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1246
1247 $template->param(
1248     'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1249 );
1250
1251 if ( C4::Context->preference('OPACAuthorIdentifiers') ) {
1252     my @author_identifiers;
1253     for my $author ( @{ $biblio->get_marc_authors } ) {
1254         my $authid    = $author->{authoritylink};
1255         my $authority = Koha::Authorities->find($authid);
1256         next unless $authority;
1257         my $identifiers = $authority->get_identifiers;
1258         next unless $identifiers && @$identifiers;
1259         my ($name) =
1260           map  { $_->{value} }
1261           grep { $_->{code} eq 'a' ? $_ : () }
1262           @{ $author->{MARCAUTHOR_SUBFIELDS_LOOP} };
1263         push @author_identifiers,
1264           { authid => $authid, name => $name, identifiers => $identifiers };
1265     }
1266     $template->param( author_identifiers => \@author_identifiers );
1267 }
1268
1269 output_html_with_http_headers $query, $cookie, $template->output;