3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2011 KohaAloha, NZ
7 # This file is part of Koha.
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.
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.
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>.
26 use C4::Acquisition qw( SearchOrders );
27 use C4::Auth qw(:DEFAULT get_session);
29 use C4::Serials; #uses getsubscriptionfrom biblionumber
34 use C4::Tags qw(get_tags);
35 use C4::XISBN qw(get_xisbns);
36 use C4::External::Amazon;
37 use C4::External::BakerTaylor qw( image_url link_url );
38 use C4::External::Syndetics qw(get_syndetics_index get_syndetics_summary get_syndetics_toc get_syndetics_excerpt get_syndetics_reviews get_syndetics_anotes );
47 use List::MoreUtils qw/any none/;
50 use C4::CourseReserves qw(GetItemCourseReservesInfo);
53 use Koha::RecordProcessor;
54 use Koha::AuthorisedValues;
55 use Koha::CirculationRules;
58 use Koha::Acquisition::Orders;
59 use Koha::Virtualshelves;
64 use Koha::SearchEngine::Search;
65 use Koha::SearchEngine::QueryBuilder;
69 my $query = CGI->new();
71 my $biblionumber = $query->param('biblionumber') || $query->param('bib') || 0;
72 $biblionumber = int($biblionumber);
74 my $specific_item = $query->param('itemnumber') ? Koha::Items->find( scalar $query->param('itemnumber') ) : undef;
75 $biblionumber = $specific_item->biblionumber if $specific_item;
77 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
79 template_name => "opac-detail.tt",
82 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
86 my @all_items = GetItemsInfo($biblionumber);
87 if( $specific_item ) {
88 @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items;
89 $template->param( specific_item => 1 );
92 my $patron = Koha::Patrons->find( $borrowernumber );
94 my $record = GetMarcBiblio({
95 biblionumber => $biblionumber,
98 print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
102 my $biblio = Koha::Biblios->find( $biblionumber );
103 unless ( $patron and $patron->category->override_hidden_items ) {
104 # only skip this check if there's a logged in user
105 # and its category overrides OpacHiddenItems
106 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
107 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
110 if ( scalar @all_items >= 1 ) {
112 GetHiddenItemnumbers( { items => \@all_items, borcat => $patron ? $patron->categorycode : undef } );
116 my $framework = $biblio ? $biblio->frameworkcode : q{};
117 my $record_processor = Koha::RecordProcessor->new({
118 filters => 'ViewPolicy',
121 frameworkcode => $framework
124 $record_processor->process($record);
126 # redirect if opacsuppression is enabled and biblio is suppressed
127 if (C4::Context->preference('OpacSuppression')) {
128 # FIXME hardcoded; the suppression flag ought to be materialized
129 # as a column on biblio or the like
130 my $opacsuppressionfield = '942';
131 my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
132 # redirect to opac-blocked info page or 404?
133 my $opacsuppressionredirect;
134 if ( C4::Context->preference("OpacSuppressionRedirect") ) {
135 $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
137 $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
139 if ( $opacsuppressionfieldvalue &&
140 $opacsuppressionfieldvalue->subfield("n") &&
141 $opacsuppressionfieldvalue->subfield("n") == 1) {
142 # if OPAC suppression by IP address
143 if (C4::Context->preference('OpacSuppressionByIPRange')) {
144 my $IPAddress = $ENV{'REMOTE_ADDR'};
145 my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
146 if ($IPAddress !~ /^$IPRange/) {
147 print $query->redirect($opacsuppressionredirect);
151 print $query->redirect($opacsuppressionredirect);
161 # get biblionumbers stored in the cart
164 if($query->cookie("bib_list")){
165 my $cart_list = $query->cookie("bib_list");
166 @cart_list = split(/\//, $cart_list);
167 if ( grep {$_ eq $biblionumber} @cart_list) {
168 $template->param( incart => 1 );
173 SetUTF8Flag($record);
174 my $marcflavour = C4::Context->preference("marcflavour");
175 my $ean = GetNormalizedEAN( $record, $marcflavour );
177 # XSLT processing of some stuff
178 my $xslfile = C4::Context->preference('OPACXSLTDetailsDisplay');
179 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
180 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
184 my $searcher = Koha::SearchEngine::Search->new(
185 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
187 my $builder = Koha::SearchEngine::QueryBuilder->new(
188 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
191 my $cleaned_title = $biblio->title;
192 $cleaned_title =~ tr|/||;
193 $cleaned_title = $builder->clean_search_term($cleaned_title);
196 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
197 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
198 : "Host-item:($cleaned_title)";
199 my ( $err, $result, $count );
201 ( $err, $result, $count ) =
202 $searcher->simple_search_compat( $query, 0, 0 );
206 $error .= $err if $err;
208 warn "Warning from simple_search_compat: $error";
212 anonymous_session => ($borrowernumber) ? 0 : 1,
213 show_analytics_link => defined $count && $count > 0 ? 1 : 0
216 my @plugin_responses = Koha::Plugins->call(
217 'opac_detail_xslt_variables',
219 biblio_id => $biblionumber,
221 patron_id => $borrowernumber
225 for my $plugin_variables ( @plugin_responses ) {
226 $variables = { %$variables, %$plugin_variables };
230 XSLTBloc => XSLTParse4Display(
231 $biblionumber, $record, "OPACXSLTDetailsDisplay", 1, undef,
232 $sysxml, $xslfile, $lang, $variables
237 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
239 # We look for the busc param to build the simple paging from the search
240 if ($OpacBrowseResults) {
241 my $session = get_session($query->cookie("CGISESSID"));
242 my %paging = (previous => {}, next => {});
243 if ($session->param('busc')) {
247 # Rebuild the string to store on session
248 # param value is URI encoded and params separator is HTML encode (&)
251 my $arrParamsBusc = shift;
253 my $pasarParams = '';
255 for (keys %$arrParamsBusc) {
256 if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
257 if (defined($arrParamsBusc->{$_})) {
258 $pasarParams .= '&' if ($j);
259 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8( $arrParamsBusc->{$_} ));
263 for my $value (@{$arrParamsBusc->{$_}}) {
264 next if !defined($value);
265 $pasarParams .= '&' if ($j);
266 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8($value));
274 # Search given the current values from the busc param
277 my ($arrParamsBusc, $offset, $results_per_page, $patron) = @_;
279 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
281 @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
282 @servers = ("biblioserver") unless (@servers);
284 my ($default_sort_by, @sort_by);
285 $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
286 @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
287 $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
288 my ($error, $results_hashref, $facets);
290 ($error, $results_hashref, $facets) = getRecords($arrParamsBusc->{'query'},$arrParamsBusc->{'simple_query'},\@sort_by,\@servers,$results_per_page,$offset,undef,$itemtypes,$arrParamsBusc->{'query_type'},$arrParamsBusc->{'scan'});
294 my $search_context = {
295 'interface' => 'opac',
296 'category' => ($patron) ? $patron->categorycode : q{}
298 for (my $i=0;$i<@servers;$i++) {
299 my $server = $servers[$i];
300 $hits = $results_hashref->{$server}->{"hits"};
301 @newresults = searchResults( $search_context, '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
306 # Build the current list of biblionumbers in this search
309 my ($newresultsRef, $results_per_page) = @_;
311 my $listBiblios = '';
313 foreach (@$newresultsRef) {
314 my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
315 $listBiblios .= $bibnum . ',';
317 last if ($j == $results_per_page);
319 chop $listBiblios if ($listBiblios =~ /,$/);
323 my $busc = $session->param("busc");
324 my @arrBusc = split(/\&(?:amp;)?/, $busc);
326 my %arrParamsBusc = ();
328 ($key, $value) = split(/=/, $_, 2);
329 if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
330 $arrParamsBusc{$key} = uri_unescape($value);
332 unless (exists($arrParamsBusc{$key})) {
333 $arrParamsBusc{$key} = [];
335 push @{$arrParamsBusc{$key}}, uri_unescape($value);
339 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
340 my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
341 $arrParamsBusc{'count'} = $results_per_page;
342 my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
343 # The value OPACnumSearchResults has changed and the search has to be rebuild
344 if ($count != $results_per_page) {
345 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
347 my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
348 for (@arrBibliosAux) {
349 last if ($_ == $biblionumber);
352 $indexBiblio += $offset;
353 $offset = int($indexBiblio / $count) * $count;
354 $arrParamsBusc{'offset'} = $offset;
356 $arrParamsBusc{'count'} = $count;
357 $results_per_page = $count;
358 my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page, $patron);
359 $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
360 delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
361 delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
362 delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
363 delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
364 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
365 $session->param("busc" => $newbusc);
366 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
368 my $modifyListBiblios = 0;
369 # We come from a previous click
370 if (exists($arrParamsBusc{'previous'})) {
371 $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
372 delete $arrParamsBusc{'previous'};
373 } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
374 $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
375 delete $arrParamsBusc{'next'};
377 if ($modifyListBiblios) {
378 if (exists($arrParamsBusc{'newlistBiblios'})) {
379 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
380 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
381 my @arrAux = split(',', $listBibliosAux);
382 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
383 if ($modifyListBiblios == 1) {
384 $arrParamsBusc{'next'} = $arrAux[0];
385 $paging{'next'}->{biblionumber} = $arrAux[0];
387 $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
388 $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
391 delete $arrParamsBusc{'listBiblios'};
393 my $offsetAux = $arrParamsBusc{'offset'};
394 $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
395 $arrParamsBusc{'offsetSearch'} = $offsetAux;
396 $offset = $arrParamsBusc{'offset'};
397 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
398 $session->param("busc" => $newbusc);
399 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
404 # Rebuild the query for the button "back to results"
406 unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
407 $buscParam .= '&' unless ($j == 0);
408 $buscParam .= $_; # string already URI encoded
412 $template->param('busc' => $buscParam);
415 # We are inside the list of biblios and we don't have to search
416 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
417 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
419 # We are at the first item of the list
420 if ($arrBiblios[0] == $biblionumber) {
421 if (@arrBiblios > 1) {
422 for (my $j = 1; $j < @arrBiblios; $j++) {
423 next unless ($arrBiblios[$j]);
424 $paging{'next'}->{biblionumber} = $arrBiblios[$j];
428 # search again if we are not at the first searching list
429 if ($offset && !$arrParamsBusc{'previous'}) {
431 $offsetSearch = $offset - $results_per_page;
433 # we are at the last item of the list
434 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
435 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
436 next unless ($arrBiblios[$j]);
437 $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
441 # search again if we are at the first list and there is more results
442 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
444 # search again if we aren't at the first list and there is more results
445 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
447 $offsetSearch = $offset + $results_per_page if ($searchAgain);
449 for (my $j = 1; $j < $#arrBiblios; $j++) {
450 if ($arrBiblios[$j] == $biblionumber) {
451 for (my $z = $j - 1; $z >= 0; $z--) {
452 next unless ($arrBiblios[$z]);
453 $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
456 for (my $z = $j + 1; $z < @arrBiblios; $z++) {
457 next unless ($arrBiblios[$z]);
458 $paging{'next'}->{biblionumber} = $arrBiblios[$z];
466 $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
469 my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page, $patron);
470 my @newresults = @$newresultsRef;
471 # build the new listBiblios
472 my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
473 unless (exists($arrParamsBusc{'listBiblios'})) {
474 $arrParamsBusc{'listBiblios'} = $listBiblios;
475 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
477 $arrParamsBusc{'newlistBiblios'} = $listBiblios;
479 # From the new list we build again the next and previous result
481 if ($arrBiblios[0] == $biblionumber) {
482 for (my $j = $#newresults; $j >= 0; $j--) {
483 next unless ($newresults[$j]);
484 $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
485 $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
486 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
489 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
490 for (my $j = 0; $j < @newresults; $j++) {
491 next unless ($newresults[$j]);
492 $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
493 $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
494 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
499 # build new busc param
500 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
501 $session->param("busc" => $newbusc);
503 my ($numberBiblioPaging, $dataBiblioPaging);
505 $numberBiblioPaging = $paging{'previous'}->{biblionumber};
506 if ($numberBiblioPaging) {
507 $template->param( 'previousBiblionumber' => $numberBiblioPaging );
508 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
509 $template->param('previousTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
512 $numberBiblioPaging = $paging{'next'}->{biblionumber};
513 if ($numberBiblioPaging) {
514 $template->param( 'nextBiblionumber' => $numberBiblioPaging );
515 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
516 $template->param('nextTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
518 # Partial list of biblio results
520 for (my $j = 0; $j < @arrBiblios; $j++) {
521 next unless ($arrBiblios[$j]);
522 $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber);
523 next unless $dataBiblioPaging;
524 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]};
526 $template->param('listResults' => \@listResults) if (@listResults);
527 $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
528 $template->param( 'offset' => $offset );
533 OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"),
536 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
537 # adding items linked via host biblios
538 my $analyticfield = '773';
539 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
540 $analyticfield = '773';
541 } elsif ($marcflavour eq 'UNIMARC') {
542 $analyticfield = '461';
544 foreach my $hostfield ( $record->field($analyticfield)) {
545 my $hostbiblionumber = $hostfield->subfield("0");
546 my $linkeditemnumber = $hostfield->subfield("9");
547 my @hostitemInfos = GetItemsInfo($hostbiblionumber);
548 foreach my $hostitemInfo (@hostitemInfos){
549 if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
550 push(@all_items, $hostitemInfo);
558 # Are there items to hide?
560 $hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0;
564 for my $itm (@all_items) {
565 if ( C4::Context->preference('hidelostitems') ) {
566 push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
568 push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
577 if (C4::Context->userenv){
578 $branch = C4::Context->userenv->{branch};
580 if ( C4::Context->preference('HighlightOwnItemsOnOPAC') ) {
582 ( ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) && $branch )
584 C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch'
587 if ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'PatronBranch' ) {
588 $branchcode = $branch;
590 elsif ( C4::Context->preference('HighlightOwnItemsOnOPACWhich') eq 'OpacURLBranch' ) {
591 $branchcode = $ENV{'BRANCHCODE'};
597 foreach my $item ( @items ) {
598 if ( $item->{branchcode} eq $branchcode ) {
599 $item->{'this_branch'} = 1;
600 push( @our_items, $item );
602 push( @other_items, $item );
606 @items = ( @our_items, @other_items );
610 my $dat = &GetBiblioData($biblionumber);
611 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
613 frameworkcode => $dat->{'frameworkcode'},
617 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
619 my $itemtype = $dat->{'itemtype'};
621 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
622 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
626 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
628 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
630 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
632 #coping with subscriptions
633 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
634 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
637 $dat->{'serial'}=1 if $subscriptionsnumber;
638 foreach my $subscription (@subscriptions) {
639 my $serials_to_display;
641 $cell{subscriptionid} = $subscription->{subscriptionid};
642 $cell{subscriptionnotes} = $subscription->{notes};
643 $cell{missinglist} = $subscription->{missinglist};
644 $cell{opacnote} = $subscription->{opacnote};
645 $cell{histstartdate} = $subscription->{histstartdate};
646 $cell{histenddate} = $subscription->{histenddate};
647 $cell{branchcode} = $subscription->{branchcode};
648 $cell{callnumber} = $subscription->{callnumber};
649 $cell{location} = $subscription->{location};
650 $cell{closed} = $subscription->{closed};
651 $cell{letter} = $subscription->{letter};
652 $cell{biblionumber} = $subscription->{biblionumber};
653 #get the three latest serials.
654 $serials_to_display = $subscription->{opacdisplaycount};
655 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
656 $cell{opacdisplaycount} = $serials_to_display;
657 $cell{latestserials} =
658 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
659 if ( $borrowernumber ) {
660 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
661 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
662 $cell{hasalert} = 1 if $subscriber;
667 $dat->{'count'} = scalar(@items);
670 my (%item_reserves, %priority);
671 my ($show_holds_count, $show_priority);
672 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
673 m/holds/o and $show_holds_count = 1;
674 m/priority/ and $show_priority = 1;
677 if ( $show_holds_count || $show_priority) {
678 my $holds = $biblio->holds;
679 $template->param( holds_count => $holds->count );
680 while ( my $hold = $holds->next ) {
681 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
682 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
685 ? ($priority{ $hold->itemnumber } = $hold->priority)
686 : ($template->param( priority => $hold->priority ));
690 $template->param( show_priority => $has_hold ) ;
693 my (@itemloop, @otheritemloop);
694 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
695 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
696 $template->param(SeparateHoldings => 1);
698 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
699 my $viewallitems = $query->param('viewallitems');
700 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
703 my ( @itemnumbers_on_order );
704 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
705 my $orders = C4::Acquisition::SearchOrders({
706 biblionumber => $biblionumber,
709 my $total_quantity = 0;
710 for my $order ( @$orders ) {
711 my $order = Koha::Acquisition::Orders->find( $order->{ordernumber} );
712 my $basket = $order->basket;
713 if ( $basket->effective_create_items eq 'ordering' ) {
714 @itemnumbers_on_order = $order->items->get_column('itemnumber');
716 $total_quantity += $order->quantity;
718 $template->{VARS}->{acquisition_details} = {
719 total_quantity => $total_quantity,
723 my $allow_onshelf_holds;
724 my ( $itemloop_has_images, $otheritemloop_has_images );
725 if ( not $viewallitems and @items > $max_items_to_display ) {
728 items_count => scalar( @items ),
731 for my $itm (@items) {
732 my $item = Koha::Items->find( $itm->{itemnumber} );
733 $itm->{holds_count} = $item_reserves{ $itm->{itemnumber} };
734 $itm->{priority} = $priority{ $itm->{itemnumber} };
736 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
737 unless $allow_onshelf_holds;
739 # get collection code description, too
740 my $ccode = $itm->{'ccode'};
741 $itm->{'ccode'} = $collections->{$ccode} if defined($ccode) && $collections && exists( $collections->{$ccode} );
742 my $copynumber = $itm->{'copynumber'};
743 $itm->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumbers) && defined($copynumber) && exists( $copynumbers->{$copynumber} ) );
744 if ( defined $itm->{'location'} ) {
745 $itm->{'location_description'} = $shelflocations->{ $itm->{'location'} };
747 if (exists $itm->{itype} && defined($itm->{itype}) && exists $itemtypes->{ $itm->{itype} }) {
748 $itm->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
749 $itm->{'description'} = $itemtypes->{ $itm->{itype} }->{translated_description};
751 foreach (qw(ccode materials enumchron copynumber itemnotes location_description uri)) {
752 $itemfields{$_} = 1 if ($itm->{$_});
755 my $reserve_status = C4::Reserves::GetReserveStatus($itm->{itemnumber});
756 if( $reserve_status eq "Waiting"){ $itm->{'waiting'} = 1; }
757 if( $reserve_status eq "Reserved"){ $itm->{'onhold'} = 1; }
759 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
760 if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
761 $itm->{transfertwhen} = $transfertwhen;
762 $itm->{transfertfrom} = $transfertfrom;
763 $itm->{transfertto} = $transfertto;
766 if ( C4::Context->preference('OPACAcquisitionDetails') ) {
768 if grep { $_ eq $itm->{itemnumber} } @itemnumbers_on_order;
771 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
772 $itm->{cover_images} = $item->cover_images;
775 my $itembranch = $itm->{$separatebranch};
776 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
777 if ($itembranch and $itembranch eq $currentbranch) {
778 push @itemloop, $itm;
779 $itemloop_has_images++ if $item->cover_images->count;
781 push @otheritemloop, $itm;
782 $otheritemloop_has_images++ if $item->cover_images->count;
785 push @itemloop, $itm;
786 $itemloop_has_images++ if $item->cover_images->count;
791 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
792 $template->param( ReservableItems => 1 );
796 itemloop_has_images => $itemloop_has_images,
797 otheritemloop_has_images => $otheritemloop_has_images,
800 # Display only one tab if one items list is empty
801 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
802 $template->param(SeparateHoldings => 0);
803 if (scalar(@itemloop) == 0) {
804 @itemloop = @otheritemloop;
808 ## get notes and subjects from MARC record
809 if (!C4::Context->preference("OPACXSLTDetailsDisplay") ) {
810 my $marcisbnsarray = GetMarcISBN ($record,$marcflavour);
811 my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
812 my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
813 my $marcseriesarray = GetMarcSeries ($record,$marcflavour);
814 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
817 MARCSUBJCTS => $marcsubjctsarray,
818 MARCAUTHORS => $marcauthorsarray,
819 MARCSERIES => $marcseriesarray,
820 MARCURLS => $marcurlsarray,
821 MARCISBNS => $marcisbnsarray,
825 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour, opac => 1 });
827 if( C4::Context->preference('ArticleRequests') ) {
828 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
829 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
830 my $artreqpossible = $patron
831 ? $biblio->can_article_request( $patron )
833 ? $itemtype->may_article_request
835 $template->param( artreqpossible => $artreqpossible );
838 my $norequests = ! $biblio->items->filter_by_for_hold->count;
840 MARCNOTES => $marcnotesarray,
841 norequests => $norequests,
842 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
843 itemdata_ccode => $itemfields{ccode},
844 itemdata_materials => $itemfields{materials},
845 itemdata_enumchron => $itemfields{enumchron},
846 itemdata_uri => $itemfields{uri},
847 itemdata_copynumber => $itemfields{copynumber},
848 itemdata_itemnotes => $itemfields{itemnotes},
849 itemdata_location => $itemfields{location_description},
850 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
853 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
854 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
855 my $subfields = substr $fieldspec, 3;
856 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
857 my @alternateholdingsinfo = ();
858 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
860 for my $field (@holdingsfields) {
861 my %holding = ( holding => '' );
862 my $havesubfield = 0;
863 for my $subfield ($field->subfields()) {
864 if ((index $subfields, $$subfield[0]) >= 0) {
865 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
866 $holding{'holding'} .= $$subfield[1];
871 push(@alternateholdingsinfo, \%holding);
876 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
880 # FIXME: The template uses this hash directly. Need to filter.
881 foreach ( keys %{$dat} ) {
882 next if ( $HideMARC->{$_} );
883 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
886 # some useful variables for enhanced content;
887 # in each case, we're grabbing the first value we find in
888 # the record and normalizing it
889 my $upc = GetNormalizedUPC($record,$marcflavour);
890 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
891 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
892 my $content_identifier_exists;
893 if ( $isbn or $ean or $oclc or $upc ) {
894 $content_identifier_exists = 1;
897 normalized_upc => $upc,
898 normalized_ean => $ean,
899 normalized_oclc => $oclc,
900 normalized_isbn => $isbn,
901 content_identifier_exists => $content_identifier_exists,
904 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
905 # COinS format FIXME: for books Only
906 my $coins = eval { $biblio->get_coins };
907 $template->param( ocoins => $coins );
909 my ( $loggedincommenter, $reviews );
910 if ( C4::Context->preference('OPACComments') ) {
911 $reviews = Koha::Reviews->search(
913 biblionumber => $biblionumber,
914 -or => { approved => 1, borrowernumber => $borrowernumber }
917 order_by => { -desc => 'datereviewed' }
920 my $libravatar_enabled = 0;
921 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
923 require Libravatar::URL;
924 Libravatar::URL->import();
927 $libravatar_enabled = 1;
930 for my $review (@$reviews) {
931 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
933 # setting some borrower info into this hash
934 if ( $review_patron ) {
935 $review->{patron} = $review_patron;
936 if ( $libravatar_enabled and $review_patron->email ) {
937 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
940 if ( $review_patron->borrowernumber eq $borrowernumber ) {
941 $loggedincommenter = 1;
947 if ( C4::Context->preference("OPACISBD") ) {
948 $template->param( ISBD => 1 );
952 itemloop => \@itemloop,
953 otheritemloop => \@otheritemloop,
954 biblionumber => $biblionumber,
955 subscriptions => \@subs,
956 subscriptionsnumber => $subscriptionsnumber,
958 loggedincommenter => $loggedincommenter
962 if (C4::Context->preference("virtualshelves") ) {
963 my $shelves = Koha::Virtualshelves->search(
965 biblionumber => $biblionumber,
969 join => 'virtualshelfcontents',
972 $template->param( shelves => $shelves );
976 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
979 XISBNS => scalar get_xisbns($isbn, $biblionumber)
982 if ($@) { warn "XISBN Failed $@"; }
986 my @sc_fields = $record->field(955);
987 my @lc_fields = $marcflavour eq 'UNIMARC'
988 ? $record->field(930)
989 : $record->field(852);
990 my @serialcollections = ();
992 foreach my $sc_field (@sc_fields) {
995 $row_data{text} = $sc_field->subfield('r');
996 $row_data{branch} = $sc_field->subfield('9');
997 foreach my $lc_field (@lc_fields) {
998 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
999 ? $lc_field->subfield('a') # 930$a
1000 : $lc_field->subfield('h') # 852$h
1001 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
1004 if ($row_data{text} && $row_data{branch}) {
1005 push (@serialcollections, \%row_data);
1009 if (scalar(@serialcollections) > 0) {
1011 serialcollection => 1,
1012 serialcollections => \@serialcollections);
1015 # Local cover Images stuff
1016 if (C4::Context->preference("OPACLocalCoverImages")){
1017 $template->param(OPACLocalCoverImages => 1);
1021 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
1022 $template->param( C4::HTML5Media->gethtml5media($record));
1025 my $syndetics_elements;
1027 if ( C4::Context->preference("SyndeticsEnabled") ) {
1028 $template->param("SyndeticsEnabled" => 1);
1029 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
1031 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
1032 for my $element (values %$syndetics_elements) {
1033 $template->param("Syndetics$element"."Exists" => 1 );
1034 #warn "Exists: "."Syndetics$element"."Exists";
1040 if ( C4::Context->preference("SyndeticsEnabled")
1041 && C4::Context->preference("SyndeticsSummary")
1042 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1044 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1045 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1051 if ( C4::Context->preference("SyndeticsEnabled")
1052 && C4::Context->preference("SyndeticsTOC")
1053 && exists($syndetics_elements->{'TOC'}) ) {
1055 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1056 $template->param( SYNDETICS_TOC => $syndetics_toc );
1061 if ( C4::Context->preference("SyndeticsEnabled")
1062 && C4::Context->preference("SyndeticsExcerpt")
1063 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1065 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1066 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1071 if ( C4::Context->preference("SyndeticsEnabled")
1072 && C4::Context->preference("SyndeticsReviews")) {
1074 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1075 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1080 if ( C4::Context->preference("SyndeticsEnabled")
1081 && C4::Context->preference("SyndeticsAuthorNotes")
1082 && exists($syndetics_elements->{'ANOTES'}) ) {
1084 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1085 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1090 # LibraryThingForLibraries ID Code and Tabbed View Option
1091 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1093 $template->param(LibraryThingForLibrariesID =>
1094 C4::Context->preference('LibraryThingForLibrariesID') );
1095 $template->param(LibraryThingForLibrariesTabbedView =>
1096 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1100 if( C4::Context->preference('NovelistSelectEnabled') )
1102 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1103 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1104 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1109 if ( C4::Context->preference("Babeltheque") ) {
1112 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1117 if ( C4::Context->preference( "SocialNetworks" ) ) {
1118 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1119 $template->param( SocialNetworks => 1 );
1122 # Shelf Browser Stuff
1123 if (C4::Context->preference("OPACShelfBrowser")) {
1124 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1125 if (defined($starting_itemnumber)) {
1126 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1127 my $nearby = GetNearbyItems($starting_itemnumber);
1130 starting_itemnumber => $starting_itemnumber,
1131 starting_homebranch => $nearby->{starting_homebranch}->{description},
1132 starting_location => $nearby->{starting_location}->{description},
1133 starting_ccode => $nearby->{starting_ccode}->{description},
1134 shelfbrowser_prev_item => $nearby->{prev_item},
1135 shelfbrowser_next_item => $nearby->{next_item},
1136 shelfbrowser_items => $nearby->{items},
1139 # in which tab shelf browser should open ?
1140 if (grep { $starting_itemnumber == $_->{itemnumber} } @itemloop) {
1141 $template->param(shelfbrowser_tab => 'holdings');
1143 $template->param(shelfbrowser_tab => 'otherholdings');
1148 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1150 if (C4::Context->preference("BakerTaylorEnabled")) {
1152 BakerTaylorEnabled => 1,
1153 BakerTaylorImageURL => &image_url(),
1154 BakerTaylorLinkURL => &link_url(),
1155 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1157 my ($bt_user, $bt_pass);
1159 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1160 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1163 BakerTaylorContentURL =>
1164 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1165 $bt_user,$bt_pass,$isbn)
1171 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1174 TagsShowOnDetail => $tag_quantity,
1175 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1177 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1178 'sort'=>'-weight', limit=>$tag_quantity}));
1181 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1182 # These values are going to be read by Javascript, at least in the case
1183 # of the google covers
1184 $template->param(covernewwindow => 'true');
1186 $template->param(covernewwindow => 'false');
1189 $template->param(borrowernumber => $borrowernumber);
1191 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1192 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1193 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1195 ratings => $ratings,
1196 my_rating => $my_rating,
1200 #Search for title in links
1201 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1202 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1203 my $issn = $marcissns->[0] || '';
1205 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1206 $dat->{title} =~ s/\/+$//; # remove trailing slash
1207 $dat->{title} =~ s/\s+$//; # remove trailing space
1208 $search_for_title = parametrized_url(
1211 TITLE => $dat->{title},
1212 AUTHOR => $dat->{author},
1215 CONTROLNUMBER => $marccontrolnumber,
1216 BIBLIONUMBER => $biblionumber,
1219 $template->param('OPACSearchForTitleIn' => $search_for_title);
1223 if ( C4::Context->preference("IDREF") ) {
1224 # If the record comes from the SUDOC
1225 if ( $record->field('009') ) {
1226 my $unimarc3 = $record->field("009")->data;
1227 if ( $unimarc3 =~ /^\d+$/ ) {
1235 # We try to select the best default tab to show, according to what
1236 # the user wants, and what's available for display
1237 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1241 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1243 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1244 ? 'serialcollection' :
1245 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1247 scalar (@itemloop) == 0
1249 $subscriptionsnumber
1251 @serialcollections > 0
1252 ? 'serialcollection' : 'subscriptions';
1253 $template->param('defaulttab' => $defaulttab);
1255 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1256 $template->param( localimages => $biblio->cover_images );
1259 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1261 if (C4::Context->preference('OpacHighlightedWords')) {
1262 $template->{VARS}->{query_desc} = $query->param('query_desc');
1264 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1266 if ( C4::Context->preference('UseCourseReserves') ) {
1267 foreach my $i ( @items ) {
1268 $i->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $i->{'itemnumber'} );
1273 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1276 output_html_with_http_headers $query, $cookie, $template->output;