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>.
27 use C4::Serials; #uses getsubscriptionfrom biblionumber
34 use C4::XISBN qw(get_xisbns);
35 use C4::External::Amazon;
36 use C4::Search; # enabled_staff_search_views
37 use C4::Tags qw(get_tags);
41 use C4::CourseReserves qw(GetItemCourseReservesInfo);
42 use C4::Acquisition qw(GetOrdersByBiblionumber);
43 use Koha::AuthorisedValues;
45 use Koha::CoverImages;
46 use Koha::Illrequests;
50 use Koha::Virtualshelves;
52 use Koha::SearchEngine::Search;
54 my $query = CGI->new();
56 my $analyze = $query->param('analyze');
58 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
60 template_name => 'catalogue/detail.tt',
63 flagsrequired => { catalogue => 1 },
67 # Determine if we should be offering any enhancement plugin buttons
68 if ( C4::Context->config('enable_plugins') ) {
69 # Only pass plugins that can offer a toolbar button
70 my @plugins = Koha::Plugins->new()->GetPlugins({
71 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
78 my $biblionumber = $query->param('biblionumber');
79 $biblionumber = HTML::Entities::encode($biblionumber);
80 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
81 my $biblio = Koha::Biblios->find( $biblionumber );
82 $template->param( 'biblio', $biblio );
84 if ( not defined $record ) {
85 # biblionumber invalid -> report and exit
86 $template->param( unknownbiblionumber => 1,
87 biblionumber => $biblionumber );
88 output_html_with_http_headers $query, $cookie, $template->output;
92 eval { $biblio->metadata->record };
93 $template->param( decoding_error => $@ );
95 if($query->cookie("holdfor")){
96 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
97 if ( $holdfor_patron ) {
99 # FIXME Should pass the patron object
100 holdfor => $query->cookie("holdfor"),
101 holdfor_surname => $holdfor_patron->surname,
102 holdfor_firstname => $holdfor_patron->firstname,
103 holdfor_cardnumber => $holdfor_patron->cardnumber,
108 if($query->cookie("searchToOrder")){
109 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
111 searchtoorder_basketno => $basketno,
112 searchtoorder_vendorid => $vendorid
116 my $fw = GetFrameworkCode($biblionumber);
117 my $showallitems = $query->param('showallitems');
118 my $marcflavour = C4::Context->preference("marcflavour");
120 # XSLT processing of some stuff
121 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
122 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
123 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
127 my $searcher = Koha::SearchEngine::Search->new(
128 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
130 my $cleaned_title = $biblio->title;
131 $cleaned_title =~ tr|/||;
133 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
134 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
135 : "Host-item:($cleaned_title)";
136 my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
138 warn "Warning from simple_search_compat: $err"
142 show_analytics_link => $count > 0 ? 1 : 0
146 XSLTDetailsDisplay => '1',
147 XSLTBloc => XSLTParse4Display(
148 $biblionumber, $record, "XSLTDetailsDisplay", 1,
149 undef, $sysxml, $xslfile, $lang,
155 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
157 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
158 # Do not propagate it as we already deal with it previously in this script
159 my $coins = eval { $biblio->get_coins };
160 $template->param( ocoins => $coins );
162 # some useful variables for enhanced content;
163 # in each case, we're grabbing the first value we find in
164 # the record and normalizing it
165 my $upc = GetNormalizedUPC($record,$marcflavour);
166 my $ean = GetNormalizedEAN($record,$marcflavour);
167 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
168 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
171 normalized_upc => $upc,
172 normalized_ean => $ean,
173 normalized_oclc => $oclc,
174 normalized_isbn => $isbn,
177 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
179 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
181 my $dbh = C4::Context->dbh;
183 my @all_items = GetItemsInfo( $biblionumber );
185 my $patron = Koha::Patrons->find( $borrowernumber );
186 for my $itm (@all_items) {
187 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
190 # flag indicating existence of at least one item linked via a host record
192 # adding items linked via host biblios
193 my @hostitems = GetHostItemsInfo($record);
196 push (@items,@hostitems);
199 my $dat = &GetBiblioData($biblionumber);
201 #coping with subscriptions
202 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
203 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
206 foreach my $subscription (@subscriptions) {
208 my $serials_to_display;
209 $cell{subscriptionid} = $subscription->{subscriptionid};
210 $cell{subscriptionnotes} = $subscription->{internalnotes};
211 $cell{missinglist} = $subscription->{missinglist};
212 $cell{librariannote} = $subscription->{librariannote};
213 $cell{branchcode} = $subscription->{branchcode};
214 $cell{hasalert} = $subscription->{hasalert};
215 $cell{callnumber} = $subscription->{callnumber};
216 $cell{location} = $subscription->{location};
217 $cell{closed} = $subscription->{closed};
218 #get the three latest serials.
219 $serials_to_display = $subscription->{staffdisplaycount};
220 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
221 $cell{staffdisplaycount} = $serials_to_display;
222 $cell{latestserials} =
223 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
228 # Get acquisition details
229 if ( C4::Context->preference('AcquisitionDetails') ) {
230 my $orders = Koha::Acquisition::Orders->search(
231 { biblionumber => $biblionumber },
234 order_by => 'basketno.booksellerid'
236 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
243 if ( C4::Context->preference('suggestion') ) {
244 my $suggestions = Koha::Suggestions->search(
246 biblionumber => $biblionumber,
250 order_by => { -desc => 'suggesteddate' }
253 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
254 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
257 if ( defined $dat->{'itemtype'} ) {
258 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
261 $dat->{'count'} = scalar @all_items + @hostitems;
262 $dat->{'showncount'} = scalar @items + @hostitems;
263 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
266 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
268 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
270 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
271 my (@itemloop, @otheritemloop, %itemfields);
273 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
275 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
277 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
279 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
281 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
283 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
286 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
289 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
290 if ($materials_authvals) {
291 foreach my $value (@$materials_authvals) {
292 $materials_map{$value->{authorised_value}} = $value->{lib};
298 my $materials_flag; # set this if the items have anything in the materials field
299 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
300 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
301 $template->param(SeparateHoldings => 1);
303 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
304 my ( $itemloop_has_images, $otheritemloop_has_images );
305 foreach my $item (@items) {
306 my $itembranchcode = $item->{$separatebranch};
308 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
311 $item->{datedue} = format_sqldatetime($item->{datedue});
313 #get shelf location and collection code description if they are authorised value.
314 # same thing for copy number
315 my $shelfcode = $item->{'location'};
316 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
317 my $ccode = $item->{'ccode'};
318 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
319 my $copynumber = $item->{'copynumber'};
320 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
321 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
322 $itemfields{$_} = 1 if ( $item->{$_} );
326 my $item_object = Koha::Items->find( $item->{itemnumber} );
327 my $holds = $item_object->current_holds;
328 if ( my $first_hold = $holds->next ) {
329 $item->{first_hold} = $first_hold;
332 if ( my $checkout = $item_object->checkout ) {
333 $item->{CheckedOutFor} = $checkout->patron;
336 # Check the transit status
337 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
338 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
339 $item->{transfertwhen} = $transfertwhen;
340 $item->{transfertfrom} = $transfertfrom;
341 $item->{transfertto} = $transfertto;
342 $item->{nocancel} = 1;
345 foreach my $f (qw( itemnotes )) {
347 $item->{$f} =~ s|\n|<br />|g;
352 #item has a host number if its biblio number does not match the current bib
354 if ($item->{biblionumber} ne $biblionumber){
355 $item->{hostbiblionumber} = $item->{biblionumber};
356 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
361 # count if item is used in analytical bibliorecords
362 # The 'countanalytics' flag is only used in the templates if analyze is set
363 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
364 if ($countanalytics > 0){
366 $item->{countanalytics} = $countanalytics;
370 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
372 if (defined $materials_map{ $item->{materials} }) {
373 $item->{materials} = $materials_map{ $item->{materials} };
377 if ( C4::Context->preference('UseCourseReserves') ) {
378 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
381 if ( C4::Context->preference('IndependentBranches') ) {
382 my $userenv = C4::Context->userenv();
383 if ( not C4::Context->IsSuperLibrarian()
384 and $userenv->{branch} ne $item->{homebranch} ) {
385 $item->{cannot_be_edited} = 1;
389 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
390 $item->{cover_images} = $item_object->cover_images;
393 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
394 if ($itembranchcode and $itembranchcode eq $currentbranch) {
395 push @itemloop, $item;
396 $itemloop_has_images++ if $item_object->cover_images->count;
398 push @otheritemloop, $item;
399 $otheritemloop_has_images++ if $item_object->cover_images->count;
402 push @itemloop, $item;
403 $itemloop_has_images++ if $item_object->cover_images->count;
408 itemloop_has_images => $itemloop_has_images,
409 otheritemloop_has_images => $otheritemloop_has_images,
412 # Display only one tab if one items list is empty
413 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
414 $template->param(SeparateHoldings => 0);
415 if (scalar(@itemloop) == 0) {
416 @itemloop = @otheritemloop;
420 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
422 borrowernumber => $borrowernumber,
427 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
429 borrowernumber => $borrowernumber,
437 add_to_some_private_shelves => $some_private_shelves,
438 add_to_some_public_shelves => $some_public_shelves,
442 MARCNOTES => $marcnotesarray,
443 itemdata_ccode => $itemfields{ccode},
444 itemdata_enumchron => $itemfields{enumchron},
445 itemdata_uri => $itemfields{uri},
446 itemdata_copynumber => $itemfields{copynumber},
447 itemdata_stocknumber => $itemfields{stocknumber},
448 itemdata_publisheddate => $itemfields{publisheddate},
449 volinfo => $itemfields{enumchron},
450 itemdata_itemnotes => $itemfields{itemnotes},
451 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
452 z3950_search_params => C4::Search::z3950_search_args($dat),
453 hostrecords => $hostrecords,
454 analytics_flag => $analytics_flag,
455 C4::Search::enabled_staff_search_views,
456 materials => $materials_flag,
459 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
460 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
461 my $subfields = substr $fieldspec, 3;
462 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
463 my @alternateholdingsinfo = ();
464 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
466 for my $field (@holdingsfields) {
467 my %holding = ( holding => '' );
468 my $havesubfield = 0;
469 for my $subfield ($field->subfields()) {
470 if ((index $subfields, $$subfield[0]) >= 0) {
471 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
472 $holding{'holding'} .= $$subfield[1];
477 push(@alternateholdingsinfo, \%holding);
482 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
486 my @results = ( $dat, );
487 foreach ( keys %{$dat} ) {
488 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
491 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
492 # method query not found?!?!
493 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
495 itemloop => \@itemloop,
496 otheritemloop => \@otheritemloop,
497 biblionumber => $biblionumber,
498 ($analyze? 'analyze':'detailview') =>1,
499 subscriptions => \@subs,
500 subscriptionsnumber => $subscriptionsnumber,
501 subscriptiontitle => $dat->{title},
502 searchid => scalar $query->param('searchid'),
505 # $debug and $template->param(debug_display => 1);
509 if (C4::Context->preference("virtualshelves") ) {
510 my $shelves = Koha::Virtualshelves->search(
512 biblionumber => $biblionumber,
516 join => 'virtualshelfcontents',
519 $template->param( 'shelves' => $shelves );
523 if (C4::Context->preference("FRBRizeEditions")==1) {
526 XISBNS => scalar get_xisbns($isbn, $biblionumber)
529 if ($@) { warn "XISBN Failed $@"; }
532 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
533 my $images = $biblio->cover_images;
534 $template->param( localimages => $biblio->cover_images );
538 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
539 $template->param( C4::HTML5Media->gethtml5media($record));
544 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
547 TagsShowOnDetail => $tag_quantity
549 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
550 'sort'=>'-weight', limit=>$tag_quantity}));
553 #we only need to pass the number of holds to the template
554 my $holds = $biblio->holds;
555 $template->param( holdcount => $holds->count );
557 # Check if there are any ILL requests connected to the biblio
559 C4::Context->preference('ILLModule')
560 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
562 $template->param( illrequests => $illrequests );
564 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
565 if ($StaffDetailItemSelection) {
566 # Only enable item selection if user can execute at least one action
568 $flags->{superlibrarian}
570 ref $flags->{tools} eq 'HASH' && (
571 $flags->{tools}->{items_batchmod} # Modify selected items
572 || $flags->{tools}->{items_batchdel} # Delete selected items
575 || ( ref $flags->{tools} eq '' && $flags->{tools} )
579 StaffDetailItemSelection => $StaffDetailItemSelection );
583 # get biblionumbers stored in the cart
586 if($query->cookie("intranet_bib_list")){
587 my $cart_list = $query->cookie("intranet_bib_list");
588 @cart_list = split(/\//, $cart_list);
589 if ( grep {$_ eq $biblionumber} @cart_list) {
590 $template->param( incart => 1 );
594 $template->param(biblio => $biblio);
596 output_html_with_http_headers $query, $cookie, $template->output;