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->config('enable_plugins') ) {
68 # Only pass plugins that can offer a toolbar button
69 my @plugins = Koha::Plugins->new()->GetPlugins({
70 method => 'intranet_catalog_biblio_enhancements_toolbar_button'
73 my @tab_plugins = Koha::Plugins->new()->GetPlugins({
74 method => 'intranet_catalog_biblio_tab',
77 foreach my $tab_plugin (@tab_plugins) {
81 @biblio_tabs = $tab_plugin->intranet_catalog_biblio_tab();
82 foreach my $tab (@biblio_tabs) {
83 my $tab_id = 'tab-' . $tab->title;
84 $tab_id =~ s/[^0-9A-Za-z]+/-/g;
90 warn "Error calling 'intranet_catalog_biblio_tab' on the " . $tab_plugin->{class} . "plugin ($_)";
100 my $biblionumber = $query->param('biblionumber');
101 $biblionumber = HTML::Entities::encode($biblionumber);
102 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
103 my $biblio = Koha::Biblios->find( $biblionumber );
104 $template->param( 'biblio', $biblio );
106 if ( not defined $record ) {
107 # biblionumber invalid -> report and exit
108 $template->param( unknownbiblionumber => 1,
109 biblionumber => $biblionumber );
110 output_html_with_http_headers $query, $cookie, $template->output;
114 eval { $biblio->metadata->record };
115 $template->param( decoding_error => $@ );
117 if($query->cookie("holdfor")){
118 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
120 # FIXME Should pass the patron object
121 holdfor => $query->cookie("holdfor"),
122 holdfor_surname => $holdfor_patron->surname,
123 holdfor_firstname => $holdfor_patron->firstname,
124 holdfor_cardnumber => $holdfor_patron->cardnumber,
128 if($query->cookie("searchToOrder")){
129 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
131 searchtoorder_basketno => $basketno,
132 searchtoorder_vendorid => $vendorid
136 my $fw = GetFrameworkCode($biblionumber);
137 my $showallitems = $query->param('showallitems');
138 my $marcflavour = C4::Context->preference("marcflavour");
140 # XSLT processing of some stuff
141 my $xslfile = C4::Context->preference('XSLTDetailsDisplay') || "default";
142 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
143 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
147 XSLTDetailsDisplay => '1',
148 XSLTBloc => XSLTParse4Display(
149 $biblionumber, $record, "XSLTDetailsDisplay",
150 1, undef, $sysxml, $xslfile, $lang
155 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
157 # Catch the exception as Koha::Biblio::Metadata->record can explode if the MARCXML is invalid
158 # Do not propagate it as we already deal with it previously in this script
159 my $coins = eval { $biblio->get_coins };
160 $template->param( ocoins => $coins );
162 # some useful variables for enhanced content;
163 # in each case, we're grabbing the first value we find in
164 # the record and normalizing it
165 my $upc = GetNormalizedUPC($record,$marcflavour);
166 my $ean = GetNormalizedEAN($record,$marcflavour);
167 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
168 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
171 normalized_upc => $upc,
172 normalized_ean => $ean,
173 normalized_oclc => $oclc,
174 normalized_isbn => $isbn,
177 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
178 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
179 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
180 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
181 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
182 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
183 my $marchostsarray = GetMarcHosts($record,$marcflavour);
185 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search->unblessed } };
187 my $dbh = C4::Context->dbh;
189 my @all_items = GetItemsInfo( $biblionumber );
191 my $patron = Koha::Patrons->find( $borrowernumber );
192 for my $itm (@all_items) {
193 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
196 # flag indicating existence of at least one item linked via a host record
198 # adding items linked via host biblios
199 my @hostitems = GetHostItemsInfo($record);
202 push (@items,@hostitems);
205 my $dat = &GetBiblioData($biblionumber);
207 #coping with subscriptions
208 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
209 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
212 foreach my $subscription (@subscriptions) {
214 my $serials_to_display;
215 $cell{subscriptionid} = $subscription->{subscriptionid};
216 $cell{subscriptionnotes} = $subscription->{internalnotes};
217 $cell{missinglist} = $subscription->{missinglist};
218 $cell{librariannote} = $subscription->{librariannote};
219 $cell{branchcode} = $subscription->{branchcode};
220 $cell{hasalert} = $subscription->{hasalert};
221 $cell{callnumber} = $subscription->{callnumber};
222 $cell{location} = $subscription->{location};
223 $cell{closed} = $subscription->{closed};
224 #get the three latest serials.
225 $serials_to_display = $subscription->{staffdisplaycount};
226 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
227 $cell{staffdisplaycount} = $serials_to_display;
228 $cell{latestserials} =
229 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
234 # Get acquisition details
235 if ( C4::Context->preference('AcquisitionDetails') ) {
236 my $orders = Koha::Acquisition::Orders->search(
237 { biblionumber => $biblionumber },
240 order_by => 'basketno.booksellerid'
242 ); # GetHistory sorted by aqbooksellerid, but does it make sense?
249 if ( C4::Context->preference('suggestion') ) {
250 my $suggestions = Koha::Suggestions->search(
252 biblionumber => $biblionumber,
255 order_by => { -desc => 'suggesteddate' }
258 $template->param( suggestions => $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);
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 foreach my $item (@items) {
310 my $itembranchcode = $item->{$separatebranch};
312 # can place holds defaults to yes
313 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
315 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
318 $item->{datedue} = format_sqldatetime($item->{datedue});
320 #get shelf location and collection code description if they are authorised value.
321 # same thing for copy number
322 my $shelfcode = $item->{'location'};
323 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
324 my $ccode = $item->{'ccode'};
325 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
326 my $copynumber = $item->{'copynumber'};
327 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
328 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
329 $itemfields{$_} = 1 if ( $item->{$_} );
333 my $item_object = Koha::Items->find( $item->{itemnumber} );
334 my $holds = $item_object->current_holds;
335 if ( my $first_hold = $holds->next ) {
336 $item->{first_hold} = $first_hold;
339 if ( my $checkout = $item_object->checkout ) {
340 $item->{CheckedOutFor} = $checkout->patron;
343 # Check the transit status
344 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
345 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
346 $item->{transfertwhen} = $transfertwhen;
347 $item->{transfertfrom} = $transfertfrom;
348 $item->{transfertto} = $transfertto;
349 $item->{nocancel} = 1;
352 foreach my $f (qw( itemnotes )) {
354 $item->{$f} =~ s|\n|<br />|g;
359 #item has a host number if its biblio number does not match the current bib
361 if ($item->{biblionumber} ne $biblionumber){
362 $item->{hostbiblionumber} = $item->{biblionumber};
363 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
368 # count if item is used in analytical bibliorecords
369 # The 'countanalytics' flag is only used in the templates if analyze is set
370 my $countanalytics = C4::Context->preference('EasyAnalyticalRecords') ? GetAnalyticsCount($item->{itemnumber}) : 0;
371 if ($countanalytics > 0){
373 $item->{countanalytics} = $countanalytics;
377 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
379 if (defined $materials_map{ $item->{materials} }) {
380 $item->{materials} = $materials_map{ $item->{materials} };
384 if ( C4::Context->preference('UseCourseReserves') ) {
385 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
388 if ( C4::Context->preference('IndependentBranches') ) {
389 my $userenv = C4::Context->userenv();
390 if ( not C4::Context->IsSuperLibrarian()
391 and $userenv->{branch} ne $item->{homebranch} ) {
392 $item->{cannot_be_edited} = 1;
396 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
397 if ($itembranchcode and $itembranchcode eq $currentbranch) {
398 push @itemloop, $item;
400 push @otheritemloop, $item;
403 push @itemloop, $item;
407 # Display only one tab if one items list is empty
408 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
409 $template->param(SeparateHoldings => 0);
410 if (scalar(@itemloop) == 0) {
411 @itemloop = @otheritemloop;
415 $template->param( norequests => $norequests );
417 MARCNOTES => $marcnotesarray,
418 MARCSUBJCTS => $marcsubjctsarray,
419 MARCAUTHORS => $marcauthorsarray,
420 MARCSERIES => $marcseriesarray,
421 MARCURLS => $marcurlsarray,
422 MARCISBNS => $marcisbnsarray,
423 MARCHOSTS => $marchostsarray,
424 itemdata_ccode => $itemfields{ccode},
425 itemdata_enumchron => $itemfields{enumchron},
426 itemdata_uri => $itemfields{uri},
427 itemdata_copynumber => $itemfields{copynumber},
428 itemdata_stocknumber => $itemfields{stocknumber},
429 itemdata_publisheddate => $itemfields{publisheddate},
430 volinfo => $itemfields{enumchron},
431 itemdata_itemnotes => $itemfields{itemnotes},
432 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
433 z3950_search_params => C4::Search::z3950_search_args($dat),
434 hostrecords => $hostrecords,
435 analytics_flag => $analytics_flag,
436 C4::Search::enabled_staff_search_views,
437 materials => $materials_flag,
440 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
441 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
442 my $subfields = substr $fieldspec, 3;
443 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
444 my @alternateholdingsinfo = ();
445 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
447 for my $field (@holdingsfields) {
448 my %holding = ( holding => '' );
449 my $havesubfield = 0;
450 for my $subfield ($field->subfields()) {
451 if ((index $subfields, $$subfield[0]) >= 0) {
452 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
453 $holding{'holding'} .= $$subfield[1];
458 push(@alternateholdingsinfo, \%holding);
463 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
467 my @results = ( $dat, );
468 foreach ( keys %{$dat} ) {
469 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
472 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
473 # method query not found?!?!
474 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
476 itemloop => \@itemloop,
477 otheritemloop => \@otheritemloop,
478 biblionumber => $biblionumber,
479 ($analyze? 'analyze':'detailview') =>1,
480 subscriptions => \@subs,
481 subscriptionsnumber => $subscriptionsnumber,
482 subscriptiontitle => $dat->{title},
483 searchid => scalar $query->param('searchid'),
486 # $debug and $template->param(debug_display => 1);
490 if (C4::Context->preference("virtualshelves") ) {
491 my $shelves = Koha::Virtualshelves->search(
493 biblionumber => $biblionumber,
497 join => 'virtualshelfcontents',
500 $template->param( 'shelves' => $shelves );
504 if (C4::Context->preference("FRBRizeEditions")==1) {
507 XISBNS => scalar get_xisbns($isbn, $biblionumber)
510 if ($@) { warn "XISBN Failed $@"; }
513 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
514 my @images = ListImagesForBiblio($biblionumber);
515 $template->{VARS}->{localimages} = \@images;
519 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
520 $template->param( C4::HTML5Media->gethtml5media($record));
526 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
529 TagsShowOnDetail => $tag_quantity
531 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
532 'sort'=>'-weight', limit=>$tag_quantity}));
535 #we only need to pass the number of holds to the template
536 my $holds = $biblio->holds;
537 $template->param( holdcount => $holds->count );
539 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
540 if ($StaffDetailItemSelection) {
541 # Only enable item selection if user can execute at least one action
543 $flags->{superlibrarian}
545 ref $flags->{tools} eq 'HASH' && (
546 $flags->{tools}->{items_batchmod} # Modify selected items
547 || $flags->{tools}->{items_batchdel} # Delete selected items
550 || ( ref $flags->{tools} eq '' && $flags->{tools} )
554 StaffDetailItemSelection => $StaffDetailItemSelection );
558 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
559 my @deletedorders_using_biblio;
560 my @orders_using_biblio;
562 my @baskets_deletedorders;
564 foreach my $myorder (@allorders_using_biblio) {
565 my $basket = $myorder->{'basketno'};
566 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
567 push @deletedorders_using_biblio, $myorder;
568 unless (grep{ $_ eq $basket } @baskets_deletedorders){
569 push @baskets_deletedorders,$myorder->{'basketno'};
573 push @orders_using_biblio, $myorder;
574 unless (grep{ $_ eq $basket } @baskets_orders){
575 push @baskets_orders,$myorder->{'basketno'};
580 $template->param(biblio => $biblio);
582 my $count_orders_using_biblio = scalar @orders_using_biblio ;
583 $template->param (countorders => $count_orders_using_biblio);
585 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
586 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
588 $template->param (basketsorders => \@baskets_orders);
589 $template->param (basketsdeletedorders => \@baskets_deletedorders);
591 output_html_with_http_headers $query, $cookie, $template->output;