Bug 29955: Fix method import issues
[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 GetHostItemsInfo GetItemsInfo );
37 use C4::Circulation qw( GetTransfers );
38 use C4::Reserves;
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 new_record_from_zebra );
43 use C4::Tags qw( get_tags );
44 use C4::XSLT qw( XSLTParse4Display );
45 use Koha::DateUtils qw( format_sqldatetime );
46 use C4::HTML5Media;
47 use C4::CourseReserves qw( GetItemCourseReservesInfo );
48 use Koha::AuthorisedValues;
49 use Koha::Biblios;
50 use Koha::Biblio::ItemGroup::Items;
51 use Koha::Biblio::ItemGroups;
52 use Koha::CoverImages;
53 use Koha::DateUtils;
54 use Koha::Illrequests;
55 use Koha::Items;
56 use Koha::ItemTypes;
57 use Koha::Patrons;
58 use Koha::Virtualshelves;
59 use Koha::Plugins;
60 use Koha::Recalls;
61 use Koha::SearchEngine::Search;
62 use Koha::SearchEngine::QueryBuilder;
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->unblessed } };
193
194 my $dbh = C4::Context->dbh;
195
196 my @all_items = GetItemsInfo( $biblionumber );
197 my @items;
198 my $patron = Koha::Patrons->find( $borrowernumber );
199 for my $itm (@all_items) {
200     push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
201 }
202
203 # flag indicating existence of at least one item linked via a host record
204 my $hostrecords;
205 # adding items linked via host biblios
206 my @hostitems = GetHostItemsInfo($marc_record);
207 if (@hostitems){
208     $hostrecords =1;
209     push (@items,@hostitems);
210 }
211
212 my $dat = &GetBiblioData($biblionumber);
213
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;
218
219 #coping with subscriptions
220 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
221 my @subscriptions       = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
222 my @subs;
223
224 foreach my $subscription (@subscriptions) {
225     my %cell;
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 );
242     push @subs, \%cell;
243 }
244
245 # Get component parts details
246 my $showcomp = C4::Context->preference('ShowComponentRecords');
247 my $show_analytics;
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};
252         my $parts;
253         for my $part ( @{$components} ) {
254             $part = C4::Search::new_record_from_zebra( 'biblioserver', $part );
255             my $id = Koha::SearchEngine::Search::extract_biblionumber( $part );
256
257             push @{$parts},
258               XSLTParse4Display(
259                 {
260                     biblionumber => $id,
261                     record       => $part,
262                     xsl_syspref  => "XSLTResultsDisplay",
263                     fix_amps     => 1,
264                 }
265               );
266         }
267         $template->param( ComponentParts => $parts );
268         my ( $comp_query, $comp_sort ) = $biblio->get_components_query;
269         my $cpq = $comp_query . "&sort_by=" . $comp_sort;
270         $template->param( ComponentPartsQuery => $cpq );
271     }
272 } else { # check if we should show analytics anyway
273     $show_analytics = 1 if !$invalid_marc_record && @{$biblio->get_marc_components(1)}; # count matters here, results does not
274     $template->param( analytics_error => 1 ) if grep { $_->message eq 'component_search' } @{$biblio->object_messages};
275 }
276
277 # XSLT processing of some stuff
278 my $xslt_variables = { show_analytics_link => $show_analytics };
279 $template->param(
280     XSLTDetailsDisplay => '1',
281     XSLTBloc => XSLTParse4Display({
282         biblionumber   => $biblionumber,
283         record         => $marc_record,
284         xsl_syspref    => "XSLTDetailsDisplay",
285         fix_amps       => 1,
286         xslt_variables => $xslt_variables,
287     }),
288 );
289
290 # Get acquisition details
291 if ( C4::Context->preference('AcquisitionDetails') ) {
292     my $orders = Koha::Acquisition::Orders->search(
293         { biblionumber => $biblionumber },
294         {
295             join => 'basketno',
296             order_by => 'basketno.booksellerid'
297         }
298     );    # GetHistory sorted by aqbooksellerid, but does it make sense?
299
300     $template->param(
301         orders => $orders,
302     );
303 }
304
305 if ( C4::Context->preference('suggestion') ) {
306     my $suggestions = Koha::Suggestions->search(
307         {
308             biblionumber => $biblionumber,
309             archived     => 0,
310         },
311         {
312             order_by => { -desc => 'suggesteddate' }
313         }
314     );
315     my $nb_archived_suggestions = Koha::Suggestions->search({ biblionumber => $biblionumber, archived => 1 })->count;
316     $template->param( suggestions => $suggestions, nb_archived_suggestions => $nb_archived_suggestions );
317 }
318
319 if ( defined $dat->{'itemtype'} ) {
320     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
321 }
322
323 $dat->{'count'} = scalar @all_items + @hostitems;
324 $dat->{'showncount'} = scalar @items + @hostitems;
325 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
326
327 my $shelflocations =
328   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
329 my $collections =
330   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
331 my $copynumbers =
332   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
333 my (@itemloop, @otheritemloop, %itemfields);
334
335 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
336 if ( $mss->count ) {
337     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
338 }
339 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
340 if ( $mss->count ) {
341     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
342 }
343 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
344 if ( $mss->count ) {
345     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
346 }
347
348 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
349 my %materials_map;
350 if ($mss->count) {
351     my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
352     if ($materials_authvals) {
353         foreach my $value (@$materials_authvals) {
354             $materials_map{$value->{authorised_value}} = $value->{lib};
355         }
356     }
357 }
358
359 my $analytics_flag;
360 my $materials_flag; # set this if the items have anything in the materials field
361 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
362 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
363     $template->param(SeparateHoldings => 1);
364 }
365 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
366 my ( $itemloop_has_images, $otheritemloop_has_images );
367 foreach my $item (@items) {
368     my $itembranchcode = $item->{$separatebranch};
369
370     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
371                                                : '';
372
373     $item->{datedue} = format_sqldatetime($item->{datedue});
374
375     #get shelf location and collection code description if they are authorised value.
376     # same thing for copy number
377     my $shelfcode = $item->{'location'};
378     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
379     my $ccode = $item->{'ccode'};
380     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
381     my $copynumber = $item->{'copynumber'};
382     $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
383     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
384         $itemfields{$_} = 1 if ( $item->{$_} );
385     }
386
387     # checking for holds
388     my $item_object = Koha::Items->find( $item->{itemnumber} );
389     $item->{object} = $item_object;
390     my $holds = $item_object->current_holds;
391     if ( my $first_hold = $holds->next ) {
392         $item->{first_hold} = $first_hold;
393     }
394
395     if ( my $checkout = $item_object->checkout ) {
396         $item->{CheckedOutFor} = $checkout->patron;
397     }
398
399     # Check the transit status
400     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
401     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
402         $item->{transfertwhen} = $transfertwhen;
403         $item->{transfertfrom} = $transfertfrom;
404         $item->{transfertto}   = $transfertto;
405         $item->{nocancel} = 1;
406     }
407
408     foreach my $f (qw( itemnotes )) {
409         if ($item->{$f}) {
410             $item->{$f} =~ s|\n|<br />|g;
411             $itemfields{$f} = 1;
412         }
413     }
414
415     #item has a host number if its biblio number does not match the current bib
416
417     if ($item->{biblionumber} ne $biblionumber){
418         $item->{hostbiblionumber} = $item->{biblionumber};
419         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
420     }
421         
422
423     if ( $analyze ) {
424         # count if item is used in analytical bibliorecords
425         # The 'countanalytics' flag is only used in the templates if analyze is set
426         my $countanalytics = GetAnalyticsCount( $item->{itemnumber} );
427         if ($countanalytics > 0){
428             $analytics_flag=1;
429             $item->{countanalytics} = $countanalytics;
430         }
431     }
432
433     if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
434         $materials_flag = 1;
435         if (defined $materials_map{ $item->{materials} }) {
436             $item->{materials} = $materials_map{ $item->{materials} };
437         }
438     }
439
440     if ( C4::Context->preference('UseCourseReserves') ) {
441         $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
442     }
443
444     if ( C4::Context->preference('IndependentBranches') ) {
445         my $userenv = C4::Context->userenv();
446         if ( not C4::Context->IsSuperLibrarian()
447             and $userenv->{branch} ne $item->{homebranch} ) {
448             $item->{cannot_be_edited} = 1;
449         }
450     }
451
452     if ( C4::Context->preference("LocalCoverImages") == 1 ) {
453         $item->{cover_images} = $item_object->cover_images;
454     }
455
456     if ( C4::Context->preference('UseRecalls') ) {
457         my $recall = Koha::Recalls->find({ item_id => $item->{itemnumber}, completed => 0 });
458         if ( defined $recall ) {
459             $item->{recalled} = 1;
460             $item->{recall} = $recall;
461         }
462     }
463
464     if ( $item_object->is_bundle ) {
465         $item->{bundled} =
466           $item_object->bundle_items->search( { itemlost => { '!=' => 0 } } )
467           ->count;
468         $item->{bundled_lost} =
469           $item_object->bundle_items->search( { itemlost => 0 } )->count;
470         $item->{is_bundle} = 1;
471     }
472
473     if ($item_object->in_bundle) {
474         $item->{bundle_host} = $item_object->bundle_host;
475     }
476
477     if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
478         if ($itembranchcode and $itembranchcode eq $currentbranch) {
479             push @itemloop, $item;
480             $itemloop_has_images++ if $item_object->cover_images->count;
481         } else {
482             push @otheritemloop, $item;
483             $otheritemloop_has_images++ if $item_object->cover_images->count;
484         }
485     } else {
486         push @itemloop, $item;
487         $itemloop_has_images++ if $item_object->cover_images->count;
488     }
489 }
490
491 $template->param(
492     itemloop_has_images      => $itemloop_has_images,
493     otheritemloop_has_images => $otheritemloop_has_images,
494 );
495
496 # Display only one tab if one items list is empty
497 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
498     $template->param(SeparateHoldings => 0);
499     if (scalar(@itemloop) == 0) {
500         @itemloop = @otheritemloop;
501     }
502 }
503
504 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
505     {
506         borrowernumber => $borrowernumber,
507         add_allowed    => 1,
508         public         => 0,
509     }
510 );
511 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
512     {
513         borrowernumber => $borrowernumber,
514         add_allowed    => 1,
515         public         => 1,
516     }
517 );
518
519
520 $template->param(
521     add_to_some_private_shelves => $some_private_shelves,
522     add_to_some_public_shelves  => $some_public_shelves,
523 );
524
525 $template->param(
526     MARCNOTES               => !$invalid_marc_record ? $biblio->get_marc_notes() : undef,
527     itemdata_ccode          => $itemfields{ccode},
528     itemdata_enumchron      => $itemfields{enumchron},
529     itemdata_uri            => $itemfields{uri},
530     itemdata_copynumber     => $itemfields{copynumber},
531     itemdata_stocknumber    => $itemfields{stocknumber},
532     itemdata_publisheddate  => $itemfields{publisheddate},
533     volinfo                 => $itemfields{enumchron},
534     itemdata_itemnotes      => $itemfields{itemnotes},
535     itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
536     z3950_search_params     => C4::Search::z3950_search_args($dat),
537     hostrecords             => $hostrecords,
538     analytics_flag          => $analytics_flag,
539     C4::Search::enabled_staff_search_views,
540     materials => $materials_flag,
541 );
542
543 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
544     my $fieldspec = C4::Context->preference("AlternateHoldingsField");
545     my $subfields = substr $fieldspec, 3;
546     my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
547     my @alternateholdingsinfo = ();
548     my @holdingsfields = $marc_record->field(substr $fieldspec, 0, 3);
549
550     for my $field (@holdingsfields) {
551         my %holding = ( holding => '' );
552         my $havesubfield = 0;
553         for my $subfield ($field->subfields()) {
554             if ((index $subfields, $$subfield[0]) >= 0) {
555                 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
556                 $holding{'holding'} .= $$subfield[1];
557                 $havesubfield++;
558             }
559         }
560         if ($havesubfield) {
561             push(@alternateholdingsinfo, \%holding);
562         }
563     }
564
565     $template->param(
566         ALTERNATEHOLDINGS   => \@alternateholdingsinfo,
567         );
568 }
569
570 my @results = ( $dat, );
571 foreach ( keys %{$dat} ) {
572     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
573 }
574
575 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
576 # method query not found?!?!
577 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
578 $template->param(
579     itemloop        => \@itemloop,
580     otheritemloop   => \@otheritemloop,
581     biblionumber        => $biblionumber,
582     ($analyze? 'analyze':'detailview') =>1,
583     subscriptions       => \@subs,
584     subscriptionsnumber => $subscriptionsnumber,
585     subscriptiontitle   => $dat->{title},
586     searchid            => scalar $query->param('searchid'),
587 );
588
589 # Lists
590
591 if (C4::Context->preference("virtualshelves") ) {
592     my $shelves = Koha::Virtualshelves->search(
593         {
594             biblionumber => $biblionumber,
595             public => 1,
596         },
597         {
598             join => 'virtualshelfcontents',
599         }
600     );
601     $template->param( 'shelves' => $shelves );
602 }
603
604 # XISBN Stuff
605 if (C4::Context->preference("FRBRizeEditions")==1) {
606     eval {
607         $template->param(
608             XISBNS => scalar get_xisbns($isbn, $biblionumber)
609         );
610     };
611     if ($@) { warn "XISBN Failed $@"; }
612 }
613
614 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
615     my $images = $biblio->cover_images;
616     $template->param(
617         localimages => $biblio->cover_images->search(
618             {}, { order_by => [ \"COALESCE(itemnumber, 0, 1)", 'timestamp' ] }
619         ),
620     );
621 }
622
623 # HTML5 Media
624 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
625     $template->param( C4::HTML5Media->gethtml5media($marc_record));
626 }
627
628 # Displaying tags
629 my $tag_quantity;
630 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
631     $template->param(
632         TagsEnabled => 1,
633         TagsShowOnDetail => $tag_quantity
634     );
635     $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
636                                 'sort'=>'-weight', limit=>$tag_quantity}));
637 }
638
639 #we only need to pass the number of holds to the template
640 my $holds = $biblio->holds;
641 $template->param( holdcount => $holds->count );
642
643 # Check if there are any ILL requests connected to the biblio
644 my $illrequests =
645     C4::Context->preference('ILLModule')
646   ? Koha::Illrequests->search( { biblio_id => $biblionumber } )
647   : [];
648 $template->param( illrequests => $illrequests );
649
650 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
651 if ($StaffDetailItemSelection) {
652     # Only enable item selection if user can execute at least one action
653     if (
654         $flags->{superlibrarian}
655         || (
656             ref $flags->{tools} eq 'HASH' && (
657                 $flags->{tools}->{items_batchmod}       # Modify selected items
658                 || $flags->{tools}->{items_batchdel}    # Delete selected items
659             )
660         )
661         || ( ref $flags->{tools} eq '' && $flags->{tools} )
662       )
663     {
664         $template->param(
665             StaffDetailItemSelection => $StaffDetailItemSelection );
666     }
667 }
668
669 # get biblionumbers stored in the cart
670 my @cart_list;
671
672 if($query->cookie("intranet_bib_list")){
673     my $cart_list = $query->cookie("intranet_bib_list");
674     @cart_list = split(/\//, $cart_list);
675     if ( grep {$_ eq $biblionumber} @cart_list) {
676         $template->param( incart => 1 );
677     }
678 }
679
680 if ( C4::Context->preference('UseCourseReserves') ) {
681     my $course_reserves = GetItemCourseReservesInfo( biblionumber => $biblionumber );
682     $template->param( course_reserves => $course_reserves );
683 }
684
685 $template->param(found1 => scalar $query->param('found1') );
686
687 $template->param(biblio => $biblio);
688
689 output_html_with_http_headers $query, $cookie, $template->output;