3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2011 KohaAloha, NZ
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use C4::Acquisition qw( SearchOrders );
27 use C4::Auth qw( get_template_and_user get_session );
29 getitemtypeimagelocation
32 GetNormalizedOCLCNumber
35 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
36 use C4::Output qw( parametrized_url output_html_with_http_headers );
49 use C4::Items qw( GetHiddenItemnumbers GetItemsInfo );
50 use C4::Circulation qw( GetTransfers );
51 use C4::Tags qw( get_tags );
52 use C4::XISBN qw( get_xisbns );
53 use C4::External::Amazon qw( get_amazon_tld );
54 use C4::External::BakerTaylor qw( image_url link_url );
55 use C4::External::Syndetics qw(
64 use C4::XSLT qw( XSLTParse4Display );
65 use C4::ShelfBrowser qw( GetNearbyItems );
66 use C4::Reserves qw( GetReserveStatus );
67 use C4::Charset qw( SetUTF8Flag );
69 use List::MoreUtils qw( any );
71 use C4::CourseReserves qw( GetItemCourseReservesInfo );
74 use Koha::RecordProcessor;
75 use Koha::AuthorisedValues;
76 use Koha::CirculationRules;
79 use Koha::Acquisition::Orders;
80 use Koha::Virtualshelves;
85 use Koha::SearchEngine::Search;
86 use Koha::SearchEngine::QueryBuilder;
89 my $query = CGI->new();
91 my $biblionumber = $query->param('biblionumber') || $query->param('bib') || 0;
92 $biblionumber = int($biblionumber);
94 my $specific_item = $query->param('itemnumber') ? Koha::Items->find( scalar $query->param('itemnumber') ) : undef;
95 $biblionumber = $specific_item->biblionumber if $specific_item;
97 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
99 template_name => "opac-detail.tt",
102 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
106 my @all_items = GetItemsInfo($biblionumber);
107 if( $specific_item ) {
108 @all_items = grep { $_->{itemnumber} == $query->param('itemnumber') } @all_items;
109 $template->param( specific_item => 1 );
112 my $patron = Koha::Patrons->find( $borrowernumber );
114 my $record = GetMarcBiblio({
115 biblionumber => $biblionumber,
118 print $query->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
122 my $biblio = Koha::Biblios->find( $biblionumber );
123 unless ( $patron and $patron->category->override_hidden_items ) {
124 # only skip this check if there's a logged in user
125 # and its category overrides OpacHiddenItems
126 if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
127 print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
130 if ( scalar @all_items >= 1 ) {
132 GetHiddenItemnumbers( { items => \@all_items, borcat => $patron ? $patron->categorycode : undef } );
136 my $framework = $biblio ? $biblio->frameworkcode : q{};
137 my $record_processor = Koha::RecordProcessor->new({
138 filters => 'ViewPolicy',
141 frameworkcode => $framework
144 $record_processor->process($record);
146 # redirect if opacsuppression is enabled and biblio is suppressed
147 if (C4::Context->preference('OpacSuppression')) {
148 # FIXME hardcoded; the suppression flag ought to be materialized
149 # as a column on biblio or the like
150 my $opacsuppressionfield = '942';
151 my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
152 # redirect to opac-blocked info page or 404?
153 my $opacsuppressionredirect;
154 if ( C4::Context->preference("OpacSuppressionRedirect") ) {
155 $opacsuppressionredirect = "/cgi-bin/koha/opac-blocked.pl";
157 $opacsuppressionredirect = "/cgi-bin/koha/errors/404.pl";
159 if ( $opacsuppressionfieldvalue &&
160 $opacsuppressionfieldvalue->subfield("n") &&
161 $opacsuppressionfieldvalue->subfield("n") == 1) {
162 # if OPAC suppression by IP address
163 if (C4::Context->preference('OpacSuppressionByIPRange')) {
164 my $IPAddress = $ENV{'REMOTE_ADDR'};
165 my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
166 if ($IPAddress !~ /^$IPRange/) {
167 print $query->redirect($opacsuppressionredirect);
171 print $query->redirect($opacsuppressionredirect);
181 # get biblionumbers stored in the cart
184 if($query->cookie("bib_list")){
185 my $cart_list = $query->cookie("bib_list");
186 @cart_list = split(/\//, $cart_list);
187 if ( grep {$_ eq $biblionumber} @cart_list) {
188 $template->param( incart => 1 );
193 SetUTF8Flag($record);
194 my $marcflavour = C4::Context->preference("marcflavour");
195 my $ean = GetNormalizedEAN( $record, $marcflavour );
198 my $searcher = Koha::SearchEngine::Search->new(
199 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
201 my $builder = Koha::SearchEngine::QueryBuilder->new(
202 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
205 my $cleaned_title = $biblio->title;
206 $cleaned_title =~ tr|/||;
207 $cleaned_title = $builder->clean_search_term($cleaned_title);
210 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
211 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
212 : "Host-item:($cleaned_title)";
213 my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
215 warn "Warning from simple_search_compat: $err"
219 anonymous_session => ($borrowernumber) ? 0 : 1,
220 show_analytics_link => $count > 0 ? 1 : 0
223 my $lang = C4::Languages::getlanguage();
224 my @plugin_responses = Koha::Plugins->call(
225 'opac_detail_xslt_variables',
227 biblio_id => $biblionumber,
229 patron_id => $borrowernumber
233 for my $plugin_variables ( @plugin_responses ) {
234 $variables = { %$variables, %$plugin_variables };
238 XSLTBloc => XSLTParse4Display(
240 biblionumber => $biblionumber,
242 xsl_syspref => 'OPACXSLTDetailsDisplay',
244 xslt_variables => $variables
250 my $OpacBrowseResults = C4::Context->preference("OpacBrowseResults");
252 # We look for the busc param to build the simple paging from the search
253 if ($OpacBrowseResults) {
254 my $session = get_session($query->cookie("CGISESSID"));
255 my %paging = (previous => {}, next => {});
256 if ($session->param('busc')) {
258 use URI::Escape qw( uri_escape_utf8 uri_unescape );
260 # Rebuild the string to store on session
261 # param value is URI encoded and params separator is HTML encode (&)
264 my $arrParamsBusc = shift;
266 my $pasarParams = '';
268 for (keys %$arrParamsBusc) {
269 if ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|total|offset|offsetSearch|next|previous|count|expand|scan)/) {
270 if (defined($arrParamsBusc->{$_})) {
271 $pasarParams .= '&' if ($j);
272 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8( $arrParamsBusc->{$_} ));
276 for my $value (@{$arrParamsBusc->{$_}}) {
277 next if !defined($value);
278 $pasarParams .= '&' if ($j);
279 $pasarParams .= $_ . '=' . Encode::decode('UTF-8', uri_escape_utf8($value));
287 # Search given the current values from the busc param
290 my ($arrParamsBusc, $offset, $results_per_page, $patron) = @_;
292 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
294 @servers = @{$arrParamsBusc->{'server'}} if $arrParamsBusc->{'server'};
295 @servers = ("biblioserver") unless (@servers);
297 my ($default_sort_by, @sort_by);
298 $default_sort_by = C4::Context->preference('OPACdefaultSortField')."_".C4::Context->preference('OPACdefaultSortOrder') if (C4::Context->preference('OPACdefaultSortField') && C4::Context->preference('OPACdefaultSortOrder'));
299 @sort_by = @{$arrParamsBusc->{'sort_by'}} if $arrParamsBusc->{'sort_by'};
300 $sort_by[0] = $default_sort_by if !$sort_by[0] && defined($default_sort_by);
301 my ($error, $results_hashref, $facets);
303 ($error, $results_hashref, $facets) = getRecords($arrParamsBusc->{'query'},$arrParamsBusc->{'simple_query'},\@sort_by,\@servers,$results_per_page,$offset,undef,$itemtypes,$arrParamsBusc->{'query_type'},$arrParamsBusc->{'scan'});
307 my $search_context = {
308 'interface' => 'opac',
309 'category' => ($patron) ? $patron->categorycode : q{}
311 for (my $i=0;$i<@servers;$i++) {
312 my $server = $servers[$i];
313 $hits = $results_hashref->{$server}->{"hits"};
314 @newresults = searchResults( $search_context, '', $hits, $results_per_page, $offset, $arrParamsBusc->{'scan'}, $results_hashref->{$server}->{"RECORDS"});
319 # Build the current list of biblionumbers in this search
322 my ($newresultsRef, $results_per_page) = @_;
324 my $listBiblios = '';
326 foreach (@$newresultsRef) {
327 my $bibnum = ($_->{biblionumber})?$_->{biblionumber}:0;
328 $listBiblios .= $bibnum . ',';
330 last if ($j == $results_per_page);
332 chop $listBiblios if ($listBiblios =~ /,$/);
336 my $busc = $session->param("busc");
337 my @arrBusc = split(/\&(?:amp;)?/, $busc);
339 my %arrParamsBusc = ();
341 ($key, $value) = split(/=/, $_, 2);
342 if ($key =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|offset|offsetSearch|count|expand|scan)/) {
343 $arrParamsBusc{$key} = uri_unescape($value);
345 unless (exists($arrParamsBusc{$key})) {
346 $arrParamsBusc{$key} = [];
348 push @{$arrParamsBusc{$key}}, uri_unescape($value);
352 my $count = C4::Context->preference('OPACnumSearchResults') || 20;
353 my $results_per_page = ($arrParamsBusc{'count'} && $arrParamsBusc{'count'} =~ /^[0-9]+?/)?$arrParamsBusc{'count'}:$count;
354 $arrParamsBusc{'count'} = $results_per_page;
355 my $offset = ($arrParamsBusc{'offset'} && $arrParamsBusc{'offset'} =~ /^[0-9]+?/)?$arrParamsBusc{'offset'}:0;
356 # The value OPACnumSearchResults has changed and the search has to be rebuild
357 if ($count != $results_per_page) {
358 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
360 my @arrBibliosAux = split(',', $arrParamsBusc{'listBiblios'});
361 for (@arrBibliosAux) {
362 last if ($_ == $biblionumber);
365 $indexBiblio += $offset;
366 $offset = int($indexBiblio / $count) * $count;
367 $arrParamsBusc{'offset'} = $offset;
369 $arrParamsBusc{'count'} = $count;
370 $results_per_page = $count;
371 my $newresultsRef = searchAgain(\%arrParamsBusc, $offset, $results_per_page, $patron);
372 $arrParamsBusc{'listBiblios'} = buildListBiblios($newresultsRef, $results_per_page);
373 delete $arrParamsBusc{'previous'} if (exists($arrParamsBusc{'previous'}));
374 delete $arrParamsBusc{'next'} if (exists($arrParamsBusc{'next'}));
375 delete $arrParamsBusc{'offsetSearch'} if (exists($arrParamsBusc{'offsetSearch'}));
376 delete $arrParamsBusc{'newlistBiblios'} if (exists($arrParamsBusc{'newlistBiblios'}));
377 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
378 $session->param("busc" => $newbusc);
379 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
381 my $modifyListBiblios = 0;
382 # We come from a previous click
383 if (exists($arrParamsBusc{'previous'})) {
384 $modifyListBiblios = 1 if ($biblionumber == $arrParamsBusc{'previous'});
385 delete $arrParamsBusc{'previous'};
386 } elsif (exists($arrParamsBusc{'next'})) { # We come from a next click
387 $modifyListBiblios = 2 if ($biblionumber == $arrParamsBusc{'next'});
388 delete $arrParamsBusc{'next'};
390 if ($modifyListBiblios) {
391 if (exists($arrParamsBusc{'newlistBiblios'})) {
392 my $listBibliosAux = $arrParamsBusc{'listBiblios'};
393 $arrParamsBusc{'listBiblios'} = $arrParamsBusc{'newlistBiblios'};
394 my @arrAux = split(',', $listBibliosAux);
395 $arrParamsBusc{'newlistBiblios'} = $listBibliosAux;
396 if ($modifyListBiblios == 1) {
397 $arrParamsBusc{'next'} = $arrAux[0];
398 $paging{'next'}->{biblionumber} = $arrAux[0];
400 $arrParamsBusc{'previous'} = $arrAux[$#arrAux];
401 $paging{'previous'}->{biblionumber} = $arrAux[$#arrAux];
404 delete $arrParamsBusc{'listBiblios'};
406 my $offsetAux = $arrParamsBusc{'offset'};
407 $arrParamsBusc{'offset'} = $arrParamsBusc{'offsetSearch'};
408 $arrParamsBusc{'offsetSearch'} = $offsetAux;
409 $offset = $arrParamsBusc{'offset'};
410 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
411 $session->param("busc" => $newbusc);
412 @arrBusc = split(/\&(?:amp;)?/, $newbusc);
417 # Rebuild the query for the button "back to results"
419 unless ($_ =~ /^(?:query|listBiblios|newlistBiblios|query_type|simple_query|next|previous|total|count|offsetSearch)/) {
420 $buscParam .= '&' unless ($j == 0);
421 $buscParam .= $_; # string already URI encoded
425 $template->param('busc' => $buscParam);
428 # We are inside the list of biblios and we don't have to search
429 if (exists($arrParamsBusc{'listBiblios'}) && $arrParamsBusc{'listBiblios'} =~ /^[0-9]+(?:,[0-9]+)*$/) {
430 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
432 # We are at the first item of the list
433 if ($arrBiblios[0] == $biblionumber) {
434 if (@arrBiblios > 1) {
435 for (my $j = 1; $j < @arrBiblios; $j++) {
436 next unless ($arrBiblios[$j]);
437 $paging{'next'}->{biblionumber} = $arrBiblios[$j];
441 # search again if we are not at the first searching list
442 if ($offset && !$arrParamsBusc{'previous'}) {
444 $offsetSearch = $offset - $results_per_page;
446 # we are at the last item of the list
447 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
448 for (my $j = $#arrBiblios - 1; $j >= 0; $j--) {
449 next unless ($arrBiblios[$j]);
450 $paging{'previous'}->{biblionumber} = $arrBiblios[$j];
454 # search again if we are at the first list and there is more results
455 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} != @arrBiblios);
457 # search again if we aren't at the first list and there is more results
458 $searchAgain = 1 if (!$arrParamsBusc{'next'} && $arrParamsBusc{'total'} > ($offset + @arrBiblios));
460 $offsetSearch = $offset + $results_per_page if ($searchAgain);
462 for (my $j = 1; $j < $#arrBiblios; $j++) {
463 if ($arrBiblios[$j] == $biblionumber) {
464 for (my $z = $j - 1; $z >= 0; $z--) {
465 next unless ($arrBiblios[$z]);
466 $paging{'previous'}->{biblionumber} = $arrBiblios[$z];
469 for (my $z = $j + 1; $z < @arrBiblios; $z++) {
470 next unless ($arrBiblios[$z]);
471 $paging{'next'}->{biblionumber} = $arrBiblios[$z];
479 $offsetSearch = 0 if (defined($offsetSearch) && $offsetSearch < 0);
482 my $newresultsRef = searchAgain(\%arrParamsBusc, $offsetSearch, $results_per_page, $patron);
483 my @newresults = @$newresultsRef;
484 # build the new listBiblios
485 my $listBiblios = buildListBiblios(\@newresults, $results_per_page);
486 unless (exists($arrParamsBusc{'listBiblios'})) {
487 $arrParamsBusc{'listBiblios'} = $listBiblios;
488 @arrBiblios = split(',', $arrParamsBusc{'listBiblios'});
490 $arrParamsBusc{'newlistBiblios'} = $listBiblios;
492 # From the new list we build again the next and previous result
494 if ($arrBiblios[0] == $biblionumber) {
495 for (my $j = $#newresults; $j >= 0; $j--) {
496 next unless ($newresults[$j]);
497 $paging{'previous'}->{biblionumber} = $newresults[$j]->{biblionumber};
498 $arrParamsBusc{'previous'} = $paging{'previous'}->{biblionumber};
499 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
502 } elsif ($arrBiblios[$#arrBiblios] == $biblionumber) {
503 for (my $j = 0; $j < @newresults; $j++) {
504 next unless ($newresults[$j]);
505 $paging{'next'}->{biblionumber} = $newresults[$j]->{biblionumber};
506 $arrParamsBusc{'next'} = $paging{'next'}->{biblionumber};
507 $arrParamsBusc{'offsetSearch'} = $offsetSearch;
512 # build new busc param
513 my $newbusc = rebuildBuscParam(\%arrParamsBusc);
514 $session->param("busc" => $newbusc);
516 my ($numberBiblioPaging, $dataBiblioPaging);
518 $numberBiblioPaging = $paging{'previous'}->{biblionumber};
519 if ($numberBiblioPaging) {
520 $template->param( 'previousBiblionumber' => $numberBiblioPaging );
521 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
522 $template->param('previousTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
525 $numberBiblioPaging = $paging{'next'}->{biblionumber};
526 if ($numberBiblioPaging) {
527 $template->param( 'nextBiblionumber' => $numberBiblioPaging );
528 $dataBiblioPaging = Koha::Biblios->find( $numberBiblioPaging );
529 $template->param('nextTitle' => $dataBiblioPaging->title) if $dataBiblioPaging;
531 # Partial list of biblio results
533 for (my $j = 0; $j < @arrBiblios; $j++) {
534 next unless ($arrBiblios[$j]);
535 $dataBiblioPaging = Koha::Biblios->find( $arrBiblios[$j] ) if ($arrBiblios[$j] != $biblionumber);
536 next unless $dataBiblioPaging;
537 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]};
539 $template->param('listResults' => \@listResults) if (@listResults);
540 $template->param('indexPag' => 1 + $offset, 'totalPag' => $arrParamsBusc{'total'}, 'indexPagEnd' => scalar(@arrBiblios) + $offset);
541 $template->param( 'offset' => $offset );
546 OPACShowCheckoutName => C4::Context->preference("OPACShowCheckoutName"),
549 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
550 # adding items linked via host biblios
551 my $analyticfield = '773';
552 if ($marcflavour eq 'MARC21'){
553 $analyticfield = '773';
554 } elsif ($marcflavour eq 'UNIMARC') {
555 $analyticfield = '461';
557 foreach my $hostfield ( $record->field($analyticfield)) {
558 my $hostbiblionumber = $hostfield->subfield("0");
559 my $linkeditemnumber = $hostfield->subfield("9");
560 my @hostitemInfos = GetItemsInfo($hostbiblionumber);
561 foreach my $hostitemInfo (@hostitemInfos){
562 if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
563 push(@all_items, $hostitemInfo);
571 # Are there items to hide?
573 $hideitems = 1 if C4::Context->preference('hidelostitems') or scalar(@hiddenitems) > 0;
577 for my $itm (@all_items) {
578 if ( C4::Context->preference('hidelostitems') ) {
579 push @items, $itm unless $itm->{itemlost} or any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
581 push @items, $itm unless any { $itm->{'itemnumber'} eq $_ } @hiddenitems;
589 my $dat = &GetBiblioData($biblionumber);
590 my $HideMARC = $record_processor->filters->[0]->should_hide_marc(
592 frameworkcode => $dat->{'frameworkcode'},
596 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
598 my $itemtype = $dat->{'itemtype'};
600 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
601 $dat->{'description'} = $itemtypes->{$itemtype}->{translated_description};
605 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
607 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
609 { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) };
611 #coping with subscriptions
612 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
613 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
616 $dat->{'serial'}=1 if $subscriptionsnumber;
617 foreach my $subscription (@subscriptions) {
618 my $serials_to_display;
620 $cell{subscriptionid} = $subscription->{subscriptionid};
621 $cell{subscriptionnotes} = $subscription->{notes};
622 $cell{missinglist} = $subscription->{missinglist};
623 $cell{opacnote} = $subscription->{opacnote};
624 $cell{histstartdate} = $subscription->{histstartdate};
625 $cell{histenddate} = $subscription->{histenddate};
626 $cell{branchcode} = $subscription->{branchcode};
627 $cell{callnumber} = $subscription->{callnumber};
628 $cell{location} = $subscription->{location};
629 $cell{closed} = $subscription->{closed};
630 $cell{letter} = $subscription->{letter};
631 $cell{biblionumber} = $subscription->{biblionumber};
632 #get the three latest serials.
633 $serials_to_display = $subscription->{opacdisplaycount};
634 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
635 $cell{opacdisplaycount} = $serials_to_display;
636 $cell{latestserials} =
637 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
638 if ( $borrowernumber ) {
639 my $subscription_object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
640 my $subscriber = $subscription_object->subscribers->find( $borrowernumber );
641 $cell{hasalert} = 1 if $subscriber;
646 $dat->{'count'} = scalar(@items);
649 my (%item_reserves, %priority);
650 my ($show_holds_count, $show_priority);
651 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
652 m/holds/o and $show_holds_count = 1;
653 m/priority/ and $show_priority = 1;
656 if ( $show_holds_count || $show_priority) {
657 my $holds = $biblio->holds;
658 $template->param( holds_count => $holds->count );
659 while ( my $hold = $holds->next ) {
660 $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber;
661 if ($show_priority && $hold->borrowernumber == $borrowernumber) {
664 ? ($priority{ $hold->itemnumber } = $hold->priority)
665 : ($template->param( priority => $hold->priority ));
669 $template->param( show_priority => $has_hold ) ;
673 my (@itemloop, @otheritemloop);
674 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
675 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
676 $template->param(SeparateHoldings => 1);
678 my $separatebranch = C4::Context->preference('OpacSeparateHoldingsBranch');
679 my $viewallitems = $query->param('viewallitems');
680 my $max_items_to_display = C4::Context->preference('OpacMaxItemsToDisplay') // 50;
683 my ( @itemnumbers_on_order );
684 if ( C4::Context->preference('OPACAcquisitionDetails' ) ) {
685 my $orders = C4::Acquisition::SearchOrders({
686 biblionumber => $biblionumber,
689 my $total_quantity = 0;
690 for my $order ( @$orders ) {
691 my $order = Koha::Acquisition::Orders->find( $order->{ordernumber} );
692 my $basket = $order->basket;
693 if ( $basket->effective_create_items eq 'ordering' ) {
694 @itemnumbers_on_order = $order->items->get_column('itemnumber');
696 $total_quantity += $order->quantity;
698 $template->{VARS}->{acquisition_details} = {
699 total_quantity => $total_quantity,
703 my $allow_onshelf_holds;
704 my ( $itemloop_has_images, $otheritemloop_has_images );
705 if ( not $viewallitems and @items > $max_items_to_display ) {
708 items_count => scalar( @items ),
711 for my $itm (@items) {
712 my $item = Koha::Items->find( $itm->{itemnumber} );
713 $itm->{holds_count} = $item_reserves{ $itm->{itemnumber} };
714 $itm->{priority} = $priority{ $itm->{itemnumber} };
717 && !$itm->{'withdrawn'}
718 && !$itm->{'itemlost'}
719 && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'})
720 && !$itemtypes->{$itm->{'itype'}}->{notforloan}
721 && $itm->{'itemnumber'};
723 $allow_onshelf_holds = Koha::CirculationRules->get_onshelfholds_policy( { item => $item, patron => $patron } )
724 unless $allow_onshelf_holds;
726 # get collection code description, too
727 my $ccode = $itm->{'ccode'};
728 $itm->{'ccode'} = $collections->{$ccode} if defined($ccode) && $collections && exists( $collections->{$ccode} );
729 my $copynumber = $itm->{'copynumber'};
730 $itm->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumbers) && defined($copynumber) && exists( $copynumbers->{$copynumber} ) );
731 if ( defined $itm->{'location'} ) {
732 $itm->{'location_description'} = $shelflocations->{ $itm->{'location'} };
734 if (exists $itm->{itype} && defined($itm->{itype}) && exists $itemtypes->{ $itm->{itype} }) {
735 $itm->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
736 $itm->{'description'} = $itemtypes->{ $itm->{itype} }->{translated_description};
738 foreach (qw(ccode materials enumchron copynumber itemnotes location_description uri)) {
739 $itemfields{$_} = 1 if ($itm->{$_});
742 my $reserve_status = C4::Reserves::GetReserveStatus($itm->{itemnumber});
743 if( $reserve_status eq "Waiting"){ $itm->{'waiting'} = 1; }
744 if( $reserve_status eq "Reserved"){ $itm->{'onhold'} = 1; }
746 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
747 if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
748 $itm->{transfertwhen} = $transfertwhen;
749 $itm->{transfertfrom} = $transfertfrom;
750 $itm->{transfertto} = $transfertto;
753 if ( C4::Context->preference('OPACAcquisitionDetails') ) {
755 if grep { $_ eq $itm->{itemnumber} } @itemnumbers_on_order;
758 if ( C4::Context->preference("OPACLocalCoverImages") == 1 ) {
759 $itm->{cover_images} = $item->cover_images;
762 my $itembranch = $itm->{$separatebranch};
763 if ($currentbranch and C4::Context->preference('OpacSeparateHoldings')) {
764 if ($itembranch and $itembranch eq $currentbranch) {
765 push @itemloop, $itm;
766 $itemloop_has_images++ if $item->cover_images->count;
768 push @otheritemloop, $itm;
769 $otheritemloop_has_images++ if $item->cover_images->count;
772 push @itemloop, $itm;
773 $itemloop_has_images++ if $item->cover_images->count;
778 if( $allow_onshelf_holds || CountItemsIssued($biblionumber) || $biblio->has_items_waiting_or_intransit ) {
779 $template->param( ReservableItems => 1 );
783 itemloop_has_images => $itemloop_has_images,
784 otheritemloop_has_images => $otheritemloop_has_images,
787 # Display only one tab if one items list is empty
788 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
789 $template->param(SeparateHoldings => 0);
790 if (scalar(@itemloop) == 0) {
791 @itemloop = @otheritemloop;
795 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour, opac => 1 });
797 if( C4::Context->preference('ArticleRequests') ) {
798 my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
799 my $itemtype = Koha::ItemTypes->find($biblio->itemtype);
800 my $artreqpossible = $patron
801 ? $biblio->can_article_request( $patron )
803 ? $itemtype->may_article_request
805 $template->param( artreqpossible => $artreqpossible );
809 MARCNOTES => $marcnotesarray,
810 norequests => $norequests,
811 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
812 itemdata_ccode => $itemfields{ccode},
813 itemdata_materials => $itemfields{materials},
814 itemdata_enumchron => $itemfields{enumchron},
815 itemdata_uri => $itemfields{uri},
816 itemdata_copynumber => $itemfields{copynumber},
817 itemdata_itemnotes => $itemfields{itemnotes},
818 itemdata_location => $itemfields{location_description},
819 OpacStarRatings => C4::Context->preference("OpacStarRatings"),
822 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
823 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
824 my $subfields = substr $fieldspec, 3;
825 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
826 my @alternateholdingsinfo = ();
827 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
829 for my $field (@holdingsfields) {
830 my %holding = ( holding => '' );
831 my $havesubfield = 0;
832 for my $subfield ($field->subfields()) {
833 if ((index $subfields, $$subfield[0]) >= 0) {
834 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
835 $holding{'holding'} .= $$subfield[1];
840 push(@alternateholdingsinfo, \%holding);
845 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
849 # FIXME: The template uses this hash directly. Need to filter.
850 foreach ( keys %{$dat} ) {
851 next if ( $HideMARC->{$_} );
852 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
855 # some useful variables for enhanced content;
856 # in each case, we're grabbing the first value we find in
857 # the record and normalizing it
858 my $upc = GetNormalizedUPC($record,$marcflavour);
859 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
860 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
861 my $content_identifier_exists;
862 if ( $isbn or $ean or $oclc or $upc ) {
863 $content_identifier_exists = 1;
866 normalized_upc => $upc,
867 normalized_ean => $ean,
868 normalized_oclc => $oclc,
869 normalized_isbn => $isbn,
870 content_identifier_exists => $content_identifier_exists,
873 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
874 # COinS format FIXME: for books Only
875 my $coins = eval { $biblio->get_coins };
876 $template->param( ocoins => $coins );
878 my ( $loggedincommenter, $reviews );
879 if ( C4::Context->preference('OPACComments') ) {
880 $reviews = Koha::Reviews->search(
882 biblionumber => $biblionumber,
883 -or => { approved => 1, borrowernumber => $borrowernumber }
886 order_by => { -desc => 'datereviewed' }
889 my $libravatar_enabled = 0;
890 if ( C4::Context->preference('ShowReviewer') and C4::Context->preference('ShowReviewerPhoto') ) {
892 require Libravatar::URL;
893 Libravatar::URL->import();
896 $libravatar_enabled = 1;
899 for my $review (@$reviews) {
900 my $review_patron = Koha::Patrons->find( $review->{borrowernumber} ); # FIXME Should be Koha::Review->reviewer or similar
902 # setting some borrower info into this hash
903 if ( $review_patron ) {
904 $review->{patron} = $review_patron;
905 if ( $libravatar_enabled and $review_patron->email ) {
906 $review->{avatarurl} = libravatar_url( email => $review_patron->email, https => $ENV{HTTPS} );
909 if ( $review_patron->borrowernumber eq $borrowernumber ) {
910 $loggedincommenter = 1;
916 if ( C4::Context->preference("OPACISBD") ) {
917 $template->param( ISBD => 1 );
921 itemloop => \@itemloop,
922 otheritemloop => \@otheritemloop,
923 biblionumber => $biblionumber,
924 subscriptions => \@subs,
925 subscriptionsnumber => $subscriptionsnumber,
927 loggedincommenter => $loggedincommenter
931 if (C4::Context->preference("virtualshelves") ) {
932 my $shelves = Koha::Virtualshelves->search(
934 biblionumber => $biblionumber,
938 join => 'virtualshelfcontents',
941 $template->param( shelves => $shelves );
945 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
948 XISBNS => scalar get_xisbns($isbn, $biblionumber)
951 if ($@) { warn "XISBN Failed $@"; }
955 my @sc_fields = $record->field(955);
956 my @lc_fields = $marcflavour eq 'UNIMARC'
957 ? $record->field(930)
958 : $record->field(852);
959 my @serialcollections = ();
961 foreach my $sc_field (@sc_fields) {
964 $row_data{text} = $sc_field->subfield('r');
965 $row_data{branch} = $sc_field->subfield('9');
966 foreach my $lc_field (@lc_fields) {
967 $row_data{itemcallnumber} = $marcflavour eq 'UNIMARC'
968 ? $lc_field->subfield('a') # 930$a
969 : $lc_field->subfield('h') # 852$h
970 if ($sc_field->subfield('5') eq $lc_field->subfield('5'));
973 if ($row_data{text} && $row_data{branch}) {
974 push (@serialcollections, \%row_data);
978 if (scalar(@serialcollections) > 0) {
980 serialcollection => 1,
981 serialcollections => \@serialcollections);
984 # Local cover Images stuff
985 if (C4::Context->preference("OPACLocalCoverImages")){
986 $template->param(OPACLocalCoverImages => 1);
990 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'opac') ) {
991 $template->param( C4::HTML5Media->gethtml5media($record));
994 my $syndetics_elements;
996 if ( C4::Context->preference("SyndeticsEnabled") ) {
997 $template->param("SyndeticsEnabled" => 1);
998 $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
1000 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
1001 for my $element (values %$syndetics_elements) {
1002 $template->param("Syndetics$element"."Exists" => 1 );
1003 #warn "Exists: "."Syndetics$element"."Exists";
1009 if ( C4::Context->preference("SyndeticsEnabled")
1010 && C4::Context->preference("SyndeticsSummary")
1011 && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
1013 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
1014 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
1020 if ( C4::Context->preference("SyndeticsEnabled")
1021 && C4::Context->preference("SyndeticsTOC")
1022 && exists($syndetics_elements->{'TOC'}) ) {
1024 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
1025 $template->param( SYNDETICS_TOC => $syndetics_toc );
1030 if ( C4::Context->preference("SyndeticsEnabled")
1031 && C4::Context->preference("SyndeticsExcerpt")
1032 && exists($syndetics_elements->{'DBCHAPTER'}) ) {
1034 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
1035 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
1040 if ( C4::Context->preference("SyndeticsEnabled")
1041 && C4::Context->preference("SyndeticsReviews")) {
1043 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
1044 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
1049 if ( C4::Context->preference("SyndeticsEnabled")
1050 && C4::Context->preference("SyndeticsAuthorNotes")
1051 && exists($syndetics_elements->{'ANOTES'}) ) {
1053 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
1054 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
1059 # LibraryThingForLibraries ID Code and Tabbed View Option
1060 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
1062 $template->param(LibraryThingForLibrariesID =>
1063 C4::Context->preference('LibraryThingForLibrariesID') );
1064 $template->param(LibraryThingForLibrariesTabbedView =>
1065 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
1069 if( C4::Context->preference('NovelistSelectEnabled') )
1071 $template->param(NovelistSelectProfile => C4::Context->preference('NovelistSelectProfile') );
1072 $template->param(NovelistSelectPassword => C4::Context->preference('NovelistSelectPassword') );
1073 $template->param(NovelistSelectView => C4::Context->preference('NovelistSelectView') );
1078 if ( C4::Context->preference("Babeltheque") ) {
1081 Babeltheque_url_js => C4::Context->preference("Babeltheque_url_js"),
1086 if ( C4::Context->preference( "SocialNetworks" ) ) {
1087 $template->param( current_url => C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber" );
1088 $template->param( SocialNetworks => 1 );
1091 # Shelf Browser Stuff
1092 if (C4::Context->preference("OPACShelfBrowser")) {
1093 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber');
1094 if (defined($starting_itemnumber)) {
1095 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
1096 my $nearby = GetNearbyItems($starting_itemnumber);
1099 starting_itemnumber => $starting_itemnumber,
1100 starting_homebranch => $nearby->{starting_homebranch}->{description},
1101 starting_location => $nearby->{starting_location}->{description},
1102 starting_ccode => $nearby->{starting_ccode}->{description},
1103 shelfbrowser_prev_item => $nearby->{prev_item},
1104 shelfbrowser_next_item => $nearby->{next_item},
1105 shelfbrowser_items => $nearby->{items},
1108 # in which tab shelf browser should open ?
1109 if (grep { $starting_itemnumber == $_->{itemnumber} } @itemloop) {
1110 $template->param(shelfbrowser_tab => 'holdings');
1112 $template->param(shelfbrowser_tab => 'otherholdings');
1117 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("OPACAmazonCoverImages"));
1119 if (C4::Context->preference("BakerTaylorEnabled")) {
1121 BakerTaylorEnabled => 1,
1122 BakerTaylorImageURL => &image_url(),
1123 BakerTaylorLinkURL => &link_url(),
1124 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
1126 my ($bt_user, $bt_pass);
1128 $bt_user = C4::Context->preference('BakerTaylorUsername') and
1129 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
1132 BakerTaylorContentURL =>
1133 sprintf("https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
1134 $bt_user,$bt_pass,$isbn)
1140 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
1143 TagsShowOnDetail => $tag_quantity,
1144 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
1146 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
1147 'sort'=>'-weight', limit=>$tag_quantity}));
1150 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
1151 # These values are going to be read by Javascript, at least in the case
1152 # of the google covers
1153 $template->param(covernewwindow => 'true');
1155 $template->param(covernewwindow => 'false');
1158 $template->param(borrowernumber => $borrowernumber);
1160 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
1161 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
1162 my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
1164 ratings => $ratings,
1165 my_rating => $my_rating,
1169 #Search for title in links
1170 my $marccontrolnumber = GetMarcControlnumber ($record, $marcflavour);
1171 my $marcissns = GetMarcISSN ( $record, $marcflavour );
1172 my $issn = $marcissns->[0] || '';
1174 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
1175 $dat->{title} =~ s/\/+$//; # remove trailing slash
1176 $dat->{title} =~ s/\s+$//; # remove trailing space
1177 $search_for_title = parametrized_url(
1180 TITLE => $dat->{title},
1181 AUTHOR => $dat->{author},
1184 CONTROLNUMBER => $marccontrolnumber,
1185 BIBLIONUMBER => $biblionumber,
1188 $template->param('OPACSearchForTitleIn' => $search_for_title);
1192 if ( C4::Context->preference("IDREF") ) {
1193 # If the record comes from the SUDOC
1194 if ( $record->field('009') ) {
1195 my $unimarc3 = $record->field("009")->data;
1196 if ( $unimarc3 =~ /^\d+$/ ) {
1204 # We try to select the best default tab to show, according to what
1205 # the user wants, and what's available for display
1206 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
1210 $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
1212 $opac_serial_default eq 'serialcollection' && @serialcollections > 0
1213 ? 'serialcollection' :
1214 $opac_serial_default eq 'holdings' && scalar (@itemloop) > 0
1216 scalar (@itemloop) == 0
1218 $subscriptionsnumber
1220 @serialcollections > 0
1221 ? 'serialcollection' : 'subscriptions';
1222 $template->param('defaulttab' => $defaulttab);
1224 if (C4::Context->preference('OPACLocalCoverImages') == 1) {
1225 $template->param( localimages => $biblio->cover_images );
1228 $template->{VARS}->{OPACPopupAuthorsSearch} = C4::Context->preference('OPACPopupAuthorsSearch');
1230 if (C4::Context->preference('OpacHighlightedWords')) {
1231 $template->{VARS}->{query_desc} = $query->param('query_desc');
1233 $template->{VARS}->{'trackclicks'} = C4::Context->preference('TrackClicks');
1235 if ( C4::Context->preference('UseCourseReserves') ) {
1236 foreach my $i ( @items ) {
1237 $i->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $i->{'itemnumber'} );
1242 'OpacLocationBranchToDisplay' => C4::Context->preference('OpacLocationBranchToDisplay'),
1245 output_html_with_http_headers $query, $cookie, $template->output;