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 # FIXME Should pass the patron object
107 holdfor => $query->cookie("holdfor"),
108 holdfor_surname => $holdfor_patron->surname,
109 holdfor_firstname => $holdfor_patron->firstname,
110 holdfor_cardnumber => $holdfor_patron->cardnumber,
115 if($query->cookie("searchToOrder")){
116 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
118 searchtoorder_basketno => $basketno,
119 searchtoorder_vendorid => $vendorid
123 my $fw = GetFrameworkCode($biblionumber);
124 my $showallitems = $query->param('showallitems');
125 my $marcflavour = C4::Context->preference("marcflavour");
127 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
129 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
130 # Do not propagate it as we already deal with it previously in this script
131 my $coins = eval { $biblio->get_coins };
132 $template->param( ocoins => $coins );
134 # some useful variables for enhanced content;
135 # in each case, we're grabbing the first value we find in
136 # the record and normalizing it
137 my $upc = GetNormalizedUPC($record,$marcflavour);
138 my $ean = GetNormalizedEAN($record,$marcflavour);
139 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
140 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
143 normalized_upc => $upc,
144 normalized_ean => $ean,
145 normalized_oclc => $oclc,
146 normalized_isbn => $isbn,
149 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
151 my $dbh = C4::Context->dbh;
153 my @all_items = GetItemsInfo( $biblionumber );
155 my $patron = Koha::Patrons->find( $borrowernumber );
156 for my $itm (@all_items) {
157 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
160 # flag indicating existence of at least one item linked via a host record
162 # adding items linked via host biblios
163 my @hostitems = GetHostItemsInfo($record);
166 push (@items,@hostitems);
169 my $dat = &GetBiblioData($biblionumber);
171 #coping with subscriptions
172 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
173 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
176 foreach my $subscription (@subscriptions) {
178 my $serials_to_display;
179 $cell{subscriptionid} = $subscription->{subscriptionid};
180 $cell{subscriptionnotes} = $subscription->{internalnotes};
181 $cell{missinglist} = $subscription->{missinglist};
182 $cell{librariannote} = $subscription->{librariannote};
183 $cell{branchcode} = $subscription->{branchcode};
184 $cell{hasalert} = $subscription->{hasalert};
185 $cell{callnumber} = $subscription->{callnumber};
186 $cell{location} = $subscription->{location};
187 $cell{closed} = $subscription->{closed};
188 #get the three latest serials.
189 $serials_to_display = $subscription->{staffdisplaycount};
190 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
191 $cell{staffdisplaycount} = $serials_to_display;
192 $cell{latestserials} =
193 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
197 # Get component parts details
198 my $showcomp = C4::Context->preference('ShowComponentRecords');
200 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
201 if ( my $components = $marc_record ? $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) : undef ) {
202 $show_analytics = 1 if @{$components}; # just show link when having results
203 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
205 for my $part ( @{$components} ) {
206 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
207 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
214 xsl_syspref => "XSLTResultsDisplay",
219 $template->param( ComponentParts => $parts );
220 $template->param( ComponentPartsQuery => $biblio->get_components_query );
222 } else { # check if we should show analytics anyway
223 $show_analytics = 1 if $marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
224 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
227 # XSLT processing of some stuff
228 my $xslt_variables = { show_analytics_link => $show_analytics };
230 XSLTDetailsDisplay => '1',
231 XSLTBloc => XSLTParse4Display({
232 biblionumber => $biblionumber,
234 xsl_syspref => "XSLTDetailsDisplay",
236 xslt_variables => $xslt_variables,
240 # Get acquisition details
241 if ( C4::Context->preference('AcquisitionDetails') ) {
242 my $orders = Koha::Acquisition::Orders->search(
243 { biblionumber => $biblionumber },
246 order_by => 'basketno.booksellerid'
248 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
255 if ( C4::Context->preference('suggestion') ) {
256 my $suggestions = Koha::Suggestions->search(
258 biblionumber => $biblionumber,
262 order_by => { -desc => 'suggesteddate' }
265 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
266 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
269 if ( defined $dat->{'itemtype'} ) {
270 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
273 $dat->{'count'} = scalar @all_items + @hostitems;
274 $dat->{'showncount'} = scalar @items + @hostitems;
275 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
278 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
280 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
282 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
283 my (@itemloop, @otheritemloop, %itemfields);
285 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
287 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
289 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
291 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
293 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
295 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
298 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
301 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
302 if ($materials_authvals) {
303 foreach my $value (@$materials_authvals) {
304 $materials_map{$value->{authorised_value}} = $value->{lib};
310 my $materials_flag; # set this if the items have anything in the materials field
311 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
312 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
313 $template->param(SeparateHoldings => 1);
315 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
316 my ( $itemloop_has_images, $otheritemloop_has_images );
317 foreach my $item (@items) {
318 my $itembranchcode = $item->{$separatebranch};
320 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
323 $item->{datedue} = format_sqldatetime($item->{datedue});
325 #get shelf location and collection code description if they are authorised value.
326 # same thing for copy number
327 my $shelfcode = $item->{'location'};
328 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
329 my $ccode = $item->{'ccode'};
330 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
331 my $copynumber = $item->{'copynumber'};
332 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
333 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
334 $itemfields{$_} = 1 if ( $item->{$_} );
338 my $item_object = Koha::Items->find( $item->{itemnumber} );
339 my $holds = $item_object->current_holds;
340 if ( my $first_hold = $holds->next ) {
341 $item->{first_hold} = $first_hold;
344 if ( my $checkout = $item_object->checkout ) {
345 $item->{CheckedOutFor} = $checkout->patron;
348 # Check the transit status
349 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
350 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
351 $item->{transfertwhen} = $transfertwhen;
352 $item->{transfertfrom} = $transfertfrom;
353 $item->{transfertto} = $transfertto;
354 $item->{nocancel} = 1;
357 foreach my $f (qw( itemnotes )) {
359 $item->{$f} =~ s|\n|<br />|g;
364 #item has a host number if its biblio number does not match the current bib
366 if ($item->{biblionumber} ne $biblionumber){
367 $item->{hostbiblionumber} = $item->{biblionumber};
368 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
373 # count if item is used in analytical bibliorecords
374 # The 'countanalytics' flag is only used in the templates if analyze is set
375 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
376 if ($countanalytics > 0){
378 $item->{countanalytics} = $countanalytics;
382 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
384 if (defined $materials_map{ $item->{materials} }) {
385 $item->{materials} = $materials_map{ $item->{materials} };
389 if ( C4::Context->preference('UseCourseReserves') ) {
390 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
393 if ( C4::Context->preference('IndependentBranches') ) {
394 my $userenv = C4::Context->userenv();
395 if ( not C4::Context->IsSuperLibrarian()
396 and $userenv->{branch} ne $item->{homebranch} ) {
397 $item->{cannot_be_edited} = 1;
401 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
402 $item->{cover_images} = $item_object->cover_images;
405 if ( C4::Context->preference('UseRecalls') ) {
406 my $recall = Koha::Recalls->find({ itemnumber => $item->{itemnumber}, old => 0 });
407 if ( defined $recall ) {
408 $item->{recalled} = 1;
409 $item->{recall} = $recall;
413 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
414 if ($itembranchcode and $itembranchcode eq $currentbranch) {
415 push @itemloop, $item;
416 $itemloop_has_images++ if $item_object->cover_images->count;
418 push @otheritemloop, $item;
419 $otheritemloop_has_images++ if $item_object->cover_images->count;
422 push @itemloop, $item;
423 $itemloop_has_images++ if $item_object->cover_images->count;
428 itemloop_has_images => $itemloop_has_images,
429 otheritemloop_has_images => $otheritemloop_has_images,
432 # Display only one tab if one items list is empty
433 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
434 $template->param(SeparateHoldings => 0);
435 if (scalar(@itemloop) == 0) {
436 @itemloop = @otheritemloop;
440 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
442 borrowernumber => $borrowernumber,
447 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
449 borrowernumber => $borrowernumber,
457 add_to_some_private_shelves => $some_private_shelves,
458 add_to_some_public_shelves => $some_public_shelves,
462 MARCNOTES => $marc_record ? $biblio->get_marc_notes({ marcflavour => $marcflavour }) : (),
463 itemdata_ccode => $itemfields{ccode},
464 itemdata_enumchron => $itemfields{enumchron},
465 itemdata_uri => $itemfields{uri},
466 itemdata_copynumber => $itemfields{copynumber},
467 itemdata_stocknumber => $itemfields{stocknumber},
468 itemdata_publisheddate => $itemfields{publisheddate},
469 volinfo => $itemfields{enumchron},
470 itemdata_itemnotes => $itemfields{itemnotes},
471 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
472 z3950_search_params => C4::Search::z3950_search_args($dat),
473 hostrecords => $hostrecords,
474 analytics_flag => $analytics_flag,
475 C4::Search::enabled_staff_search_views,
476 materials => $materials_flag,
479 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
480 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
481 my $subfields = substr $fieldspec, 3;
482 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
483 my @alternateholdingsinfo = ();
484 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
486 for my $field (@holdingsfields) {
487 my %holding = ( holding => '' );
488 my $havesubfield = 0;
489 for my $subfield ($field->subfields()) {
490 if ((index $subfields, $$subfield[0]) >= 0) {
491 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
492 $holding{'holding'} .= $$subfield[1];
497 push(@alternateholdingsinfo, \%holding);
502 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
506 my @results = ( $dat, );
507 foreach ( keys %{$dat} ) {
508 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
511 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
512 # method query not found?!?!
513 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
515 itemloop => \@itemloop,
516 otheritemloop => \@otheritemloop,
517 biblionumber => $biblionumber,
518 ($analyze? 'analyze':'detailview') =>1,
519 subscriptions => \@subs,
520 subscriptionsnumber => $subscriptionsnumber,
521 subscriptiontitle => $dat->{title},
522 searchid => scalar $query->param('searchid'),
527 if (C4::Context->preference("virtualshelves") ) {
528 my $shelves = Koha::Virtualshelves->search(
530 biblionumber => $biblionumber,
534 join => 'virtualshelfcontents',
537 $template->param( 'shelves' => $shelves );
541 if (C4::Context->preference("FRBRizeEditions")==1) {
544 XISBNS => scalar get_xisbns($isbn, $biblionumber)
547 if ($@) { warn "XISBN Failed $@"; }
550 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
551 my $images = $biblio->cover_images;
552 $template->param( localimages => $biblio->cover_images );
556 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
557 $template->param( C4::HTML5Media->gethtml5media($record));
562 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
565 TagsShowOnDetail => $tag_quantity
567 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
568 'sort'=>'-weight', limit=>$tag_quantity}));
571 #we only need to pass the number of holds to the template
572 my $holds = $biblio->holds;
573 $template->param( holdcount => $holds->count );
575 # Check if there are any ILL requests connected to the biblio
577 C4::Context->preference('ILLModule')
578 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
580 $template->param( illrequests => $illrequests );
582 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
583 if ($StaffDetailItemSelection) {
584 # Only enable item selection if user can execute at least one action
586 $flags->{superlibrarian}
588 ref $flags->{tools} eq 'HASH' && (
589 $flags->{tools}->{items_batchmod} # Modify selected items
590 || $flags->{tools}->{items_batchdel} # Delete selected items
593 || ( ref $flags->{tools} eq '' && $flags->{tools} )
597 StaffDetailItemSelection => $StaffDetailItemSelection );
601 # get biblionumbers stored in the cart
604 if($query->cookie("intranet_bib_list")){
605 my $cart_list = $query->cookie("intranet_bib_list");
606 @cart_list = split(/\//, $cart_list);
607 if ( grep {$_ eq $biblionumber} @cart_list) {
608 $template->param( incart => 1 );
612 if ( C4::Context->preference('UseCourseReserves') ) {
613 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
614 $template->param( course_reserves => $course_reserves );
617 $template->param(biblio => $biblio);
619 output_html_with_http_headers $query, $cookie, $template->output;