Merge remote branch 'kc/new/bug_5035' into kcmaster
[koha.git] / opac / opac-detail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth;
26 use C4::Branch;
27 use C4::Koha;
28 use C4::Serials;    #uses getsubscriptionfrom biblionumber
29 use C4::Output;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Circulation;
33 use C4::Tags qw(get_tags);
34 use C4::Dates qw/format_date/;
35 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
36 use C4::External::Amazon;
37 use C4::External::Syndetics qw(get_syndetics_index get_syndetics_summary get_syndetics_toc get_syndetics_excerpt get_syndetics_reviews get_syndetics_anotes );
38 use C4::Review;
39 use C4::Members;
40 use C4::VirtualShelves;
41 use C4::XSLT;
42
43 BEGIN {
44         if (C4::Context->preference('BakerTaylorEnabled')) {
45                 require C4::External::BakerTaylor;
46                 import C4::External::BakerTaylor qw(&image_url &link_url);
47         }
48 }
49
50 my $query = new CGI;
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52     {
53         template_name   => "opac-detail.tmpl",
54         query           => $query,
55         type            => "opac",
56         authnotrequired => 1,
57         flagsrequired   => { borrow => 1 },
58     }
59 );
60
61 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
62
63 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
64 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
65
66 my $record       = GetMarcBiblio($biblionumber);
67 if ( ! $record ) {
68     print $query->redirect("/cgi-bin/koha/errors/404.pl");
69     exit;
70 }
71 $template->param( biblionumber => $biblionumber );
72 # XSLT processing of some stuff
73 if (C4::Context->preference("OPACXSLTDetailsDisplay") ) {
74     $template->param( 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, 'Detail', 'opac') );
75 }
76
77 $template->param('OPACShowCheckoutName' => C4::Context->preference("OPACShowCheckoutName") ); 
78 # change back when ive fixed request.pl
79 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
80 my @items;
81 @items = @all_items unless C4::Context->preference('hidelostitems');
82
83 if (C4::Context->preference('hidelostitems')) {
84     # Hide host items
85     for my $itm (@all_items) {
86         push @items, $itm unless $itm->{itemlost};
87     }
88 }
89 my $dat = &GetBiblioData($biblionumber);
90
91 my $itemtypes = GetItemTypes();
92 # imageurl:
93 my $itemtype = $dat->{'itemtype'};
94 if ( $itemtype ) {
95     $dat->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
96     $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
97 }
98 my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'}, 'opac');
99 my $collections =  GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'}, 'opac');
100
101 #coping with subscriptions
102 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
103 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
104
105 my @subs;
106 $dat->{'serial'}=1 if $subscriptionsnumber;
107 foreach my $subscription (@subscriptions) {
108     my $serials_to_display;
109     my %cell;
110     $cell{subscriptionid}    = $subscription->{subscriptionid};
111     $cell{subscriptionnotes} = $subscription->{notes};
112     $cell{missinglist}       = $subscription->{missinglist};
113     $cell{opacnote}          = $subscription->{opacnote};
114     $cell{histstartdate}     = format_date($subscription->{histstartdate});
115     $cell{histenddate}       = format_date($subscription->{histenddate});
116     $cell{branchcode}        = $subscription->{branchcode};
117     $cell{branchname}        = GetBranchName($subscription->{branchcode});
118     $cell{hasalert}          = $subscription->{hasalert};
119     #get the three latest serials.
120     $serials_to_display = $subscription->{opacdisplaycount};
121     $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
122         $cell{opacdisplaycount} = $serials_to_display;
123     $cell{latestserials} =
124       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
125     push @subs, \%cell;
126 }
127
128 $dat->{'count'} = scalar(@items);
129
130 # If there is a lot of items, and the user has not decided
131 # to view them all yet, we first warn him
132 # TODO: The limit of 50 could be a syspref
133 my $viewallitems = $query->param('viewallitems');
134 if ($dat->{'count'} >= 50 && !$viewallitems) {
135     $template->param('lotsofitems' => 1);
136 }
137
138 my $biblio_authorised_value_images = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $biblionumber, $record ) );
139
140 my $norequests = 1;
141 my $branches = GetBranches();
142 my %itemfields;
143 for my $itm (@items) {
144     $norequests = 0
145        if ( (not $itm->{'wthdrawn'} )
146          && (not $itm->{'itemlost'} )
147          && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'} )
148                  && (not $itemtypes->{$itm->{'itype'}}->{notforloan} )
149          && ($itm->{'itemnumber'} ) );
150
151     if ( defined $itm->{'publictype'} ) {
152         # I can't actually find any case in which this is defined. --amoore 2008-12-09
153         $itm->{ $itm->{'publictype'} } = 1;
154     }
155     $itm->{datedue}      = format_date($itm->{datedue});
156     $itm->{datelastseen} = format_date($itm->{datelastseen});
157
158     # get collection code description, too
159     if ( my $ccode = $itm->{'ccode'} ) {
160         $itm->{'ccode'} = $collections->{$ccode} if ( defined($collections) && exists( $collections->{$ccode} ) );
161     }
162     if ( defined $itm->{'location'} ) {
163         $itm->{'location_description'} = $shelflocations->{ $itm->{'location'} };
164     }
165     if (exists $itm->{itype} && defined($itm->{itype}) && exists $itemtypes->{ $itm->{itype} }) {
166         $itm->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
167         $itm->{'description'} = $itemtypes->{ $itm->{itype} }->{'description'};
168     }
169     foreach (qw(ccode enumchron copynumber itemnotes uri)) {
170         $itemfields{$_} = 1 if ($itm->{$_});
171     }
172
173      # walk through the item-level authorised values and populate some images
174      my $item_authorised_value_images = C4::Items::get_authorised_value_images( C4::Items::get_item_authorised_values( $itm->{'itemnumber'} ) );
175      # warn( Data::Dumper->Dump( [ $item_authorised_value_images ], [ 'item_authorised_value_images' ] ) );
176
177      if ( $itm->{'itemlost'} ) {
178          my $lostimageinfo = List::Util::first { $_->{'category'} eq 'LOST' } @$item_authorised_value_images;
179          $itm->{'lostimageurl'}   = $lostimageinfo->{ 'imageurl' };
180          $itm->{'lostimagelabel'} = $lostimageinfo->{ 'label' };
181      }
182
183      if( $itm->{'count_reserves'}){
184           if( $itm->{'count_reserves'} eq "Waiting"){ $itm->{'waiting'} = 1; }
185           if( $itm->{'count_reserves'} eq "Reserved"){ $itm->{'onhold'} = 1; }
186      }
187     
188      my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
189      if ( defined( $transfertwhen ) && $transfertwhen ne '' ) {
190         $itm->{transfertwhen} = format_date($transfertwhen);
191         $itm->{transfertfrom} = $branches->{$transfertfrom}{branchname};
192         $itm->{transfertto}   = $branches->{$transfertto}{branchname};
193      }
194 }
195
196 ## get notes and subjects from MARC record
197 my $dbh              = C4::Context->dbh;
198 my $marcflavour      = C4::Context->preference("marcflavour");
199 my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
200 my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
201 my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
202 my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
203 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
204 my $subtitle         = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber));
205
206     $template->param(
207                      MARCNOTES               => $marcnotesarray,
208                      MARCSUBJCTS             => $marcsubjctsarray,
209                      MARCAUTHORS             => $marcauthorsarray,
210                      MARCSERIES              => $marcseriesarray,
211                      MARCURLS                => $marcurlsarray,
212                      norequests              => $norequests,
213                      RequestOnOpac           => C4::Context->preference("RequestOnOpac"),
214                      itemdata_ccode          => $itemfields{ccode},
215                      itemdata_enumchron      => $itemfields{enumchron},
216                      itemdata_uri            => $itemfields{uri},
217                      itemdata_copynumber     => $itemfields{copynumber},
218                      itemdata_itemnotes          => $itemfields{itemnotes},
219                      authorised_value_images => $biblio_authorised_value_images,
220                      subtitle                => $subtitle,
221     );
222
223 foreach ( keys %{$dat} ) {
224     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
225 }
226
227 # some useful variables for enhanced content;
228 # in each case, we're grabbing the first value we find in
229 # the record and normalizing it
230 my $upc = GetNormalizedUPC($record,$marcflavour);
231 my $ean = GetNormalizedEAN($record,$marcflavour);
232 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
233 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
234 my $content_identifier_exists = 1 if ($isbn or $ean or $oclc or $upc);
235 $template->param(
236         normalized_upc => $upc,
237         normalized_ean => $ean,
238         normalized_oclc => $oclc,
239         normalized_isbn => $isbn,
240         content_identifier_exists =>  $content_identifier_exists,
241 );
242
243 # COinS format FIXME: for books Only
244 $template->param(
245     ocoins => GetCOinSBiblio($biblionumber),
246 );
247
248 my $reviews = getreviews( $biblionumber, 1 );
249 my $loggedincommenter;
250 foreach ( @$reviews ) {
251     my $borrowerData   = GetMember('borrowernumber' => $_->{borrowernumber});
252     # setting some borrower info into this hash
253     $_->{title}     = $borrowerData->{'title'};
254     $_->{surname}   = $borrowerData->{'surname'};
255     $_->{firstname} = $borrowerData->{'firstname'};
256     $_->{userid}    = $borrowerData->{'userid'};
257     $_->{cardnumber}    = $borrowerData->{'cardnumber'};
258     $_->{datereviewed} = format_date($_->{datereviewed});
259     if ($borrowerData->{'borrowernumber'} eq $borrowernumber) {
260                 $_->{your_comment} = 1;
261                 $loggedincommenter = 1;
262         }
263 }
264
265
266 if(C4::Context->preference("ISBD")) {
267         $template->param(ISBD => 1);
268 }
269
270 $template->param(
271     ITEM_RESULTS        => \@items,
272     subscriptionsnumber => $subscriptionsnumber,
273     biblionumber        => $biblionumber,
274     subscriptions       => \@subs,
275     subscriptionsnumber => $subscriptionsnumber,
276     reviews             => $reviews,
277     loggedincommenter   => $loggedincommenter
278 );
279
280 # Lists
281
282 if (C4::Context->preference("virtualshelves") ) {
283    $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
284 }
285
286
287 # XISBN Stuff
288 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
289     eval {
290         $template->param(
291             XISBNS => get_xisbns($isbn)
292         );
293     };
294     if ($@) { warn "XISBN Failed $@"; }
295 }
296
297 # Serial Collection
298 my @sc_fields = $record->field(955);
299 my @serialcollections = ();
300
301 foreach my $sc_field (@sc_fields) {
302     my %row_data;
303
304     $row_data{text}    = $sc_field->subfield('r');
305     $row_data{branch}  = $sc_field->subfield('9');
306
307     if ($row_data{text} && $row_data{branch}) { 
308         push (@serialcollections, \%row_data);
309     }
310 }
311
312 if (scalar(@serialcollections) > 0) {
313     $template->param(
314         serialcollection  => 1,
315         serialcollections => \@serialcollections);
316 }
317
318 # Amazon.com Stuff
319 if ( C4::Context->preference("OPACAmazonEnabled") ) {
320     $template->param( AmazonTld => get_amazon_tld() );
321     my $amazon_reviews  = C4::Context->preference("OPACAmazonReviews");
322     my $amazon_similars = C4::Context->preference("OPACAmazonSimilarItems");
323     my @services;
324     if ( $amazon_reviews ) {
325         push( @services, 'EditorialReview', 'Reviews' );
326     }
327     if ( $amazon_similars ) {
328         push( @services, 'Similarities' );
329     }
330     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
331     my $similar_products_exist;
332     if ( $amazon_reviews ) {
333         my $item = $amazon_details->{Items}->{Item}->[0];
334         my $customer_reviews = \@{ $item->{CustomerReviews}->{Review} };
335         for my $one_review ( @$customer_reviews ) {
336             $one_review->{Date} = format_date($one_review->{Date});
337         }
338         my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
339         my $average_rating = $item->{CustomerReviews}->{AverageRating} || 0;
340         $template->param( amazon_average_rating    => $average_rating * 20);
341         $template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews );
342         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
343     }
344     if ( $amazon_similars ) {
345         my $item = $amazon_details->{Items}->{Item}->[0];
346         my @similar_products;
347         for my $similar_product (@{ $item->{SimilarProducts}->{SimilarProduct} }) {
348             # do we have any of these isbns in our collection?
349             my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
350             # verify that there is at least one similar item
351             if (scalar(@$similar_biblionumbers)){
352                 $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
353                 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
354             }
355         }
356         $template->param( OPACAmazonSimilarItems => $similar_products_exist );
357         $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
358     }
359 }
360
361 my $syndetics_elements;
362
363 if ( C4::Context->preference("SyndeticsEnabled") ) {
364     $template->param("SyndeticsEnabled" => 1);
365     $template->param("SyndeticsClientCode" => C4::Context->preference("SyndeticsClientCode"));
366         eval {
367             $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
368             for my $element (values %$syndetics_elements) {
369                 $template->param("Syndetics$element"."Exists" => 1 );
370                 #warn "Exists: "."Syndetics$element"."Exists";
371         }
372     };
373     warn $@ if $@;
374 }
375
376 if ( C4::Context->preference("SyndeticsEnabled")
377         && C4::Context->preference("SyndeticsSummary")
378         && ( exists($syndetics_elements->{'SUMMARY'}) || exists($syndetics_elements->{'AVSUMMARY'}) ) ) {
379         eval {
380             my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc, $syndetics_elements);
381             $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
382         };
383         warn $@ if $@;
384
385 }
386
387 if ( C4::Context->preference("SyndeticsEnabled")
388         && C4::Context->preference("SyndeticsTOC")
389         && exists($syndetics_elements->{'TOC'}) ) {
390         eval {
391     my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
392     $template->param( SYNDETICS_TOC => $syndetics_toc );
393         };
394         warn $@ if $@;
395 }
396
397 if ( C4::Context->preference("SyndeticsEnabled")
398     && C4::Context->preference("SyndeticsExcerpt")
399     && exists($syndetics_elements->{'DBCHAPTER'}) ) {
400     eval {
401     my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
402     $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
403     };
404         warn $@ if $@;
405 }
406
407 if ( C4::Context->preference("SyndeticsEnabled")
408     && C4::Context->preference("SyndeticsReviews")) {
409     eval {
410     my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
411     $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
412     };
413         warn $@ if $@;
414 }
415
416 if ( C4::Context->preference("SyndeticsEnabled")
417     && C4::Context->preference("SyndeticsAuthorNotes")
418         && exists($syndetics_elements->{'ANOTES'}) ) {
419     eval {
420     my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
421     $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
422     };
423     warn $@ if $@;
424 }
425
426 # LibraryThingForLibraries ID Code and Tabbed View Option
427 if( C4::Context->preference('LibraryThingForLibrariesEnabled') ) 
428
429 $template->param(LibraryThingForLibrariesID =>
430 C4::Context->preference('LibraryThingForLibrariesID') ); 
431 $template->param(LibraryThingForLibrariesTabbedView =>
432 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
433
434
435
436 # Babelthèque
437 if ( C4::Context->preference("Babeltheque") ) {
438     $template->param( 
439         Babeltheque => 1,
440     );
441 }
442
443 # Shelf Browser Stuff
444 if (C4::Context->preference("OPACShelfBrowser")) {
445     # pick the first itemnumber unless one was selected by the user
446     my $starting_itemnumber = $query->param('shelfbrowse_itemnumber'); # || $items[0]->{itemnumber};
447     $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
448     # find the right cn_sort value for this item
449     my ($starting_cn_sort, $starting_homebranch, $starting_location);
450     my $sth_get_cn_sort = $dbh->prepare("SELECT cn_sort,homebranch,location from items where itemnumber=?");
451     $sth_get_cn_sort->execute($starting_itemnumber);
452     while (my $result = $sth_get_cn_sort->fetchrow_hashref()) {
453         $starting_cn_sort = $result->{'cn_sort'};
454         $starting_homebranch->{code} = $result->{'homebranch'};
455         $starting_homebranch->{description} = $branches->{$result->{'homebranch'}}{branchname};
456         $starting_location->{code} = $result->{'location'};
457         $starting_location->{description} = GetAuthorisedValueDesc('','',   $result->{'location'} ,'','','LOC', 'opac');
458     
459     }
460     
461     ## List of Previous Items
462     # order by cn_sort, which should include everything we need for ordering purposes (though not
463     # for limits, those need to be handled separately
464     my $sth_shelfbrowse_previous;
465     if (defined $starting_location->{code}) {
466       $sth_shelfbrowse_previous = $dbh->prepare("
467         SELECT *
468         FROM items
469         WHERE
470             ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) AND
471             homebranch = ? AND location = ?
472         ORDER BY cn_sort DESC, itemnumber LIMIT 3
473         ");
474       $sth_shelfbrowse_previous->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
475     } else {
476       $sth_shelfbrowse_previous = $dbh->prepare("
477         SELECT *
478         FROM items
479         WHERE
480             ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) AND
481             homebranch = ?
482         ORDER BY cn_sort DESC, itemnumber LIMIT 3
483         ");
484       $sth_shelfbrowse_previous->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code});
485     }
486     my @previous_items;
487     while (my $this_item = $sth_shelfbrowse_previous->fetchrow_hashref()) {
488         my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
489         $sth_get_biblio->execute($this_item->{biblionumber});
490         while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
491                         $this_item->{'title'} = $this_biblio->{'title'};
492                         my $this_record = GetMarcBiblio($this_biblio->{'biblionumber'});
493                         $this_item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
494                         $this_item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
495                         $this_item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
496         }
497         unshift @previous_items, $this_item;
498     }
499     
500     ## List of Next Items; this also intentionally catches the current item
501     my $sth_shelfbrowse_next;
502     if (defined $starting_location->{code}) {
503       $sth_shelfbrowse_next = $dbh->prepare("
504         SELECT *
505         FROM items
506         WHERE
507             ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) AND
508             homebranch = ? AND location = ?
509         ORDER BY cn_sort, itemnumber LIMIT 3
510         ");
511       $sth_shelfbrowse_next->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
512     } else {
513       $sth_shelfbrowse_next = $dbh->prepare("
514         SELECT *
515         FROM items
516         WHERE
517             ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) AND
518             homebranch = ?
519         ORDER BY cn_sort, itemnumber LIMIT 3
520         ");
521       $sth_shelfbrowse_next->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code});
522     }
523     my @next_items;
524     while (my $this_item = $sth_shelfbrowse_next->fetchrow_hashref()) {
525         my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
526         $sth_get_biblio->execute($this_item->{biblionumber});
527         while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
528             $this_item->{'title'} = $this_biblio->{'title'};
529                         my $this_record = GetMarcBiblio($this_biblio->{'biblionumber'});
530             $this_item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
531             $this_item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
532             $this_item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
533         }
534         push @next_items, $this_item;
535     }
536     
537     # alas, these won't auto-vivify, see http://www.perlmonks.org/?node_id=508481
538     my $shelfbrowser_next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
539     my $shelfbrowser_next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
540     
541     $template->param(
542         starting_homebranch => $starting_homebranch->{description},
543         starting_location => $starting_location->{description},
544         starting_itemnumber => $starting_itemnumber,
545         shelfbrowser_prev_itemnumber => (@previous_items ? $previous_items[0]->{itemnumber} : 0),
546         shelfbrowser_next_itemnumber => $shelfbrowser_next_itemnumber,
547         shelfbrowser_prev_biblionumber => (@previous_items ? $previous_items[0]->{biblionumber} : 0),
548         shelfbrowser_next_biblionumber => $shelfbrowser_next_biblionumber,
549         PREVIOUS_SHELF_BROWSE => \@previous_items,
550         NEXT_SHELF_BROWSE => \@next_items,
551     );
552 }
553
554 if (C4::Context->preference("BakerTaylorEnabled")) {
555         $template->param(
556                 BakerTaylorEnabled  => 1,
557                 BakerTaylorImageURL => &image_url(),
558                 BakerTaylorLinkURL  => &link_url(),
559                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
560         );
561         my ($bt_user, $bt_pass);
562         if ($isbn and
563                 $bt_user = C4::Context->preference('BakerTaylorUsername') and
564                 $bt_pass = C4::Context->preference('BakerTaylorPassword')    )
565         {
566                 $template->param(
567                 BakerTaylorContentURL   =>
568                 sprintf("http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
569                                 $bt_user,$bt_pass,$isbn)
570                 );
571         }
572 }
573
574 my $tag_quantity;
575 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
576         $template->param(
577                 TagsEnabled => 1,
578                 TagsShowOnDetail => $tag_quantity,
579                 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
580         );
581         $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
582                                                                 'sort'=>'-weight', limit=>$tag_quantity}));
583 }
584
585 if (C4::Context->preference("OPACURLOpenInNewWindow")) {
586     # These values are going to be read by Javascript, at least in the case
587     # of the google covers
588     $template->param(covernewwindow => 'true');
589 } else {
590     $template->param(covernewwindow => 'false');
591 }
592
593 #Search for title in links
594 my $marccontrolnumber   = GetMarcControlnumber   ($record, $marcflavour);
595
596 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
597     $dat->{author} ? $search_for_title =~ s/{AUTHOR}/$dat->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
598     $dat->{title} =~ s/\/+$//; # remove trailing slash
599     $dat->{title} =~ s/\s+$//; # remove trailing space
600     $dat->{title} ? $search_for_title =~ s/{TITLE}/$dat->{title}/g : $search_for_title =~ s/{TITLE}//g;
601     $isbn ? $search_for_title =~ s/{ISBN}/$isbn/g : $search_for_title =~ s/{ISBN}//g;
602     $marccontrolnumber ? $search_for_title =~ s/{CONTROLNUMBER}/$marccontrolnumber/g : $search_for_title =~ s/{CONTROLNUMBER}//g;
603  $template->param('OPACSearchForTitleIn' => $search_for_title);
604 }
605
606 # We try to select the best default tab to show, according to what
607 # the user wants, and what's available for display
608 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
609 my $defaulttab = 
610     $opac_serial_default eq 'subscriptions' && $subscriptionsnumber
611         ? 'subscriptions' :
612     $opac_serial_default eq 'serialcollection' && @serialcollections > 0
613         ? 'serialcollection' :
614     $opac_serial_default eq 'holdings' && $dat->{'count'} > 0
615         ? 'holdings' :
616     $subscriptionsnumber
617         ? 'subscriptions' :
618     @serialcollections > 0 
619         ? 'serialcollection' : 'subscription';
620 $template->param('defaulttab' => $defaulttab);
621
622 output_html_with_http_headers $query, $cookie, $template->output;