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") );
98 # FIXME Should pass the patron object
99 holdfor => $query->cookie("holdfor"),
100 holdfor_surname => $holdfor_patron->surname,
101 holdfor_firstname => $holdfor_patron->firstname,
102 holdfor_cardnumber => $holdfor_patron->cardnumber,
106 if($query->cookie("searchToOrder")){
107 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
109 searchtoorder_basketno => $basketno,
110 searchtoorder_vendorid => $vendorid
114 my $fw = GetFrameworkCode($biblionumber);
115 my $showallitems = $query->param('showallitems');
116 my $marcflavour = C4::Context->preference("marcflavour");
118 # XSLT processing of some stuff
119 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
120 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
121 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
125 my $searcher = Koha::SearchEngine::Search->new(
126 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
128 my $cleaned_title = $biblio->title;
129 $cleaned_title =~ tr|/||;
131 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
132 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
133 : "Host-item:$cleaned_title";
134 my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
136 warn "Warning from simple_search_compat: $err"
140 show_analytics_link => $count > 0 ? 1 : 0
144 XSLTDetailsDisplay => '1',
145 XSLTBloc => XSLTParse4Display(
146 $biblionumber, $record, "XSLTDetailsDisplay", 1,
147 undef, $sysxml, $xslfile, $lang,
153 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
155 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
156 # Do not propagate it as we already deal with it previously in this script
157 my $coins = eval { $biblio->get_coins };
158 $template->param( ocoins => $coins );
160 # some useful variables for enhanced content;
161 # in each case, we're grabbing the first value we find in
162 # the record and normalizing it
163 my $upc = GetNormalizedUPC($record,$marcflavour);
164 my $ean = GetNormalizedEAN($record,$marcflavour);
165 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
166 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
169 normalized_upc => $upc,
170 normalized_ean => $ean,
171 normalized_oclc => $oclc,
172 normalized_isbn => $isbn,
175 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
177 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
179 my $dbh = C4::Context->dbh;
181 my @all_items = GetItemsInfo( $biblionumber );
183 my $patron = Koha::Patrons->find( $borrowernumber );
184 for my $itm (@all_items) {
185 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
188 # flag indicating existence of at least one item linked via a host record
190 # adding items linked via host biblios
191 my @hostitems = GetHostItemsInfo($record);
194 push (@items,@hostitems);
197 my $dat = &GetBiblioData($biblionumber);
199 #coping with subscriptions
200 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
201 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
204 foreach my $subscription (@subscriptions) {
206 my $serials_to_display;
207 $cell{subscriptionid} = $subscription->{subscriptionid};
208 $cell{subscriptionnotes} = $subscription->{internalnotes};
209 $cell{missinglist} = $subscription->{missinglist};
210 $cell{librariannote} = $subscription->{librariannote};
211 $cell{branchcode} = $subscription->{branchcode};
212 $cell{hasalert} = $subscription->{hasalert};
213 $cell{callnumber} = $subscription->{callnumber};
214 $cell{location} = $subscription->{location};
215 $cell{closed} = $subscription->{closed};
216 #get the three latest serials.
217 $serials_to_display = $subscription->{staffdisplaycount};
218 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
219 $cell{staffdisplaycount} = $serials_to_display;
220 $cell{latestserials} =
221 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
226 # Get acquisition details
227 if ( C4::Context->preference('AcquisitionDetails') ) {
228 my $orders = Koha::Acquisition::Orders->search(
229 { biblionumber => $biblionumber },
232 order_by => 'basketno.booksellerid'
234 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
241 if ( C4::Context->preference('suggestion') ) {
242 my $suggestions = Koha::Suggestions->search(
244 biblionumber => $biblionumber,
248 order_by => { -desc => 'suggesteddate' }
251 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
252 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
255 if ( defined $dat->{'itemtype'} ) {
256 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
259 $dat->{'count'} = scalar @all_items + @hostitems;
260 $dat->{'showncount'} = scalar @items + @hostitems;
261 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
264 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
266 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
268 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
269 my (@itemloop, @otheritemloop, %itemfields);
271 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
273 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
275 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
277 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
279 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
281 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
284 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
287 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
288 if ($materials_authvals) {
289 foreach my $value (@$materials_authvals) {
290 $materials_map{$value->{authorised_value}} = $value->{lib};
296 my $materials_flag; # set this if the items have anything in the materials field
297 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
298 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
299 $template->param(SeparateHoldings => 1);
301 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
302 my ( $itemloop_has_images, $otheritemloop_has_images );
303 foreach my $item (@items) {
304 my $itembranchcode = $item->{$separatebranch};
306 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
309 $item->{datedue} = format_sqldatetime($item->{datedue});
311 #get shelf location and collection code description if they are authorised value.
312 # same thing for copy number
313 my $shelfcode = $item->{'location'};
314 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
315 my $ccode = $item->{'ccode'};
316 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
317 my $copynumber = $item->{'copynumber'};
318 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
319 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
320 $itemfields{$_} = 1 if ( $item->{$_} );
324 my $item_object = Koha::Items->find( $item->{itemnumber} );
325 my $holds = $item_object->current_holds;
326 if ( my $first_hold = $holds->next ) {
327 $item->{first_hold} = $first_hold;
330 if ( my $checkout = $item_object->checkout ) {
331 $item->{CheckedOutFor} = $checkout->patron;
334 # Check the transit status
335 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
336 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
337 $item->{transfertwhen} = $transfertwhen;
338 $item->{transfertfrom} = $transfertfrom;
339 $item->{transfertto} = $transfertto;
340 $item->{nocancel} = 1;
343 foreach my $f (qw( itemnotes )) {
345 $item->{$f} =~ s|\n|<br />|g;
350 #item has a host number if its biblio number does not match the current bib
352 if ($item->{biblionumber} ne $biblionumber){
353 $item->{hostbiblionumber} = $item->{biblionumber};
354 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
359 # count if item is used in analytical bibliorecords
360 # The 'countanalytics' flag is only used in the templates if analyze is set
361 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
362 if ($countanalytics > 0){
364 $item->{countanalytics} = $countanalytics;
368 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
370 if (defined $materials_map{ $item->{materials} }) {
371 $item->{materials} = $materials_map{ $item->{materials} };
375 if ( C4::Context->preference('UseCourseReserves') ) {
376 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
379 if ( C4::Context->preference('IndependentBranches') ) {
380 my $userenv = C4::Context->userenv();
381 if ( not C4::Context->IsSuperLibrarian()
382 and $userenv->{branch} ne $item->{homebranch} ) {
383 $item->{cannot_be_edited} = 1;
387 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
388 $item->{cover_images} = $item_object->cover_images;
391 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
392 if ($itembranchcode and $itembranchcode eq $currentbranch) {
393 push @itemloop, $item;
394 $itemloop_has_images++ if $item_object->cover_images->count;
396 push @otheritemloop, $item;
397 $otheritemloop_has_images++ if $item_object->cover_images->count;
400 push @itemloop, $item;
401 $itemloop_has_images++ if $item_object->cover_images->count;
406 itemloop_has_images => $itemloop_has_images,
407 otheritemloop_has_images => $otheritemloop_has_images,
410 # Display only one tab if one items list is empty
411 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
412 $template->param(SeparateHoldings => 0);
413 if (scalar(@itemloop) == 0) {
414 @itemloop = @otheritemloop;
418 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
420 borrowernumber => $borrowernumber,
425 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
427 borrowernumber => $borrowernumber,
435 add_to_some_private_shelves => $some_private_shelves,
436 add_to_some_public_shelves => $some_public_shelves,
440 MARCNOTES => $marcnotesarray,
441 itemdata_ccode => $itemfields{ccode},
442 itemdata_enumchron => $itemfields{enumchron},
443 itemdata_uri => $itemfields{uri},
444 itemdata_copynumber => $itemfields{copynumber},
445 itemdata_stocknumber => $itemfields{stocknumber},
446 volinfo => $itemfields{enumchron},
447 itemdata_itemnotes => $itemfields{itemnotes},
448 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
449 z3950_search_params => C4::Search::z3950_search_args($dat),
450 hostrecords => $hostrecords,
451 analytics_flag => $analytics_flag,
452 C4::Search::enabled_staff_search_views,
453 materials => $materials_flag,
456 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
457 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
458 my $subfields = substr $fieldspec, 3;
459 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
460 my @alternateholdingsinfo = ();
461 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
463 for my $field (@holdingsfields) {
464 my %holding = ( holding => '' );
465 my $havesubfield = 0;
466 for my $subfield ($field->subfields()) {
467 if ((index $subfields, $$subfield[0]) >= 0) {
468 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
469 $holding{'holding'} .= $$subfield[1];
474 push(@alternateholdingsinfo, \%holding);
479 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
483 my @results = ( $dat, );
484 foreach ( keys %{$dat} ) {
485 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
488 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
489 # method query not found?!?!
490 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
492 itemloop => \@itemloop,
493 otheritemloop => \@otheritemloop,
494 biblionumber => $biblionumber,
495 ($analyze? 'analyze':'detailview') =>1,
496 subscriptions => \@subs,
497 subscriptionsnumber => $subscriptionsnumber,
498 subscriptiontitle => $dat->{title},
499 searchid => scalar $query->param('searchid'),
502 # $debug and $template->param(debug_display => 1);
506 if (C4::Context->preference("virtualshelves") ) {
507 my $shelves = Koha::Virtualshelves->search(
509 biblionumber => $biblionumber,
513 join => 'virtualshelfcontents',
516 $template->param( 'shelves' => $shelves );
520 if (C4::Context->preference("FRBRizeEditions")==1) {
523 XISBNS => scalar get_xisbns($isbn, $biblionumber)
526 if ($@) { warn "XISBN Failed $@"; }
529 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
530 my $images = $biblio->cover_images;
531 $template->param( localimages => $biblio->cover_images );
535 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
536 $template->param( C4::HTML5Media->gethtml5media($record));
541 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
544 TagsShowOnDetail => $tag_quantity
546 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
547 'sort'=>'-weight', limit=>$tag_quantity}));
550 #we only need to pass the number of holds to the template
551 my $holds = $biblio->holds;
552 $template->param( holdcount => $holds->count );
554 # Check if there are any ILL requests connected to the biblio
556 C4::Context->preference('ILLModule')
557 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
559 $template->param( illrequests => $illrequests );
561 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
562 if ($StaffDetailItemSelection) {
563 # Only enable item selection if user can execute at least one action
565 $flags->{superlibrarian}
567 ref $flags->{tools} eq 'HASH' && (
568 $flags->{tools}->{items_batchmod} # Modify selected items
569 || $flags->{tools}->{items_batchdel} # Delete selected items
572 || ( ref $flags->{tools} eq '' && $flags->{tools} )
576 StaffDetailItemSelection => $StaffDetailItemSelection );
580 # get biblionumbers stored in the cart
583 if($query->cookie("intranet_bib_list")){
584 my $cart_list = $query->cookie("intranet_bib_list");
585 @cart_list = split(/\//, $cart_list);
586 if ( grep {$_ eq $biblionumber} @cart_list) {
587 $template->param( incart => 1 );
591 $template->param(biblio => $biblio);
593 output_html_with_http_headers $query, $cookie, $template->output;