3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 use C4::Serials; #uses getsubscriptionfrom biblionumber
34 use C4::XISBN qw(get_xisbns);
35 use C4::External::Amazon;
36 use C4::Search; # enabled_staff_search_views
37 use C4::Tags qw(get_tags);
41 use C4::CourseReserves qw(GetItemCourseReservesInfo);
42 use C4::Acquisition qw(GetOrdersByBiblionumber);
43 use Koha::AuthorisedValues;
45 use Koha::CoverImages;
46 use Koha::Illrequests;
50 use Koha::Virtualshelves;
52 use Koha::SearchEngine::Search;
53 use Koha::SearchEngine::QueryBuilder;
55 my $query = CGI->new();
57 my $analyze = $query->param('analyze');
59 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
61 template_name => 'catalogue/detail.tt',
64 flagsrequired => { catalogue => 1 },
68 # Determine if we should be offering any enhancement plugin buttons
69 if ( C4::Context->config('enable_plugins') ) {
70 # Only pass plugins that can offer a toolbar button
71 my @plugins = Koha::Plugins->new()->GetPlugins({
72 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
79 my $biblionumber = $query->param('biblionumber');
80 $biblionumber = HTML::Entities::encode($biblionumber);
81 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
82 my $biblio = Koha::Biblios->find( $biblionumber );
83 $template->param( 'biblio', $biblio );
85 if ( not defined $record ) {
86 # biblionumber invalid -> report and exit
87 $template->param( unknownbiblionumber => 1,
88 biblionumber => $biblionumber );
89 output_html_with_http_headers $query, $cookie, $template->output;
93 eval { $biblio->metadata->record };
94 $template->param( decoding_error => $@ );
96 if($query->cookie("holdfor")){
97 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
98 if ( $holdfor_patron ) {
100 # FIXME Should pass the patron object
101 holdfor => $query->cookie("holdfor"),
102 holdfor_surname => $holdfor_patron->surname,
103 holdfor_firstname => $holdfor_patron->firstname,
104 holdfor_cardnumber => $holdfor_patron->cardnumber,
109 if($query->cookie("searchToOrder")){
110 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
112 searchtoorder_basketno => $basketno,
113 searchtoorder_vendorid => $vendorid
117 my $fw = GetFrameworkCode($biblionumber);
118 my $showallitems = $query->param('showallitems');
119 my $marcflavour = C4::Context->preference("marcflavour");
121 # XSLT processing of some stuff
122 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
123 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
124 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
128 my $searcher = Koha::SearchEngine::Search->new(
129 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
131 my $builder = Koha::SearchEngine::QueryBuilder->new(
132 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
134 my $cleaned_title = $biblio->title;
135 $cleaned_title =~ tr|/||;
136 $cleaned_title = $builder->clean_search_term($cleaned_title);
139 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
140 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
141 : "Host-item:(\"$cleaned_title\")";
142 my ( $err, $result, $count );
144 ( $err, $result, $count ) =
145 $searcher->simple_search_compat( $query, 0, 0 );
150 $error .= $err if $err;
152 warn "Warning from simple_search_compat: $error";
153 $template->param( analytics_error => 1 );
156 my $show_analytics_link = defined $count && $count > 0 ? 1 : 0;
159 show_analytics_link => $show_analytics_link,
160 ( $show_analytics_link ? ( analytics_query => $cleaned_title ) : () )
164 XSLTDetailsDisplay => '1',
165 XSLTBloc => XSLTParse4Display(
166 $biblionumber, $record, "XSLTDetailsDisplay", 1,
167 undef, $sysxml, $xslfile, $lang,
173 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
175 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
176 # Do not propagate it as we already deal with it previously in this script
177 my $coins = eval { $biblio->get_coins };
178 $template->param( ocoins => $coins );
180 # some useful variables for enhanced content;
181 # in each case, we're grabbing the first value we find in
182 # the record and normalizing it
183 my $upc = GetNormalizedUPC($record,$marcflavour);
184 my $ean = GetNormalizedEAN($record,$marcflavour);
185 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
186 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
189 normalized_upc => $upc,
190 normalized_ean => $ean,
191 normalized_oclc => $oclc,
192 normalized_isbn => $isbn,
195 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
197 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
199 my $dbh = C4::Context->dbh;
201 my @all_items = GetItemsInfo( $biblionumber );
203 my $patron = Koha::Patrons->find( $borrowernumber );
204 for my $itm (@all_items) {
205 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
208 # flag indicating existence of at least one item linked via a host record
210 # adding items linked via host biblios
211 my @hostitems = GetHostItemsInfo($record);
214 push (@items,@hostitems);
217 my $dat = &GetBiblioData($biblionumber);
219 #coping with subscriptions
220 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
221 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
224 foreach my $subscription (@subscriptions) {
226 my $serials_to_display;
227 $cell{subscriptionid} = $subscription->{subscriptionid};
228 $cell{subscriptionnotes} = $subscription->{internalnotes};
229 $cell{missinglist} = $subscription->{missinglist};
230 $cell{librariannote} = $subscription->{librariannote};
231 $cell{branchcode} = $subscription->{branchcode};
232 $cell{hasalert} = $subscription->{hasalert};
233 $cell{callnumber} = $subscription->{callnumber};
234 $cell{location} = $subscription->{location};
235 $cell{closed} = $subscription->{closed};
236 #get the three latest serials.
237 $serials_to_display = $subscription->{staffdisplaycount};
238 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
239 $cell{staffdisplaycount} = $serials_to_display;
240 $cell{latestserials} =
241 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
245 # Get acquisition details
246 if ( C4::Context->preference('AcquisitionDetails') ) {
247 my $orders = Koha::Acquisition::Orders->search(
248 { biblionumber => $biblionumber },
251 order_by => 'basketno.booksellerid'
253 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
260 if ( C4::Context->preference('suggestion') ) {
261 my $suggestions = Koha::Suggestions->search(
263 biblionumber => $biblionumber,
267 order_by => { -desc => 'suggesteddate' }
270 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
271 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
274 if ( defined $dat->{'itemtype'} ) {
275 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
278 $dat->{'count'} = scalar @all_items + @hostitems;
279 $dat->{'showncount'} = scalar @items + @hostitems;
280 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
283 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
285 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
287 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
288 my (@itemloop, @otheritemloop, %itemfields);
290 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
292 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
294 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
296 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
298 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
300 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
303 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
306 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
307 if ($materials_authvals) {
308 foreach my $value (@$materials_authvals) {
309 $materials_map{$value->{authorised_value}} = $value->{lib};
315 my $materials_flag; # set this if the items have anything in the materials field
316 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
317 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
318 $template->param(SeparateHoldings => 1);
320 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
321 my ( $itemloop_has_images, $otheritemloop_has_images );
322 foreach my $item (@items) {
323 my $itembranchcode = $item->{$separatebranch};
325 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
328 $item->{datedue} = format_sqldatetime($item->{datedue});
330 #get shelf location and collection code description if they are authorised value.
331 # same thing for copy number
332 my $shelfcode = $item->{'location'};
333 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
334 my $ccode = $item->{'ccode'};
335 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
336 my $copynumber = $item->{'copynumber'};
337 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
338 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri publisheddate)) { # Warning when removing GetItemsInfo - publisheddate (at least) is not part of the items table
339 $itemfields{$_} = 1 if ( $item->{$_} );
343 my $item_object = Koha::Items->find( $item->{itemnumber} );
344 my $holds = $item_object->current_holds;
345 if ( my $first_hold = $holds->next ) {
346 $item->{first_hold} = $first_hold;
349 if ( my $checkout = $item_object->checkout ) {
350 $item->{CheckedOutFor} = $checkout->patron;
353 # Check the transit status
354 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
355 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
356 $item->{transfertwhen} = $transfertwhen;
357 $item->{transfertfrom} = $transfertfrom;
358 $item->{transfertto} = $transfertto;
359 $item->{nocancel} = 1;
362 foreach my $f (qw( itemnotes )) {
364 $item->{$f} =~ s|\n|<br />|g;
369 #item has a host number if its biblio number does not match the current bib
371 if ($item->{biblionumber} ne $biblionumber){
372 $item->{hostbiblionumber} = $item->{biblionumber};
373 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
378 # count if item is used in analytical bibliorecords
379 # The 'countanalytics' flag is only used in the templates if analyze is set
380 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
381 if ($countanalytics > 0){
383 $item->{countanalytics} = $countanalytics;
387 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
389 if (defined $materials_map{ $item->{materials} }) {
390 $item->{materials} = $materials_map{ $item->{materials} };
394 if ( C4::Context->preference('UseCourseReserves') ) {
395 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
398 if ( C4::Context->preference('IndependentBranches') ) {
399 my $userenv = C4::Context->userenv();
400 if ( not C4::Context->IsSuperLibrarian()
401 and $userenv->{branch} ne $item->{homebranch} ) {
402 $item->{cannot_be_edited} = 1;
406 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
407 $item->{cover_images} = $item_object->cover_images;
410 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
411 if ($itembranchcode and $itembranchcode eq $currentbranch) {
412 push @itemloop, $item;
413 $itemloop_has_images++ if $item_object->cover_images->count;
415 push @otheritemloop, $item;
416 $otheritemloop_has_images++ if $item_object->cover_images->count;
419 push @itemloop, $item;
420 $itemloop_has_images++ if $item_object->cover_images->count;
425 itemloop_has_images => $itemloop_has_images,
426 otheritemloop_has_images => $otheritemloop_has_images,
429 # Display only one tab if one items list is empty
430 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
431 $template->param(SeparateHoldings => 0);
432 if (scalar(@itemloop) == 0) {
433 @itemloop = @otheritemloop;
437 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
439 borrowernumber => $borrowernumber,
444 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
446 borrowernumber => $borrowernumber,
454 add_to_some_private_shelves => $some_private_shelves,
455 add_to_some_public_shelves => $some_public_shelves,
459 MARCNOTES => $marcnotesarray,
460 itemdata_ccode => $itemfields{ccode},
461 itemdata_enumchron => $itemfields{enumchron},
462 itemdata_uri => $itemfields{uri},
463 itemdata_copynumber => $itemfields{copynumber},
464 itemdata_stocknumber => $itemfields{stocknumber},
465 itemdata_publisheddate => $itemfields{publisheddate},
466 volinfo => $itemfields{enumchron},
467 itemdata_itemnotes => $itemfields{itemnotes},
468 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
469 z3950_search_params => C4::Search::z3950_search_args($dat),
470 hostrecords => $hostrecords,
471 analytics_flag => $analytics_flag,
472 C4::Search::enabled_staff_search_views,
473 materials => $materials_flag,
476 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
477 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
478 my $subfields = substr $fieldspec, 3;
479 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
480 my @alternateholdingsinfo = ();
481 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
483 for my $field (@holdingsfields) {
484 my %holding = ( holding => '' );
485 my $havesubfield = 0;
486 for my $subfield ($field->subfields()) {
487 if ((index $subfields, $$subfield[0]) >= 0) {
488 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
489 $holding{'holding'} .= $$subfield[1];
494 push(@alternateholdingsinfo, \%holding);
499 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
503 my @results = ( $dat, );
504 foreach ( keys %{$dat} ) {
505 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
508 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
509 # method query not found?!?!
510 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
512 itemloop => \@itemloop,
513 otheritemloop => \@otheritemloop,
514 biblionumber => $biblionumber,
515 ($analyze? 'analyze':'detailview') =>1,
516 subscriptions => \@subs,
517 subscriptionsnumber => $subscriptionsnumber,
518 subscriptiontitle => $dat->{title},
519 searchid => scalar $query->param('searchid'),
522 # $debug and $template->param(debug_display => 1);
526 if (C4::Context->preference("virtualshelves") ) {
527 my $shelves = Koha::Virtualshelves->search(
529 biblionumber => $biblionumber,
533 join => 'virtualshelfcontents',
536 $template->param( 'shelves' => $shelves );
540 if (C4::Context->preference("FRBRizeEditions")==1) {
543 XISBNS => scalar get_xisbns($isbn, $biblionumber)
546 if ($@) { warn "XISBN Failed $@"; }
549 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
550 my $images = $biblio->cover_images;
551 $template->param( localimages => $biblio->cover_images );
555 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
556 $template->param( C4::HTML5Media->gethtml5media($record));
561 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
564 TagsShowOnDetail => $tag_quantity
566 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
567 'sort'=>'-weight', limit=>$tag_quantity}));
570 #we only need to pass the number of holds to the template
571 my $holds = $biblio->holds;
572 $template->param( holdcount => $holds->count );
574 # Check if there are any ILL requests connected to the biblio
576 C4::Context->preference('ILLModule')
577 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
579 $template->param( illrequests => $illrequests );
581 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
582 if ($StaffDetailItemSelection) {
583 # Only enable item selection if user can execute at least one action
585 $flags->{superlibrarian}
587 ref $flags->{tools} eq 'HASH' && (
588 $flags->{tools}->{items_batchmod} # Modify selected items
589 || $flags->{tools}->{items_batchdel} # Delete selected items
592 || ( ref $flags->{tools} eq '' && $flags->{tools} )
596 StaffDetailItemSelection => $StaffDetailItemSelection );
600 # get biblionumbers stored in the cart
603 if($query->cookie("intranet_bib_list")){
604 my $cart_list = $query->cookie("intranet_bib_list");
605 @cart_list = split(/\//, $cart_list);
606 if ( grep {$_ eq $biblionumber} @cart_list) {
607 $template->param( incart => 1 );
611 $template->param(biblio => $biblio);
613 output_html_with_http_headers $query, $cookie, $template->output;