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;
58 use Koha::SearchEngine::Search;
59 use Koha::SearchEngine::QueryBuilder;
61 my $query = CGI->new();
63 my $analyze = $query->param('analyze');
65 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
67 template_name => 'catalogue/detail.tt',
70 flagsrequired => { catalogue => 1 },
74 # Determine if we should be offering any enhancement plugin buttons
75 if ( C4::Context->config('enable_plugins') ) {
76 # Only pass plugins that can offer a toolbar button
77 my @plugins = Koha::Plugins->new()->GetPlugins({
78 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
85 my $biblionumber = $query->param('biblionumber');
86 $biblionumber = HTML::Entities::encode($biblionumber);
87 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
88 my $biblio = Koha::Biblios->find( $biblionumber );
89 $template->param( 'biblio', $biblio );
91 if ( not defined $record ) {
92 # biblionumber invalid -> report and exit
93 $template->param( unknownbiblionumber => 1,
94 biblionumber => $biblionumber );
95 output_html_with_http_headers $query, $cookie, $template->output;
99 my $marc_record = eval { $biblio->metadata->record };
100 $template->param( decoding_error => $@ );
102 if($query->cookie("holdfor")){
103 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
104 if ( $holdfor_patron ) {
106 holdfor => $query->cookie("holdfor"),
107 holdfor_patron => $holdfor_patron,
112 if($query->cookie("searchToOrder")){
113 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
115 searchtoorder_basketno => $basketno,
116 searchtoorder_vendorid => $vendorid
120 my $fw = GetFrameworkCode($biblionumber);
121 my $showallitems = $query->param('showallitems');
122 my $marcflavour = C4::Context->preference("marcflavour");
124 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
126 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
127 # Do not propagate it as we already deal with it previously in this script
128 my $coins = eval { $biblio->get_coins };
129 $template->param( ocoins => $coins );
131 # some useful variables for enhanced content;
132 # in each case, we're grabbing the first value we find in
133 # the record and normalizing it
134 my $upc = GetNormalizedUPC($record,$marcflavour);
135 my $ean = GetNormalizedEAN($record,$marcflavour);
136 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
137 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
138 my $content_identifier_exists;
139 if ( $isbn or $ean or $oclc or $upc ) {
140 $content_identifier_exists = 1;
144 normalized_upc => $upc,
145 normalized_ean => $ean,
146 normalized_oclc => $oclc,
147 normalized_isbn => $isbn,
148 content_identifier_exists => $content_identifier_exists,
151 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
153 my $dbh = C4::Context->dbh;
155 my @all_items = GetItemsInfo( $biblionumber );
157 my $patron = Koha::Patrons->find( $borrowernumber );
158 for my $itm (@all_items) {
159 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
162 # flag indicating existence of at least one item linked via a host record
164 # adding items linked via host biblios
165 my @hostitems = GetHostItemsInfo($record);
168 push (@items,@hostitems);
171 my $dat = &GetBiblioData($biblionumber);
173 #coping with subscriptions
174 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
175 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
178 foreach my $subscription (@subscriptions) {
180 my $serials_to_display;
181 $cell{subscriptionid} = $subscription->{subscriptionid};
182 $cell{subscriptionnotes} = $subscription->{internalnotes};
183 $cell{missinglist} = $subscription->{missinglist};
184 $cell{librariannote} = $subscription->{librariannote};
185 $cell{branchcode} = $subscription->{branchcode};
186 $cell{hasalert} = $subscription->{hasalert};
187 $cell{callnumber} = $subscription->{callnumber};
188 $cell{location} = $subscription->{location};
189 $cell{closed} = $subscription->{closed};
190 #get the three latest serials.
191 $serials_to_display = $subscription->{staffdisplaycount};
192 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
193 $cell{staffdisplaycount} = $serials_to_display;
194 $cell{latestserials} =
195 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
199 # Get component parts details
200 my $showcomp = C4::Context->preference('ShowComponentRecords');
202 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
203 if ( my $components = $marc_record ? $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) : undef ) {
204 $show_analytics = 1 if @{$components}; # just show link when having results
205 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
207 for my $part ( @{$components} ) {
208 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
209 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
216 xsl_syspref => "XSLTResultsDisplay",
221 $template->param( ComponentParts => $parts );
222 my ( $comp_query, $comp_sort ) = $biblio->get_components_query;
223 my $cpq = $comp_query . "&sort_by=" . $comp_sort;
224 $template->param( ComponentPartsQuery => $cpq );
226 } else { # check if we should show analytics anyway
227 $show_analytics = 1 if $marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
228 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
231 # XSLT processing of some stuff
232 my $xslt_variables = { show_analytics_link => $show_analytics };
234 XSLTDetailsDisplay => '1',
235 XSLTBloc => XSLTParse4Display({
236 biblionumber => $biblionumber,
238 xsl_syspref => "XSLTDetailsDisplay",
240 xslt_variables => $xslt_variables,
244 # Get acquisition details
245 if ( C4::Context->preference('AcquisitionDetails') ) {
246 my $orders = Koha::Acquisition::Orders->search(
247 { biblionumber => $biblionumber },
250 order_by => 'basketno.booksellerid'
252 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
259 if ( C4::Context->preference('suggestion') ) {
260 my $suggestions = Koha::Suggestions->search(
262 biblionumber => $biblionumber,
266 order_by => { -desc => 'suggesteddate' }
269 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
270 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
273 if ( defined $dat->{'itemtype'} ) {
274 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
277 $dat->{'count'} = scalar @all_items + @hostitems;
278 $dat->{'showncount'} = scalar @items + @hostitems;
279 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
282 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
284 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
286 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
287 my (@itemloop, @otheritemloop, %itemfields);
289 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
291 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
293 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
295 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
297 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
299 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
302 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
305 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
306 if ($materials_authvals) {
307 foreach my $value (@$materials_authvals) {
308 $materials_map{$value->{authorised_value}} = $value->{lib};
314 my $materials_flag; # set this if the items have anything in the materials field
315 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
316 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
317 $template->param(SeparateHoldings => 1);
319 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
320 my ( $itemloop_has_images, $otheritemloop_has_images );
321 foreach my $item (@items) {
322 my $itembranchcode = $item->{$separatebranch};
324 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
327 $item->{datedue} = format_sqldatetime($item->{datedue});
329 #get shelf location and collection code description if they are authorised value.
330 # same thing for copy number
331 my $shelfcode = $item->{'location'};
332 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
333 my $ccode = $item->{'ccode'};
334 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
335 my $copynumber = $item->{'copynumber'};
336 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
337 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
338 $itemfields{$_} = 1 if ( $item->{$_} );
342 my $item_object = Koha::Items->find( $item->{itemnumber} );
343 my $holds = $item_object->current_holds;
344 if ( my $first_hold = $holds->next ) {
345 $item->{first_hold} = $first_hold;
348 if ( my $checkout = $item_object->checkout ) {
349 $item->{CheckedOutFor} = $checkout->patron;
352 # Check the transit status
353 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
354 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
355 $item->{transfertwhen} = $transfertwhen;
356 $item->{transfertfrom} = $transfertfrom;
357 $item->{transfertto} = $transfertto;
358 $item->{nocancel} = 1;
361 foreach my $f (qw( itemnotes )) {
363 $item->{$f} =~ s|\n|<br />|g;
368 #item has a host number if its biblio number does not match the current bib
370 if ($item->{biblionumber} ne $biblionumber){
371 $item->{hostbiblionumber} = $item->{biblionumber};
372 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
377 # count if item is used in analytical bibliorecords
378 # The 'countanalytics' flag is only used in the templates if analyze is set
379 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
380 if ($countanalytics > 0){
382 $item->{countanalytics} = $countanalytics;
386 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
388 if (defined $materials_map{ $item->{materials} }) {
389 $item->{materials} = $materials_map{ $item->{materials} };
393 if ( C4::Context->preference('UseCourseReserves') ) {
394 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
397 if ( C4::Context->preference('IndependentBranches') ) {
398 my $userenv = C4::Context->userenv();
399 if ( not C4::Context->IsSuperLibrarian()
400 and $userenv->{branch} ne $item->{homebranch} ) {
401 $item->{cannot_be_edited} = 1;
405 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
406 $item->{cover_images} = $item_object->cover_images;
409 if ( C4::Context->preference('UseRecalls') ) {
410 my $recall = Koha::Recalls->find({ item_id => $item->{itemnumber}, completed => 0 });
411 if ( defined $recall ) {
412 $item->{recalled} = 1;
413 $item->{recall} = $recall;
417 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
418 if ($itembranchcode and $itembranchcode eq $currentbranch) {
419 push @itemloop, $item;
420 $itemloop_has_images++ if $item_object->cover_images->count;
422 push @otheritemloop, $item;
423 $otheritemloop_has_images++ if $item_object->cover_images->count;
426 push @itemloop, $item;
427 $itemloop_has_images++ if $item_object->cover_images->count;
432 itemloop_has_images => $itemloop_has_images,
433 otheritemloop_has_images => $otheritemloop_has_images,
436 # Display only one tab if one items list is empty
437 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
438 $template->param(SeparateHoldings => 0);
439 if (scalar(@itemloop) == 0) {
440 @itemloop = @otheritemloop;
444 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
446 borrowernumber => $borrowernumber,
451 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
453 borrowernumber => $borrowernumber,
461 add_to_some_private_shelves => $some_private_shelves,
462 add_to_some_public_shelves => $some_public_shelves,
466 MARCNOTES => $marc_record ? $biblio->get_marc_notes() : undef,
467 itemdata_ccode => $itemfields{ccode},
468 itemdata_enumchron => $itemfields{enumchron},
469 itemdata_uri => $itemfields{uri},
470 itemdata_copynumber => $itemfields{copynumber},
471 itemdata_stocknumber => $itemfields{stocknumber},
472 itemdata_publisheddate => $itemfields{publisheddate},
473 volinfo => $itemfields{enumchron},
474 itemdata_itemnotes => $itemfields{itemnotes},
475 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
476 z3950_search_params => C4::Search::z3950_search_args($dat),
477 hostrecords => $hostrecords,
478 analytics_flag => $analytics_flag,
479 C4::Search::enabled_staff_search_views,
480 materials => $materials_flag,
483 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
484 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
485 my $subfields = substr $fieldspec, 3;
486 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
487 my @alternateholdingsinfo = ();
488 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
490 for my $field (@holdingsfields) {
491 my %holding = ( holding => '' );
492 my $havesubfield = 0;
493 for my $subfield ($field->subfields()) {
494 if ((index $subfields, $$subfield[0]) >= 0) {
495 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
496 $holding{'holding'} .= $$subfield[1];
501 push(@alternateholdingsinfo, \%holding);
506 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
510 my @results = ( $dat, );
511 foreach ( keys %{$dat} ) {
512 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
515 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
516 # method query not found?!?!
517 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
519 itemloop => \@itemloop,
520 otheritemloop => \@otheritemloop,
521 biblionumber => $biblionumber,
522 ($analyze? 'analyze':'detailview') =>1,
523 subscriptions => \@subs,
524 subscriptionsnumber => $subscriptionsnumber,
525 subscriptiontitle => $dat->{title},
526 searchid => scalar $query->param('searchid'),
531 if (C4::Context->preference("virtualshelves") ) {
532 my $shelves = Koha::Virtualshelves->search(
534 biblionumber => $biblionumber,
538 join => 'virtualshelfcontents',
541 $template->param( 'shelves' => $shelves );
545 if (C4::Context->preference("FRBRizeEditions")==1) {
548 XISBNS => scalar get_xisbns($isbn, $biblionumber)
551 if ($@) { warn "XISBN Failed $@"; }
554 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
555 my $images = $biblio->cover_images;
557 localimages => $biblio->cover_images->search(
558 {}, { order_by => [ \"COALESCE(itemnumber, 0, 1)", 'timestamp' ] }
564 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
565 $template->param( C4::HTML5Media->gethtml5media($record));
570 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
573 TagsShowOnDetail => $tag_quantity
575 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
576 'sort'=>'-weight', limit=>$tag_quantity}));
579 #we only need to pass the number of holds to the template
580 my $holds = $biblio->holds;
581 $template->param( holdcount => $holds->count );
583 # Check if there are any ILL requests connected to the biblio
585 C4::Context->preference('ILLModule')
586 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
588 $template->param( illrequests => $illrequests );
590 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
591 if ($StaffDetailItemSelection) {
592 # Only enable item selection if user can execute at least one action
594 $flags->{superlibrarian}
596 ref $flags->{tools} eq 'HASH' && (
597 $flags->{tools}->{items_batchmod} # Modify selected items
598 || $flags->{tools}->{items_batchdel} # Delete selected items
601 || ( ref $flags->{tools} eq '' && $flags->{tools} )
605 StaffDetailItemSelection => $StaffDetailItemSelection );
609 # get biblionumbers stored in the cart
612 if($query->cookie("intranet_bib_list")){
613 my $cart_list = $query->cookie("intranet_bib_list");
614 @cart_list = split(/\//, $cart_list);
615 if ( grep {$_ eq $biblionumber} @cart_list) {
616 $template->param( incart => 1 );
620 if ( C4::Context->preference('UseCourseReserves') ) {
621 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
622 $template->param( course_reserves => $course_reserves );
625 $template->param(found1 => scalar $query->param('found1') );
627 $template->param(biblio => $biblio);
629 output_html_with_http_headers $query, $cookie, $template->output;