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 );
36 use C4::Items qw( GetAnalyticsCount );
38 use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
39 use C4::XISBN qw( get_xisbns );
40 use C4::External::Amazon qw( get_amazon_tld );
41 use C4::Search qw( z3950_search_args enabled_staff_search_views new_record_from_zebra );
42 use C4::Tags qw( get_tags );
43 use C4::XSLT qw( XSLTParse4Display );
44 use Koha::DateUtils qw( format_sqldatetime );
46 use C4::CourseReserves qw( GetItemCourseReservesInfo );
47 use Koha::AuthorisedValues;
49 use Koha::Biblio::ItemGroup::Items;
50 use Koha::Biblio::ItemGroups;
51 use Koha::CoverImages;
53 use Koha::Illrequests;
57 use Koha::Virtualshelves;
60 use Koha::SearchEngine::Search;
61 use Koha::SearchEngine::QueryBuilder;
62 use Koha::Serial::Items;
64 my $query = CGI->new();
66 my $analyze = $query->param('analyze');
68 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
70 template_name => 'catalogue/detail.tt',
73 flagsrequired => { catalogue => 1 },
77 # Determine if we should be offering any enhancement plugin buttons
78 if ( C4::Context->config('enable_plugins') ) {
79 # Only pass plugins that can offer a toolbar button
80 my @plugins = Koha::Plugins->new()->GetPlugins({
81 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
88 my $biblionumber = $query->param('biblionumber');
89 $biblionumber = HTML::Entities::encode($biblionumber);
90 my $biblio = Koha::Biblios->find( $biblionumber );
91 $template->param( 'biblio', $biblio );
94 # biblionumber invalid -> report and exit
95 $template->param( unknownbiblionumber => 1,
96 biblionumber => $biblionumber );
97 output_html_with_http_headers $query, $cookie, $template->output;
101 my $marc_record = eval { $biblio->metadata->record };
102 my $invalid_marc_record = $@ || !$marc_record;
103 if ($invalid_marc_record) {
104 $template->param( decoding_error => $@ );
105 my $marc_xml = C4::Charset::StripNonXmlChars( $biblio->metadata->metadata );
107 $marc_record = eval {
108 MARC::Record::new_from_xml( $marc_xml, 'UTF-8',
109 C4::Context->preference('marcflavour') );
113 my $op = $query->param('op') || q{};
114 if ( $op eq 'set_item_group' ) {
115 my $item_group_id = $query->param('item_group_id');
116 my @itemnumbers = $query->multi_param('itemnumber');
118 foreach my $item_id (@itemnumbers) {
119 my $item_group_item = Koha::Biblio::ItemGroup::Items->find( { item_id => $item_id } );
121 if ($item_group_item) {
122 $item_group_item->item_group_id($item_group_id);
125 $item_group_item = Koha::Biblio::ItemGroup::Item->new(
128 item_group_id => $item_group_id,
133 $item_group_item->store();
136 elsif ( $op eq 'unset_item_group' ) {
137 my $item_group_id = $query->param('item_group_id');
138 my @itemnumbers = $query->multi_param('itemnumber');
140 foreach my $item_id (@itemnumbers) {
141 my $item_group_item = Koha::Biblio::ItemGroup::Items->find( { item_id => $item_id } );
142 $item_group_item->delete() if $item_group_item;
146 if($query->cookie("holdfor")){
147 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
148 if ( $holdfor_patron ) {
150 holdfor => $query->cookie("holdfor"),
151 holdfor_patron => $holdfor_patron,
156 if($query->cookie("searchToOrder")){
157 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
159 searchtoorder_basketno => $basketno,
160 searchtoorder_vendorid => $vendorid
164 my $fw = GetFrameworkCode($biblionumber);
165 my $showallitems = $query->param('showallitems');
166 my $marcflavour = C4::Context->preference("marcflavour");
168 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
170 $template->param( ocoins => !$invalid_marc_record ? $biblio->get_coins : undef );
172 # some useful variables for enhanced content;
173 # in each case, we're grabbing the first value we find in
174 # the record and normalizing it
175 my $upc = GetNormalizedUPC($marc_record,$marcflavour);
176 my $ean = GetNormalizedEAN($marc_record,$marcflavour);
177 my $oclc = GetNormalizedOCLCNumber($marc_record,$marcflavour);
178 my $isbn = GetNormalizedISBN(undef,$marc_record,$marcflavour);
179 my $content_identifier_exists;
180 if ( $isbn or $ean or $oclc or $upc ) {
181 $content_identifier_exists = 1;
185 normalized_upc => $upc,
186 normalized_ean => $ean,
187 normalized_oclc => $oclc,
188 normalized_isbn => $isbn,
189 content_identifier_exists => $content_identifier_exists,
192 my $itemtypes = { map { $_->itemtype => $_ } @{ Koha::ItemTypes->search_with_localization->as_list } };
193 my $all_items = $biblio->items->search_ordered;
195 my $patron = Koha::Patrons->find( $borrowernumber );
196 while ( my $item = $all_items->next ) {
198 unless $item->itemlost
199 && $patron->category->hidelostitems
203 # flag indicating existence of at least one item linked via a host record
205 # adding items linked via host biblios
206 my $hostitems = $biblio->host_items;
207 if ( $hostitems->count ) {
209 push @items, $hostitems->as_list;
212 my $dat = &GetBiblioData($biblionumber);
214 #is biblio a collection and are bundles enabled
215 my $leader = $marc_record->leader();
216 $dat->{bundlesEnabled} = ( ( substr( $leader, 7, 1 ) eq 'c' )
217 && C4::Context->preference('BundleNotLoanValue') ) ? 1 : 0;
219 #coping with subscriptions
220 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
221 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
224 foreach my $subscription (@subscriptions) {
226 my $serials_to_display;
227 $cell{subscriptionid} = $subscription->{subscriptionid};
228 $cell{subscriptionnotes} = $subscription->{internalnotes};
229 $cell{missinglist} = $subscription->{missinglist};
230 $cell{librariannote} = $subscription->{librariannote};
231 $cell{branchcode} = $subscription->{branchcode};
232 $cell{hasalert} = $subscription->{hasalert};
233 $cell{callnumber} = $subscription->{callnumber};
234 $cell{location} = $subscription->{location};
235 $cell{closed} = $subscription->{closed};
236 #get the three latest serials.
237 $serials_to_display = $subscription->{staffdisplaycount};
238 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
239 $cell{staffdisplaycount} = $serials_to_display;
240 $cell{latestserials} =
241 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
245 # Get component parts details
246 my $showcomp = C4::Context->preference('ShowComponentRecords');
248 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
249 if ( my $components = !$invalid_marc_record ? $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) : undef ) {
250 $show_analytics = 1 if @{$components}; # just show link when having results
251 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
253 for my $part ( @{$components} ) {
254 $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
255 my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
262 xsl_syspref => "XSLTResultsDisplay",
264 interface => 'intranet'
268 $template->param( ComponentParts => $parts );
269 my ( $comp_query, $comp_sort ) = $biblio->get_components_query;
270 my $cpq = $comp_query . "&sort_by=" . $comp_sort;
271 $template->param( ComponentPartsQuery => $cpq );
273 } else { # check if we should show analytics anyway
274 $show_analytics = 1 if !$invalid_marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
275 $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
278 # XSLT processing of some stuff
279 my $xslt_variables = { show_analytics_link => $show_analytics };
281 XSLTDetailsDisplay => '1',
282 XSLTBloc => XSLTParse4Display({
283 biblionumber => $biblionumber,
284 record => $marc_record,
285 xsl_syspref => "XSLTDetailsDisplay",
287 xslt_variables => $xslt_variables,
288 interface => 'intranet'
292 # Get acquisition details
293 if ( C4::Context->preference('AcquisitionDetails') ) {
294 my $orders = Koha::Acquisition::Orders->search(
295 { biblionumber => $biblionumber },
298 order_by => 'basketno.booksellerid'
300 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
307 if ( C4::Context->preference('suggestion') ) {
308 my $suggestions = Koha::Suggestions->search(
310 biblionumber => $biblionumber,
314 order_by => { -desc => 'suggesteddate' }
317 my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
318 $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
321 if ( defined $dat->{'itemtype'} ) {
322 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }->imageurl );
325 $dat->{'count'} = $all_items->count + $hostitems->count;
326 $dat->{'showncount'} = scalar @items + $hostitems->count;
327 $dat->{'hiddencount'} = $all_items->count + $hostitems->count - scalar @items;
330 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
332 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
334 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
335 my (@itemloop, @otheritemloop, %itemfields);
337 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
339 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
341 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
343 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
345 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
347 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
350 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
353 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
354 if ($materials_authvals) {
355 foreach my $value (@$materials_authvals) {
356 $materials_map{$value->{authorised_value}} = $value->{lib};
362 my $materials_flag; # set this if the items have anything in the materials field
363 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
364 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
365 $template->param(SeparateHoldings => 1);
367 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
368 my ( $itemloop_has_images, $otheritemloop_has_images );
370 foreach my $item (@items) {
371 my $itembranchcode = $item->$separatebranch;
373 my $item_info = $item->unblessed;
374 $item_info->{itemtype} = $itemtypes->{$item->effective_itemtype};
376 #get shelf location and collection code description if they are authorised value.
377 # same thing for copy number
378 my $shelfcode = $item->location;
379 $item_info->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
380 my $ccode = $item->ccode;
381 $item_info->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
382 my $copynumber = $item->copynumber;
383 $item_info->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
384 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri )) {
385 $itemfields{$_} = 1 if $item->$_;
388 # FIXME The following must be Koha::Item->serial
389 my $serial_item = Koha::Serial::Items->find($item->itemnumber);
390 if ( $serial_item ) {
391 $item_info->{serial} = $serial_item;
392 $itemfields{publisheddate} = 1;
395 $item_info->{object} = $item;
398 my $holds = $item->current_holds;
399 if ( my $first_hold = $holds->next ) {
400 $item_info->{first_hold} = $first_hold;
403 $item_info->{checkout} = $item->checkout;
405 # Check the transit status
406 my $transfer = $item->get_transfer;
407 if ( $transfer && $transfer->in_transit ) {
408 $item_info->{transfertwhen} = $transfer->datesent;
409 $item_info->{transfertfrom} = $transfer->frombranch;
410 $item_info->{transfertto} = $transfer->tobranch;
411 $item_info->{nocancel} = 1;
414 foreach my $f (qw( itemnotes )) {
415 if ($item_info->{$f}) {
416 $item_info->{$f} =~ s|\n|<br />|g;
421 #item has a host number if its biblio number does not match the current bib
423 if ($item->biblionumber ne $biblionumber){
424 $item_info->{hostbiblionumber} = $item->biblionumber;
425 $item_info->{hosttitle} = $item->biblio->title;
430 # count if item is used in analytical bibliorecords
431 # The 'countanalytics' flag is only used in the templates if analyze is set
432 my $countanalytics = GetAnalyticsCount( $item->itemnumber );
433 if ($countanalytics > 0){
435 $item_info->{countanalytics} = $countanalytics;
439 if (defined($item->materials) && $item->materials =~ /\S/){
441 if (defined $materials_map{ $item->materials }) {
442 $item_info->{materials} = $materials_map{ $item->materials };
446 if ( C4::Context->preference('UseCourseReserves') ) {
447 $item_info->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
450 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
451 $item_info->{cover_images} = $item->cover_images;
454 if ( C4::Context->preference('UseRecalls') ) {
455 $item_info->{recall} = $item->recall;
458 if ( C4::Context->preference('IndependentBranches') ) {
459 my $userenv = C4::Context->userenv();
460 if ( not C4::Context->IsSuperLibrarian()
461 and $userenv->{branch} ne $item->homebranch ) {
462 $item_info->{cannot_be_edited} = 1;
463 $item_info->{not_same_branch} = 1;
467 if ( $item->is_bundle ) {
468 $item_info->{bundled} =
469 $item->bundle_items->search( { itemlost => { '!=' => 0 } } )
471 $item_info->{bundled_lost} =
472 $item->bundle_items->search( { itemlost => 0 } )->count;
473 $item_info->{is_bundle} = 1;
476 if ($item->in_bundle) {
477 $item_info->{bundle_host} = $item->bundle_host;
480 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
481 if ($itembranchcode and $itembranchcode eq $currentbranch) {
482 push @itemloop, $item_info;
483 $itemloop_has_images++ if $item->cover_images->count;
485 push @otheritemloop, $item_info;
486 $otheritemloop_has_images++ if $item->cover_images->count;
489 push @itemloop, $item_info;
490 $itemloop_has_images++ if $item->cover_images->count;
495 itemloop_has_images => $itemloop_has_images,
496 otheritemloop_has_images => $otheritemloop_has_images,
499 # Display only one tab if one items list is empty
500 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
501 $template->param(SeparateHoldings => 0);
502 if (scalar(@itemloop) == 0) {
503 @itemloop = @otheritemloop;
507 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
509 borrowernumber => $borrowernumber,
514 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
516 borrowernumber => $borrowernumber,
524 add_to_some_private_shelves => $some_private_shelves,
525 add_to_some_public_shelves => $some_public_shelves,
529 MARCNOTES => !$invalid_marc_record ? $biblio->get_marc_notes() : undef,
530 itemdata_ccode => $itemfields{ccode},
531 itemdata_enumchron => $itemfields{enumchron},
532 itemdata_uri => $itemfields{uri},
533 itemdata_copynumber => $itemfields{copynumber},
534 itemdata_stocknumber => $itemfields{stocknumber},
535 itemdata_publisheddate => $itemfields{publisheddate},
536 volinfo => $itemfields{enumchron},
537 itemdata_itemnotes => $itemfields{itemnotes},
538 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
539 z3950_search_params => C4::Search::z3950_search_args($dat),
540 hostrecords => $hostrecords,
541 analytics_flag => $analytics_flag,
542 C4::Search::enabled_staff_search_views,
543 materials => $materials_flag,
546 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
547 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
548 my $subfields = substr $fieldspec, 3;
549 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
550 my @alternateholdingsinfo = ();
551 my @holdingsfields = $marc_record->field(substr $fieldspec, 0, 3);
553 for my $field (@holdingsfields) {
554 my %holding = ( holding => '' );
555 my $havesubfield = 0;
556 for my $subfield ($field->subfields()) {
557 if ((index $subfields, $$subfield[0]) >= 0) {
558 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
559 $holding{'holding'} .= $$subfield[1];
564 push(@alternateholdingsinfo, \%holding);
569 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
573 my @results = ( $dat, );
574 foreach ( keys %{$dat} ) {
575 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
578 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
579 # method query not found?!?!
580 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
582 itemloop => \@itemloop,
583 otheritemloop => \@otheritemloop,
584 biblionumber => $biblionumber,
585 ($analyze? 'analyze':'detailview') =>1,
586 subscriptions => \@subs,
587 subscriptionsnumber => $subscriptionsnumber,
588 subscriptiontitle => $dat->{title},
589 searchid => scalar $query->param('searchid'),
594 if (C4::Context->preference("virtualshelves") ) {
595 my $shelves = Koha::Virtualshelves->search(
597 biblionumber => $biblionumber,
601 join => 'virtualshelfcontents',
604 $template->param( 'shelves' => $shelves );
608 if (C4::Context->preference("FRBRizeEditions")==1) {
611 XISBNS => scalar get_xisbns($isbn, $biblionumber)
614 if ($@) { warn "XISBN Failed $@"; }
617 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
618 my $images = $biblio->cover_images;
620 localimages => $biblio->cover_images->search(
621 {}, { order_by => [ \"COALESCE(itemnumber, 0, 1)", 'timestamp' ] }
627 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
628 $template->param( C4::HTML5Media->gethtml5media($marc_record));
633 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
636 TagsShowOnDetail => $tag_quantity
638 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
639 'sort'=>'-weight', limit=>$tag_quantity}));
642 #we only need to pass the number of holds to the template
643 my $holds = $biblio->holds;
644 $template->param( holdcount => $holds->count );
646 # Check if there are any ILL requests connected to the biblio
648 C4::Context->preference('ILLModule')
649 ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
651 $template->param( illrequests => $illrequests );
653 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
654 if ($StaffDetailItemSelection) {
655 # Only enable item selection if user can execute at least one action
657 $flags->{superlibrarian}
659 ref $flags->{tools} eq 'HASH' && (
660 $flags->{tools}->{items_batchmod} # Modify selected items
661 || $flags->{tools}->{items_batchdel} # Delete selected items
664 || ( ref $flags->{tools} eq '' && $flags->{tools} )
668 StaffDetailItemSelection => $StaffDetailItemSelection );
672 # get biblionumbers stored in the cart
675 if($query->cookie("intranet_bib_list")){
676 my $cart_list = $query->cookie("intranet_bib_list");
677 @cart_list = split(/\//, $cart_list);
678 if ( grep {$_ eq $biblionumber} @cart_list) {
679 $template->param( incart => 1 );
683 if ( C4::Context->preference('UseCourseReserves') ) {
684 my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
685 $template->param( course_reserves => $course_reserves );
688 $template->param(found1 => scalar $query->param('found1') );
690 $template->param(biblio => $biblio);
692 output_html_with_http_headers $query, $cookie, $template->output;