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::Search qw( new_record_from_zebra searchResults getRecords );
36 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
37 use C4::Output qw( parametrized_url output_html_with_http_headers );
48 use C4::Tags qw( get_tags );
49 use C4::XISBN qw( get_xisbns );
50 use C4::External::Amazon qw( get_amazon_tld );
51 use C4::External::BakerTaylor qw( image_url link_url );
52 use C4::External::Syndetics qw(
61 use C4::XSLT qw( XSLTParse4Display );
62 use C4::ShelfBrowser qw( GetNearbyItems );
63 use C4::Reserves qw( GetReserveStatus );
64 use C4::Charset qw( SetUTF8Flag );
66 use List::MoreUtils qw( any );
68 use C4::CourseReserves qw( GetItemCourseReservesInfo );
71 use Koha::RecordProcessor;
72 use Koha::AuthorisedValues;
73 use Koha::CirculationRules;
76 use Koha::Acquisition::Orders;
77 use Koha::Virtualshelves;
82 use Koha::Serial::Items;
83 use Koha::SearchEngine::Search;
84 use Koha::SearchEngine::QueryBuilder;
87 use JSON qw( decode_json );
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 $patron = Koha::Patrons->find( $borrowernumber );
108 my $biblio = Koha::Biblios->find( $biblionumber );
109 my $record = $biblio ? $biblio->metadata->record : undef;
110 unless ( $biblio && $record ) {
111 print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
115 my $items = $biblio->items->search_ordered;
116 if ($specific_item) {
117 $items = $items->search( { itemnumber => scalar $query->param('itemnumber') } );
118 $template->param( specific_item => 1 );
121 unless ( $patron and $patron->category->override_hidden_items ) {
122 # only skip this check if there's a logged in user
123 # and its category overrides OpacHiddenItems
124 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
125 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
128 if ( $items->count >= 1 ) {
129 $items = $items->filter_by_visible_in_opac( { patron => $patron } );
133 my $framework = $biblio ? $biblio->frameworkcode : q{};
134 my $record_processor = Koha::RecordProcessor->new({
135 filters => 'ViewPolicy',
138 frameworkcode => $framework
141 $record_processor->process($record);
143 # redirect if opacsuppression is enabled and biblio is suppressed
144 if (C4::Context->preference('OpacSuppression')) {
145 # FIXME hardcoded; the suppression flag ought to be materialized
146 # as a column on biblio or the like
147 my $opacsuppressionfield = '942';
148 my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
149 # redirect to opac-blocked info page or 404?
150 my $opacsuppressionredirect;
151 if ( C4::Context->preference("OpacSuppressionRedirect") ) {
152 $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
154 $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
156 if ( $opacsuppressionfieldvalue &&
157 $opacsuppressionfieldvalue->subfield("n") &&
158 $opacsuppressionfieldvalue->subfield("n") == 1) {
159 # if OPAC suppression by IP address
160 if (C4::Context->preference('OpacSuppressionByIPRange')) {
161 my $IPAddress = $ENV{'REMOTE_ADDR'};
162 my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
163 if ($IPAddress !~ /^$IPRange/) {
164 print $query->redirect($opacsuppressionredirect);
168 print $query->redirect($opacsuppressionredirect);
178 # get biblionumbers stored in the cart
181 if($query->cookie("bib_list")){
182 my $cart_list = $query->cookie("bib_list");
183 @cart_list = split(/\//, $cart_list);
184 if ( grep {$_ eq $biblionumber} @cart_list) {
185 $template->param( incart => 1 );
190 SetUTF8Flag($record);
191 my $marcflavour = C4::Context->preference("marcflavour");
192 my $ean = GetNormalizedEAN( $record, $marcflavour );
194 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
196 # We look for the busc param to build the simple paging from the search
197 if ($OpacBrowseResults) {
198 my $session = get_session($query->cookie("CGISESSID"));
199 my %paging = (previous => {}, next => {});
200 if ($session->param('busc')) {
201 use URI::Escape qw( uri_escape_utf8 uri_unescape );
203 # Rebuild the string to store on session
204 # param value is URI encoded and params separator is HTML encode (&)
207 my $arrParamsBusc = shift;
209 my $pasarParams = '';
211 for (keys %$arrParamsBusc) {
212 if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
213 if (defined($arrParamsBusc->{$_})) {
214 $pasarParams .= '&' if ($j);
215 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8( $arrParamsBusc->{$_} ));
219 for my $value (@{$arrParamsBusc->{$_}}) {
220 next if !defined($value);
221 $pasarParams .= '&' if ($j);
222 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8($value));
230 # Search given the current values from the busc param
233 my ($arrParamsBusc, $offset, $results_per_page, $patron) = @_;
235 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
237 @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
238 @servers = ("biblioserver") unless (@servers);
240 my ($default_sort_by, @sort_by);
241 $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
242 @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
243 $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
244 my ($error, $results_hashref, $facets);
246 my $searcher = Koha::SearchEngine::Search->new(
247 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
248 my $json = JSON->new->utf8->allow_nonref(1);
249 ($error, $results_hashref, $facets) = $searcher->search_compat($json->decode($arrParamsBusc->{'query'}),$arrParamsBusc->{'simple_query'},\@sort_by,\@servers,$results_per_page,$offset,undef,$itemtypes,$arrParamsBusc->{'query_type'},$arrParamsBusc->{'scan'});
253 my $search_context = {
257 for (my $i=0;$i<@servers;$i++) {
258 my $server = $servers[$i];
259 $hits = $results_hashref->{$server}->{"hits"};
260 @newresults = searchResults( $search_context, '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
265 # Build the current list of biblionumbers in this search
268 my ($newresultsRef, $results_per_page) = @_;
270 my $listBiblios = '';
272 foreach (@$newresultsRef) {
273 my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
274 $listBiblios .= $bibnum . ',';
276 last if ($j == $results_per_page);
278 chop $listBiblios if ($listBiblios =~ /,$/);
282 my $busc = $session->param("busc");
283 my @arrBusc = split(/\&(?:amp;)?/, $busc);
285 my %arrParamsBusc = ();
287 ($key, $value) = split(/=/, $_, 2);
288 if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
289 $arrParamsBusc{$key} = uri_unescape($value);
291 unless (exists($arrParamsBusc{$key})) {
292 $arrParamsBusc{$key} = [];
294 push @{$arrParamsBusc{$key}}, uri_unescape($value);
298 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
299 my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
300 $arrParamsBusc{'count'} = $results_per_page;
301 my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
302 # The value OPACnumSearchResults has changed and the search has to be rebuild
303 if ($count != $results_per_page) {
304 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
306 my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
307 for (@arrBibliosAux) {
308 last if ($_ == $biblionumber);
311 $indexBiblio += $offset;
312 $offset = int($indexBiblio / $count) * $count;
313 $arrParamsBusc{'offset'} = $offset;
315 $arrParamsBusc{'count'} = $count;
316 $results_per_page = $count;
317 my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page, $patron);
318 $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
319 delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
320 delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
321 delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
322 delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
323 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
324 $session->param("busc" => $newbusc);
325 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
327 my $modifyListBiblios = 0;
328 # We come from a previous click
329 if (exists($arrParamsBusc{'previous'})) {
330 $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
331 delete $arrParamsBusc{'previous'};
332 } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
333 $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
334 delete $arrParamsBusc{'next'};
336 if ($modifyListBiblios) {
337 if (exists($arrParamsBusc{'newlistBiblios'})) {
338 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
339 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
340 my @arrAux = split(',', $listBibliosAux);
341 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
342 if ($modifyListBiblios == 1) {
343 $arrParamsBusc{'next'} = $arrAux[0];
344 $paging{'next'}->{biblionumber} = $arrAux[0];
346 $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
347 $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
350 delete $arrParamsBusc{'listBiblios'};
352 my $offsetAux = $arrParamsBusc{'offset'};
353 $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
354 $arrParamsBusc{'offsetSearch'} = $offsetAux;
355 $offset = $arrParamsBusc{'offset'};
356 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
357 $session->param("busc" => $newbusc);
358 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
363 # Rebuild the query for the button "back to results"
365 unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
366 $buscParam .= '&' unless ($j == 0);
367 $buscParam .= $_; # string already URI encoded
371 $template->param('busc' => $buscParam);
374 # We are inside the list of biblios and we don't have to search
375 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
376 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
378 # We are at the first item of the list
379 if ($arrBiblios[0] == $biblionumber) {
380 if (@arrBiblios > 1) {
381 for (my $j = 1; $j < @arrBiblios; $j++) {
382 next unless ($arrBiblios[$j]);
383 $paging{'next'}->{biblionumber} = $arrBiblios[$j];
387 # search again if we are not at the first searching list
388 if ($offset && !$arrParamsBusc{'previous'}) {
390 $offsetSearch = $offset - $results_per_page;
392 # we are at the last item of the list
393 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
394 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
395 next unless ($arrBiblios[$j]);
396 $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
400 # search again if we are at the first list and there is more results
401 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
403 # search again if we aren't at the first list and there is more results
404 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
406 $offsetSearch = $offset + $results_per_page if ($searchAgain);
408 for (my $j = 1; $j < $#arrBiblios; $j++) {
409 if ($arrBiblios[$j] == $biblionumber) {
410 for (my $z = $j - 1; $z >= 0; $z--) {
411 next unless ($arrBiblios[$z]);
412 $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
415 for (my $z = $j + 1; $z < @arrBiblios; $z++) {
416 next unless ($arrBiblios[$z]);
417 $paging{'next'}->{biblionumber} = $arrBiblios[$z];
425 $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
428 my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page, $patron);
429 my @newresults = @$newresultsRef;
430 # build the new listBiblios
431 my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
432 unless (exists($arrParamsBusc{'listBiblios'})) {
433 $arrParamsBusc{'listBiblios'} = $listBiblios;
434 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
436 $arrParamsBusc{'newlistBiblios'} = $listBiblios;
438 # From the new list we build again the next and previous result
440 if ($arrBiblios[0] == $biblionumber) {
441 for (my $j = $#newresults; $j >= 0; $j--) {
442 next unless ($newresults[$j]);
443 $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
444 $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
445 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
448 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
449 for (my $j = 0; $j < @newresults; $j++) {
450 next unless ($newresults[$j]);
451 $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
452 $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
453 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
458 # build new busc param
459 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
460 $session->param("busc" => $newbusc);
462 my ($numberBiblioPaging, $dataBiblioPaging);
464 $numberBiblioPaging = $paging{'previous'}->{biblionumber};
465 if ($numberBiblioPaging) {
466 $template->param( 'previousBiblionumber' => $numberBiblioPaging );
467 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
468 $template->param('previousTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
471 $numberBiblioPaging = $paging{'next'}->{biblionumber};
472 if ($numberBiblioPaging) {
473 $template->param( 'nextBiblionumber' => $numberBiblioPaging );
474 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
475 $template->param('nextTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
477 # Partial list of biblio results
479 for (my $j = 0; $j < @arrBiblios; $j++) {
480 next unless ($arrBiblios[$j]);
481 $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber);
482 next unless $dataBiblioPaging;
483 push @listResults, {index => $j + 1 + $offset, biblionumber => $arrBiblios[$j], title => ($arrBiblios[$j] == $biblionumber)?'':$dataBiblioPaging->title, author => ($arrBiblios[$j] != $biblionumber && $dataBiblioPaging->author)?$dataBiblioPaging->author:'', part_number => ($arrBiblios[$j] == $biblionumber)?'':$dataBiblioPaging->part_number, part_name => ($arrBiblios[$j] == $biblionumber)?'':$dataBiblioPaging->part_name, url => ($arrBiblios[$j] == $biblionumber)?'':'opac-detail.pl?biblionumber=' . $arrBiblios[$j]};
485 $template->param('listResults' => \@listResults) if (@listResults);
486 $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
487 $template->param( 'offset' => $offset );
492 OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"),
495 $items = Koha::Items->search_ordered(
497 'me.biblionumber' => $biblionumber,
500 $biblio->host_items->get_column('itemnumber')
504 { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
505 )->filter_by_visible_in_opac({ patron => $patron }) unless $specific_item;
507 my $dat = &GetBiblioData($biblionumber);
508 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
510 frameworkcode => $dat->{'frameworkcode'},
514 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
516 my $itemtype = $dat->{'itemtype'};
518 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
519 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
523 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
525 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
527 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
529 #coping with subscriptions
530 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
531 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
534 $dat->{'serial'}=1 if $subscriptionsnumber;
535 foreach my $subscription (@subscriptions) {
536 my $serials_to_display;
538 $cell{subscriptionid} = $subscription->{subscriptionid};
539 $cell{subscriptionnotes} = $subscription->{notes};
540 $cell{missinglist} = $subscription->{missinglist};
541 $cell{opacnote} = $subscription->{opacnote};
542 $cell{histstartdate} = $subscription->{histstartdate};
543 $cell{histenddate} = $subscription->{histenddate};
544 $cell{branchcode} = $subscription->{branchcode};
545 $cell{callnumber} = $subscription->{callnumber};
546 $cell{location} = $subscription->{location};
547 $cell{closed} = $subscription->{closed};
548 $cell{letter} = $subscription->{letter};
549 $cell{biblionumber} = $subscription->{biblionumber};
550 #get the three latest serials.
551 $serials_to_display = $subscription->{opacdisplaycount};
552 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
553 $cell{opacdisplaycount} = $serials_to_display;
554 $cell{latestserials} =
555 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
556 if ( $borrowernumber ) {
557 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
558 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
559 $cell{hasalert} = 1 if $subscriber;
564 $dat->{'count'} = $items->count;
566 my (%item_reserves, %priority);
567 my ($show_holds_count, $show_priority);
568 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
569 m/holds/o and $show_holds_count = 1;
570 m/priority/ and $show_priority = 1;
573 if ( $show_holds_count || $show_priority) {
574 my $holds = $biblio->holds;
575 $template->param( holds_count => $holds->count );
576 while ( my $hold = $holds->next ) {
577 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
578 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
581 ? ($priority{ $hold->itemnumber } = $hold->priority)
582 : ($template->param( priority => $hold->priority ));
586 $template->param( show_priority => $has_hold ) ;
589 my (@itemloop, @otheritemloop);
590 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
591 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
592 $template->param(SeparateHoldings => 1);
594 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
595 my $viewallitems = $query->param('viewallitems');
596 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
598 # Get component parts details
599 my $showcomp = C4::Context->preference('ShowComponentRecords');
600 my ( $parts, $show_analytics );
601 if ( $showcomp eq 'both' || $showcomp eq 'opac' ) {
602 if ( my $components = $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) ) {
603 $show_analytics = 1 if @{$components}; # just show link when having results
604 for my $part ( @{$components} ) {
605 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
606 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
613 xsl_syspref => 'OPACXSLTResultsDisplay',
618 $template->param( ComponentParts => $parts );
619 my ( $comp_query, $comp_query_str, $comp_sort ) = $biblio->get_components_query;
620 my $cpq = $comp_query_str . "&sort_by=" . $comp_sort;
621 $template->param( ComponentPartsQuery => $cpq );
623 } else { # check if we should show analytics anyway
624 $show_analytics = 1 if @{$biblio->get_marc_components(1)}; # count matters here, results does not
627 # XSLT processing of some stuff
629 my $lang = C4::Languages::getlanguage();
630 my @plugin_responses = Koha::Plugins->call(
631 'opac_detail_xslt_variables',
633 biblio_id => $biblionumber,
635 patron_id => $borrowernumber,
638 for my $plugin_variables ( @plugin_responses ) {
639 $variables = { %$variables, %$plugin_variables };
641 $variables->{anonymous_session} = $borrowernumber ? 0 : 1;
642 $variables->{show_analytics_link} = $show_analytics;
644 XSLTBloc => XSLTParse4Display({
645 biblionumber => $biblionumber,
647 xsl_syspref => 'OPACXSLTDetailsDisplay',
649 xslt_variables => $variables,
654 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
655 my $orders = $biblio->orders->filter_by_active;
656 my $total_quantity = 0;
657 while ( my $order = $orders->next ) {
658 $total_quantity += $order->quantity;
660 $template->{VARS}->{acquisition_details} = {
661 total_quantity => $total_quantity,
665 my $allow_onshelf_holds;
666 my ( $itemloop_has_images, $otheritemloop_has_images );
667 if ( not $viewallitems and $items->count > $max_items_to_display ) {
670 items_count => $items->count,
675 while ( my $item = $items->next ) {
676 my $item_info = $item->unblessed;
677 $item_info->{holds_count} = $item_reserves{ $item->itemnumber };
678 $item_info->{priority} = $priority{ $item->itemnumber };
680 # Get opac_info from Additional contents for home and holding library
681 my ( $opac_info_home, $opac_info_holding );
682 $opac_info_holding = $library_info->{ $item->holdingbranch } // $item->holding_branch->opac_info({ lang => $lang });
683 $library_info->{ $item->holdingbranch } = $opac_info_holding;
684 $opac_info_home = $library_info->{ $item->homebranch } // $item->home_branch->opac_info({ lang => $lang });
685 $library_info->{ $item->homebranch } = $opac_info_home;
686 $item_info->{holding_library_info} = $opac_info_holding->content if $opac_info_holding;
687 $item_info->{home_library_info} = $opac_info_home->content if $opac_info_home;
689 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy(
690 { item => $item, patron => $patron } )
691 unless $allow_onshelf_holds;
693 # get collection code description, too
694 my $ccode = $item->ccode;
695 $item_info->{'ccode'} = $collections->{$ccode}
698 && exists( $collections->{$ccode} );
700 my $copynumber = $item->copynumber;
701 $item_info->{copynumber} = $copynumbers->{$copynumber}
702 if ( defined($copynumbers)
703 && defined($copynumber)
704 && exists( $copynumbers->{$copynumber} ) );
706 if ( defined $item->location ) {
707 $item_info->{'location_description'} =
708 $shelflocations->{ $item->location };
711 my $itemtype = $item->itemtype;
712 $item_info->{'imageurl'} = getitemtypeimagelocation( 'opac',
713 $itemtypes->{ $itemtype->itemtype }->{'imageurl'} );
714 $item_info->{'description'} =
715 $itemtypes->{ $itemtype->itemtype }->{translated_description};
718 qw(ccode materials enumchron copynumber itemnotes location_description uri)
721 $itemfields{$field} = 1 if $item_info->{$field};
724 # FIXME The following must be Koha::Item->serial
725 my $serial_item = Koha::Serial::Items->find($item->itemnumber);
726 if ( $serial_item ) {
727 $item_info->{serial} = $serial_item;
730 $item_info->{checkout} = $item->checkout;
731 $item_info->{object} = $item;
734 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
735 $item_info->{cover_images} = $item->cover_images;
739 if ( C4::Context->preference('UseCourseReserves') ) {
740 $item_info->{course_reserves} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
743 $item_info->{holding_branch} = $item->holding_branch;
744 $item_info->{home_branch} = $item->home_branch;
746 my $itembranch = $item->$separatebranch;
748 and C4::Context->preference('OpacSeparateHoldings') )
750 if ( $itembranch and $itembranch eq $currentbranch ) {
751 push @itemloop, $item_info;
752 $itemloop_has_images++ if $item->cover_images->count;
755 push @otheritemloop, $item_info;
756 $otheritemloop_has_images++ if $item->cover_images->count;
760 push @itemloop, $item_info;
761 $itemloop_has_images++ if $item->cover_images->count;
766 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
767 $template->param( ReservableItems => 1 );
771 itemloop_has_images => $itemloop_has_images,
772 otheritemloop_has_images => $otheritemloop_has_images,
775 # Display only one tab if one items list is empty
776 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
777 $template->param(SeparateHoldings => 0);
778 if (scalar(@itemloop) == 0) {
779 @itemloop = @otheritemloop;
783 my $marcnotesarray = $biblio->get_marc_notes({ opac => 1, record => $record });
785 if( C4::Context->preference('ArticleRequests') ) {
786 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
787 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
788 my $artreqpossible = $patron
789 ? $biblio->can_article_request( $patron )
791 ? $itemtype->may_article_request
793 $template->param( artreqpossible => $artreqpossible );
796 my $norequests = ! $biblio->items->filter_by_for_hold->count;
798 MARCNOTES => $marcnotesarray,
799 norequests => $norequests,
800 itemdata_ccode => $itemfields{ccode},
801 itemdata_materials => $itemfields{materials},
802 itemdata_enumchron => $itemfields{enumchron},
803 itemdata_uri => $itemfields{uri},
804 itemdata_copynumber => $itemfields{copynumber},
805 itemdata_itemnotes => $itemfields{itemnotes},
806 itemdata_location => $itemfields{location_description},
807 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
810 if (C4::Context->preference("AlternateHoldingsField") && $items->count == 0) {
811 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
812 my $subfields = substr $fieldspec, 3;
813 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
814 my @alternateholdingsinfo = ();
815 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
817 for my $field (@holdingsfields) {
818 my %holding = ( holding => '' );
819 my $havesubfield = 0;
820 for my $subfield ($field->subfields()) {
821 if ((index $subfields, $$subfield[0]) >= 0) {
822 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
823 $holding{'holding'} .= $$subfield[1];
828 push(@alternateholdingsinfo, \%holding);
833 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
837 # FIXME: The template uses this hash directly. Need to filter.
838 foreach ( keys %{$dat} ) {
839 next if ( $HideMARC->{$_} );
840 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
843 # some useful variables for enhanced content;
844 # in each case, we're grabbing the first value we find in
845 # the record and normalizing it
846 my $upc = GetNormalizedUPC($record,$marcflavour);
847 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
848 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
849 my $content_identifier_exists;
850 if ( $isbn or $ean or $oclc or $upc ) {
851 $content_identifier_exists = 1;
854 normalized_upc => $upc,
855 normalized_ean => $ean,
856 normalized_oclc => $oclc,
857 normalized_isbn => $isbn,
858 content_identifier_exists => $content_identifier_exists,
861 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
862 # COinS format FIXME: for books Only
863 my $coins = eval { $biblio->get_coins };
864 $template->param( ocoins => $coins );
866 my ( $loggedincommenter, $reviews );
867 if ( C4::Context->preference('OPACComments') ) {
868 $reviews = Koha::Reviews->search(
870 biblionumber => $biblionumber,
871 -or => { approved => 1, borrowernumber => $borrowernumber }
874 order_by => { -desc => 'datereviewed' }
877 my $libravatar_enabled = 0;
878 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
880 require Libravatar::URL;
881 Libravatar::URL->import();
884 $libravatar_enabled = 1;
887 for my $review (@$reviews) {
888 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
890 # setting some borrower info into this hash
891 if ( $review_patron ) {
892 $review->{patron} = $review_patron;
893 if ( $libravatar_enabled and $review_patron->email ) {
894 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
897 if ( $review_patron->borrowernumber eq $borrowernumber ) {
898 $loggedincommenter = 1;
904 if ( C4::Context->preference("OPACISBD") ) {
905 $template->param( ISBD => 1 );
909 itemloop => \@itemloop,
910 otheritemloop => \@otheritemloop,
911 biblionumber => $biblionumber,
912 subscriptions => \@subs,
913 subscriptionsnumber => $subscriptionsnumber,
915 loggedincommenter => $loggedincommenter
919 if (C4::Context->preference("virtualshelves") ) {
920 my $shelves = Koha::Virtualshelves->search(
922 biblionumber => $biblionumber,
926 join => 'virtualshelfcontents',
929 $template->param( shelves => $shelves );
933 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
936 XISBNS => scalar get_xisbns($isbn, $biblionumber)
939 if ($@) { warn "XISBN Failed $@"; }
943 my @sc_fields = $record->field(955);
944 my @lc_fields = $marcflavour eq 'UNIMARC'
945 ? $record->field(930)
946 : $record->field(852);
947 my @serialcollections = ();
949 foreach my $sc_field (@sc_fields) {
952 $row_data{text} = $sc_field->subfield('r');
953 $row_data{branch} = $sc_field->subfield('9');
954 foreach my $lc_field (@lc_fields) {
955 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
956 ? $lc_field->subfield('a') # 930$a
957 : $lc_field->subfield('h') # 852$h
958 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
961 if ($row_data{text} && $row_data{branch}) {
962 push (@serialcollections, \%row_data);
966 if (scalar(@serialcollections) > 0) {
968 serialcollection => 1,
969 serialcollections => \@serialcollections);
972 # Local cover Images stuff
973 if (C4::Context->preference("OPACLocalCoverImages")){
974 $template->param(OPACLocalCoverImages => 1);
978 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
979 $template->param( C4::HTML5Media->gethtml5media($record));
982 my $syndetics_elements;
984 if ( C4::Context->preference("SyndeticsEnabled") ) {
985 $template->param("SyndeticsEnabled" => 1);
986 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
988 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
989 for my $element (values %$syndetics_elements) {
990 $template->param("Syndetics$element"."Exists" => 1 );
991 #warn "Exists: "."Syndetics$element"."Exists";
997 if ( C4::Context->preference("SyndeticsEnabled")
998 && C4::Context->preference("SyndeticsSummary")
999 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1001 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1002 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1008 if ( C4::Context->preference("SyndeticsEnabled")
1009 && C4::Context->preference("SyndeticsTOC")
1010 && exists($syndetics_elements->{'TOC'}) ) {
1012 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1013 $template->param( SYNDETICS_TOC => $syndetics_toc );
1018 if ( C4::Context->preference("SyndeticsEnabled")
1019 && C4::Context->preference("SyndeticsExcerpt")
1020 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1022 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1023 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1028 if ( C4::Context->preference("SyndeticsEnabled")
1029 && C4::Context->preference("SyndeticsReviews")) {
1031 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1032 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1037 if ( C4::Context->preference("SyndeticsEnabled")
1038 && C4::Context->preference("SyndeticsAuthorNotes")
1039 && exists($syndetics_elements->{'ANOTES'}) ) {
1041 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1042 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1047 # LibraryThingForLibraries ID Code and Tabbed View Option
1048 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1050 $template->param(LibraryThingForLibrariesID =>
1051 C4::Context->preference('LibraryThingForLibrariesID') );
1052 $template->param(LibraryThingForLibrariesTabbedView =>
1053 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1057 if( C4::Context->preference('NovelistSelectEnabled') )
1059 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1060 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1061 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1066 if ( C4::Context->preference("Babeltheque") ) {
1069 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1074 if ( C4::Context->preference( "SocialNetworks" ) ) {
1075 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1076 $template->param( SocialNetworks => 1 );
1079 # Shelf Browser Stuff
1080 if (C4::Context->preference("OPACShelfBrowser")) {
1081 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1082 if (defined($starting_itemnumber)) {
1083 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1084 my $nearby = GetNearbyItems($starting_itemnumber);
1087 starting_itemnumber => $starting_itemnumber,
1088 starting_homebranch => $nearby->{starting_homebranch}->{description},
1089 starting_location => $nearby->{starting_location}->{description},
1090 starting_ccode => $nearby->{starting_ccode}->{description},
1091 shelfbrowser_prev_item => $nearby->{prev_item},
1092 shelfbrowser_next_item => $nearby->{next_item},
1093 shelfbrowser_items => $nearby->{items},
1096 # in which tab shelf browser should open ?
1097 if (grep { $starting_itemnumber == $_->{itemnumber} } @itemloop) {
1098 $template->param(shelfbrowser_tab => 'holdings');
1100 $template->param(shelfbrowser_tab => 'otherholdings');
1105 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1107 if (C4::Context->preference("BakerTaylorEnabled")) {
1109 BakerTaylorEnabled => 1,
1110 BakerTaylorImageURL => &image_url(),
1111 BakerTaylorLinkURL => &link_url(),
1112 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1114 my ($bt_user, $bt_pass);
1116 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1117 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1120 BakerTaylorContentURL =>
1121 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1122 $bt_user,$bt_pass,$isbn)
1128 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1131 TagsShowOnDetail => $tag_quantity,
1132 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1134 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1135 'sort'=>'-weight', limit=>$tag_quantity}));
1138 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1139 # These values are going to be read by Javascript, at least in the case
1140 # of the google covers
1141 $template->param(covernewwindow => 'true');
1143 $template->param(covernewwindow => 'false');
1146 $template->param(borrowernumber => $borrowernumber);
1148 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1149 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1150 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1152 ratings => $ratings,
1153 my_rating => $my_rating,
1157 #Search for title in links
1158 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1159 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1160 my $issn = $marcissns->[0] || '';
1162 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1163 $dat->{title} =~ s/\/+$//; # remove trailing slash
1164 $dat->{title} =~ s/\s+$//; # remove trailing space
1165 my $oclc_no = Koha::Util::MARC::oclc_number( $record );
1166 $search_for_title = parametrized_url(
1169 TITLE => $dat->{title},
1170 AUTHOR => $dat->{author},
1173 CONTROLNUMBER => $marccontrolnumber,
1174 BIBLIONUMBER => $biblionumber,
1175 OCLC_NO => $oclc_no,
1178 $template->param('OPACSearchForTitleIn' => $search_for_title);
1182 if ( C4::Context->preference("IDREF") ) {
1183 # If the record comes from the SUDOC
1184 if ( $record->field('009') ) {
1185 my $unimarc3 = $record->field("009")->data;
1186 if ( $unimarc3 =~ /^\d+$/ ) {
1194 # We try to select the best default tab to show, according to what
1195 # the user wants, and what's available for display
1196 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1200 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1202 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1203 ? 'serialcollection' :
1204 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1206 ( $showcomp eq 'both' || $showcomp eq 'opac' ) && scalar (@itemloop) == 0 && $parts
1208 scalar (@itemloop) == 0
1210 $subscriptionsnumber
1212 @serialcollections > 0
1213 ? 'serialcollection' : 'subscriptions';
1214 $template->param('defaulttab' => $defaulttab);
1216 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1217 $template->param( localimages => $biblio->cover_images );
1220 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1222 if (C4::Context->preference('OpacHighlightedWords')) {
1223 $template->{VARS}->{query_desc} = $query->param('query_desc');
1225 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1228 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1231 if ( C4::Context->preference('OPACAuthorIdentifiers') ) {
1232 my @author_identifiers;
1233 for my $author ( @{ $biblio->get_marc_authors } ) {
1234 my $authid = $author->{authoritylink};
1235 my $authority = Koha::Authorities->find($authid);
1236 next unless $authority;
1237 my $identifiers = $authority->get_identifiers;
1238 next unless $identifiers && @$identifiers;
1241 grep { $_->{code} eq 'a' ? $_ : () }
1242 @{ $author->{MARCAUTHOR_SUBFIELDS_LOOP} };
1243 push @author_identifiers,
1244 { authid => $authid, name => $name, identifiers => $identifiers };
1246 $template->param( author_identifiers => \@author_identifiers );
1249 output_html_with_http_headers $query, $cookie, $template->output;