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 );
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;
59 my $query = CGI->new();
61 my $analyze = $query->param('analyze');
63 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
65 template_name => 'catalogue/detail.tt',
68 flagsrequired => { catalogue => 1 },
72 # Determine if we should be offering any enhancement plugin buttons
73 if ( C4::Context->config('enable_plugins') ) {
74 # Only pass plugins that can offer a toolbar button
75 my @plugins = Koha::Plugins->new()->GetPlugins({
76 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
83 my $biblionumber = $query->param('biblionumber');
84 $biblionumber = HTML::Entities::encode($biblionumber);
85 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
86 my $biblio = Koha::Biblios->find( $biblionumber );
87 $template->param( 'biblio', $biblio );
89 if ( not defined $record ) {
90 # biblionumber invalid -> report and exit
91 $template->param( unknownbiblionumber => 1,
92 biblionumber => $biblionumber );
93 output_html_with_http_headers $query, $cookie, $template->output;
97 eval { $biblio->metadata->record };
98 $template->param( decoding_error => $@ );
100 if($query->cookie("holdfor")){
101 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
102 if ( $holdfor_patron ) {
104 # FIXME Should pass the patron object
105 holdfor => $query->cookie("holdfor"),
106 holdfor_surname => $holdfor_patron->surname,
107 holdfor_firstname => $holdfor_patron->firstname,
108 holdfor_cardnumber => $holdfor_patron->cardnumber,
113 if($query->cookie("searchToOrder")){
114 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
116 searchtoorder_basketno => $basketno,
117 searchtoorder_vendorid => $vendorid
121 my $fw = GetFrameworkCode($biblionumber);
122 my $showallitems = $query->param('showallitems');
123 my $marcflavour = C4::Context->preference("marcflavour");
126 # XSLT processing of some stuff
127 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
128 my $lang = C4::Languages::getlanguage();
129 my $sysxml = C4::XSLT::get_xslt_sysprefs();
131 my $searcher = Koha::SearchEngine::Search->new(
132 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
134 my $cleaned_title = $biblio->title;
135 $cleaned_title =~ tr|/||;
137 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
138 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
139 : "Host-item:($cleaned_title)";
140 my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
142 warn "Warning from simple_search_compat: $err"
146 show_analytics_link => $count > 0 ? 1 : 0
150 XSLTDetailsDisplay => '1',
151 XSLTBloc => XSLTParse4Display(
152 $biblionumber, $record, "XSLTDetailsDisplay", 1,
153 undef, $sysxml, $xslfile, $lang,
159 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
161 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
162 # Do not propagate it as we already deal with it previously in this script
163 my $coins = eval { $biblio->get_coins };
164 $template->param( ocoins => $coins );
166 # some useful variables for enhanced content;
167 # in each case, we're grabbing the first value we find in
168 # the record and normalizing it
169 my $upc = GetNormalizedUPC($record,$marcflavour);
170 my $ean = GetNormalizedEAN($record,$marcflavour);
171 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
172 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
175 normalized_upc => $upc,
176 normalized_ean => $ean,
177 normalized_oclc => $oclc,
178 normalized_isbn => $isbn,
181 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
183 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
185 my $dbh = C4::Context->dbh;
187 my @all_items = GetItemsInfo( $biblionumber );
189 my $patron = Koha::Patrons->find( $borrowernumber );
190 for my $itm (@all_items) {
191 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
194 # flag indicating existence of at least one item linked via a host record
196 # adding items linked via host biblios
197 my @hostitems = GetHostItemsInfo($record);
200 push (@items,@hostitems);
203 my $dat = &GetBiblioData($biblionumber);
205 #coping with subscriptions
206 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
207 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
210 foreach my $subscription (@subscriptions) {
212 my $serials_to_display;
213 $cell{subscriptionid} = $subscription->{subscriptionid};
214 $cell{subscriptionnotes} = $subscription->{internalnotes};
215 $cell{missinglist} = $subscription->{missinglist};
216 $cell{librariannote} = $subscription->{librariannote};
217 $cell{branchcode} = $subscription->{branchcode};
218 $cell{hasalert} = $subscription->{hasalert};
219 $cell{callnumber} = $subscription->{callnumber};
220 $cell{location} = $subscription->{location};
221 $cell{closed} = $subscription->{closed};
222 #get the three latest serials.
223 $serials_to_display = $subscription->{staffdisplaycount};
224 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
225 $cell{staffdisplaycount} = $serials_to_display;
226 $cell{latestserials} =
227 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
232 # Get acquisition details
233 if ( C4::Context->preference('AcquisitionDetails') ) {
234 my $orders = Koha::Acquisition::Orders->search(
235 { biblionumber => $biblionumber },
238 order_by => 'basketno.booksellerid'
240 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
247 if ( C4::Context->preference('suggestion') ) {
248 my $suggestions = Koha::Suggestions->search(
250 biblionumber => $biblionumber,
254 order_by => { -desc => 'suggesteddate' }
257 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
258 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
261 if ( defined $dat->{'itemtype'} ) {
262 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
265 $dat->{'count'} = scalar @all_items + @hostitems;
266 $dat->{'showncount'} = scalar @items + @hostitems;
267 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
270 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
272 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
274 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
275 my (@itemloop, @otheritemloop, %itemfields);
277 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
279 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
281 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
283 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
285 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
287 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
290 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
293 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
294 if ($materials_authvals) {
295 foreach my $value (@$materials_authvals) {
296 $materials_map{$value->{authorised_value}} = $value->{lib};
302 my $materials_flag; # set this if the items have anything in the materials field
303 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
304 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
305 $template->param(SeparateHoldings => 1);
307 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
308 my ( $itemloop_has_images, $otheritemloop_has_images );
309 foreach my $item (@items) {
310 my $itembranchcode = $item->{$separatebranch};
312 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
315 $item->{datedue} = format_sqldatetime($item->{datedue});
317 #get shelf location and collection code description if they are authorised value.
318 # same thing for copy number
319 my $shelfcode = $item->{'location'};
320 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
321 my $ccode = $item->{'ccode'};
322 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
323 my $copynumber = $item->{'copynumber'};
324 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
325 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
326 $itemfields{$_} = 1 if ( $item->{$_} );
330 my $item_object = Koha::Items->find( $item->{itemnumber} );
331 my $holds = $item_object->current_holds;
332 if ( my $first_hold = $holds->next ) {
333 $item->{first_hold} = $first_hold;
336 if ( my $checkout = $item_object->checkout ) {
337 $item->{CheckedOutFor} = $checkout->patron;
340 # Check the transit status
341 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
342 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
343 $item->{transfertwhen} = $transfertwhen;
344 $item->{transfertfrom} = $transfertfrom;
345 $item->{transfertto} = $transfertto;
346 $item->{nocancel} = 1;
349 foreach my $f (qw( itemnotes )) {
351 $item->{$f} =~ s|\n|<br />|g;
356 #item has a host number if its biblio number does not match the current bib
358 if ($item->{biblionumber} ne $biblionumber){
359 $item->{hostbiblionumber} = $item->{biblionumber};
360 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
365 # count if item is used in analytical bibliorecords
366 # The 'countanalytics' flag is only used in the templates if analyze is set
367 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
368 if ($countanalytics > 0){
370 $item->{countanalytics} = $countanalytics;
374 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
376 if (defined $materials_map{ $item->{materials} }) {
377 $item->{materials} = $materials_map{ $item->{materials} };
381 if ( C4::Context->preference('UseCourseReserves') ) {
382 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
385 if ( C4::Context->preference('IndependentBranches') ) {
386 my $userenv = C4::Context->userenv();
387 if ( not C4::Context->IsSuperLibrarian()
388 and $userenv->{branch} ne $item->{homebranch} ) {
389 $item->{cannot_be_edited} = 1;
393 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
394 $item->{cover_images} = $item_object->cover_images;
397 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
398 if ($itembranchcode and $itembranchcode eq $currentbranch) {
399 push @itemloop, $item;
400 $itemloop_has_images++ if $item_object->cover_images->count;
402 push @otheritemloop, $item;
403 $otheritemloop_has_images++ if $item_object->cover_images->count;
406 push @itemloop, $item;
407 $itemloop_has_images++ if $item_object->cover_images->count;
412 itemloop_has_images => $itemloop_has_images,
413 otheritemloop_has_images => $otheritemloop_has_images,
416 # Display only one tab if one items list is empty
417 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
418 $template->param(SeparateHoldings => 0);
419 if (scalar(@itemloop) == 0) {
420 @itemloop = @otheritemloop;
424 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
426 borrowernumber => $borrowernumber,
431 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
433 borrowernumber => $borrowernumber,
441 add_to_some_private_shelves => $some_private_shelves,
442 add_to_some_public_shelves => $some_public_shelves,
446 MARCNOTES => $marcnotesarray,
447 itemdata_ccode => $itemfields{ccode},
448 itemdata_enumchron => $itemfields{enumchron},
449 itemdata_uri => $itemfields{uri},
450 itemdata_copynumber => $itemfields{copynumber},
451 itemdata_stocknumber => $itemfields{stocknumber},
452 itemdata_publisheddate => $itemfields{publisheddate},
453 volinfo => $itemfields{enumchron},
454 itemdata_itemnotes => $itemfields{itemnotes},
455 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
456 z3950_search_params => C4::Search::z3950_search_args($dat),
457 hostrecords => $hostrecords,
458 analytics_flag => $analytics_flag,
459 C4::Search::enabled_staff_search_views,
460 materials => $materials_flag,
463 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
464 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
465 my $subfields = substr $fieldspec, 3;
466 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
467 my @alternateholdingsinfo = ();
468 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
470 for my $field (@holdingsfields) {
471 my %holding = ( holding => '' );
472 my $havesubfield = 0;
473 for my $subfield ($field->subfields()) {
474 if ((index $subfields, $$subfield[0]) >= 0) {
475 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
476 $holding{'holding'} .= $$subfield[1];
481 push(@alternateholdingsinfo, \%holding);
486 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
490 my @results = ( $dat, );
491 foreach ( keys %{$dat} ) {
492 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
495 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
496 # method query not found?!?!
497 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
499 itemloop => \@itemloop,
500 otheritemloop => \@otheritemloop,
501 biblionumber => $biblionumber,
502 ($analyze? 'analyze':'detailview') =>1,
503 subscriptions => \@subs,
504 subscriptionsnumber => $subscriptionsnumber,
505 subscriptiontitle => $dat->{title},
506 searchid => scalar $query->param('searchid'),
511 if (C4::Context->preference("virtualshelves") ) {
512 my $shelves = Koha::Virtualshelves->search(
514 biblionumber => $biblionumber,
518 join => 'virtualshelfcontents',
521 $template->param( 'shelves' => $shelves );
525 if (C4::Context->preference("FRBRizeEditions")==1) {
528 XISBNS => scalar get_xisbns($isbn, $biblionumber)
531 if ($@) { warn "XISBN Failed $@"; }
534 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
535 my $images = $biblio->cover_images;
536 $template->param( localimages => $biblio->cover_images );
540 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
541 $template->param( C4::HTML5Media->gethtml5media($record));
546 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
549 TagsShowOnDetail => $tag_quantity
551 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
552 'sort'=>'-weight', limit=>$tag_quantity}));
555 #we only need to pass the number of holds to the template
556 my $holds = $biblio->holds;
557 $template->param( holdcount => $holds->count );
559 # Check if there are any ILL requests connected to the biblio
561 C4::Context->preference('ILLModule')
562 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
564 $template->param( illrequests => $illrequests );
566 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
567 if ($StaffDetailItemSelection) {
568 # Only enable item selection if user can execute at least one action
570 $flags->{superlibrarian}
572 ref $flags->{tools} eq 'HASH' && (
573 $flags->{tools}->{items_batchmod} # Modify selected items
574 || $flags->{tools}->{items_batchdel} # Delete selected items
577 || ( ref $flags->{tools} eq '' && $flags->{tools} )
581 StaffDetailItemSelection => $StaffDetailItemSelection );
585 # get biblionumbers stored in the cart
588 if($query->cookie("intranet_bib_list")){
589 my $cart_list = $query->cookie("intranet_bib_list");
590 @cart_list = split(/\//, $cart_list);
591 if ( grep {$_ eq $biblionumber} @cart_list) {
592 $template->param( incart => 1 );
596 if ( C4::Context->preference('UseCourseReserves') ) {
597 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
598 $template->param( course_reserves => $course_reserves );
601 $template->param(biblio => $biblio);
603 output_html_with_http_headers $query, $cookie, $template->output;