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>.
23 use C4::Auth qw( get_template_and_user );
27 getitemtypeimagelocation
30 GetNormalizedOCLCNumber
33 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
34 use C4::Output qw( output_html_with_http_headers );
35 use C4::Biblio qw( GetBiblioData GetFrameworkCode GetMarcBiblio );
36 use C4::Items qw( GetAnalyticsCount GetHostItemsInfo GetItemsInfo );
37 use C4::Circulation qw( GetTransfers );
39 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
40 use C4::XISBN qw( get_xisbns );
41 use C4::External::Amazon qw( get_amazon_tld );
42 use C4::Search qw( z3950_search_args enabled_staff_search_views new_record_from_zebra );
43 use C4::Tags qw( get_tags );
44 use C4::XSLT qw( XSLTParse4Display );
45 use Koha::DateUtils qw( format_sqldatetime );
47 use C4::CourseReserves qw( GetItemCourseReservesInfo );
48 use Koha::AuthorisedValues;
50 use Koha::CoverImages;
51 use Koha::Illrequests;
55 use Koha::Virtualshelves;
57 use Koha::SearchEngine::Search;
58 use Koha::SearchEngine::QueryBuilder;
60 my $query = CGI->new();
62 my $analyze = $query->param('analyze');
64 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
66 template_name => 'catalogue/detail.tt',
69 flagsrequired => { catalogue => 1 },
73 # Determine if we should be offering any enhancement plugin buttons
74 if ( C4::Context->config('enable_plugins') ) {
75 # Only pass plugins that can offer a toolbar button
76 my @plugins = Koha::Plugins->new()->GetPlugins({
77 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
84 my $biblionumber = $query->param('biblionumber');
85 $biblionumber = HTML::Entities::encode($biblionumber);
86 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
87 my $biblio = Koha::Biblios->find( $biblionumber );
88 $template->param( 'biblio', $biblio );
90 if ( not defined $record ) {
91 # biblionumber invalid -> report and exit
92 $template->param( unknownbiblionumber => 1,
93 biblionumber => $biblionumber );
94 output_html_with_http_headers $query, $cookie, $template->output;
98 eval { $biblio->metadata->record };
99 $template->param( decoding_error => $@ );
101 if($query->cookie("holdfor")){
102 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
103 if ( $holdfor_patron ) {
105 # FIXME Should pass the patron object
106 holdfor => $query->cookie("holdfor"),
107 holdfor_surname => $holdfor_patron->surname,
108 holdfor_firstname => $holdfor_patron->firstname,
109 holdfor_cardnumber => $holdfor_patron->cardnumber,
114 if($query->cookie("searchToOrder")){
115 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
117 searchtoorder_basketno => $basketno,
118 searchtoorder_vendorid => $vendorid
122 my $fw = GetFrameworkCode($biblionumber);
123 my $showallitems = $query->param('showallitems');
124 my $marcflavour = C4::Context->preference("marcflavour");
126 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
128 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
129 # Do not propagate it as we already deal with it previously in this script
130 my $coins = eval { $biblio->get_coins };
131 $template->param( ocoins => $coins );
133 # some useful variables for enhanced content;
134 # in each case, we're grabbing the first value we find in
135 # the record and normalizing it
136 my $upc = GetNormalizedUPC($record,$marcflavour);
137 my $ean = GetNormalizedEAN($record,$marcflavour);
138 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
139 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
142 normalized_upc => $upc,
143 normalized_ean => $ean,
144 normalized_oclc => $oclc,
145 normalized_isbn => $isbn,
148 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
150 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
152 my $dbh = C4::Context->dbh;
154 my @all_items = GetItemsInfo( $biblionumber );
156 my $patron = Koha::Patrons->find( $borrowernumber );
157 for my $itm (@all_items) {
158 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
161 # flag indicating existence of at least one item linked via a host record
163 # adding items linked via host biblios
164 my @hostitems = GetHostItemsInfo($record);
167 push (@items,@hostitems);
170 my $dat = &GetBiblioData($biblionumber);
172 #coping with subscriptions
173 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
174 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
177 foreach my $subscription (@subscriptions) {
179 my $serials_to_display;
180 $cell{subscriptionid} = $subscription->{subscriptionid};
181 $cell{subscriptionnotes} = $subscription->{internalnotes};
182 $cell{missinglist} = $subscription->{missinglist};
183 $cell{librariannote} = $subscription->{librariannote};
184 $cell{branchcode} = $subscription->{branchcode};
185 $cell{hasalert} = $subscription->{hasalert};
186 $cell{callnumber} = $subscription->{callnumber};
187 $cell{location} = $subscription->{location};
188 $cell{closed} = $subscription->{closed};
189 #get the three latest serials.
190 $serials_to_display = $subscription->{staffdisplaycount};
191 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
192 $cell{staffdisplaycount} = $serials_to_display;
193 $cell{latestserials} =
194 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
198 # Get component parts details
199 my $showcomp = C4::Context->preference('ShowComponentRecords');
201 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
202 if ( my $components = $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) ) {
203 $show_analytics = 1 if @{$components}; # just show link when having results
204 $template->param( analytics_error => 1 ) if @{$biblio->messages};
206 for my $part ( @{$components} ) {
207 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
208 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
215 xsl_syspref => "XSLTResultsDisplay",
220 $template->param( ComponentParts => $parts );
221 $template->param( ComponentPartsQuery => $biblio->get_components_query );
223 } else { # check if we should show analytics anyway
224 $show_analytics = 1 if @{$biblio->get_marc_components(1)}; # count matters here, results does not
225 $template->param( analytics_error => 1 ) if @{$biblio->messages};
228 # XSLT processing of some stuff
229 my $xslt_variables = { show_analytics_link => $show_analytics };
231 XSLTDetailsDisplay => '1',
232 XSLTBloc => XSLTParse4Display({
233 biblionumber => $biblionumber,
235 xsl_syspref => "XSLTDetailsDisplay",
237 xslt_variables => $xslt_variables,
241 # Get acquisition details
242 if ( C4::Context->preference('AcquisitionDetails') ) {
243 my $orders = Koha::Acquisition::Orders->search(
244 { biblionumber => $biblionumber },
247 order_by => 'basketno.booksellerid'
249 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
256 if ( C4::Context->preference('suggestion') ) {
257 my $suggestions = Koha::Suggestions->search(
259 biblionumber => $biblionumber,
263 order_by => { -desc => 'suggesteddate' }
266 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
267 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
270 if ( defined $dat->{'itemtype'} ) {
271 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
274 $dat->{'count'} = scalar @all_items + @hostitems;
275 $dat->{'showncount'} = scalar @items + @hostitems;
276 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
279 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
281 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
283 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
284 my (@itemloop, @otheritemloop, %itemfields);
286 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
288 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
290 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
292 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
294 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
296 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
299 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
302 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
303 if ($materials_authvals) {
304 foreach my $value (@$materials_authvals) {
305 $materials_map{$value->{authorised_value}} = $value->{lib};
311 my $materials_flag; # set this if the items have anything in the materials field
312 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
313 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
314 $template->param(SeparateHoldings => 1);
316 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
317 my ( $itemloop_has_images, $otheritemloop_has_images );
318 foreach my $item (@items) {
319 my $itembranchcode = $item->{$separatebranch};
321 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
324 $item->{datedue} = format_sqldatetime($item->{datedue});
326 #get shelf location and collection code description if they are authorised value.
327 # same thing for copy number
328 my $shelfcode = $item->{'location'};
329 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
330 my $ccode = $item->{'ccode'};
331 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
332 my $copynumber = $item->{'copynumber'};
333 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
334 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
335 $itemfields{$_} = 1 if ( $item->{$_} );
339 my $item_object = Koha::Items->find( $item->{itemnumber} );
340 my $holds = $item_object->current_holds;
341 if ( my $first_hold = $holds->next ) {
342 $item->{first_hold} = $first_hold;
345 if ( my $checkout = $item_object->checkout ) {
346 $item->{CheckedOutFor} = $checkout->patron;
349 # Check the transit status
350 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
351 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
352 $item->{transfertwhen} = $transfertwhen;
353 $item->{transfertfrom} = $transfertfrom;
354 $item->{transfertto} = $transfertto;
355 $item->{nocancel} = 1;
358 foreach my $f (qw( itemnotes )) {
360 $item->{$f} =~ s|\n|<br />|g;
365 #item has a host number if its biblio number does not match the current bib
367 if ($item->{biblionumber} ne $biblionumber){
368 $item->{hostbiblionumber} = $item->{biblionumber};
369 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
374 # count if item is used in analytical bibliorecords
375 # The 'countanalytics' flag is only used in the templates if analyze is set
376 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
377 if ($countanalytics > 0){
379 $item->{countanalytics} = $countanalytics;
383 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
385 if (defined $materials_map{ $item->{materials} }) {
386 $item->{materials} = $materials_map{ $item->{materials} };
390 if ( C4::Context->preference('UseCourseReserves') ) {
391 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
394 if ( C4::Context->preference('IndependentBranches') ) {
395 my $userenv = C4::Context->userenv();
396 if ( not C4::Context->IsSuperLibrarian()
397 and $userenv->{branch} ne $item->{homebranch} ) {
398 $item->{cannot_be_edited} = 1;
402 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
403 $item->{cover_images} = $item_object->cover_images;
406 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
407 if ($itembranchcode and $itembranchcode eq $currentbranch) {
408 push @itemloop, $item;
409 $itemloop_has_images++ if $item_object->cover_images->count;
411 push @otheritemloop, $item;
412 $otheritemloop_has_images++ if $item_object->cover_images->count;
415 push @itemloop, $item;
416 $itemloop_has_images++ if $item_object->cover_images->count;
421 itemloop_has_images => $itemloop_has_images,
422 otheritemloop_has_images => $otheritemloop_has_images,
425 # Display only one tab if one items list is empty
426 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
427 $template->param(SeparateHoldings => 0);
428 if (scalar(@itemloop) == 0) {
429 @itemloop = @otheritemloop;
433 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
435 borrowernumber => $borrowernumber,
440 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
442 borrowernumber => $borrowernumber,
450 add_to_some_private_shelves => $some_private_shelves,
451 add_to_some_public_shelves => $some_public_shelves,
455 MARCNOTES => $marcnotesarray,
456 itemdata_ccode => $itemfields{ccode},
457 itemdata_enumchron => $itemfields{enumchron},
458 itemdata_uri => $itemfields{uri},
459 itemdata_copynumber => $itemfields{copynumber},
460 itemdata_stocknumber => $itemfields{stocknumber},
461 itemdata_publisheddate => $itemfields{publisheddate},
462 volinfo => $itemfields{enumchron},
463 itemdata_itemnotes => $itemfields{itemnotes},
464 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
465 z3950_search_params => C4::Search::z3950_search_args($dat),
466 hostrecords => $hostrecords,
467 analytics_flag => $analytics_flag,
468 C4::Search::enabled_staff_search_views,
469 materials => $materials_flag,
472 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
473 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
474 my $subfields = substr $fieldspec, 3;
475 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
476 my @alternateholdingsinfo = ();
477 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
479 for my $field (@holdingsfields) {
480 my %holding = ( holding => '' );
481 my $havesubfield = 0;
482 for my $subfield ($field->subfields()) {
483 if ((index $subfields, $$subfield[0]) >= 0) {
484 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
485 $holding{'holding'} .= $$subfield[1];
490 push(@alternateholdingsinfo, \%holding);
495 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
499 my @results = ( $dat, );
500 foreach ( keys %{$dat} ) {
501 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
504 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
505 # method query not found?!?!
506 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
508 itemloop => \@itemloop,
509 otheritemloop => \@otheritemloop,
510 biblionumber => $biblionumber,
511 ($analyze? 'analyze':'detailview') =>1,
512 subscriptions => \@subs,
513 subscriptionsnumber => $subscriptionsnumber,
514 subscriptiontitle => $dat->{title},
515 searchid => scalar $query->param('searchid'),
520 if (C4::Context->preference("virtualshelves") ) {
521 my $shelves = Koha::Virtualshelves->search(
523 biblionumber => $biblionumber,
527 join => 'virtualshelfcontents',
530 $template->param( 'shelves' => $shelves );
534 if (C4::Context->preference("FRBRizeEditions")==1) {
537 XISBNS => scalar get_xisbns($isbn, $biblionumber)
540 if ($@) { warn "XISBN Failed $@"; }
543 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
544 my $images = $biblio->cover_images;
545 $template->param( localimages => $biblio->cover_images );
549 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
550 $template->param( C4::HTML5Media->gethtml5media($record));
555 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
558 TagsShowOnDetail => $tag_quantity
560 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
561 'sort'=>'-weight', limit=>$tag_quantity}));
564 #we only need to pass the number of holds to the template
565 my $holds = $biblio->holds;
566 $template->param( holdcount => $holds->count );
568 # Check if there are any ILL requests connected to the biblio
570 C4::Context->preference('ILLModule')
571 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
573 $template->param( illrequests => $illrequests );
575 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
576 if ($StaffDetailItemSelection) {
577 # Only enable item selection if user can execute at least one action
579 $flags->{superlibrarian}
581 ref $flags->{tools} eq 'HASH' && (
582 $flags->{tools}->{items_batchmod} # Modify selected items
583 || $flags->{tools}->{items_batchdel} # Delete selected items
586 || ( ref $flags->{tools} eq '' && $flags->{tools} )
590 StaffDetailItemSelection => $StaffDetailItemSelection );
594 # get biblionumbers stored in the cart
597 if($query->cookie("intranet_bib_list")){
598 my $cart_list = $query->cookie("intranet_bib_list");
599 @cart_list = split(/\//, $cart_list);
600 if ( grep {$_ eq $biblionumber} @cart_list) {
601 $template->param( incart => 1 );
605 if ( C4::Context->preference('UseCourseReserves') ) {
606 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
607 $template->param( course_reserves => $course_reserves );
610 $template->param(biblio => $biblio);
612 output_html_with_http_headers $query, $cookie, $template->output;