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);
42 use C4::CourseReserves qw(GetItemCourseReservesInfo);
43 use C4::Acquisition qw(GetOrdersByBiblionumber);
44 use Koha::AuthorisedValues;
49 use Koha::Virtualshelves;
52 my $query = CGI->new();
54 my $analyze = $query->param('analyze');
56 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
58 template_name => 'catalogue/detail.tt',
62 flagsrequired => { catalogue => 1 },
66 # Determine if we should be offering any enhancement plugin buttons
67 if ( C4::Context->preference('UseKohaPlugins') &&
68 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'
74 my @tab_plugins = Koha::Plugins->new()->GetPlugins({
75 method => 'intranet_catalog_biblio_tab',
78 foreach my $tab_plugin (@tab_plugins) {
82 @biblio_tabs = $tab_plugin->intranet_catalog_biblio_tab();
83 foreach my $tab (@biblio_tabs) {
84 my $tab_id = 'tab-' . $tab->title;
85 $tab_id =~ s/[^0-9A-Za-z]+/-/g;
91 warn "Error calling 'intranet_catalog_biblio_tab' on the " . $tab_plugin->{class} . "plugin ($_)";
101 my $biblionumber = $query->param('biblionumber');
102 $biblionumber = HTML::Entities::encode($biblionumber);
103 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
104 my $biblio = Koha::Biblios->find( $biblionumber );
105 $template->param( 'biblio', $biblio );
107 if ( not defined $record ) {
108 # biblionumber invalid -> report and exit
109 $template->param( unknownbiblionumber => 1,
110 biblionumber => $biblionumber );
111 output_html_with_http_headers $query, $cookie, $template->output;
115 eval { $biblio->metadata->record };
116 $template->param( decoding_error => $@ );
118 if($query->cookie("holdfor")){
119 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
121 # FIXME Should pass the patron object
122 holdfor => $query->cookie("holdfor"),
123 holdfor_surname => $holdfor_patron->surname,
124 holdfor_firstname => $holdfor_patron->firstname,
125 holdfor_cardnumber => $holdfor_patron->cardnumber,
129 my $fw = GetFrameworkCode($biblionumber);
130 my $showallitems = $query->param('showallitems');
131 my $marcflavour = C4::Context->preference("marcflavour");
133 # XSLT processing of some stuff
134 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
135 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
136 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
140 XSLTDetailsDisplay => '1',
141 XSLTBloc => XSLTParse4Display(
142 $biblionumber, $record, "XSLTDetailsDisplay",
143 1, undef, $sysxml, $xslfile, $lang
148 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
150 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
151 # Do not propagate it as we already deal with it previously in this script
152 my $coins = eval { $biblio->get_coins };
153 $template->param( ocoins => $coins );
155 # some useful variables for enhanced content;
156 # in each case, we're grabbing the first value we find in
157 # the record and normalizing it
158 my $upc = GetNormalizedUPC($record,$marcflavour);
159 my $ean = GetNormalizedEAN($record,$marcflavour);
160 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
161 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
164 normalized_upc => $upc,
165 normalized_ean => $ean,
166 normalized_oclc => $oclc,
167 normalized_isbn => $isbn,
170 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
171 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
172 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
173 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
174 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
175 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
176 my $marchostsarray = GetMarcHosts($record,$marcflavour);
178 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
180 my $dbh = C4::Context->dbh;
182 my @all_items = GetItemsInfo( $biblionumber );
184 my $patron = Koha::Patrons->find( $borrowernumber );
185 for my $itm (@all_items) {
186 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
189 # flag indicating existence of at least one item linked via a host record
191 # adding items linked via host biblios
192 my @hostitems = GetHostItemsInfo($record);
195 push (@items,@hostitems);
198 my $dat = &GetBiblioData($biblionumber);
200 #coping with subscriptions
201 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
202 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
205 foreach my $subscription (@subscriptions) {
207 my $serials_to_display;
208 $cell{subscriptionid} = $subscription->{subscriptionid};
209 $cell{subscriptionnotes} = $subscription->{internalnotes};
210 $cell{missinglist} = $subscription->{missinglist};
211 $cell{librariannote} = $subscription->{librariannote};
212 $cell{branchcode} = $subscription->{branchcode};
213 $cell{hasalert} = $subscription->{hasalert};
214 $cell{callnumber} = $subscription->{callnumber};
215 $cell{location} = $subscription->{location};
216 $cell{closed} = $subscription->{closed};
217 #get the three latest serials.
218 $serials_to_display = $subscription->{staffdisplaycount};
219 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
220 $cell{staffdisplaycount} = $serials_to_display;
221 $cell{latestserials} =
222 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
227 # Get acquisition details
228 if ( C4::Context->preference('AcquisitionDetails') ) {
229 my $orders = Koha::Acquisition::Orders->search(
230 { biblionumber => $biblionumber },
233 order_by => 'basketno.booksellerid'
235 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
242 if ( defined $dat->{'itemtype'} ) {
243 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
246 $dat->{'count'} = scalar @all_items + @hostitems;
247 $dat->{'showncount'} = scalar @items + @hostitems;
248 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
251 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
253 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
255 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
256 my (@itemloop, @otheritemloop, %itemfields);
259 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
261 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
263 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
265 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
267 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
269 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
272 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
275 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
276 if ($materials_authvals) {
277 foreach my $value (@$materials_authvals) {
278 $materials_map{$value->{authorised_value}} = $value->{lib};
284 my $materials_flag; # set this if the items have anything in the materials field
285 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
286 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
287 $template->param(SeparateHoldings => 1);
289 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
290 foreach my $item (@items) {
291 my $itembranchcode = $item->{$separatebranch};
293 # can place holds defaults to yes
294 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
296 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
299 $item->{datedue} = format_sqldatetime($item->{datedue});
301 #get shelf location and collection code description if they are authorised value.
302 # same thing for copy number
303 my $shelfcode = $item->{'location'};
304 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
305 my $ccode = $item->{'ccode'};
306 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
307 my $copynumber = $item->{'copynumber'};
308 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
309 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
310 $itemfields{$_} = 1 if ( $item->{$_} );
314 my $item_object = Koha::Items->find( $item->{itemnumber} );
315 my $holds = $item_object->current_holds;
316 if ( my $first_hold = $holds->next ) {
317 $item->{first_hold} = $first_hold;
320 if ( my $checkout = $item_object->checkout ) {
321 $item->{CheckedOutFor} = $checkout->patron;
324 # Check the transit status
325 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
326 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
327 $item->{transfertwhen} = $transfertwhen;
328 $item->{transfertfrom} = $transfertfrom;
329 $item->{transfertto} = $transfertto;
330 $item->{nocancel} = 1;
333 foreach my $f (qw( itemnotes )) {
335 $item->{$f} =~ s|\n|<br />|g;
340 #item has a host number if its biblio number does not match the current bib
342 if ($item->{biblionumber} ne $biblionumber){
343 $item->{hostbiblionumber} = $item->{biblionumber};
344 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
349 # count if item is used in analytical bibliorecords
350 # The 'countanalytics' flag is only used in the templates if analyze is set
351 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
352 if ($countanalytics > 0){
354 $item->{countanalytics} = $countanalytics;
358 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
360 if (defined $materials_map{ $item->{materials} }) {
361 $item->{materials} = $materials_map{ $item->{materials} };
365 if ( C4::Context->preference('UseCourseReserves') ) {
366 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
369 if ( C4::Context->preference('IndependentBranches') ) {
370 my $userenv = C4::Context->userenv();
371 if ( not C4::Context->IsSuperLibrarian()
372 and $userenv->{branch} ne $item->{homebranch} ) {
373 $item->{cannot_be_edited} = 1;
377 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
378 and C4::Context->preference('SeparateHoldings')) {
379 if ($itembranchcode and $itembranchcode eq $currentbranch) {
380 push @itemloop, $item;
382 push @otheritemloop, $item;
385 push @itemloop, $item;
389 # Display only one tab if one items list is empty
390 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
391 $template->param(SeparateHoldings => 0);
392 if (scalar(@itemloop) == 0) {
393 @itemloop = @otheritemloop;
397 $template->param( norequests => $norequests );
399 MARCNOTES => $marcnotesarray,
400 MARCSUBJCTS => $marcsubjctsarray,
401 MARCAUTHORS => $marcauthorsarray,
402 MARCSERIES => $marcseriesarray,
403 MARCURLS => $marcurlsarray,
404 MARCISBNS => $marcisbnsarray,
405 MARCHOSTS => $marchostsarray,
406 itemdata_ccode => $itemfields{ccode},
407 itemdata_enumchron => $itemfields{enumchron},
408 itemdata_uri => $itemfields{uri},
409 itemdata_copynumber => $itemfields{copynumber},
410 itemdata_stocknumber => $itemfields{stocknumber},
411 itemdata_publisheddate => $itemfields{publisheddate},
412 volinfo => $itemfields{enumchron},
413 itemdata_itemnotes => $itemfields{itemnotes},
414 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
415 z3950_search_params => C4::Search::z3950_search_args($dat),
416 hostrecords => $hostrecords,
417 analytics_flag => $analytics_flag,
418 C4::Search::enabled_staff_search_views,
419 materials => $materials_flag,
422 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
423 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
424 my $subfields = substr $fieldspec, 3;
425 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
426 my @alternateholdingsinfo = ();
427 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
429 for my $field (@holdingsfields) {
430 my %holding = ( holding => '' );
431 my $havesubfield = 0;
432 for my $subfield ($field->subfields()) {
433 if ((index $subfields, $$subfield[0]) >= 0) {
434 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
435 $holding{'holding'} .= $$subfield[1];
440 push(@alternateholdingsinfo, \%holding);
445 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
449 my @results = ( $dat, );
450 foreach ( keys %{$dat} ) {
451 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
454 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
455 # method query not found?!?!
456 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
458 itemloop => \@itemloop,
459 otheritemloop => \@otheritemloop,
460 biblionumber => $biblionumber,
461 ($analyze? 'analyze':'detailview') =>1,
462 subscriptions => \@subs,
463 subscriptionsnumber => $subscriptionsnumber,
464 subscriptiontitle => $dat->{title},
465 searchid => scalar $query->param('searchid'),
468 # $debug and $template->param(debug_display => 1);
472 if (C4::Context->preference("virtualshelves") ) {
473 my $shelves = Koha::Virtualshelves->search(
475 biblionumber => $biblionumber,
479 join => 'virtualshelfcontents',
482 $template->param( 'shelves' => $shelves );
486 if (C4::Context->preference("FRBRizeEditions")==1) {
489 XISBNS => scalar get_xisbns($isbn, $biblionumber)
492 if ($@) { warn "XISBN Failed $@"; }
495 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
496 my @images = ListImagesForBiblio($biblionumber);
497 $template->{VARS}->{localimages} = \@images;
501 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
502 $template->param( C4::HTML5Media->gethtml5media($record));
508 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
511 TagsShowOnDetail => $tag_quantity
513 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
514 'sort'=>'-weight', limit=>$tag_quantity}));
517 #we only need to pass the number of holds to the template
518 my $holds = $biblio->holds;
519 $template->param( holdcount => $holds->count );
521 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
522 if ($StaffDetailItemSelection) {
523 # Only enable item selection if user can execute at least one action
525 $flags->{superlibrarian}
527 ref $flags->{tools} eq 'HASH' && (
528 $flags->{tools}->{items_batchmod} # Modify selected items
529 || $flags->{tools}->{items_batchdel} # Delete selected items
532 || ( ref $flags->{tools} eq '' && $flags->{tools} )
536 StaffDetailItemSelection => $StaffDetailItemSelection );
540 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
541 my @deletedorders_using_biblio;
542 my @orders_using_biblio;
544 my @baskets_deletedorders;
546 foreach my $myorder (@allorders_using_biblio) {
547 my $basket = $myorder->{'basketno'};
548 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
549 push @deletedorders_using_biblio, $myorder;
550 unless (grep(/^$basket$/, @baskets_deletedorders)){
551 push @baskets_deletedorders,$myorder->{'basketno'};
555 push @orders_using_biblio, $myorder;
556 unless (grep(/^$basket$/, @baskets_orders)){
557 push @baskets_orders,$myorder->{'basketno'};
562 $template->param(biblio => $biblio);
564 my $count_orders_using_biblio = scalar @orders_using_biblio ;
565 $template->param (countorders => $count_orders_using_biblio);
567 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
568 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
570 $template->param (basketsorders => \@baskets_orders);
571 $template->param (basketsdeletedorders => \@baskets_deletedorders);
573 output_html_with_http_headers $query, $cookie, $template->output;