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");
127 # XSLT processing of some stuff
130 XSLTDetailsDisplay => '1',
131 XSLTBloc => XSLTParse4Display(
133 biblionumber => $biblionumber,
135 xsl_syspref => "XSLTDetailsDisplay",
142 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
144 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
145 # Do not propagate it as we already deal with it previously in this script
146 my $coins = eval { $biblio->get_coins };
147 $template->param( ocoins => $coins );
149 # some useful variables for enhanced content;
150 # in each case, we're grabbing the first value we find in
151 # the record and normalizing it
152 my $upc = GetNormalizedUPC($record,$marcflavour);
153 my $ean = GetNormalizedEAN($record,$marcflavour);
154 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
155 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
158 normalized_upc => $upc,
159 normalized_ean => $ean,
160 normalized_oclc => $oclc,
161 normalized_isbn => $isbn,
164 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
166 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
168 my $dbh = C4::Context->dbh;
170 my @all_items = GetItemsInfo( $biblionumber );
172 my $patron = Koha::Patrons->find( $borrowernumber );
173 for my $itm (@all_items) {
174 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
177 # flag indicating existence of at least one item linked via a host record
179 # adding items linked via host biblios
180 my @hostitems = GetHostItemsInfo($record);
183 push (@items,@hostitems);
186 my $dat = &GetBiblioData($biblionumber);
188 #coping with subscriptions
189 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
190 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
193 foreach my $subscription (@subscriptions) {
195 my $serials_to_display;
196 $cell{subscriptionid} = $subscription->{subscriptionid};
197 $cell{subscriptionnotes} = $subscription->{internalnotes};
198 $cell{missinglist} = $subscription->{missinglist};
199 $cell{librariannote} = $subscription->{librariannote};
200 $cell{branchcode} = $subscription->{branchcode};
201 $cell{hasalert} = $subscription->{hasalert};
202 $cell{callnumber} = $subscription->{callnumber};
203 $cell{location} = $subscription->{location};
204 $cell{closed} = $subscription->{closed};
205 #get the three latest serials.
206 $serials_to_display = $subscription->{staffdisplaycount};
207 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
208 $cell{staffdisplaycount} = $serials_to_display;
209 $cell{latestserials} =
210 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
214 # Get component parts details
215 my $showcomp = C4::Context->preference('ShowComponentRecords');
216 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
217 if ( my $components = $biblio->get_marc_components(300) ) {
219 for my $part ( @{$components} ) {
220 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
225 biblionumber => $biblionumber,
227 xsl_syspref => "XSLTResultsDisplay",
232 $template->param( ComponentParts => $parts );
233 $template->param( ComponentPartsQuery => $biblio->get_components_query );
237 # Get acquisition details
238 if ( C4::Context->preference('AcquisitionDetails') ) {
239 my $orders = Koha::Acquisition::Orders->search(
240 { biblionumber => $biblionumber },
243 order_by => 'basketno.booksellerid'
245 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
252 if ( C4::Context->preference('suggestion') ) {
253 my $suggestions = Koha::Suggestions->search(
255 biblionumber => $biblionumber,
259 order_by => { -desc => 'suggesteddate' }
262 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
263 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
266 if ( defined $dat->{'itemtype'} ) {
267 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
270 $dat->{'count'} = scalar @all_items + @hostitems;
271 $dat->{'showncount'} = scalar @items + @hostitems;
272 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
275 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
277 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
279 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
280 my (@itemloop, @otheritemloop, %itemfields);
282 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
284 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
286 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
288 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
290 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
292 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
295 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
298 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
299 if ($materials_authvals) {
300 foreach my $value (@$materials_authvals) {
301 $materials_map{$value->{authorised_value}} = $value->{lib};
307 my $materials_flag; # set this if the items have anything in the materials field
308 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
309 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
310 $template->param(SeparateHoldings => 1);
312 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
313 my ( $itemloop_has_images, $otheritemloop_has_images );
314 foreach my $item (@items) {
315 my $itembranchcode = $item->{$separatebranch};
317 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
320 $item->{datedue} = format_sqldatetime($item->{datedue});
322 #get shelf location and collection code description if they are authorised value.
323 # same thing for copy number
324 my $shelfcode = $item->{'location'};
325 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
326 my $ccode = $item->{'ccode'};
327 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
328 my $copynumber = $item->{'copynumber'};
329 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
330 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
331 $itemfields{$_} = 1 if ( $item->{$_} );
335 my $item_object = Koha::Items->find( $item->{itemnumber} );
336 my $holds = $item_object->current_holds;
337 if ( my $first_hold = $holds->next ) {
338 $item->{first_hold} = $first_hold;
341 if ( my $checkout = $item_object->checkout ) {
342 $item->{CheckedOutFor} = $checkout->patron;
345 # Check the transit status
346 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
347 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
348 $item->{transfertwhen} = $transfertwhen;
349 $item->{transfertfrom} = $transfertfrom;
350 $item->{transfertto} = $transfertto;
351 $item->{nocancel} = 1;
354 foreach my $f (qw( itemnotes )) {
356 $item->{$f} =~ s|\n|<br />|g;
361 #item has a host number if its biblio number does not match the current bib
363 if ($item->{biblionumber} ne $biblionumber){
364 $item->{hostbiblionumber} = $item->{biblionumber};
365 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
370 # count if item is used in analytical bibliorecords
371 # The 'countanalytics' flag is only used in the templates if analyze is set
372 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
373 if ($countanalytics > 0){
375 $item->{countanalytics} = $countanalytics;
379 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
381 if (defined $materials_map{ $item->{materials} }) {
382 $item->{materials} = $materials_map{ $item->{materials} };
386 if ( C4::Context->preference('UseCourseReserves') ) {
387 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
390 if ( C4::Context->preference('IndependentBranches') ) {
391 my $userenv = C4::Context->userenv();
392 if ( not C4::Context->IsSuperLibrarian()
393 and $userenv->{branch} ne $item->{homebranch} ) {
394 $item->{cannot_be_edited} = 1;
398 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
399 $item->{cover_images} = $item_object->cover_images;
402 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
403 if ($itembranchcode and $itembranchcode eq $currentbranch) {
404 push @itemloop, $item;
405 $itemloop_has_images++ if $item_object->cover_images->count;
407 push @otheritemloop, $item;
408 $otheritemloop_has_images++ if $item_object->cover_images->count;
411 push @itemloop, $item;
412 $itemloop_has_images++ if $item_object->cover_images->count;
417 itemloop_has_images => $itemloop_has_images,
418 otheritemloop_has_images => $otheritemloop_has_images,
421 # Display only one tab if one items list is empty
422 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
423 $template->param(SeparateHoldings => 0);
424 if (scalar(@itemloop) == 0) {
425 @itemloop = @otheritemloop;
429 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
431 borrowernumber => $borrowernumber,
436 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
438 borrowernumber => $borrowernumber,
446 add_to_some_private_shelves => $some_private_shelves,
447 add_to_some_public_shelves => $some_public_shelves,
451 MARCNOTES => $marcnotesarray,
452 itemdata_ccode => $itemfields{ccode},
453 itemdata_enumchron => $itemfields{enumchron},
454 itemdata_uri => $itemfields{uri},
455 itemdata_copynumber => $itemfields{copynumber},
456 itemdata_stocknumber => $itemfields{stocknumber},
457 itemdata_publisheddate => $itemfields{publisheddate},
458 volinfo => $itemfields{enumchron},
459 itemdata_itemnotes => $itemfields{itemnotes},
460 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
461 z3950_search_params => C4::Search::z3950_search_args($dat),
462 hostrecords => $hostrecords,
463 analytics_flag => $analytics_flag,
464 C4::Search::enabled_staff_search_views,
465 materials => $materials_flag,
468 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
469 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
470 my $subfields = substr $fieldspec, 3;
471 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
472 my @alternateholdingsinfo = ();
473 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
475 for my $field (@holdingsfields) {
476 my %holding = ( holding => '' );
477 my $havesubfield = 0;
478 for my $subfield ($field->subfields()) {
479 if ((index $subfields, $$subfield[0]) >= 0) {
480 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
481 $holding{'holding'} .= $$subfield[1];
486 push(@alternateholdingsinfo, \%holding);
491 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
495 my @results = ( $dat, );
496 foreach ( keys %{$dat} ) {
497 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
500 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
501 # method query not found?!?!
502 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
504 itemloop => \@itemloop,
505 otheritemloop => \@otheritemloop,
506 biblionumber => $biblionumber,
507 ($analyze? 'analyze':'detailview') =>1,
508 subscriptions => \@subs,
509 subscriptionsnumber => $subscriptionsnumber,
510 subscriptiontitle => $dat->{title},
511 searchid => scalar $query->param('searchid'),
516 if (C4::Context->preference("virtualshelves") ) {
517 my $shelves = Koha::Virtualshelves->search(
519 biblionumber => $biblionumber,
523 join => 'virtualshelfcontents',
526 $template->param( 'shelves' => $shelves );
530 if (C4::Context->preference("FRBRizeEditions")==1) {
533 XISBNS => scalar get_xisbns($isbn, $biblionumber)
536 if ($@) { warn "XISBN Failed $@"; }
539 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
540 my $images = $biblio->cover_images;
541 $template->param( localimages => $biblio->cover_images );
545 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
546 $template->param( C4::HTML5Media->gethtml5media($record));
551 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
554 TagsShowOnDetail => $tag_quantity
556 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
557 'sort'=>'-weight', limit=>$tag_quantity}));
560 #we only need to pass the number of holds to the template
561 my $holds = $biblio->holds;
562 $template->param( holdcount => $holds->count );
564 # Check if there are any ILL requests connected to the biblio
566 C4::Context->preference('ILLModule')
567 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
569 $template->param( illrequests => $illrequests );
571 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
572 if ($StaffDetailItemSelection) {
573 # Only enable item selection if user can execute at least one action
575 $flags->{superlibrarian}
577 ref $flags->{tools} eq 'HASH' && (
578 $flags->{tools}->{items_batchmod} # Modify selected items
579 || $flags->{tools}->{items_batchdel} # Delete selected items
582 || ( ref $flags->{tools} eq '' && $flags->{tools} )
586 StaffDetailItemSelection => $StaffDetailItemSelection );
590 # get biblionumbers stored in the cart
593 if($query->cookie("intranet_bib_list")){
594 my $cart_list = $query->cookie("intranet_bib_list");
595 @cart_list = split(/\//, $cart_list);
596 if ( grep {$_ eq $biblionumber} @cart_list) {
597 $template->param( incart => 1 );
601 if ( C4::Context->preference('UseCourseReserves') ) {
602 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
603 $template->param( course_reserves => $course_reserves );
606 $template->param(biblio => $biblio);
608 output_html_with_http_headers $query, $cookie, $template->output;