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 my $marc_record = 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 $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
150 my $dbh = C4::Context->dbh;
152 my @all_items = GetItemsInfo( $biblionumber );
154 my $patron = Koha::Patrons->find( $borrowernumber );
155 for my $itm (@all_items) {
156 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
159 # flag indicating existence of at least one item linked via a host record
161 # adding items linked via host biblios
162 my @hostitems = GetHostItemsInfo($record);
165 push (@items,@hostitems);
168 my $dat = &GetBiblioData($biblionumber);
170 #coping with subscriptions
171 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
172 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
175 foreach my $subscription (@subscriptions) {
177 my $serials_to_display;
178 $cell{subscriptionid} = $subscription->{subscriptionid};
179 $cell{subscriptionnotes} = $subscription->{internalnotes};
180 $cell{missinglist} = $subscription->{missinglist};
181 $cell{librariannote} = $subscription->{librariannote};
182 $cell{branchcode} = $subscription->{branchcode};
183 $cell{hasalert} = $subscription->{hasalert};
184 $cell{callnumber} = $subscription->{callnumber};
185 $cell{location} = $subscription->{location};
186 $cell{closed} = $subscription->{closed};
187 #get the three latest serials.
188 $serials_to_display = $subscription->{staffdisplaycount};
189 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
190 $cell{staffdisplaycount} = $serials_to_display;
191 $cell{latestserials} =
192 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
196 # Get component parts details
197 my $showcomp = C4::Context->preference('ShowComponentRecords');
199 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
200 if ( my $components = $marc_record ? $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) : undef ) {
201 $show_analytics = 1 if @{$components}; # just show link when having results
202 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
204 for my $part ( @{$components} ) {
205 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
206 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
213 xsl_syspref => "XSLTResultsDisplay",
218 $template->param( ComponentParts => $parts );
219 $template->param( ComponentPartsQuery => $biblio->get_components_query );
221 } else { # check if we should show analytics anyway
222 $show_analytics = 1 if $marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
223 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
226 # XSLT processing of some stuff
227 my $xslt_variables = { show_analytics_link => $show_analytics };
229 XSLTDetailsDisplay => '1',
230 XSLTBloc => XSLTParse4Display({
231 biblionumber => $biblionumber,
233 xsl_syspref => "XSLTDetailsDisplay",
235 xslt_variables => $xslt_variables,
239 # Get acquisition details
240 if ( C4::Context->preference('AcquisitionDetails') ) {
241 my $orders = Koha::Acquisition::Orders->search(
242 { biblionumber => $biblionumber },
245 order_by => 'basketno.booksellerid'
247 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
254 if ( C4::Context->preference('suggestion') ) {
255 my $suggestions = Koha::Suggestions->search(
257 biblionumber => $biblionumber,
261 order_by => { -desc => 'suggesteddate' }
264 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
265 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
268 if ( defined $dat->{'itemtype'} ) {
269 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
272 $dat->{'count'} = scalar @all_items + @hostitems;
273 $dat->{'showncount'} = scalar @items + @hostitems;
274 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
277 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
279 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
281 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
282 my (@itemloop, @otheritemloop, %itemfields);
284 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
286 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
288 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
290 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
292 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
294 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
297 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
300 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
301 if ($materials_authvals) {
302 foreach my $value (@$materials_authvals) {
303 $materials_map{$value->{authorised_value}} = $value->{lib};
309 my $materials_flag; # set this if the items have anything in the materials field
310 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
311 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
312 $template->param(SeparateHoldings => 1);
314 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
315 my ( $itemloop_has_images, $otheritemloop_has_images );
316 foreach my $item (@items) {
317 my $itembranchcode = $item->{$separatebranch};
319 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
322 $item->{datedue} = format_sqldatetime($item->{datedue});
324 #get shelf location and collection code description if they are authorised value.
325 # same thing for copy number
326 my $shelfcode = $item->{'location'};
327 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
328 my $ccode = $item->{'ccode'};
329 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
330 my $copynumber = $item->{'copynumber'};
331 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
332 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
333 $itemfields{$_} = 1 if ( $item->{$_} );
337 my $item_object = Koha::Items->find( $item->{itemnumber} );
338 my $holds = $item_object->current_holds;
339 if ( my $first_hold = $holds->next ) {
340 $item->{first_hold} = $first_hold;
343 if ( my $checkout = $item_object->checkout ) {
344 $item->{CheckedOutFor} = $checkout->patron;
347 # Check the transit status
348 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
349 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
350 $item->{transfertwhen} = $transfertwhen;
351 $item->{transfertfrom} = $transfertfrom;
352 $item->{transfertto} = $transfertto;
353 $item->{nocancel} = 1;
356 foreach my $f (qw( itemnotes )) {
358 $item->{$f} =~ s|\n|<br />|g;
363 #item has a host number if its biblio number does not match the current bib
365 if ($item->{biblionumber} ne $biblionumber){
366 $item->{hostbiblionumber} = $item->{biblionumber};
367 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
372 # count if item is used in analytical bibliorecords
373 # The 'countanalytics' flag is only used in the templates if analyze is set
374 my $countanalytics = GetAnalyticsCount( $item->{itemnumber} );
375 if ($countanalytics > 0){
377 $item->{countanalytics} = $countanalytics;
381 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
383 if (defined $materials_map{ $item->{materials} }) {
384 $item->{materials} = $materials_map{ $item->{materials} };
388 if ( C4::Context->preference('UseCourseReserves') ) {
389 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
392 if ( C4::Context->preference('IndependentBranches') ) {
393 my $userenv = C4::Context->userenv();
394 if ( not C4::Context->IsSuperLibrarian()
395 and $userenv->{branch} ne $item->{homebranch} ) {
396 $item->{cannot_be_edited} = 1;
400 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
401 $item->{cover_images} = $item_object->cover_images;
404 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
405 if ($itembranchcode and $itembranchcode eq $currentbranch) {
406 push @itemloop, $item;
407 $itemloop_has_images++ if $item_object->cover_images->count;
409 push @otheritemloop, $item;
410 $otheritemloop_has_images++ if $item_object->cover_images->count;
413 push @itemloop, $item;
414 $itemloop_has_images++ if $item_object->cover_images->count;
419 itemloop_has_images => $itemloop_has_images,
420 otheritemloop_has_images => $otheritemloop_has_images,
423 # Display only one tab if one items list is empty
424 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
425 $template->param(SeparateHoldings => 0);
426 if (scalar(@itemloop) == 0) {
427 @itemloop = @otheritemloop;
431 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
433 borrowernumber => $borrowernumber,
438 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
440 borrowernumber => $borrowernumber,
448 add_to_some_private_shelves => $some_private_shelves,
449 add_to_some_public_shelves => $some_public_shelves,
453 MARCNOTES => $marc_record ? $biblio->get_marc_notes({ marcflavour => $marcflavour }) : (),
454 itemdata_ccode => $itemfields{ccode},
455 itemdata_enumchron => $itemfields{enumchron},
456 itemdata_uri => $itemfields{uri},
457 itemdata_copynumber => $itemfields{copynumber},
458 itemdata_stocknumber => $itemfields{stocknumber},
459 itemdata_publisheddate => $itemfields{publisheddate},
460 volinfo => $itemfields{enumchron},
461 itemdata_itemnotes => $itemfields{itemnotes},
462 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
463 z3950_search_params => C4::Search::z3950_search_args($dat),
464 hostrecords => $hostrecords,
465 analytics_flag => $analytics_flag,
466 C4::Search::enabled_staff_search_views,
467 materials => $materials_flag,
470 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
471 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
472 my $subfields = substr $fieldspec, 3;
473 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
474 my @alternateholdingsinfo = ();
475 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
477 for my $field (@holdingsfields) {
478 my %holding = ( holding => '' );
479 my $havesubfield = 0;
480 for my $subfield ($field->subfields()) {
481 if ((index $subfields, $$subfield[0]) >= 0) {
482 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
483 $holding{'holding'} .= $$subfield[1];
488 push(@alternateholdingsinfo, \%holding);
493 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
497 my @results = ( $dat, );
498 foreach ( keys %{$dat} ) {
499 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
502 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
503 # method query not found?!?!
504 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
506 itemloop => \@itemloop,
507 otheritemloop => \@otheritemloop,
508 biblionumber => $biblionumber,
509 ($analyze? 'analyze':'detailview') =>1,
510 subscriptions => \@subs,
511 subscriptionsnumber => $subscriptionsnumber,
512 subscriptiontitle => $dat->{title},
513 searchid => scalar $query->param('searchid'),
518 if (C4::Context->preference("virtualshelves") ) {
519 my $shelves = Koha::Virtualshelves->search(
521 biblionumber => $biblionumber,
525 join => 'virtualshelfcontents',
528 $template->param( 'shelves' => $shelves );
532 if (C4::Context->preference("FRBRizeEditions")==1) {
535 XISBNS => scalar get_xisbns($isbn, $biblionumber)
538 if ($@) { warn "XISBN Failed $@"; }
541 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
542 my $images = $biblio->cover_images;
544 localimages => $biblio->cover_images->search(
545 {}, { order_by => [ \"COALESCE(itemnumber, 0, 1)", 'timestamp' ] }
551 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
552 $template->param( C4::HTML5Media->gethtml5media($record));
557 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
560 TagsShowOnDetail => $tag_quantity
562 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
563 'sort'=>'-weight', limit=>$tag_quantity}));
566 #we only need to pass the number of holds to the template
567 my $holds = $biblio->holds;
568 $template->param( holdcount => $holds->count );
570 # Check if there are any ILL requests connected to the biblio
572 C4::Context->preference('ILLModule')
573 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
575 $template->param( illrequests => $illrequests );
577 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
578 if ($StaffDetailItemSelection) {
579 # Only enable item selection if user can execute at least one action
581 $flags->{superlibrarian}
583 ref $flags->{tools} eq 'HASH' && (
584 $flags->{tools}->{items_batchmod} # Modify selected items
585 || $flags->{tools}->{items_batchdel} # Delete selected items
588 || ( ref $flags->{tools} eq '' && $flags->{tools} )
592 StaffDetailItemSelection => $StaffDetailItemSelection );
596 # get biblionumbers stored in the cart
599 if($query->cookie("intranet_bib_list")){
600 my $cart_list = $query->cookie("intranet_bib_list");
601 @cart_list = split(/\//, $cart_list);
602 if ( grep {$_ eq $biblionumber} @cart_list) {
603 $template->param( incart => 1 );
607 if ( C4::Context->preference('UseCourseReserves') ) {
608 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
609 $template->param( course_reserves => $course_reserves );
612 $template->param(found1 => scalar $query->param('found1') );
614 $template->param(biblio => $biblio);
616 output_html_with_http_headers $query, $cookie, $template->output;