Bug 33497: Don't get host items if the MARC record is invalid
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18
19 use Modern::Perl;
20
21 use CGI qw ( -utf8 );
22 use HTML::Entities;
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Koha qw(
26     GetAuthorisedValues
27     getitemtypeimagelocation
28     GetNormalizedEAN
29     GetNormalizedISBN
30     GetNormalizedOCLCNumber
31     GetNormalizedUPC
32 );
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 );
37 use C4::Reserves;
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 );
45 use C4::HTML5Media;
46 use C4::CourseReserves qw( GetItemCourseReservesInfo );
47 use Koha::AuthorisedValues;
48 use Koha::Biblios;
49 use Koha::Biblio::ItemGroup::Items;
50 use Koha::Biblio::ItemGroups;
51 use Koha::CoverImages;
52 use Koha::DateUtils;
53 use Koha::Illrequests;
54 use Koha::Items;
55 use Koha::ItemTypes;
56 use Koha::Patrons;
57 use Koha::Virtualshelves;
58 use Koha::Plugins;
59 use Koha::Recalls;
60 use Koha::SearchEngine::Search;
61 use Koha::SearchEngine::QueryBuilder;
62 use Koha::Serial::Items;
63
64 my $query = CGI->new();
65
66 my $analyze = $query->param('analyze');
67
68 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
69     {
70     template_name   =>  'catalogue/detail.tt',
71         query           => $query,
72         type            => "intranet",
73         flagsrequired   => { catalogue => 1 },
74     }
75 );
76
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'
82     });
83     $template->param(
84         plugins => \@plugins,
85     );
86 }
87
88 my $biblionumber = $query->param('biblionumber');
89 $biblionumber = HTML::Entities::encode($biblionumber);
90 my $biblio = Koha::Biblios->find( $biblionumber );
91 $template->param( 'biblio', $biblio );
92
93 unless ( $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;
98     exit;
99 }
100
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 );
106
107     $marc_record = eval {
108         MARC::Record::new_from_xml( $marc_xml, 'UTF-8',
109             C4::Context->preference('marcflavour') );
110     };
111 }
112
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');
117
118     foreach my $item_id (@itemnumbers) {
119         my $item_group_item = Koha::Biblio::ItemGroup::Items->find( { item_id => $item_id } );
120
121         if ($item_group_item) {
122             $item_group_item->item_group_id($item_group_id);
123         }
124         else {
125             $item_group_item = Koha::Biblio::ItemGroup::Item->new(
126                 {
127                     item_id        => $item_id,
128                     item_group_id  => $item_group_id,
129                 }
130             );
131         }
132
133         $item_group_item->store();
134     }
135 }
136 elsif ( $op eq 'unset_item_group' ) {
137     my $item_group_id   = $query->param('item_group_id');
138     my @itemnumbers = $query->multi_param('itemnumber');
139
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;
143     }
144 }
145
146 if($query->cookie("holdfor")){
147     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
148     if ( $holdfor_patron ) {
149         $template->param(
150             holdfor        => $query->cookie("holdfor"),
151             holdfor_patron => $holdfor_patron,
152         );
153     }
154 }
155
156 if($query->cookie("searchToOrder")){
157     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
158     $template->param(
159         searchtoorder_basketno => $basketno,
160         searchtoorder_vendorid => $vendorid
161     );
162 }
163
164 my $fw           = GetFrameworkCode($biblionumber);
165 my $showallitems = $query->param('showallitems');
166 my $marcflavour  = C4::Context->preference("marcflavour");
167
168 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
169
170 $template->param( ocoins => !$invalid_marc_record ? $biblio->get_coins : undef );
171
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;
182 }
183
184 $template->param(
185     normalized_upc => $upc,
186     normalized_ean => $ean,
187     normalized_oclc => $oclc,
188     normalized_isbn => $isbn,
189     content_identifier_exists =>  $content_identifier_exists,
190 );
191
192 my $itemtypes = { map { $_->itemtype => $_ } @{ Koha::ItemTypes->search_with_localization->as_list } };
193 my $all_items = $biblio->items->search_ordered;
194 my @items;
195 my $patron = Koha::Patrons->find( $borrowernumber );
196 $params->{ itemlost } = 0 if $patron->category->hidelostitems && !$showallitems;
197 my $items_params = {
198     ( $invalid_marc_record ? () : ( host_items => 1 ) ),
199 };
200 my $items = $biblio->items($items_params)->search_ordered( $params, { prefetch => ['issue','current_branchtransfers'] } );
201
202 # flag indicating existence of at least one item linked via a host record
203 my $hostrecords = $biblio->host_items->count;
204
205 my $dat = &GetBiblioData($biblionumber);
206 $dat->{'count'} = $biblio->items($items_params)->count;
207 $dat->{'showncount'} = $items->count;
208 $dat->{'hiddencount'} = $dat->{'count'} - $dat->{'showncount'};
209
210 #is biblio a collection and are bundles enabled
211 my $leader = $marc_record->leader();
212 $dat->{bundlesEnabled} = ( ( substr( $leader, 7, 1 ) eq 'c' )
213       && C4::Context->preference('BundleNotLoanValue') ) ? 1 : 0;
214
215 #coping with subscriptions
216 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
217 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
218 my @subs;
219
220 foreach my $subscription (@subscriptions) {
221     my %cell;
222     my $serials_to_display;
223     $cell{subscriptionid}    = $subscription->{subscriptionid};
224     $cell{subscriptionnotes} = $subscription->{internalnotes};
225     $cell{missinglist}       = $subscription->{missinglist};
226     $cell{librariannote}     = $subscription->{librariannote};
227     $cell{branchcode}        = $subscription->{branchcode};
228     $cell{hasalert}          = $subscription->{hasalert};
229     $cell{callnumber}        = $subscription->{callnumber};
230     $cell{location}          = $subscription->{location};
231     $cell{closed}            = $subscription->{closed};
232     #get the three latest serials.
233     $serials_to_display = $subscription->{staffdisplaycount};
234     $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
235     $cell{staffdisplaycount} = $serials_to_display;
236     $cell{latestserials} =
237       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
238     push @subs, \%cell;
239 }
240
241 # Get component parts details
242 my $showcomp = C4::Context->preference('ShowComponentRecords');
243 my $show_analytics;
244 if ( $showcomp eq 'both' || $showcomp eq 'staff' ) {
245     if ( my $components = !$invalid_marc_record ? $biblio->get_marc_components(C4::Context->preference('MaxComponentRecords')) : undef ) {
246         $show_analytics = 1 if @{$components}; # just show link when having results
247         $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
248         my $parts;
249         for my $part ( @{$components} ) {
250             $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
251             my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
252
253             push @{$parts},
254               XSLTParse4Display(
255                 {
256                     biblionumber => $id,
257                     record       => $part,
258                     xsl_syspref  => "XSLTResultsDisplay",
259                     fix_amps     => 1,
260                 }
261               );
262         }
263         $template->param( ComponentParts => $parts );
264         my ( $comp_query, $comp_query_str, $comp_sort ) = $biblio->get_components_query;
265         my $cpq = $comp_query_str . "&sort_by=" . $comp_sort;
266         $template->param( ComponentPartsQuery => $cpq );
267     }
268 } else { # check if we should show analytics anyway
269     $show_analytics = 1 if !$invalid_marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
270     $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
271 }
272
273 # XSLT processing of some stuff
274 my $xslt_variables = { show_analytics_link => $show_analytics };
275 $template->param(
276     XSLTDetailsDisplay => '1',
277     XSLTBloc => XSLTParse4Display({
278         biblionumber   => $biblionumber,
279         record         => $marc_record,
280         xsl_syspref    => "XSLTDetailsDisplay",
281         fix_amps       => 1,
282         xslt_variables => $xslt_variables,
283     }),
284 );
285
286 # Get acquisition details
287 if ( C4::Context->preference('AcquisitionDetails') ) {
288     my $orders = Koha::Acquisition::Orders->search(
289         { biblionumber => $biblionumber },
290         {
291             join => 'basketno',
292             order_by => 'basketno.booksellerid'
293         }
294     );    # GetHistory sorted by aqbooksellerid, but does it make sense?
295
296     $template->param(
297         orders => $orders,
298     );
299 }
300
301 if ( C4::Context->preference('suggestion') ) {
302     my $suggestions = Koha::Suggestions->search(
303         {
304             biblionumber => $biblionumber,
305             archived     => 0,
306         },
307         {
308             order_by => { -desc => 'suggesteddate' }
309         }
310     );
311     my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
312     $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
313 }
314
315 if ( defined $dat->{'itemtype'} ) {
316     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }->imageurl );
317 }
318
319 $dat->{'count'} = $all_items->count + $hostitems->count;
320 $dat->{'showncount'} = scalar @items + $hostitems->count;
321 $dat->{'hiddencount'} = $all_items->count + $hostitems->count - scalar @items;
322
323 my $shelflocations =
324   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
325 my $collections =
326   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
327 my $copynumbers =
328   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
329 my (@itemloop, @otheritemloop, %itemfields);
330
331 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
332 if ( $mss->count ) {
333     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
334 }
335 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
336 if ( $mss->count ) {
337     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
338 }
339 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
340 if ( $mss->count ) {
341     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
342 }
343
344 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
345 my %materials_map;
346 if ($mss->count) {
347     my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
348     if ($materials_authvals) {
349         foreach my $value (@$materials_authvals) {
350             $materials_map{$value->{authorised_value}} = $value->{lib};
351         }
352     }
353 }
354
355 my $analytics_flag;
356 my $materials_flag; # set this if the items have anything in the materials field
357 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
358 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
359     $template->param(SeparateHoldings => 1);
360 }
361 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
362 my ( $itemloop_has_images, $otheritemloop_has_images );
363
364 while ( my $item = $items->next ) {
365     my $itembranchcode = $item->$separatebranch;
366
367     my $item_info = $item->unblessed;
368     $item_info->{itemtype} = $itemtypes->{$item->effective_itemtype};
369
370     #get shelf location and collection code description if they are authorised value.
371     # same thing for copy number
372     my $shelfcode = $item->location;
373     $item_info->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
374     my $ccode = $item->ccode;
375     $item_info->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
376     my $copynumber = $item->copynumber;
377     $item_info->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
378     foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri )) {
379         $itemfields{$_} = 1 if $item->$_;
380     }
381
382     # FIXME The following must be Koha::Item->serial
383     my $serial_item = Koha::Serial::Items->find($item->itemnumber);
384     if ( $serial_item ) {
385         my $serial = Koha::Serials->find($serial_item->serialid);
386         $item_info->{serial} = $serial if $serial;
387         $itemfields{publisheddate} = 1;
388     }
389
390     $item_info->{object} = $item;
391
392     # checking for holds
393     my $holds = $item->current_holds;
394     if ( my $first_hold = $holds->next ) {
395         $item_info->{first_hold} = $first_hold;
396     }
397
398     $item_info->{checkout} = $item->checkout;
399
400     # Check the transit status
401     my $transfer = $item->get_transfer;
402     if ( $transfer ) {
403         $item_info->{transfer} = $transfer;
404     }
405
406     foreach my $f (qw( itemnotes )) {
407         if ($item_info->{$f}) {
408             $item_info->{$f} =~ s|\n|<br />|g;
409             $itemfields{$f} = 1;
410         }
411     }
412
413     #item has a host number if its biblio number does not match the current bib
414
415     if ($item->biblionumber ne $biblionumber){
416         $item_info->{hostbiblionumber} = $item->biblionumber;
417         $item_info->{hosttitle} = $item->biblio->title;
418     }
419
420
421     if ( $analyze ) {
422         # count if item is used in analytical bibliorecords
423         # The 'countanalytics' flag is only used in the templates if analyze is set
424         my $countanalytics = GetAnalyticsCount( $item->itemnumber );
425         if ($countanalytics > 0){
426             $analytics_flag=1;
427             $item_info->{countanalytics} = $countanalytics;
428         }
429     }
430
431     if (defined($item->materials) && $item->materials =~ /\S/){
432         $materials_flag = 1;
433         if (defined $materials_map{ $item->materials }) {
434             $item_info->{materials} = $materials_map{ $item->materials };
435         }
436     }
437
438     if ( C4::Context->preference('UseCourseReserves') ) {
439         $item_info->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->itemnumber );
440     }
441
442     if ( C4::Context->preference("LocalCoverImages") == 1 ) {
443         $item_info->{cover_images} = $item->cover_images;
444     }
445
446     if ( C4::Context->preference('UseRecalls') ) {
447         $item_info->{recall} = $item->recall;
448     }
449
450     if ( C4::Context->preference('IndependentBranches') ) {
451         my $userenv = C4::Context->userenv();
452         if ( not C4::Context->IsSuperLibrarian()
453             and $userenv->{branch} ne $item->homebranch ) {
454             $item_info->{cannot_be_edited} = 1;
455             $item_info->{not_same_branch} = 1;
456         }
457     }
458
459     if ( $item->is_bundle ) {
460         $item_info->{bundled} =
461           $item->bundle_items->search( { itemlost => { '!=' => 0 } } )
462           ->count;
463         $item_info->{bundled_lost} =
464           $item->bundle_items->search( { itemlost => 0 } )->count;
465         $item_info->{is_bundle} = 1;
466     }
467
468     if ($item->in_bundle) {
469         $item_info->{bundle_host} = $item->bundle_host;
470     }
471
472     if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
473         if ($itembranchcode and $itembranchcode eq $currentbranch) {
474             push @itemloop, $item_info;
475             $itemloop_has_images++ if $item->cover_images->count;
476         } else {
477             push @otheritemloop, $item_info;
478             $otheritemloop_has_images++ if $item->cover_images->count;
479         }
480     } else {
481         push @itemloop, $item_info;
482         $itemloop_has_images++ if $item->cover_images->count;
483     }
484 }
485
486 $template->param(
487     itemloop_has_images      => $itemloop_has_images,
488     otheritemloop_has_images => $otheritemloop_has_images,
489 );
490
491 # Display only one tab if one items list is empty
492 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
493     $template->param(SeparateHoldings => 0);
494     if (scalar(@itemloop) == 0) {
495         @itemloop = @otheritemloop;
496     }
497 }
498
499 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
500     {
501         borrowernumber => $borrowernumber,
502         add_allowed    => 1,
503         public         => 0,
504     }
505 );
506 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
507     {
508         borrowernumber => $borrowernumber,
509         add_allowed    => 1,
510         public         => 1,
511     }
512 );
513
514
515 $template->param(
516     add_to_some_private_shelves => $some_private_shelves,
517     add_to_some_public_shelves  => $some_public_shelves,
518 );
519
520 $template->param(
521     MARCNOTES               => !$invalid_marc_record ? $biblio->get_marc_notes() : undef,
522     itemdata_ccode          => $itemfields{ccode},
523     itemdata_enumchron      => $itemfields{enumchron},
524     itemdata_uri            => $itemfields{uri},
525     itemdata_copynumber     => $itemfields{copynumber},
526     itemdata_stocknumber    => $itemfields{stocknumber},
527     itemdata_publisheddate  => $itemfields{publisheddate},
528     volinfo                 => $itemfields{enumchron},
529     itemdata_itemnotes      => $itemfields{itemnotes},
530     itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
531     z3950_search_params     => C4::Search::z3950_search_args($dat),
532     hostrecords             => $hostrecords,
533     analytics_flag          => $analytics_flag,
534     C4::Search::enabled_staff_search_views,
535     materials => $materials_flag,
536 );
537
538 if (C4::Context->preference("AlternateHoldingsField") && $items->count == 0) {
539     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
540     my $subfields = substr $fieldspec, 3;
541     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
542     my @alternateholdingsinfo = ();
543     my @holdingsfields = $marc_record->field(substr $fieldspec, 0, 3);
544
545     for my $field (@holdingsfields) {
546         my %holding = ( holding => '' );
547         my $havesubfield = 0;
548         for my $subfield ($field->subfields()) {
549             if ((index $subfields, $$subfield[0]) >= 0) {
550                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
551                 $holding{'holding'} .= $$subfield[1];
552                 $havesubfield++;
553             }
554         }
555         if ($havesubfield) {
556             push(@alternateholdingsinfo, \%holding);
557         }
558     }
559
560     $template->param(
561         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
562         );
563 }
564
565 my @results = ( $dat, );
566 foreach ( keys %{$dat} ) {
567     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
568 }
569
570 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
571 # method query not found?!?!
572 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
573 $template->param(
574     itemloop        => \@itemloop,
575     otheritemloop   => \@otheritemloop,
576     biblionumber        => $biblionumber,
577     ($analyze? 'analyze':'detailview') =>1,
578     subscriptions       => \@subs,
579     subscriptionsnumber => $subscriptionsnumber,
580     subscriptiontitle   => $dat->{title},
581     searchid            => scalar $query->param('searchid'),
582 );
583
584 # Lists
585
586 if (C4::Context->preference("virtualshelves") ) {
587     my $shelves = Koha::Virtualshelves->search(
588         {
589             biblionumber => $biblionumber,
590             public => 1,
591         },
592         {
593             join => 'virtualshelfcontents',
594         }
595     );
596     $template->param( 'shelves' => $shelves );
597 }
598
599 # XISBN Stuff
600 if (C4::Context->preference("FRBRizeEditions")==1) {
601     eval {
602         $template->param(
603             XISBNS => scalar get_xisbns($isbn, $biblionumber)
604         );
605     };
606     if ($@) { warn "XISBN Failed $@"; }
607 }
608
609 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
610     my $images = $biblio->cover_images;
611     $template->param(
612         localimages => $biblio->cover_images->search(
613             {}, { order_by => [ \"COALESCE(itemnumber, 0, 1)", 'timestamp' ] }
614         ),
615     );
616 }
617
618 # HTML5 Media
619 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
620     $template->param( C4::HTML5Media->gethtml5media($marc_record));
621 }
622
623 # Displaying tags
624 my $tag_quantity;
625 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
626     $template->param(
627         TagsEnabled => 1,
628         TagsShowOnDetail => $tag_quantity
629     );
630     $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
631                                 'sort'=>'-weight', limit=>$tag_quantity}));
632 }
633
634 #we only need to pass the number of holds to the template
635 my $holds = $biblio->holds;
636 $template->param( holdcount => $holds->count );
637
638 # Check if there are any ILL requests connected to the biblio
639 my $illrequests =
640     C4::Context->preference('ILLModule')
641   ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
642   : [];
643 $template->param( illrequests => $illrequests );
644
645 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
646 if ($StaffDetailItemSelection) {
647     # Only enable item selection if user can execute at least one action
648     if (
649         $flags->{superlibrarian}
650         || (
651             ref $flags->{tools} eq 'HASH' && (
652                 $flags->{tools}->{items_batchmod}       # Modify selected items
653                 || $flags->{tools}->{items_batchdel}    # Delete selected items
654             )
655         )
656         || ( ref $flags->{tools} eq '' && $flags->{tools} )
657       )
658     {
659         $template->param(
660             StaffDetailItemSelection => $StaffDetailItemSelection );
661     }
662 }
663
664 # get biblionumbers stored in the cart
665 my @cart_list;
666
667 if($query->cookie("intranet_bib_list")){
668     my $cart_list = $query->cookie("intranet_bib_list");
669     @cart_list = split(/\//, $cart_list);
670     if ( grep {$_ eq $biblionumber} @cart_list) {
671         $template->param( incart => 1 );
672     }
673 }
674
675 if ( C4::Context->preference('UseCourseReserves') ) {
676     my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
677     $template->param( course_reserves => $course_reserves );
678 }
679
680 $template->param(found1 => scalar $query->param('found1') );
681
682 $template->param(biblio => $biblio);
683
684 output_html_with_http_headers $query, $cookie, $template->output;