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 my $host_items = $biblio->host_items->filter_by_visible_in_opac({ patron => $patron });
498 $items = $biblio->items->search_ordered(
502 $items->get_column('itemnumber'),
503 $host_items->get_column('itemnumber')
509 my $dat = &GetBiblioData($biblionumber);
510 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
512 frameworkcode => $dat->{'frameworkcode'},
516 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
518 my $itemtype = $dat->{'itemtype'};
520 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
521 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
525 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
527 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
529 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
531 #coping with subscriptions
532 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
533 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
536 $dat->{'serial'}=1 if $subscriptionsnumber;
537 foreach my $subscription (@subscriptions) {
538 my $serials_to_display;
540 $cell{subscriptionid} = $subscription->{subscriptionid};
541 $cell{subscriptionnotes} = $subscription->{notes};
542 $cell{missinglist} = $subscription->{missinglist};
543 $cell{opacnote} = $subscription->{opacnote};
544 $cell{histstartdate} = $subscription->{histstartdate};
545 $cell{histenddate} = $subscription->{histenddate};
546 $cell{branchcode} = $subscription->{branchcode};
547 $cell{callnumber} = $subscription->{callnumber};
548 $cell{location} = $subscription->{location};
549 $cell{closed} = $subscription->{closed};
550 $cell{letter} = $subscription->{letter};
551 $cell{biblionumber} = $subscription->{biblionumber};
552 #get the three latest serials.
553 $serials_to_display = $subscription->{opacdisplaycount};
554 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
555 $cell{opacdisplaycount} = $serials_to_display;
556 $cell{latestserials} =
557 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
558 if ( $borrowernumber ) {
559 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
560 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
561 $cell{hasalert} = 1 if $subscriber;
566 $dat->{'count'} = $items->count;
568 my (%item_reserves, %priority);
569 my ($show_holds_count, $show_priority);
570 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
571 m/holds/o and $show_holds_count = 1;
572 m/priority/ and $show_priority = 1;
575 if ( $show_holds_count || $show_priority) {
576 my $holds = $biblio->holds;
577 $template->param( holds_count => $holds->count );
578 while ( my $hold = $holds->next ) {
579 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
580 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
583 ? ($priority{ $hold->itemnumber } = $hold->priority)
584 : ($template->param( priority => $hold->priority ));
588 $template->param( show_priority => $has_hold ) ;
591 my (@itemloop, @otheritemloop);
592 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
593 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
594 $template->param(SeparateHoldings => 1);
596 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
597 my $viewallitems = $query->param('viewallitems');
598 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
600 # Get component parts details
601 my $showcomp = C4::Context->preference('ShowComponentRecords');
602 my ( $parts, $show_analytics );
603 if ( $showcomp eq 'both' || $showcomp eq 'opac' ) {
604 if ( my $components = $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) ) {
605 $show_analytics = 1 if @{$components}; # just show link when having results
606 for my $part ( @{$components} ) {
607 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
608 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
615 xsl_syspref => 'OPACXSLTResultsDisplay',
620 $template->param( ComponentParts => $parts );
621 my ( $comp_query, $comp_sort ) = $biblio->get_components_query;
622 my $cpq = $comp_query . "&sort_by=" . $comp_sort;
623 $template->param( ComponentPartsQuery => $cpq );
625 } else { # check if we should show analytics anyway
626 $show_analytics = 1 if @{$biblio->get_marc_components(1)}; # count matters here, results does not
629 # XSLT processing of some stuff
631 my @plugin_responses = Koha::Plugins->call(
632 'opac_detail_xslt_variables',
634 biblio_id => $biblionumber,
635 lang => C4::Languages::getlanguage(),
636 patron_id => $borrowernumber,
639 for my $plugin_variables ( @plugin_responses ) {
640 $variables = { %$variables, %$plugin_variables };
642 $variables->{anonymous_session} = $borrowernumber ? 0 : 1;
643 $variables->{show_analytics_link} = $show_analytics;
645 XSLTBloc => XSLTParse4Display({
646 biblionumber => $biblionumber,
648 xsl_syspref => 'OPACXSLTDetailsDisplay',
650 xslt_variables => $variables,
655 my ( @itemnumbers_on_order );
656 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
657 my $orders = C4::Acquisition::SearchOrders({
658 biblionumber => $biblionumber,
661 my $total_quantity = 0;
662 for my $order ( @$orders ) {
663 my $order = Koha::Acquisition::Orders->find( $order->{ordernumber} );
664 my $basket = $order->basket;
665 if ( $basket->effective_create_items eq 'ordering' ) {
666 @itemnumbers_on_order = $order->items->get_column('itemnumber');
668 $total_quantity += $order->quantity;
670 $template->{VARS}->{acquisition_details} = {
671 total_quantity => $total_quantity,
675 my $allow_onshelf_holds;
676 my ( $itemloop_has_images, $otheritemloop_has_images );
677 if ( not $viewallitems and $items->count > $max_items_to_display ) {
680 items_count => $items->count,
684 while ( my $item = $items->next ) {
685 my $item_info = $item->unblessed;
686 $item->{holds_count} = $item_reserves{ $item->itemnumber };
687 $item->{priority} = $priority{ $item->itemnumber };
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 C4::Reserves::GetReserveStatus( $item->itemnumber );
735 if ( $reserve_status eq "Waiting" ) { $item_info->{'waiting'} = 1; }
736 if ( $reserve_status eq "Reserved" ) { $item_info->{'onhold'} = 1; }
738 my ( $transfertwhen, $transfertfrom, $transfertto ) =
739 GetTransfers( $item->itemnumber );
740 if ( defined($transfertwhen) && $transfertwhen ne '' ) {
741 $item_info->{transfertwhen} = $transfertwhen;
742 $item_info->{transfertfrom} = $transfertfrom;
743 $item_info->{transfertto} = $transfertto;
746 if ( C4::Context->preference('OPACAcquisitionDetails') ) {
747 $item_info->{on_order} = 1
748 if grep { $_ eq $item->itemnumber } @itemnumbers_on_order;
751 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
752 $item_info->{cover_images} = $item->cover_images;
756 if ( $item->in_bundle ) {
757 $item_info->{bundle_host} = $item->bundle_host;
760 if ( C4::Context->preference('UseCourseReserves') ) {
761 $item_info->{course_reserves} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
764 $item_info->{holding_branch} = $item->holding_branch;
765 $item_info->{home_branch} = $item->home_branch;
767 my $itembranch = $item->$separatebranch;
769 and C4::Context->preference('OpacSeparateHoldings') )
771 if ( $itembranch and $itembranch eq $currentbranch ) {
772 push @itemloop, $item_info;
773 $itemloop_has_images++ if $item->cover_images->count;
776 push @otheritemloop, $item_info;
777 $otheritemloop_has_images++ if $item->cover_images->count;
781 push @itemloop, $item_info;
782 $itemloop_has_images++ if $item->cover_images->count;
787 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
788 $template->param( ReservableItems => 1 );
792 itemloop_has_images => $itemloop_has_images,
793 otheritemloop_has_images => $otheritemloop_has_images,
796 # Display only one tab if one items list is empty
797 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
798 $template->param(SeparateHoldings => 0);
799 if (scalar(@itemloop) == 0) {
800 @itemloop = @otheritemloop;
804 my $marcnotesarray = $biblio->get_marc_notes({ opac => 1, record => $record });
806 if( C4::Context->preference('ArticleRequests') ) {
807 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
808 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
809 my $artreqpossible = $patron
810 ? $biblio->can_article_request( $patron )
812 ? $itemtype->may_article_request
814 $template->param( artreqpossible => $artreqpossible );
817 my $norequests = ! $biblio->items->filter_by_for_hold->count;
819 MARCNOTES => $marcnotesarray,
820 norequests => $norequests,
821 itemdata_ccode => $itemfields{ccode},
822 itemdata_materials => $itemfields{materials},
823 itemdata_enumchron => $itemfields{enumchron},
824 itemdata_uri => $itemfields{uri},
825 itemdata_copynumber => $itemfields{copynumber},
826 itemdata_itemnotes => $itemfields{itemnotes},
827 itemdata_location => $itemfields{location_description},
828 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
831 if (C4::Context->preference("AlternateHoldingsField") && $items->count == 0) {
832 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
833 my $subfields = substr $fieldspec, 3;
834 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
835 my @alternateholdingsinfo = ();
836 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
838 for my $field (@holdingsfields) {
839 my %holding = ( holding => '' );
840 my $havesubfield = 0;
841 for my $subfield ($field->subfields()) {
842 if ((index $subfields, $$subfield[0]) >= 0) {
843 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
844 $holding{'holding'} .= $$subfield[1];
849 push(@alternateholdingsinfo, \%holding);
854 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
858 # FIXME: The template uses this hash directly. Need to filter.
859 foreach ( keys %{$dat} ) {
860 next if ( $HideMARC->{$_} );
861 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
864 # some useful variables for enhanced content;
865 # in each case, we're grabbing the first value we find in
866 # the record and normalizing it
867 my $upc = GetNormalizedUPC($record,$marcflavour);
868 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
869 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
870 my $content_identifier_exists;
871 if ( $isbn or $ean or $oclc or $upc ) {
872 $content_identifier_exists = 1;
875 normalized_upc => $upc,
876 normalized_ean => $ean,
877 normalized_oclc => $oclc,
878 normalized_isbn => $isbn,
879 content_identifier_exists => $content_identifier_exists,
882 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
883 # COinS format FIXME: for books Only
884 my $coins = eval { $biblio->get_coins };
885 $template->param( ocoins => $coins );
887 my ( $loggedincommenter, $reviews );
888 if ( C4::Context->preference('OPACComments') ) {
889 $reviews = Koha::Reviews->search(
891 biblionumber => $biblionumber,
892 -or => { approved => 1, borrowernumber => $borrowernumber }
895 order_by => { -desc => 'datereviewed' }
898 my $libravatar_enabled = 0;
899 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
901 require Libravatar::URL;
902 Libravatar::URL->import();
905 $libravatar_enabled = 1;
908 for my $review (@$reviews) {
909 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
911 # setting some borrower info into this hash
912 if ( $review_patron ) {
913 $review->{patron} = $review_patron;
914 if ( $libravatar_enabled and $review_patron->email ) {
915 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
918 if ( $review_patron->borrowernumber eq $borrowernumber ) {
919 $loggedincommenter = 1;
925 if ( C4::Context->preference("OPACISBD") ) {
926 $template->param( ISBD => 1 );
930 itemloop => \@itemloop,
931 otheritemloop => \@otheritemloop,
932 biblionumber => $biblionumber,
933 subscriptions => \@subs,
934 subscriptionsnumber => $subscriptionsnumber,
936 loggedincommenter => $loggedincommenter
940 if (C4::Context->preference("virtualshelves") ) {
941 my $shelves = Koha::Virtualshelves->search(
943 biblionumber => $biblionumber,
947 join => 'virtualshelfcontents',
950 $template->param( shelves => $shelves );
954 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
957 XISBNS => scalar get_xisbns($isbn, $biblionumber)
960 if ($@) { warn "XISBN Failed $@"; }
964 my @sc_fields = $record->field(955);
965 my @lc_fields = $marcflavour eq 'UNIMARC'
966 ? $record->field(930)
967 : $record->field(852);
968 my @serialcollections = ();
970 foreach my $sc_field (@sc_fields) {
973 $row_data{text} = $sc_field->subfield('r');
974 $row_data{branch} = $sc_field->subfield('9');
975 foreach my $lc_field (@lc_fields) {
976 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
977 ? $lc_field->subfield('a') # 930$a
978 : $lc_field->subfield('h') # 852$h
979 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
982 if ($row_data{text} && $row_data{branch}) {
983 push (@serialcollections, \%row_data);
987 if (scalar(@serialcollections) > 0) {
989 serialcollection => 1,
990 serialcollections => \@serialcollections);
993 # Local cover Images stuff
994 if (C4::Context->preference("OPACLocalCoverImages")){
995 $template->param(OPACLocalCoverImages => 1);
999 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
1000 $template->param( C4::HTML5Media->gethtml5media($record));
1003 my $syndetics_elements;
1005 if ( C4::Context->preference("SyndeticsEnabled") ) {
1006 $template->param("SyndeticsEnabled" => 1);
1007 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
1009 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
1010 for my $element (values %$syndetics_elements) {
1011 $template->param("Syndetics$element"."Exists" => 1 );
1012 #warn "Exists: "."Syndetics$element"."Exists";
1018 if ( C4::Context->preference("SyndeticsEnabled")
1019 && C4::Context->preference("SyndeticsSummary")
1020 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1022 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1023 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1029 if ( C4::Context->preference("SyndeticsEnabled")
1030 && C4::Context->preference("SyndeticsTOC")
1031 && exists($syndetics_elements->{'TOC'}) ) {
1033 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1034 $template->param( SYNDETICS_TOC => $syndetics_toc );
1039 if ( C4::Context->preference("SyndeticsEnabled")
1040 && C4::Context->preference("SyndeticsExcerpt")
1041 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1043 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1044 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1049 if ( C4::Context->preference("SyndeticsEnabled")
1050 && C4::Context->preference("SyndeticsReviews")) {
1052 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1053 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1058 if ( C4::Context->preference("SyndeticsEnabled")
1059 && C4::Context->preference("SyndeticsAuthorNotes")
1060 && exists($syndetics_elements->{'ANOTES'}) ) {
1062 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1063 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1068 # LibraryThingForLibraries ID Code and Tabbed View Option
1069 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1071 $template->param(LibraryThingForLibrariesID =>
1072 C4::Context->preference('LibraryThingForLibrariesID') );
1073 $template->param(LibraryThingForLibrariesTabbedView =>
1074 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1078 if( C4::Context->preference('NovelistSelectEnabled') )
1080 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1081 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1082 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1087 if ( C4::Context->preference("Babeltheque") ) {
1090 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1095 if ( C4::Context->preference( "SocialNetworks" ) ) {
1096 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1097 $template->param( SocialNetworks => 1 );
1100 # Shelf Browser Stuff
1101 if (C4::Context->preference("OPACShelfBrowser")) {
1102 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1103 if (defined($starting_itemnumber)) {
1104 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1105 my $nearby = GetNearbyItems($starting_itemnumber);
1108 starting_itemnumber => $starting_itemnumber,
1109 starting_homebranch => $nearby->{starting_homebranch}->{description},
1110 starting_location => $nearby->{starting_location}->{description},
1111 starting_ccode => $nearby->{starting_ccode}->{description},
1112 shelfbrowser_prev_item => $nearby->{prev_item},
1113 shelfbrowser_next_item => $nearby->{next_item},
1114 shelfbrowser_items => $nearby->{items},
1117 # in which tab shelf browser should open ?
1118 if (grep { $starting_itemnumber == $_->itemnumber } @itemloop) {
1119 $template->param(shelfbrowser_tab => 'holdings');
1121 $template->param(shelfbrowser_tab => 'otherholdings');
1126 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1128 if (C4::Context->preference("BakerTaylorEnabled")) {
1130 BakerTaylorEnabled => 1,
1131 BakerTaylorImageURL => &image_url(),
1132 BakerTaylorLinkURL => &link_url(),
1133 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1135 my ($bt_user, $bt_pass);
1137 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1138 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1141 BakerTaylorContentURL =>
1142 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1143 $bt_user,$bt_pass,$isbn)
1149 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1152 TagsShowOnDetail => $tag_quantity,
1153 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1155 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1156 'sort'=>'-weight', limit=>$tag_quantity}));
1159 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1160 # These values are going to be read by Javascript, at least in the case
1161 # of the google covers
1162 $template->param(covernewwindow => 'true');
1164 $template->param(covernewwindow => 'false');
1167 $template->param(borrowernumber => $borrowernumber);
1169 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1170 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1171 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1173 ratings => $ratings,
1174 my_rating => $my_rating,
1178 #Search for title in links
1179 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1180 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1181 my $issn = $marcissns->[0] || '';
1183 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1184 $dat->{title} =~ s/\/+$//; # remove trailing slash
1185 $dat->{title} =~ s/\s+$//; # remove trailing space
1186 my $oclc_no = Koha::Util::MARC::oclc_number( $record );
1187 $search_for_title = parametrized_url(
1190 TITLE => $dat->{title},
1191 AUTHOR => $dat->{author},
1194 CONTROLNUMBER => $marccontrolnumber,
1195 BIBLIONUMBER => $biblionumber,
1196 OCLC_NO => $oclc_no,
1199 $template->param('OPACSearchForTitleIn' => $search_for_title);
1203 if ( C4::Context->preference("IDREF") ) {
1204 # If the record comes from the SUDOC
1205 if ( $record->field('009') ) {
1206 my $unimarc3 = $record->field("009")->data;
1207 if ( $unimarc3 =~ /^\d+$/ ) {
1215 # We try to select the best default tab to show, according to what
1216 # the user wants, and what's available for display
1217 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1221 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1223 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1224 ? 'serialcollection' :
1225 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1227 ( $showcomp eq 'both' || $showcomp eq 'opac' ) && scalar (@itemloop) == 0 && $parts
1229 scalar (@itemloop) == 0
1231 $subscriptionsnumber
1233 @serialcollections > 0
1234 ? 'serialcollection' : 'subscriptions';
1235 $template->param('defaulttab' => $defaulttab);
1237 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1238 $template->param( localimages => $biblio->cover_images );
1241 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1243 if (C4::Context->preference('OpacHighlightedWords')) {
1244 $template->{VARS}->{query_desc} = $query->param('query_desc');
1246 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1249 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1252 if ( C4::Context->preference('OPACAuthorIdentifiers') ) {
1253 my @author_identifiers;
1254 for my $author ( @{ $biblio->get_marc_authors } ) {
1255 my $authid = $author->{authoritylink};
1256 my $authority = Koha::Authorities->find($authid);
1257 next unless $authority;
1258 my $identifiers = $authority->get_identifiers;
1259 next unless $identifiers && @$identifiers;
1262 grep { $_->{code} eq 'a' ? $_ : () }
1263 @{ $author->{MARCAUTHOR_SUBFIELDS_LOOP} };
1264 push @author_identifiers,
1265 { authid => $authid, name => $name, identifiers => $identifiers };
1267 $template->param( author_identifiers => \@author_identifiers );
1270 output_html_with_http_headers $query, $cookie, $template->output;