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