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