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::Circulation qw( GetTransfers );
49 use C4::Tags qw( get_tags );
50 use C4::XISBN qw( get_xisbns );
51 use C4::External::Amazon qw( get_amazon_tld );
52 use C4::External::BakerTaylor qw( image_url link_url );
53 use C4::External::Syndetics qw(
62 use C4::XSLT qw( XSLTParse4Display );
63 use C4::ShelfBrowser qw( GetNearbyItems );
64 use C4::Reserves qw( GetReserveStatus );
65 use C4::Charset qw( SetUTF8Flag );
67 use List::MoreUtils qw( any );
69 use C4::CourseReserves qw( GetItemCourseReservesInfo );
72 use Koha::RecordProcessor;
73 use Koha::AuthorisedValues;
74 use Koha::CirculationRules;
77 use Koha::Acquisition::Orders;
78 use Koha::Virtualshelves;
83 use Koha::Serial::Items;
84 use Koha::SearchEngine::Search;
85 use Koha::SearchEngine::QueryBuilder;
88 use JSON qw( decode_json );
90 my $query = CGI->new();
92 my $biblionumber = $query->param('biblionumber') || $query->param('bib') || 0;
93 $biblionumber = int($biblionumber);
95 my $specific_item = $query->param('itemnumber') ? Koha::Items->find( scalar $query->param('itemnumber') ) : undef;
96 $biblionumber = $specific_item->biblionumber if $specific_item;
98 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
100 template_name => "opac-detail.tt",
103 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
107 my $patron = Koha::Patrons->find( $borrowernumber );
109 my $biblio = Koha::Biblios->find( $biblionumber );
110 my $record = $biblio ? $biblio->metadata->record : undef;
111 unless ( $biblio && $record ) {
112 print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
116 my $items = $biblio->items->search_ordered;
117 if ($specific_item) {
118 $items = $items->search( { itemnumber => scalar $query->param('itemnumber') } );
119 $template->param( specific_item => 1 );
122 unless ( $patron and $patron->category->override_hidden_items ) {
123 # only skip this check if there's a logged in user
124 # and its category overrides OpacHiddenItems
125 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
126 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
129 if ( $items->count >= 1 ) {
130 $items = $items->filter_by_visible_in_opac( { patron => $patron } );
134 my $framework = $biblio ? $biblio->frameworkcode : q{};
135 my $record_processor = Koha::RecordProcessor->new({
136 filters => 'ViewPolicy',
139 frameworkcode => $framework
142 $record_processor->process($record);
144 # redirect if opacsuppression is enabled and biblio is suppressed
145 if (C4::Context->preference('OpacSuppression')) {
146 # FIXME hardcoded; the suppression flag ought to be materialized
147 # as a column on biblio or the like
148 my $opacsuppressionfield = '942';
149 my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
150 # redirect to opac-blocked info page or 404?
151 my $opacsuppressionredirect;
152 if ( C4::Context->preference("OpacSuppressionRedirect") ) {
153 $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
155 $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
157 if ( $opacsuppressionfieldvalue &&
158 $opacsuppressionfieldvalue->subfield("n") &&
159 $opacsuppressionfieldvalue->subfield("n") == 1) {
160 # if OPAC suppression by IP address
161 if (C4::Context->preference('OpacSuppressionByIPRange')) {
162 my $IPAddress = $ENV{'REMOTE_ADDR'};
163 my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
164 if ($IPAddress !~ /^$IPRange/) {
165 print $query->redirect($opacsuppressionredirect);
169 print $query->redirect($opacsuppressionredirect);
179 # get biblionumbers stored in the cart
182 if($query->cookie("bib_list")){
183 my $cart_list = $query->cookie("bib_list");
184 @cart_list = split(/\//, $cart_list);
185 if ( grep {$_ eq $biblionumber} @cart_list) {
186 $template->param( incart => 1 );
191 SetUTF8Flag($record);
192 my $marcflavour = C4::Context->preference("marcflavour");
193 my $ean = GetNormalizedEAN( $record, $marcflavour );
195 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
197 # We look for the busc param to build the simple paging from the search
198 if ($OpacBrowseResults) {
199 my $session = get_session($query->cookie("CGISESSID"));
200 my %paging = (previous => {}, next => {});
201 if ($session->param('busc')) {
202 use URI::Escape qw( uri_escape_utf8 uri_unescape );
204 # Rebuild the string to store on session
205 # param value is URI encoded and params separator is HTML encode (&)
208 my $arrParamsBusc = shift;
210 my $pasarParams = '';
212 for (keys %$arrParamsBusc) {
213 if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
214 if (defined($arrParamsBusc->{$_})) {
215 $pasarParams .= '&' if ($j);
216 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8( $arrParamsBusc->{$_} ));
220 for my $value (@{$arrParamsBusc->{$_}}) {
221 next if !defined($value);
222 $pasarParams .= '&' if ($j);
223 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8($value));
231 # Search given the current values from the busc param
234 my ($arrParamsBusc, $offset, $results_per_page, $patron) = @_;
236 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
238 @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
239 @servers = ("biblioserver") unless (@servers);
241 my ($default_sort_by, @sort_by);
242 $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
243 @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
244 $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
245 my ($error, $results_hashref, $facets);
247 my $searcher = Koha::SearchEngine::Search->new(
248 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
249 my $json = JSON->new->utf8->allow_nonref(1);
250 ($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'});
254 my $search_context = {
258 for (my $i=0;$i<@servers;$i++) {
259 my $server = $servers[$i];
260 $hits = $results_hashref->{$server}->{"hits"};
261 @newresults = searchResults( $search_context, '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
266 # Build the current list of biblionumbers in this search
269 my ($newresultsRef, $results_per_page) = @_;
271 my $listBiblios = '';
273 foreach (@$newresultsRef) {
274 my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
275 $listBiblios .= $bibnum . ',';
277 last if ($j == $results_per_page);
279 chop $listBiblios if ($listBiblios =~ /,$/);
283 my $busc = $session->param("busc");
284 my @arrBusc = split(/\&(?:amp;)?/, $busc);
286 my %arrParamsBusc = ();
288 ($key, $value) = split(/=/, $_, 2);
289 if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
290 $arrParamsBusc{$key} = uri_unescape($value);
292 unless (exists($arrParamsBusc{$key})) {
293 $arrParamsBusc{$key} = [];
295 push @{$arrParamsBusc{$key}}, uri_unescape($value);
299 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
300 my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
301 $arrParamsBusc{'count'} = $results_per_page;
302 my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
303 # The value OPACnumSearchResults has changed and the search has to be rebuild
304 if ($count != $results_per_page) {
305 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
307 my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
308 for (@arrBibliosAux) {
309 last if ($_ == $biblionumber);
312 $indexBiblio += $offset;
313 $offset = int($indexBiblio / $count) * $count;
314 $arrParamsBusc{'offset'} = $offset;
316 $arrParamsBusc{'count'} = $count;
317 $results_per_page = $count;
318 my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page, $patron);
319 $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
320 delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
321 delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
322 delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
323 delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
324 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
325 $session->param("busc" => $newbusc);
326 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
328 my $modifyListBiblios = 0;
329 # We come from a previous click
330 if (exists($arrParamsBusc{'previous'})) {
331 $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
332 delete $arrParamsBusc{'previous'};
333 } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
334 $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
335 delete $arrParamsBusc{'next'};
337 if ($modifyListBiblios) {
338 if (exists($arrParamsBusc{'newlistBiblios'})) {
339 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
340 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
341 my @arrAux = split(',', $listBibliosAux);
342 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
343 if ($modifyListBiblios == 1) {
344 $arrParamsBusc{'next'} = $arrAux[0];
345 $paging{'next'}->{biblionumber} = $arrAux[0];
347 $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
348 $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
351 delete $arrParamsBusc{'listBiblios'};
353 my $offsetAux = $arrParamsBusc{'offset'};
354 $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
355 $arrParamsBusc{'offsetSearch'} = $offsetAux;
356 $offset = $arrParamsBusc{'offset'};
357 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
358 $session->param("busc" => $newbusc);
359 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
364 # Rebuild the query for the button "back to results"
366 unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
367 $buscParam .= '&' unless ($j == 0);
368 $buscParam .= $_; # string already URI encoded
372 $template->param('busc' => $buscParam);
375 # We are inside the list of biblios and we don't have to search
376 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
377 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
379 # We are at the first item of the list
380 if ($arrBiblios[0] == $biblionumber) {
381 if (@arrBiblios > 1) {
382 for (my $j = 1; $j < @arrBiblios; $j++) {
383 next unless ($arrBiblios[$j]);
384 $paging{'next'}->{biblionumber} = $arrBiblios[$j];
388 # search again if we are not at the first searching list
389 if ($offset && !$arrParamsBusc{'previous'}) {
391 $offsetSearch = $offset - $results_per_page;
393 # we are at the last item of the list
394 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
395 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
396 next unless ($arrBiblios[$j]);
397 $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
401 # search again if we are at the first list and there is more results
402 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
404 # search again if we aren't at the first list and there is more results
405 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
407 $offsetSearch = $offset + $results_per_page if ($searchAgain);
409 for (my $j = 1; $j < $#arrBiblios; $j++) {
410 if ($arrBiblios[$j] == $biblionumber) {
411 for (my $z = $j - 1; $z >= 0; $z--) {
412 next unless ($arrBiblios[$z]);
413 $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
416 for (my $z = $j + 1; $z < @arrBiblios; $z++) {
417 next unless ($arrBiblios[$z]);
418 $paging{'next'}->{biblionumber} = $arrBiblios[$z];
426 $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
429 my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page, $patron);
430 my @newresults = @$newresultsRef;
431 # build the new listBiblios
432 my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
433 unless (exists($arrParamsBusc{'listBiblios'})) {
434 $arrParamsBusc{'listBiblios'} = $listBiblios;
435 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
437 $arrParamsBusc{'newlistBiblios'} = $listBiblios;
439 # From the new list we build again the next and previous result
441 if ($arrBiblios[0] == $biblionumber) {
442 for (my $j = $#newresults; $j >= 0; $j--) {
443 next unless ($newresults[$j]);
444 $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
445 $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
446 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
449 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
450 for (my $j = 0; $j < @newresults; $j++) {
451 next unless ($newresults[$j]);
452 $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
453 $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
454 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
459 # build new busc param
460 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
461 $session->param("busc" => $newbusc);
463 my ($numberBiblioPaging, $dataBiblioPaging);
465 $numberBiblioPaging = $paging{'previous'}->{biblionumber};
466 if ($numberBiblioPaging) {
467 $template->param( 'previousBiblionumber' => $numberBiblioPaging );
468 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
469 $template->param('previousTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
472 $numberBiblioPaging = $paging{'next'}->{biblionumber};
473 if ($numberBiblioPaging) {
474 $template->param( 'nextBiblionumber' => $numberBiblioPaging );
475 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
476 $template->param('nextTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
478 # Partial list of biblio results
480 for (my $j = 0; $j < @arrBiblios; $j++) {
481 next unless ($arrBiblios[$j]);
482 $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber);
483 next unless $dataBiblioPaging;
484 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]};
486 $template->param('listResults' => \@listResults) if (@listResults);
487 $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
488 $template->param( 'offset' => $offset );
493 OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"),
496 $items = Koha::Items->search_ordered(
498 'me.biblionumber' => $biblionumber,
501 $biblio->host_items->get_column('itemnumber')
505 { prefetch => [ 'issue', 'homebranch', 'holdingbranch' ] }
506 )->filter_by_visible_in_opac({ patron => $patron }) unless $specific_item;
508 my $dat = &GetBiblioData($biblionumber);
509 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
511 frameworkcode => $dat->{'frameworkcode'},
515 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
517 my $itemtype = $dat->{'itemtype'};
519 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
520 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
524 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
526 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
528 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
530 #coping with subscriptions
531 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
532 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
535 $dat->{'serial'}=1 if $subscriptionsnumber;
536 foreach my $subscription (@subscriptions) {
537 my $serials_to_display;
539 $cell{subscriptionid} = $subscription->{subscriptionid};
540 $cell{subscriptionnotes} = $subscription->{notes};
541 $cell{missinglist} = $subscription->{missinglist};
542 $cell{opacnote} = $subscription->{opacnote};
543 $cell{histstartdate} = $subscription->{histstartdate};
544 $cell{histenddate} = $subscription->{histenddate};
545 $cell{branchcode} = $subscription->{branchcode};
546 $cell{callnumber} = $subscription->{callnumber};
547 $cell{location} = $subscription->{location};
548 $cell{closed} = $subscription->{closed};
549 $cell{letter} = $subscription->{letter};
550 $cell{biblionumber} = $subscription->{biblionumber};
551 #get the three latest serials.
552 $serials_to_display = $subscription->{opacdisplaycount};
553 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
554 $cell{opacdisplaycount} = $serials_to_display;
555 $cell{latestserials} =
556 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
557 if ( $borrowernumber ) {
558 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
559 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
560 $cell{hasalert} = 1 if $subscriber;
565 $dat->{'count'} = $items->count;
567 my (%item_reserves, %priority);
568 my ($show_holds_count, $show_priority);
569 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
570 m/holds/o and $show_holds_count = 1;
571 m/priority/ and $show_priority = 1;
574 if ( $show_holds_count || $show_priority) {
575 my $holds = $biblio->holds;
576 $template->param( holds_count => $holds->count );
577 while ( my $hold = $holds->next ) {
578 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
579 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
582 ? ($priority{ $hold->itemnumber } = $hold->priority)
583 : ($template->param( priority => $hold->priority ));
587 $template->param( show_priority => $has_hold ) ;
590 my (@itemloop, @otheritemloop);
591 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
592 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
593 $template->param(SeparateHoldings => 1);
595 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
596 my $viewallitems = $query->param('viewallitems');
597 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
599 # Get component parts details
600 my $showcomp = C4::Context->preference('ShowComponentRecords');
601 my ( $parts, $show_analytics );
602 if ( $showcomp eq 'both' || $showcomp eq 'opac' ) {
603 if ( my $components = $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) ) {
604 $show_analytics = 1 if @{$components}; # just show link when having results
605 for my $part ( @{$components} ) {
606 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
607 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
614 xsl_syspref => 'OPACXSLTResultsDisplay',
619 $template->param( ComponentParts => $parts );
620 my ( $comp_query, $comp_sort ) = $biblio->get_components_query;
621 my $cpq = $comp_query . "&sort_by=" . $comp_sort;
622 $template->param( ComponentPartsQuery => $cpq );
624 } else { # check if we should show analytics anyway
625 $show_analytics = 1 if @{$biblio->get_marc_components(1)}; # count matters here, results does not
628 # XSLT processing of some stuff
630 my @plugin_responses = Koha::Plugins->call(
631 'opac_detail_xslt_variables',
633 biblio_id => $biblionumber,
634 lang => C4::Languages::getlanguage(),
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 my ( @itemnumbers_on_order );
655 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
656 my $orders = C4::Acquisition::SearchOrders({
657 biblionumber => $biblionumber,
660 my $total_quantity = 0;
661 for my $order ( @$orders ) {
662 my $order = Koha::Acquisition::Orders->find( $order->{ordernumber} );
663 my $basket = $order->basket;
664 if ( $basket->effective_create_items eq 'ordering' ) {
665 @itemnumbers_on_order = $order->items->get_column('itemnumber');
667 $total_quantity += $order->quantity;
669 $template->{VARS}->{acquisition_details} = {
670 total_quantity => $total_quantity,
674 my $allow_onshelf_holds;
675 my ( $itemloop_has_images, $otheritemloop_has_images );
676 if ( not $viewallitems and $items->count > $max_items_to_display ) {
679 items_count => $items->count,
683 while ( my $item = $items->next ) {
684 my $item_info = $item->unblessed;
685 $item_info->{holds_count} = $item_reserves{ $item->itemnumber };
686 $item_info->{priority} = $priority{ $item->itemnumber };
688 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy(
689 { item => $item, patron => $patron } )
690 unless $allow_onshelf_holds;
692 # get collection code description, too
693 my $ccode = $item->ccode;
694 $item_info->{'ccode'} = $collections->{$ccode}
697 && exists( $collections->{$ccode} );
699 my $copynumber = $item->copynumber;
700 $item_info->{copynumber} = $copynumbers->{$copynumber}
701 if ( defined($copynumbers)
702 && defined($copynumber)
703 && exists( $copynumbers->{$copynumber} ) );
705 if ( defined $item->location ) {
706 $item_info->{'location_description'} =
707 $shelflocations->{ $item->location };
710 my $itemtype = $item->itemtype;
711 $item_info->{'imageurl'} = getitemtypeimagelocation( 'opac',
712 $itemtypes->{ $itemtype->itemtype }->{'imageurl'} );
713 $item_info->{'description'} =
714 $itemtypes->{ $itemtype->itemtype }->{translated_description};
717 qw(ccode materials enumchron copynumber itemnotes location_description uri)
720 $itemfields{$field} = 1 if $item_info->{$field};
723 # FIXME The following must be Koha::Item->serial
724 my $serial_item = Koha::Serial::Items->find($item->itemnumber);
725 if ( $serial_item ) {
726 $item_info->{serial} = $serial_item;
729 $item_info->{checkout} = $item->checkout;
730 $item_info->{object} = $item;
732 my ( $transfertwhen, $transfertfrom, $transfertto ) =
733 GetTransfers( $item->itemnumber );
734 if ( defined($transfertwhen) && $transfertwhen ne '' ) {
735 $item_info->{transfertwhen} = $transfertwhen;
736 $item_info->{transfertfrom} = $transfertfrom;
737 $item_info->{transfertto} = $transfertto;
740 if ( C4::Context->preference('OPACAcquisitionDetails') ) {
741 $item_info->{on_order} = 1
742 if grep { $_ eq $item->itemnumber } @itemnumbers_on_order;
745 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
746 $item_info->{cover_images} = $item->cover_images;
750 if ( $item->in_bundle ) {
751 $item_info->{bundle_host} = $item->bundle_host;
754 if ( C4::Context->preference('UseCourseReserves') ) {
755 $item_info->{course_reserves} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
758 $item_info->{holding_branch} = $item->holding_branch;
759 $item_info->{home_branch} = $item->home_branch;
761 my $itembranch = $item->$separatebranch;
763 and C4::Context->preference('OpacSeparateHoldings') )
765 if ( $itembranch and $itembranch eq $currentbranch ) {
766 push @itemloop, $item_info;
767 $itemloop_has_images++ if $item->cover_images->count;
770 push @otheritemloop, $item_info;
771 $otheritemloop_has_images++ if $item->cover_images->count;
775 push @itemloop, $item_info;
776 $itemloop_has_images++ if $item->cover_images->count;
781 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
782 $template->param( ReservableItems => 1 );
786 itemloop_has_images => $itemloop_has_images,
787 otheritemloop_has_images => $otheritemloop_has_images,
790 # Display only one tab if one items list is empty
791 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
792 $template->param(SeparateHoldings => 0);
793 if (scalar(@itemloop) == 0) {
794 @itemloop = @otheritemloop;
798 my $marcnotesarray = $biblio->get_marc_notes({ opac => 1, record => $record });
800 if( C4::Context->preference('ArticleRequests') ) {
801 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
802 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
803 my $artreqpossible = $patron
804 ? $biblio->can_article_request( $patron )
806 ? $itemtype->may_article_request
808 $template->param( artreqpossible => $artreqpossible );
811 my $norequests = ! $biblio->items->filter_by_for_hold->count;
813 MARCNOTES => $marcnotesarray,
814 norequests => $norequests,
815 itemdata_ccode => $itemfields{ccode},
816 itemdata_materials => $itemfields{materials},
817 itemdata_enumchron => $itemfields{enumchron},
818 itemdata_uri => $itemfields{uri},
819 itemdata_copynumber => $itemfields{copynumber},
820 itemdata_itemnotes => $itemfields{itemnotes},
821 itemdata_location => $itemfields{location_description},
822 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
825 if (C4::Context->preference("AlternateHoldingsField") && $items->count == 0) {
826 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
827 my $subfields = substr $fieldspec, 3;
828 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
829 my @alternateholdingsinfo = ();
830 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
832 for my $field (@holdingsfields) {
833 my %holding = ( holding => '' );
834 my $havesubfield = 0;
835 for my $subfield ($field->subfields()) {
836 if ((index $subfields, $$subfield[0]) >= 0) {
837 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
838 $holding{'holding'} .= $$subfield[1];
843 push(@alternateholdingsinfo, \%holding);
848 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
852 # FIXME: The template uses this hash directly. Need to filter.
853 foreach ( keys %{$dat} ) {
854 next if ( $HideMARC->{$_} );
855 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
858 # some useful variables for enhanced content;
859 # in each case, we're grabbing the first value we find in
860 # the record and normalizing it
861 my $upc = GetNormalizedUPC($record,$marcflavour);
862 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
863 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
864 my $content_identifier_exists;
865 if ( $isbn or $ean or $oclc or $upc ) {
866 $content_identifier_exists = 1;
869 normalized_upc => $upc,
870 normalized_ean => $ean,
871 normalized_oclc => $oclc,
872 normalized_isbn => $isbn,
873 content_identifier_exists => $content_identifier_exists,
876 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
877 # COinS format FIXME: for books Only
878 my $coins = eval { $biblio->get_coins };
879 $template->param( ocoins => $coins );
881 my ( $loggedincommenter, $reviews );
882 if ( C4::Context->preference('OPACComments') ) {
883 $reviews = Koha::Reviews->search(
885 biblionumber => $biblionumber,
886 -or => { approved => 1, borrowernumber => $borrowernumber }
889 order_by => { -desc => 'datereviewed' }
892 my $libravatar_enabled = 0;
893 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
895 require Libravatar::URL;
896 Libravatar::URL->import();
899 $libravatar_enabled = 1;
902 for my $review (@$reviews) {
903 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
905 # setting some borrower info into this hash
906 if ( $review_patron ) {
907 $review->{patron} = $review_patron;
908 if ( $libravatar_enabled and $review_patron->email ) {
909 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
912 if ( $review_patron->borrowernumber eq $borrowernumber ) {
913 $loggedincommenter = 1;
919 if ( C4::Context->preference("OPACISBD") ) {
920 $template->param( ISBD => 1 );
924 itemloop => \@itemloop,
925 otheritemloop => \@otheritemloop,
926 biblionumber => $biblionumber,
927 subscriptions => \@subs,
928 subscriptionsnumber => $subscriptionsnumber,
930 loggedincommenter => $loggedincommenter
934 if (C4::Context->preference("virtualshelves") ) {
935 my $shelves = Koha::Virtualshelves->search(
937 biblionumber => $biblionumber,
941 join => 'virtualshelfcontents',
944 $template->param( shelves => $shelves );
948 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
951 XISBNS => scalar get_xisbns($isbn, $biblionumber)
954 if ($@) { warn "XISBN Failed $@"; }
958 my @sc_fields = $record->field(955);
959 my @lc_fields = $marcflavour eq 'UNIMARC'
960 ? $record->field(930)
961 : $record->field(852);
962 my @serialcollections = ();
964 foreach my $sc_field (@sc_fields) {
967 $row_data{text} = $sc_field->subfield('r');
968 $row_data{branch} = $sc_field->subfield('9');
969 foreach my $lc_field (@lc_fields) {
970 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
971 ? $lc_field->subfield('a') # 930$a
972 : $lc_field->subfield('h') # 852$h
973 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
976 if ($row_data{text} && $row_data{branch}) {
977 push (@serialcollections, \%row_data);
981 if (scalar(@serialcollections) > 0) {
983 serialcollection => 1,
984 serialcollections => \@serialcollections);
987 # Local cover Images stuff
988 if (C4::Context->preference("OPACLocalCoverImages")){
989 $template->param(OPACLocalCoverImages => 1);
993 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
994 $template->param( C4::HTML5Media->gethtml5media($record));
997 my $syndetics_elements;
999 if ( C4::Context->preference("SyndeticsEnabled") ) {
1000 $template->param("SyndeticsEnabled" => 1);
1001 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
1003 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
1004 for my $element (values %$syndetics_elements) {
1005 $template->param("Syndetics$element"."Exists" => 1 );
1006 #warn "Exists: "."Syndetics$element"."Exists";
1012 if ( C4::Context->preference("SyndeticsEnabled")
1013 && C4::Context->preference("SyndeticsSummary")
1014 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1016 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1017 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1023 if ( C4::Context->preference("SyndeticsEnabled")
1024 && C4::Context->preference("SyndeticsTOC")
1025 && exists($syndetics_elements->{'TOC'}) ) {
1027 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1028 $template->param( SYNDETICS_TOC => $syndetics_toc );
1033 if ( C4::Context->preference("SyndeticsEnabled")
1034 && C4::Context->preference("SyndeticsExcerpt")
1035 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1037 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1038 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1043 if ( C4::Context->preference("SyndeticsEnabled")
1044 && C4::Context->preference("SyndeticsReviews")) {
1046 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1047 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1052 if ( C4::Context->preference("SyndeticsEnabled")
1053 && C4::Context->preference("SyndeticsAuthorNotes")
1054 && exists($syndetics_elements->{'ANOTES'}) ) {
1056 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1057 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1062 # LibraryThingForLibraries ID Code and Tabbed View Option
1063 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1065 $template->param(LibraryThingForLibrariesID =>
1066 C4::Context->preference('LibraryThingForLibrariesID') );
1067 $template->param(LibraryThingForLibrariesTabbedView =>
1068 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1072 if( C4::Context->preference('NovelistSelectEnabled') )
1074 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1075 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1076 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1081 if ( C4::Context->preference("Babeltheque") ) {
1084 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1089 if ( C4::Context->preference( "SocialNetworks" ) ) {
1090 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1091 $template->param( SocialNetworks => 1 );
1094 # Shelf Browser Stuff
1095 if (C4::Context->preference("OPACShelfBrowser")) {
1096 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1097 if (defined($starting_itemnumber)) {
1098 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1099 my $nearby = GetNearbyItems($starting_itemnumber);
1102 starting_itemnumber => $starting_itemnumber,
1103 starting_homebranch => $nearby->{starting_homebranch}->{description},
1104 starting_location => $nearby->{starting_location}->{description},
1105 starting_ccode => $nearby->{starting_ccode}->{description},
1106 shelfbrowser_prev_item => $nearby->{prev_item},
1107 shelfbrowser_next_item => $nearby->{next_item},
1108 shelfbrowser_items => $nearby->{items},
1111 # in which tab shelf browser should open ?
1112 if (grep { $starting_itemnumber == $_->itemnumber } @itemloop) {
1113 $template->param(shelfbrowser_tab => 'holdings');
1115 $template->param(shelfbrowser_tab => 'otherholdings');
1120 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1122 if (C4::Context->preference("BakerTaylorEnabled")) {
1124 BakerTaylorEnabled => 1,
1125 BakerTaylorImageURL => &image_url(),
1126 BakerTaylorLinkURL => &link_url(),
1127 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1129 my ($bt_user, $bt_pass);
1131 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1132 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1135 BakerTaylorContentURL =>
1136 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1137 $bt_user,$bt_pass,$isbn)
1143 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1146 TagsShowOnDetail => $tag_quantity,
1147 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1149 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1150 'sort'=>'-weight', limit=>$tag_quantity}));
1153 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1154 # These values are going to be read by Javascript, at least in the case
1155 # of the google covers
1156 $template->param(covernewwindow => 'true');
1158 $template->param(covernewwindow => 'false');
1161 $template->param(borrowernumber => $borrowernumber);
1163 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1164 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1165 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1167 ratings => $ratings,
1168 my_rating => $my_rating,
1172 #Search for title in links
1173 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1174 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1175 my $issn = $marcissns->[0] || '';
1177 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1178 $dat->{title} =~ s/\/+$//; # remove trailing slash
1179 $dat->{title} =~ s/\s+$//; # remove trailing space
1180 my $oclc_no = Koha::Util::MARC::oclc_number( $record );
1181 $search_for_title = parametrized_url(
1184 TITLE => $dat->{title},
1185 AUTHOR => $dat->{author},
1188 CONTROLNUMBER => $marccontrolnumber,
1189 BIBLIONUMBER => $biblionumber,
1190 OCLC_NO => $oclc_no,
1193 $template->param('OPACSearchForTitleIn' => $search_for_title);
1197 if ( C4::Context->preference("IDREF") ) {
1198 # If the record comes from the SUDOC
1199 if ( $record->field('009') ) {
1200 my $unimarc3 = $record->field("009")->data;
1201 if ( $unimarc3 =~ /^\d+$/ ) {
1209 # We try to select the best default tab to show, according to what
1210 # the user wants, and what's available for display
1211 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1215 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1217 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1218 ? 'serialcollection' :
1219 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1221 ( $showcomp eq 'both' || $showcomp eq 'opac' ) && scalar (@itemloop) == 0 && $parts
1223 scalar (@itemloop) == 0
1225 $subscriptionsnumber
1227 @serialcollections > 0
1228 ? 'serialcollection' : 'subscriptions';
1229 $template->param('defaulttab' => $defaulttab);
1231 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1232 $template->param( localimages => $biblio->cover_images );
1235 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1237 if (C4::Context->preference('OpacHighlightedWords')) {
1238 $template->{VARS}->{query_desc} = $query->param('query_desc');
1240 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1243 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1246 if ( C4::Context->preference('OPACAuthorIdentifiers') ) {
1247 my @author_identifiers;
1248 for my $author ( @{ $biblio->get_marc_authors } ) {
1249 my $authid = $author->{authoritylink};
1250 my $authority = Koha::Authorities->find($authid);
1251 next unless $authority;
1252 my $identifiers = $authority->get_identifiers;
1253 next unless $identifiers && @$identifiers;
1256 grep { $_->{code} eq 'a' ? $_ : () }
1257 @{ $author->{MARCAUTHOR_SUBFIELDS_LOOP} };
1258 push @author_identifiers,
1259 { authid => $authid, name => $name, identifiers => $identifiers };
1261 $template->param( author_identifiers => \@author_identifiers );
1264 output_html_with_http_headers $query, $cookie, $template->output;