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
128 my $searcher = Koha::SearchEngine::Search->new(
129 { index => $Koha::SearchEngine::BIBLIOS_INDEX }
131 my $cleaned_title = $biblio->title;
132 $cleaned_title =~ tr|/||;
134 ( C4::Context->preference('UseControlNumber') and $record->field('001') )
135 ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
136 : "Host-item:($cleaned_title)";
137 my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
139 warn "Warning from simple_search_compat: $err"
143 show_analytics_link => $count > 0 ? 1 : 0
147 XSLTDetailsDisplay => '1',
148 XSLTBloc => XSLTParse4Display(
150 biblionumber => $biblionumber,
152 xsl_syspref => "XSLTDetailsDisplay",
154 xslt_variables => $variables
160 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
162 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
163 # Do not propagate it as we already deal with it previously in this script
164 my $coins = eval { $biblio->get_coins };
165 $template->param( ocoins => $coins );
167 # some useful variables for enhanced content;
168 # in each case, we're grabbing the first value we find in
169 # the record and normalizing it
170 my $upc = GetNormalizedUPC($record,$marcflavour);
171 my $ean = GetNormalizedEAN($record,$marcflavour);
172 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
173 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
176 normalized_upc => $upc,
177 normalized_ean => $ean,
178 normalized_oclc => $oclc,
179 normalized_isbn => $isbn,
182 my $marcnotesarray = $biblio->get_marc_notes({ marcflavour => $marcflavour });
184 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
186 my $dbh = C4::Context->dbh;
188 my @all_items = GetItemsInfo( $biblionumber );
190 my $patron = Koha::Patrons->find( $borrowernumber );
191 for my $itm (@all_items) {
192 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
195 # flag indicating existence of at least one item linked via a host record
197 # adding items linked via host biblios
198 my @hostitems = GetHostItemsInfo($record);
201 push (@items,@hostitems);
204 my $dat = &GetBiblioData($biblionumber);
206 #coping with subscriptions
207 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
208 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
211 foreach my $subscription (@subscriptions) {
213 my $serials_to_display;
214 $cell{subscriptionid} = $subscription->{subscriptionid};
215 $cell{subscriptionnotes} = $subscription->{internalnotes};
216 $cell{missinglist} = $subscription->{missinglist};
217 $cell{librariannote} = $subscription->{librariannote};
218 $cell{branchcode} = $subscription->{branchcode};
219 $cell{hasalert} = $subscription->{hasalert};
220 $cell{callnumber} = $subscription->{callnumber};
221 $cell{location} = $subscription->{location};
222 $cell{closed} = $subscription->{closed};
223 #get the three latest serials.
224 $serials_to_display = $subscription->{staffdisplaycount};
225 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
226 $cell{staffdisplaycount} = $serials_to_display;
227 $cell{latestserials} =
228 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
233 # Get acquisition details
234 if ( C4::Context->preference('AcquisitionDetails') ) {
235 my $orders = Koha::Acquisition::Orders->search(
236 { biblionumber => $biblionumber },
239 order_by => 'basketno.booksellerid'
241 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
248 if ( C4::Context->preference('suggestion') ) {
249 my $suggestions = Koha::Suggestions->search(
251 biblionumber => $biblionumber,
255 order_by => { -desc => 'suggesteddate' }
258 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
259 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
262 if ( defined $dat->{'itemtype'} ) {
263 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
266 $dat->{'count'} = scalar @all_items + @hostitems;
267 $dat->{'showncount'} = scalar @items + @hostitems;
268 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
271 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
273 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
275 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
276 my (@itemloop, @otheritemloop, %itemfields);
278 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
280 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
282 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
284 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
286 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
288 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
291 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
294 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
295 if ($materials_authvals) {
296 foreach my $value (@$materials_authvals) {
297 $materials_map{$value->{authorised_value}} = $value->{lib};
303 my $materials_flag; # set this if the items have anything in the materials field
304 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
305 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
306 $template->param(SeparateHoldings => 1);
308 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
309 my ( $itemloop_has_images, $otheritemloop_has_images );
310 foreach my $item (@items) {
311 my $itembranchcode = $item->{$separatebranch};
313 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
316 $item->{datedue} = format_sqldatetime($item->{datedue});
318 #get shelf location and collection code description if they are authorised value.
319 # same thing for copy number
320 my $shelfcode = $item->{'location'};
321 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
322 my $ccode = $item->{'ccode'};
323 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
324 my $copynumber = $item->{'copynumber'};
325 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
326 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
327 $itemfields{$_} = 1 if ( $item->{$_} );
331 my $item_object = Koha::Items->find( $item->{itemnumber} );
332 my $holds = $item_object->current_holds;
333 if ( my $first_hold = $holds->next ) {
334 $item->{first_hold} = $first_hold;
337 if ( my $checkout = $item_object->checkout ) {
338 $item->{CheckedOutFor} = $checkout->patron;
341 # Check the transit status
342 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
343 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
344 $item->{transfertwhen} = $transfertwhen;
345 $item->{transfertfrom} = $transfertfrom;
346 $item->{transfertto} = $transfertto;
347 $item->{nocancel} = 1;
350 foreach my $f (qw( itemnotes )) {
352 $item->{$f} =~ s|\n|<br />|g;
357 #item has a host number if its biblio number does not match the current bib
359 if ($item->{biblionumber} ne $biblionumber){
360 $item->{hostbiblionumber} = $item->{biblionumber};
361 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
366 # count if item is used in analytical bibliorecords
367 # The 'countanalytics' flag is only used in the templates if analyze is set
368 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
369 if ($countanalytics > 0){
371 $item->{countanalytics} = $countanalytics;
375 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
377 if (defined $materials_map{ $item->{materials} }) {
378 $item->{materials} = $materials_map{ $item->{materials} };
382 if ( C4::Context->preference('UseCourseReserves') ) {
383 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
386 if ( C4::Context->preference('IndependentBranches') ) {
387 my $userenv = C4::Context->userenv();
388 if ( not C4::Context->IsSuperLibrarian()
389 and $userenv->{branch} ne $item->{homebranch} ) {
390 $item->{cannot_be_edited} = 1;
394 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
395 $item->{cover_images} = $item_object->cover_images;
398 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
399 if ($itembranchcode and $itembranchcode eq $currentbranch) {
400 push @itemloop, $item;
401 $itemloop_has_images++ if $item_object->cover_images->count;
403 push @otheritemloop, $item;
404 $otheritemloop_has_images++ if $item_object->cover_images->count;
407 push @itemloop, $item;
408 $itemloop_has_images++ if $item_object->cover_images->count;
413 itemloop_has_images => $itemloop_has_images,
414 otheritemloop_has_images => $otheritemloop_has_images,
417 # Display only one tab if one items list is empty
418 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
419 $template->param(SeparateHoldings => 0);
420 if (scalar(@itemloop) == 0) {
421 @itemloop = @otheritemloop;
425 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
427 borrowernumber => $borrowernumber,
432 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
434 borrowernumber => $borrowernumber,
442 add_to_some_private_shelves => $some_private_shelves,
443 add_to_some_public_shelves => $some_public_shelves,
447 MARCNOTES => $marcnotesarray,
448 itemdata_ccode => $itemfields{ccode},
449 itemdata_enumchron => $itemfields{enumchron},
450 itemdata_uri => $itemfields{uri},
451 itemdata_copynumber => $itemfields{copynumber},
452 itemdata_stocknumber => $itemfields{stocknumber},
453 itemdata_publisheddate => $itemfields{publisheddate},
454 volinfo => $itemfields{enumchron},
455 itemdata_itemnotes => $itemfields{itemnotes},
456 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
457 z3950_search_params => C4::Search::z3950_search_args($dat),
458 hostrecords => $hostrecords,
459 analytics_flag => $analytics_flag,
460 C4::Search::enabled_staff_search_views,
461 materials => $materials_flag,
464 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
465 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
466 my $subfields = substr $fieldspec, 3;
467 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
468 my @alternateholdingsinfo = ();
469 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
471 for my $field (@holdingsfields) {
472 my %holding = ( holding => '' );
473 my $havesubfield = 0;
474 for my $subfield ($field->subfields()) {
475 if ((index $subfields, $$subfield[0]) >= 0) {
476 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
477 $holding{'holding'} .= $$subfield[1];
482 push(@alternateholdingsinfo, \%holding);
487 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
491 my @results = ( $dat, );
492 foreach ( keys %{$dat} ) {
493 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
496 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
497 # method query not found?!?!
498 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
500 itemloop => \@itemloop,
501 otheritemloop => \@otheritemloop,
502 biblionumber => $biblionumber,
503 ($analyze? 'analyze':'detailview') =>1,
504 subscriptions => \@subs,
505 subscriptionsnumber => $subscriptionsnumber,
506 subscriptiontitle => $dat->{title},
507 searchid => scalar $query->param('searchid'),
512 if (C4::Context->preference("virtualshelves") ) {
513 my $shelves = Koha::Virtualshelves->search(
515 biblionumber => $biblionumber,
519 join => 'virtualshelfcontents',
522 $template->param( 'shelves' => $shelves );
526 if (C4::Context->preference("FRBRizeEditions")==1) {
529 XISBNS => scalar get_xisbns($isbn, $biblionumber)
532 if ($@) { warn "XISBN Failed $@"; }
535 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
536 my $images = $biblio->cover_images;
537 $template->param( localimages => $biblio->cover_images );
541 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
542 $template->param( C4::HTML5Media->gethtml5media($record));
547 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
550 TagsShowOnDetail => $tag_quantity
552 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
553 'sort'=>'-weight', limit=>$tag_quantity}));
556 #we only need to pass the number of holds to the template
557 my $holds = $biblio->holds;
558 $template->param( holdcount => $holds->count );
560 # Check if there are any ILL requests connected to the biblio
562 C4::Context->preference('ILLModule')
563 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
565 $template->param( illrequests => $illrequests );
567 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
568 if ($StaffDetailItemSelection) {
569 # Only enable item selection if user can execute at least one action
571 $flags->{superlibrarian}
573 ref $flags->{tools} eq 'HASH' && (
574 $flags->{tools}->{items_batchmod} # Modify selected items
575 || $flags->{tools}->{items_batchdel} # Delete selected items
578 || ( ref $flags->{tools} eq '' && $flags->{tools} )
582 StaffDetailItemSelection => $StaffDetailItemSelection );
586 # get biblionumbers stored in the cart
589 if($query->cookie("intranet_bib_list")){
590 my $cart_list = $query->cookie("intranet_bib_list");
591 @cart_list = split(/\//, $cart_list);
592 if ( grep {$_ eq $biblionumber} @cart_list) {
593 $template->param( incart => 1 );
597 if ( C4::Context->preference('UseCourseReserves') ) {
598 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
599 $template->param( course_reserves => $course_reserves );
602 $template->param(biblio => $biblio);
604 output_html_with_http_headers $query, $cookie, $template->output;