opac-detail.pl - cleanup incl. exit after redirect
[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 require Exporter;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Serials;    #uses getsubscriptionfrom biblionumber
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Dates qw/format_date/;
30 use C4::XISBN qw(get_xisbns get_biblio_from_xisbn);
31 use C4::Amazon;
32 use C4::Review;
33 use C4::Serials;
34 use C4::Members;
35
36 my $query = new CGI;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-detail.tmpl",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => 1,
43         flagsrequired   => { borrow => 1 },
44     }
45 );
46
47 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
48 $template->param( biblionumber => $biblionumber );
49
50 # change back when ive fixed request.pl
51 my @items = &GetItemsInfo( $biblionumber, 'opac' );
52 my $dat = &GetBiblioData($biblionumber);
53
54 if (!$dat) {
55     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
56         exit;
57 }
58
59 #coping with subscriptions
60 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
61 my @subscriptions       =
62   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
63 my @subs;
64 foreach my $subscription (@subscriptions) {
65     my %cell;
66     $cell{subscriptionid}    = $subscription->{subscriptionid};
67     $cell{subscriptionnotes} = $subscription->{notes};
68
69     #get the three latest serials.
70     $cell{latestserials} =
71       GetLatestSerials( $subscription->{subscriptionid}, 3 );
72     push @subs, \%cell;
73 }
74
75 $dat->{'count'} = @items;
76
77 #adding RequestOnOpac filter to allow or not the display of plce reserve button
78 # FIXME - use me or delete me.
79 my $RequestOnOpac;
80 if (C4::Context->preference("RequestOnOpac")) {
81         $RequestOnOpac = 1;
82 }
83
84 my $norequests = 1;
85 foreach my $itm (@items) {
86      $norequests = 0 && $norequests
87        if ( (not $itm->{'wthdrawn'} )
88          || (not $itm->{'itemlost'} )
89          || (not $itm->{'itemnotforloan'} )
90          || ($itm->{'itemnumber'} ) );
91         $itm->{ $itm->{'publictype'} } = 1;
92
93         #get collection code description, too
94         $itm->{'ccode'}  = GetAuthorisedValueDesc('','',   $itm->{'ccode'} ,'','','CCODE');
95 }
96
97 $template->param( norequests => $norequests, RequestOnOpac=>$RequestOnOpac );
98
99 ## get notes and subjects from MARC record
100     my $dbh              = C4::Context->dbh;
101     my $marcflavour      = C4::Context->preference("marcflavour");
102     my $record           = GetMarcBiblio($biblionumber);
103     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
104     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
105     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
106         my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
107         my $marcurlsarray       = GetMarcUrls($record,$marcflavour);
108
109     $template->param(
110         MARCNOTES   => $marcnotesarray,
111         MARCSUBJCTS => $marcsubjctsarray,
112         MARCAUTHORS => $marcauthorsarray,
113                 MARCSERIES  => $marcseriesarray,
114                 MARCURLS        => $marcurlsarray,
115     );
116
117 foreach ( keys %{$dat} ) {
118     $template->param( "$_" => $dat->{$_} . "" );
119 }
120
121 my $reviews = getreviews( $biblionumber, 1 );
122 foreach ( @$reviews ) {
123     my $borrower_number_review = $_->{borrowernumber};
124     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
125     # setting some borrower info into this hash
126     $_->{title}     = $borrowerData->{'title'};
127     $_->{surname}   = $borrowerData->{'surname'};
128     $_->{firstname} = $borrowerData->{'firstname'};
129     $_->{datereviewed} = format_date($_->{datereviewed});
130 }
131
132 $template->param(
133     ITEM_RESULTS        => \@items,
134     subscriptionsnumber => $subscriptionsnumber,
135     biblionumber        => $biblionumber,
136     subscriptions       => \@subs,
137     subscriptionsnumber => $subscriptionsnumber,
138     reviews             => $reviews
139 );
140
141 # XISBN Stuff
142 my $xisbn=$dat->{'isbn'};
143 $xisbn =~ s/(p|-| |:)//g;
144 $template->param(amazonisbn => $xisbn);
145 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
146     eval {
147         $template->param(
148             xisbn => $xisbn,
149             XISBNS => get_xisbns($xisbn)
150         );
151     };
152     if ($@) { warn "XISBN Failed $@"; }
153 }
154 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
155     my $amazon_details = &get_amazon_details( $xisbn );
156     foreach my $result ( @{ $amazon_details->{Details} } ) {
157         $template->param( item_description => $result->{ProductDescription} );
158         $template->param( image            => $result->{ImageUrlMedium} );
159         $template->param( list_price       => $result->{ListPrice} );
160         $template->param( amazon_url       => $result->{url} );
161     }
162
163     my @products;
164     my @reviews;
165     for my $details ( @{ $amazon_details->{Details} } ) {
166         next unless $details->{SimilarProducts};
167         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
168             if (C4::Context->preference("OPACAmazonSimilarItems") ) {
169                 my $xbiblios;
170                 my @xisbns;
171
172                 if (C4::Context->preference("OPACXISBNAmazonSimilarItems") ) {
173                     my $xbiblio = get_biblio_from_xisbn($product);
174                     push @xisbns, $xbiblio;
175                     $xbiblios = \@xisbns;
176                 }
177                 else {
178                     $xbiblios = get_xisbns($product);
179                 }
180                 push @products, +{ product => $xbiblios };
181             }
182         }
183         next unless $details->{Reviews};
184         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
185             $template->param( rating => $product * 20 );
186         }
187         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
188             push @reviews,
189               +{
190                 summary => $reviews->{Summary},
191                 comment => $reviews->{Comment},
192               };
193         }
194     }
195     $template->param( SIMILAR_PRODUCTS => \@products );
196     $template->param( AMAZONREVIEWS    => \@reviews );
197 }
198
199 output_html_with_http_headers $query, $cookie, $template->output;