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