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