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( get_template_and_user get_session );
29 getitemtypeimagelocation
32 GetNormalizedOCLCNumber
35 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
36 use C4::Output qw( parametrized_url output_html_with_http_headers );
49 use C4::Items qw( GetHiddenItemnumbers GetItemsInfo );
50 use C4::Circulation qw( GetTransfers );
51 use C4::Tags qw( get_tags );
52 use C4::XISBN qw( get_xisbns );
53 use C4::External::Amazon qw( get_amazon_tld );
54 use C4::External::BakerTaylor qw( image_url link_url );
55 use C4::External::Syndetics qw(
64 use C4::XSLT qw( XSLTParse4Display );
65 use C4::ShelfBrowser qw( GetNearbyItems );
66 use C4::Reserves qw( GetReserveStatus );
67 use C4::Charset qw( SetUTF8Flag );
69 use List::MoreUtils qw( any );
71 use C4::CourseReserves qw( GetItemCourseReservesInfo );
74 use Koha::RecordProcessor;
75 use Koha::AuthorisedValues;
76 use Koha::CirculationRules;
79 use Koha::Acquisition::Orders;
80 use Koha::Virtualshelves;
85 use Koha::SearchEngine::Search;
86 use Koha::SearchEngine::QueryBuilder;
89 my $query = CGI->new();
91 my $biblionumber = $query->param('biblionumber') || $query->param('bib') || 0;
92 $biblionumber = int($biblionumber);
94 my $specific_item = $query->param('itemnumber') ? Koha::Items->find( scalar $query->param('itemnumber') ) : undef;
95 $biblionumber = $specific_item->biblionumber if $specific_item;
97 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
99 template_name => "opac-detail.tt",
102 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
106 my @all_items = GetItemsInfo($biblionumber);
107 if( $specific_item ) {
108 @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items;
109 $template->param( specific_item => 1 );
112 my $patron = Koha::Patrons->find( $borrowernumber );
114 my $record = GetMarcBiblio({
115 biblionumber => $biblionumber,
118 print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
122 my $biblio = Koha::Biblios->find( $biblionumber );
123 unless ( $patron and $patron->category->override_hidden_items ) {
124 # only skip this check if there's a logged in user
125 # and its category overrides OpacHiddenItems
126 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
127 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
130 if ( scalar @all_items >= 1 ) {
132 GetHiddenItemnumbers( { items => \@all_items, borcat => $patron ? $patron->categorycode : undef } );
136 my $framework = $biblio ? $biblio->frameworkcode : q{};
137 my $record_processor = Koha::RecordProcessor->new({
138 filters => 'ViewPolicy',
141 frameworkcode => $framework
144 $record_processor->process($record);
146 # redirect if opacsuppression is enabled and biblio is suppressed
147 if (C4::Context->preference('OpacSuppression')) {
148 # FIXME hardcoded; the suppression flag ought to be materialized
149 # as a column on biblio or the like
150 my $opacsuppressionfield = '942';
151 my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
152 # redirect to opac-blocked info page or 404?
153 my $opacsuppressionredirect;
154 if ( C4::Context->preference("OpacSuppressionRedirect") ) {
155 $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
157 $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
159 if ( $opacsuppressionfieldvalue &&
160 $opacsuppressionfieldvalue->subfield("n") &&
161 $opacsuppressionfieldvalue->subfield("n") == 1) {
162 # if OPAC suppression by IP address
163 if (C4::Context->preference('OpacSuppressionByIPRange')) {
164 my $IPAddress = $ENV{'REMOTE_ADDR'};
165 my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
166 if ($IPAddress !~ /^$IPRange/) {
167 print $query->redirect($opacsuppressionredirect);
171 print $query->redirect($opacsuppressionredirect);
181 # get biblionumbers stored in the cart
184 if($query->cookie("bib_list")){
185 my $cart_list = $query->cookie("bib_list");
186 @cart_list = split(/\//, $cart_list);
187 if ( grep {$_ eq $biblionumber} @cart_list) {
188 $template->param( incart => 1 );
193 SetUTF8Flag($record);
194 my $marcflavour = C4::Context->preference("marcflavour");
195 my $ean = GetNormalizedEAN( $record, $marcflavour );
199 anonymous_session => ($borrowernumber) ? 0 : 1,
202 my $lang = C4::Languages::getlanguage();
203 my @plugin_responses = Koha::Plugins->call(
204 'opac_detail_xslt_variables',
206 biblio_id => $biblionumber,
208 patron_id => $borrowernumber
212 for my $plugin_variables ( @plugin_responses ) {
213 $variables = { %$variables, %$plugin_variables };
217 XSLTBloc => XSLTParse4Display(
219 biblionumber => $biblionumber,
221 xsl_syspref => 'OPACXSLTDetailsDisplay',
223 xslt_variables => $variables
229 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
231 # We look for the busc param to build the simple paging from the search
232 if ($OpacBrowseResults) {
233 my $session = get_session($query->cookie("CGISESSID"));
234 my %paging = (previous => {}, next => {});
235 if ($session->param('busc')) {
237 use URI::Escape qw( uri_escape_utf8 uri_unescape );
239 # Rebuild the string to store on session
240 # param value is URI encoded and params separator is HTML encode (&)
243 my $arrParamsBusc = shift;
245 my $pasarParams = '';
247 for (keys %$arrParamsBusc) {
248 if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
249 if (defined($arrParamsBusc->{$_})) {
250 $pasarParams .= '&' if ($j);
251 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8( $arrParamsBusc->{$_} ));
255 for my $value (@{$arrParamsBusc->{$_}}) {
256 next if !defined($value);
257 $pasarParams .= '&' if ($j);
258 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8($value));
266 # Search given the current values from the busc param
269 my ($arrParamsBusc, $offset, $results_per_page, $patron) = @_;
271 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
273 @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
274 @servers = ("biblioserver") unless (@servers);
276 my ($default_sort_by, @sort_by);
277 $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
278 @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
279 $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
280 my ($error, $results_hashref, $facets);
282 ($error, $results_hashref, $facets) = getRecords($arrParamsBusc->{'query'},$arrParamsBusc->{'simple_query'},\@sort_by,\@servers,$results_per_page,$offset,undef,$itemtypes,$arrParamsBusc->{'query_type'},$arrParamsBusc->{'scan'});
286 my $search_context = {
287 'interface' => 'opac',
288 'category' => ($patron) ? $patron->categorycode : q{}
290 for (my $i=0;$i<@servers;$i++) {
291 my $server = $servers[$i];
292 $hits = $results_hashref->{$server}->{"hits"};
293 @newresults = searchResults( $search_context, '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
298 # Build the current list of biblionumbers in this search
301 my ($newresultsRef, $results_per_page) = @_;
303 my $listBiblios = '';
305 foreach (@$newresultsRef) {
306 my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
307 $listBiblios .= $bibnum . ',';
309 last if ($j == $results_per_page);
311 chop $listBiblios if ($listBiblios =~ /,$/);
315 my $busc = $session->param("busc");
316 my @arrBusc = split(/\&(?:amp;)?/, $busc);
318 my %arrParamsBusc = ();
320 ($key, $value) = split(/=/, $_, 2);
321 if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
322 $arrParamsBusc{$key} = uri_unescape($value);
324 unless (exists($arrParamsBusc{$key})) {
325 $arrParamsBusc{$key} = [];
327 push @{$arrParamsBusc{$key}}, uri_unescape($value);
331 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
332 my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
333 $arrParamsBusc{'count'} = $results_per_page;
334 my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
335 # The value OPACnumSearchResults has changed and the search has to be rebuild
336 if ($count != $results_per_page) {
337 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
339 my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
340 for (@arrBibliosAux) {
341 last if ($_ == $biblionumber);
344 $indexBiblio += $offset;
345 $offset = int($indexBiblio / $count) * $count;
346 $arrParamsBusc{'offset'} = $offset;
348 $arrParamsBusc{'count'} = $count;
349 $results_per_page = $count;
350 my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page, $patron);
351 $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
352 delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
353 delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
354 delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
355 delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
356 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
357 $session->param("busc" => $newbusc);
358 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
360 my $modifyListBiblios = 0;
361 # We come from a previous click
362 if (exists($arrParamsBusc{'previous'})) {
363 $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
364 delete $arrParamsBusc{'previous'};
365 } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
366 $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
367 delete $arrParamsBusc{'next'};
369 if ($modifyListBiblios) {
370 if (exists($arrParamsBusc{'newlistBiblios'})) {
371 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
372 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
373 my @arrAux = split(',', $listBibliosAux);
374 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
375 if ($modifyListBiblios == 1) {
376 $arrParamsBusc{'next'} = $arrAux[0];
377 $paging{'next'}->{biblionumber} = $arrAux[0];
379 $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
380 $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
383 delete $arrParamsBusc{'listBiblios'};
385 my $offsetAux = $arrParamsBusc{'offset'};
386 $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
387 $arrParamsBusc{'offsetSearch'} = $offsetAux;
388 $offset = $arrParamsBusc{'offset'};
389 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
390 $session->param("busc" => $newbusc);
391 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
396 # Rebuild the query for the button "back to results"
398 unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
399 $buscParam .= '&' unless ($j == 0);
400 $buscParam .= $_; # string already URI encoded
404 $template->param('busc' => $buscParam);
407 # We are inside the list of biblios and we don't have to search
408 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
409 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
411 # We are at the first item of the list
412 if ($arrBiblios[0] == $biblionumber) {
413 if (@arrBiblios > 1) {
414 for (my $j = 1; $j < @arrBiblios; $j++) {
415 next unless ($arrBiblios[$j]);
416 $paging{'next'}->{biblionumber} = $arrBiblios[$j];
420 # search again if we are not at the first searching list
421 if ($offset && !$arrParamsBusc{'previous'}) {
423 $offsetSearch = $offset - $results_per_page;
425 # we are at the last item of the list
426 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
427 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
428 next unless ($arrBiblios[$j]);
429 $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
433 # search again if we are at the first list and there is more results
434 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
436 # search again if we aren't at the first list and there is more results
437 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
439 $offsetSearch = $offset + $results_per_page if ($searchAgain);
441 for (my $j = 1; $j < $#arrBiblios; $j++) {
442 if ($arrBiblios[$j] == $biblionumber) {
443 for (my $z = $j - 1; $z >= 0; $z--) {
444 next unless ($arrBiblios[$z]);
445 $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
448 for (my $z = $j + 1; $z < @arrBiblios; $z++) {
449 next unless ($arrBiblios[$z]);
450 $paging{'next'}->{biblionumber} = $arrBiblios[$z];
458 $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
461 my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page, $patron);
462 my @newresults = @$newresultsRef;
463 # build the new listBiblios
464 my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
465 unless (exists($arrParamsBusc{'listBiblios'})) {
466 $arrParamsBusc{'listBiblios'} = $listBiblios;
467 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
469 $arrParamsBusc{'newlistBiblios'} = $listBiblios;
471 # From the new list we build again the next and previous result
473 if ($arrBiblios[0] == $biblionumber) {
474 for (my $j = $#newresults; $j >= 0; $j--) {
475 next unless ($newresults[$j]);
476 $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
477 $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
478 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
481 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
482 for (my $j = 0; $j < @newresults; $j++) {
483 next unless ($newresults[$j]);
484 $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
485 $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
486 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
491 # build new busc param
492 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
493 $session->param("busc" => $newbusc);
495 my ($numberBiblioPaging, $dataBiblioPaging);
497 $numberBiblioPaging = $paging{'previous'}->{biblionumber};
498 if ($numberBiblioPaging) {
499 $template->param( 'previousBiblionumber' => $numberBiblioPaging );
500 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
501 $template->param('previousTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
504 $numberBiblioPaging = $paging{'next'}->{biblionumber};
505 if ($numberBiblioPaging) {
506 $template->param( 'nextBiblionumber' => $numberBiblioPaging );
507 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
508 $template->param('nextTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
510 # Partial list of biblio results
512 for (my $j = 0; $j < @arrBiblios; $j++) {
513 next unless ($arrBiblios[$j]);
514 $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber);
515 next unless $dataBiblioPaging;
516 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]};
518 $template->param('listResults' => \@listResults) if (@listResults);
519 $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
520 $template->param( 'offset' => $offset );
525 OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"),
528 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
529 # adding items linked via host biblios
530 my $analyticfield = '773';
531 if ($marcflavour eq 'MARC21'){
532 $analyticfield = '773';
533 } elsif ($marcflavour eq 'UNIMARC') {
534 $analyticfield = '461';
536 foreach my $hostfield ( $record->field($analyticfield)) {
537 my $hostbiblionumber = $hostfield->subfield("0");
538 my $linkeditemnumber = $hostfield->subfield("9");
539 my @hostitemInfos = GetItemsInfo($hostbiblionumber);
540 foreach my $hostitemInfo (@hostitemInfos){
541 if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
542 push(@all_items, $hostitemInfo);
550 # Are there items to hide?
552 $hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0;
556 for my $itm (@all_items) {
557 if ( C4::Context->preference('hidelostitems') ) {
558 push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
560 push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
568 my $dat = &GetBiblioData($biblionumber);
569 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
571 frameworkcode => $dat->{'frameworkcode'},
575 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
577 my $itemtype = $dat->{'itemtype'};
579 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
580 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
584 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
586 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
588 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
590 #coping with subscriptions
591 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
592 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
595 $dat->{'serial'}=1 if $subscriptionsnumber;
596 foreach my $subscription (@subscriptions) {
597 my $serials_to_display;
599 $cell{subscriptionid} = $subscription->{subscriptionid};
600 $cell{subscriptionnotes} = $subscription->{notes};
601 $cell{missinglist} = $subscription->{missinglist};
602 $cell{opacnote} = $subscription->{opacnote};
603 $cell{histstartdate} = $subscription->{histstartdate};
604 $cell{histenddate} = $subscription->{histenddate};
605 $cell{branchcode} = $subscription->{branchcode};
606 $cell{callnumber} = $subscription->{callnumber};
607 $cell{location} = $subscription->{location};
608 $cell{closed} = $subscription->{closed};
609 $cell{letter} = $subscription->{letter};
610 $cell{biblionumber} = $subscription->{biblionumber};
611 #get the three latest serials.
612 $serials_to_display = $subscription->{opacdisplaycount};
613 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
614 $cell{opacdisplaycount} = $serials_to_display;
615 $cell{latestserials} =
616 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
617 if ( $borrowernumber ) {
618 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
619 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
620 $cell{hasalert} = 1 if $subscriber;
625 $dat->{'count'} = scalar(@items);
628 my (%item_reserves, %priority);
629 my ($show_holds_count, $show_priority);
630 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
631 m/holds/o and $show_holds_count = 1;
632 m/priority/ and $show_priority = 1;
635 if ( $show_holds_count || $show_priority) {
636 my $holds = $biblio->holds;
637 $template->param( holds_count => $holds->count );
638 while ( my $hold = $holds->next ) {
639 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
640 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
643 ? ($priority{ $hold->itemnumber } = $hold->priority)
644 : ($template->param( priority => $hold->priority ));
648 $template->param( show_priority => $has_hold ) ;
651 my (@itemloop, @otheritemloop);
652 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
653 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
654 $template->param(SeparateHoldings => 1);
656 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
657 my $viewallitems = $query->param('viewallitems');
658 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
660 # Get component parts details
661 my $showcomp = C4::Context->preference('ShowComponentRecords');
662 if ( $showcomp eq 'both' || $showcomp eq 'opac' ) {
663 if ( my $components = $biblio->get_marc_components(300) ) {
665 for my $part ( @{$components} ) {
666 $part = MARC::Record->new_from_xml( $part, 'UTF-8' );
671 biblionumber => $biblionumber,
673 xsl_syspref => 'OPACXSLTResultsDisplay',
678 $template->param( ComponentParts => $parts );
683 my ( @itemnumbers_on_order );
684 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
685 my $orders = C4::Acquisition::SearchOrders({
686 biblionumber => $biblionumber,
689 my $total_quantity = 0;
690 for my $order ( @$orders ) {
691 my $order = Koha::Acquisition::Orders->find( $order->{ordernumber} );
692 my $basket = $order->basket;
693 if ( $basket->effective_create_items eq 'ordering' ) {
694 @itemnumbers_on_order = $order->items->get_column('itemnumber');
696 $total_quantity += $order->quantity;
698 $template->{VARS}->{acquisition_details} = {
699 total_quantity => $total_quantity,
703 my $allow_onshelf_holds;
704 my ( $itemloop_has_images, $otheritemloop_has_images );
705 if ( not $viewallitems and @items > $max_items_to_display ) {
708 items_count => scalar( @items ),
711 for my $itm (@items) {
712 my $item = Koha::Items->find( $itm->{itemnumber} );
713 $itm->{holds_count} = $item_reserves{ $itm->{itemnumber} };
714 $itm->{priority} = $priority{ $itm->{itemnumber} };
716 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
717 unless $allow_onshelf_holds;
719 # get collection code description, too
720 my $ccode = $itm->{'ccode'};
721 $itm->{'ccode'} = $collections->{$ccode} if defined($ccode) && $collections && exists( $collections->{$ccode} );
722 my $copynumber = $itm->{'copynumber'};
723 $itm->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumbers) && defined($copynumber) && exists( $copynumbers->{$copynumber} ) );
724 if ( defined $itm->{'location'} ) {
725 $itm->{'location_description'} = $shelflocations->{ $itm->{'location'} };
727 if (exists $itm->{itype} && defined($itm->{itype}) && exists $itemtypes->{ $itm->{itype} }) {
728 $itm->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
729 $itm->{'description'} = $itemtypes->{ $itm->{itype} }->{translated_description};
731 foreach (qw(ccode materials enumchron copynumber itemnotes location_description uri)) {
732 $itemfields{$_} = 1 if ($itm->{$_});
735 my $reserve_status = C4::Reserves::GetReserveStatus($itm->{itemnumber});
736 if( $reserve_status eq "Waiting"){ $itm->{'waiting'} = 1; }
737 if( $reserve_status eq "Reserved"){ $itm->{'onhold'} = 1; }
739 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
740 if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
741 $itm->{transfertwhen} = $transfertwhen;
742 $itm->{transfertfrom} = $transfertfrom;
743 $itm->{transfertto} = $transfertto;
746 if ( C4::Context->preference('OPACAcquisitionDetails') ) {
748 if grep { $_ eq $itm->{itemnumber} } @itemnumbers_on_order;
751 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
752 $itm->{cover_images} = $item->cover_images;
755 my $itembranch = $itm->{$separatebranch};
756 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
757 if ($itembranch and $itembranch eq $currentbranch) {
758 push @itemloop, $itm;
759 $itemloop_has_images++ if $item->cover_images->count;
761 push @otheritemloop, $itm;
762 $otheritemloop_has_images++ if $item->cover_images->count;
765 push @itemloop, $itm;
766 $itemloop_has_images++ if $item->cover_images->count;
771 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
772 $template->param( ReservableItems => 1 );
776 itemloop_has_images => $itemloop_has_images,
777 otheritemloop_has_images => $otheritemloop_has_images,
780 # Display only one tab if one items list is empty
781 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
782 $template->param(SeparateHoldings => 0);
783 if (scalar(@itemloop) == 0) {
784 @itemloop = @otheritemloop;
788 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour, opac => 1 });
790 if( C4::Context->preference('ArticleRequests') ) {
791 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
792 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
793 my $artreqpossible = $patron
794 ? $biblio->can_article_request( $patron )
796 ? $itemtype->may_article_request
798 $template->param( artreqpossible => $artreqpossible );
801 my $norequests = ! $biblio->items->filter_by_for_hold->count;
803 MARCNOTES => $marcnotesarray,
804 norequests => $norequests,
805 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
806 itemdata_ccode => $itemfields{ccode},
807 itemdata_materials => $itemfields{materials},
808 itemdata_enumchron => $itemfields{enumchron},
809 itemdata_uri => $itemfields{uri},
810 itemdata_copynumber => $itemfields{copynumber},
811 itemdata_itemnotes => $itemfields{itemnotes},
812 itemdata_location => $itemfields{location_description},
813 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
816 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
817 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
818 my $subfields = substr $fieldspec, 3;
819 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
820 my @alternateholdingsinfo = ();
821 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
823 for my $field (@holdingsfields) {
824 my %holding = ( holding => '' );
825 my $havesubfield = 0;
826 for my $subfield ($field->subfields()) {
827 if ((index $subfields, $$subfield[0]) >= 0) {
828 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
829 $holding{'holding'} .= $$subfield[1];
834 push(@alternateholdingsinfo, \%holding);
839 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
843 # FIXME: The template uses this hash directly. Need to filter.
844 foreach ( keys %{$dat} ) {
845 next if ( $HideMARC->{$_} );
846 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
849 # some useful variables for enhanced content;
850 # in each case, we're grabbing the first value we find in
851 # the record and normalizing it
852 my $upc = GetNormalizedUPC($record,$marcflavour);
853 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
854 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
855 my $content_identifier_exists;
856 if ( $isbn or $ean or $oclc or $upc ) {
857 $content_identifier_exists = 1;
860 normalized_upc => $upc,
861 normalized_ean => $ean,
862 normalized_oclc => $oclc,
863 normalized_isbn => $isbn,
864 content_identifier_exists => $content_identifier_exists,
867 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
868 # COinS format FIXME: for books Only
869 my $coins = eval { $biblio->get_coins };
870 $template->param( ocoins => $coins );
872 my ( $loggedincommenter, $reviews );
873 if ( C4::Context->preference('OPACComments') ) {
874 $reviews = Koha::Reviews->search(
876 biblionumber => $biblionumber,
877 -or => { approved => 1, borrowernumber => $borrowernumber }
880 order_by => { -desc => 'datereviewed' }
883 my $libravatar_enabled = 0;
884 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
886 require Libravatar::URL;
887 Libravatar::URL->import();
890 $libravatar_enabled = 1;
893 for my $review (@$reviews) {
894 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
896 # setting some borrower info into this hash
897 if ( $review_patron ) {
898 $review->{patron} = $review_patron;
899 if ( $libravatar_enabled and $review_patron->email ) {
900 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
903 if ( $review_patron->borrowernumber eq $borrowernumber ) {
904 $loggedincommenter = 1;
910 if ( C4::Context->preference("OPACISBD") ) {
911 $template->param( ISBD => 1 );
915 itemloop => \@itemloop,
916 otheritemloop => \@otheritemloop,
917 biblionumber => $biblionumber,
918 subscriptions => \@subs,
919 subscriptionsnumber => $subscriptionsnumber,
921 loggedincommenter => $loggedincommenter
925 if (C4::Context->preference("virtualshelves") ) {
926 my $shelves = Koha::Virtualshelves->search(
928 biblionumber => $biblionumber,
932 join => 'virtualshelfcontents',
935 $template->param( shelves => $shelves );
939 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
942 XISBNS => scalar get_xisbns($isbn, $biblionumber)
945 if ($@) { warn "XISBN Failed $@"; }
949 my @sc_fields = $record->field(955);
950 my @lc_fields = $marcflavour eq 'UNIMARC'
951 ? $record->field(930)
952 : $record->field(852);
953 my @serialcollections = ();
955 foreach my $sc_field (@sc_fields) {
958 $row_data{text} = $sc_field->subfield('r');
959 $row_data{branch} = $sc_field->subfield('9');
960 foreach my $lc_field (@lc_fields) {
961 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
962 ? $lc_field->subfield('a') # 930$a
963 : $lc_field->subfield('h') # 852$h
964 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
967 if ($row_data{text} && $row_data{branch}) {
968 push (@serialcollections, \%row_data);
972 if (scalar(@serialcollections) > 0) {
974 serialcollection => 1,
975 serialcollections => \@serialcollections);
978 # Local cover Images stuff
979 if (C4::Context->preference("OPACLocalCoverImages")){
980 $template->param(OPACLocalCoverImages => 1);
984 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
985 $template->param( C4::HTML5Media->gethtml5media($record));
988 my $syndetics_elements;
990 if ( C4::Context->preference("SyndeticsEnabled") ) {
991 $template->param("SyndeticsEnabled" => 1);
992 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
994 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
995 for my $element (values %$syndetics_elements) {
996 $template->param("Syndetics$element"."Exists" => 1 );
997 #warn "Exists: "."Syndetics$element"."Exists";
1003 if ( C4::Context->preference("SyndeticsEnabled")
1004 && C4::Context->preference("SyndeticsSummary")
1005 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1007 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1008 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1014 if ( C4::Context->preference("SyndeticsEnabled")
1015 && C4::Context->preference("SyndeticsTOC")
1016 && exists($syndetics_elements->{'TOC'}) ) {
1018 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1019 $template->param( SYNDETICS_TOC => $syndetics_toc );
1024 if ( C4::Context->preference("SyndeticsEnabled")
1025 && C4::Context->preference("SyndeticsExcerpt")
1026 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1028 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1029 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1034 if ( C4::Context->preference("SyndeticsEnabled")
1035 && C4::Context->preference("SyndeticsReviews")) {
1037 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1038 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1043 if ( C4::Context->preference("SyndeticsEnabled")
1044 && C4::Context->preference("SyndeticsAuthorNotes")
1045 && exists($syndetics_elements->{'ANOTES'}) ) {
1047 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1048 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1053 # LibraryThingForLibraries ID Code and Tabbed View Option
1054 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1056 $template->param(LibraryThingForLibrariesID =>
1057 C4::Context->preference('LibraryThingForLibrariesID') );
1058 $template->param(LibraryThingForLibrariesTabbedView =>
1059 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1063 if( C4::Context->preference('NovelistSelectEnabled') )
1065 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1066 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1067 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1072 if ( C4::Context->preference("Babeltheque") ) {
1075 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1080 if ( C4::Context->preference( "SocialNetworks" ) ) {
1081 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1082 $template->param( SocialNetworks => 1 );
1085 # Shelf Browser Stuff
1086 if (C4::Context->preference("OPACShelfBrowser")) {
1087 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1088 if (defined($starting_itemnumber)) {
1089 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1090 my $nearby = GetNearbyItems($starting_itemnumber);
1093 starting_itemnumber => $starting_itemnumber,
1094 starting_homebranch => $nearby->{starting_homebranch}->{description},
1095 starting_location => $nearby->{starting_location}->{description},
1096 starting_ccode => $nearby->{starting_ccode}->{description},
1097 shelfbrowser_prev_item => $nearby->{prev_item},
1098 shelfbrowser_next_item => $nearby->{next_item},
1099 shelfbrowser_items => $nearby->{items},
1102 # in which tab shelf browser should open ?
1103 if (grep { $starting_itemnumber == $_->{itemnumber} } @itemloop) {
1104 $template->param(shelfbrowser_tab => 'holdings');
1106 $template->param(shelfbrowser_tab => 'otherholdings');
1111 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1113 if (C4::Context->preference("BakerTaylorEnabled")) {
1115 BakerTaylorEnabled => 1,
1116 BakerTaylorImageURL => &image_url(),
1117 BakerTaylorLinkURL => &link_url(),
1118 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1120 my ($bt_user, $bt_pass);
1122 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1123 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1126 BakerTaylorContentURL =>
1127 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1128 $bt_user,$bt_pass,$isbn)
1134 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1137 TagsShowOnDetail => $tag_quantity,
1138 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1140 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1141 'sort'=>'-weight', limit=>$tag_quantity}));
1144 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1145 # These values are going to be read by Javascript, at least in the case
1146 # of the google covers
1147 $template->param(covernewwindow => 'true');
1149 $template->param(covernewwindow => 'false');
1152 $template->param(borrowernumber => $borrowernumber);
1154 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1155 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1156 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1158 ratings => $ratings,
1159 my_rating => $my_rating,
1163 #Search for title in links
1164 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1165 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1166 my $issn = $marcissns->[0] || '';
1168 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1169 $dat->{title} =~ s/\/+$//; # remove trailing slash
1170 $dat->{title} =~ s/\s+$//; # remove trailing space
1171 $search_for_title = parametrized_url(
1174 TITLE => $dat->{title},
1175 AUTHOR => $dat->{author},
1178 CONTROLNUMBER => $marccontrolnumber,
1179 BIBLIONUMBER => $biblionumber,
1182 $template->param('OPACSearchForTitleIn' => $search_for_title);
1186 if ( C4::Context->preference("IDREF") ) {
1187 # If the record comes from the SUDOC
1188 if ( $record->field('009') ) {
1189 my $unimarc3 = $record->field("009")->data;
1190 if ( $unimarc3 =~ /^\d+$/ ) {
1198 # We try to select the best default tab to show, according to what
1199 # the user wants, and what's available for display
1200 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1204 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1206 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1207 ? 'serialcollection' :
1208 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1210 scalar (@itemloop) == 0
1212 $subscriptionsnumber
1214 @serialcollections > 0
1215 ? 'serialcollection' : 'subscriptions';
1216 $template->param('defaulttab' => $defaulttab);
1218 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1219 $template->param( localimages => $biblio->cover_images );
1222 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1224 if (C4::Context->preference('OpacHighlightedWords')) {
1225 $template->{VARS}->{query_desc} = $query->param('query_desc');
1227 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1229 if ( C4::Context->preference('UseCourseReserves') ) {
1230 foreach my $i ( @items ) {
1231 $i->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $i->{'itemnumber'} );
1236 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1239 output_html_with_http_headers $query, $cookie, $template->output;