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