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