Bug Fixing.
[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 # $Id$
21
22 use strict;
23 require Exporter;
24 use CGI;
25 use C4::Auth;
26 use C4::Serials;    #uses getsubscriptionfrom biblionumber
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Amazon;
30 use C4::Review;
31 use C4::Serials;
32 use C4::Members;
33
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-detail.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => 1,
42         flagsrequired   => { borrow => 1 },
43     }
44 );
45
46 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
47 $template->param( biblionumber => $biblionumber );
48
49 # change back when ive fixed request.pl
50 my @items = &GetItemsInfo( $biblionumber, 'opac' );
51 my $dat = &GetBiblioData($biblionumber);
52 #coping with subscriptions
53 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
54 my @subscriptions       =
55   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
56 my @subs;
57 foreach my $subscription (@subscriptions) {
58     my %cell;
59     $cell{subscriptionid}    = $subscription->{subscriptionid};
60     $cell{subscriptionnotes} = $subscription->{notes};
61
62     #get the three latest serials.
63     $cell{latestserials} =
64       GetLatestSerials( $subscription->{subscriptionid}, 3 );
65     push @subs, \%cell;
66 }
67
68 $dat->{'count'} = @items;
69
70 #adding RequestOnOpac filter to allow or not the display of plce reserve button
71 # FIXME - use me or delete me.
72 my $RequestOnOpac;
73 if (C4::Context->preference("RequestOnOpac")) {
74         $RequestOnOpac = 1;
75 }
76
77 my $norequests = 1;
78 foreach my $itm (@items) {
79     $norequests = 0
80       unless ( ( $itm->{'wthdrawn'} )
81         || ( $itm->{'itemlost'} )
82         || ( $itm->{'itemnotforloan'} )
83         || ( !$itm->{'itemnumber'} ) );
84     $itm->{ $itm->{'publictype'} } = 1;
85 }
86
87 $template->param( norequests => $norequests, );
88
89 ## get notes and subjects from MARC record
90     my $dbh              = C4::Context->dbh;
91     my $marcflavour      = C4::Context->preference("marcflavour");
92     my $record           = GetMarcBiblio($biblionumber);
93     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
94     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
95     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
96         my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
97
98     $template->param(
99         MARCNOTES   => $marcnotesarray,
100         MARCSUBJCTS => $marcsubjctsarray,
101         MARCAUTHORS => $marcauthorsarray,
102                 MARCSERIES  => $marcseriesarray
103     );
104
105 my @results = ( $dat, );
106 foreach ( keys %{$dat} ) {
107     $template->param( "$_" => $dat->{$_} . "" );
108 }
109
110 my $reviews = getreviews( $biblionumber, 1 );
111 foreach ( @$reviews ) {
112     my $borrower_number_review = $_->{borrowernumber};
113     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
114     # setting some borrower info into this hash
115     $_->{title}     = $borrowerData->{'title'};
116     $_->{surname}   = $borrowerData->{'surname'};
117     $_->{firstname} = $borrowerData->{'firstname'};
118 }
119
120 $template->param(
121     ITEM_RESULTS        => \@items,
122     subscriptionsnumber => $subscriptionsnumber,
123     biblionumber        => $biblionumber,
124     subscriptions       => \@subs,
125     subscriptionsnumber => $subscriptionsnumber,
126     reviews             => $reviews
127 );
128
129 ## Amazon.com stuff
130 #not used unless preference set
131 if ( C4::Context->preference("AmazonContent") == 1 ) {
132     use C4::Amazon;
133     $dat->{'amazonisbn'} = $dat->{'isbn'};
134     $dat->{'amazonisbn'} =~ s|-||g;
135
136     $template->param( amazonisbn => $dat->{amazonisbn} );
137
138     my $amazon_details = &get_amazon_details( $dat->{amazonisbn} );
139
140     foreach my $result ( @{ $amazon_details->{Details} } ) {
141         $template->param( item_description => $result->{ProductDescription} );
142         $template->param( image            => $result->{ImageUrlMedium} );
143         $template->param( list_price       => $result->{ListPrice} );
144         $template->param( amazon_url       => $result->{url} );
145     }
146
147     my @products;
148     my @reviews;
149     for my $details ( @{ $amazon_details->{Details} } ) {
150         next unless $details->{SimilarProducts};
151         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
152             push @products, +{ Product => $product };
153         }
154         next unless $details->{Reviews};
155         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
156             $template->param( rating => $product * 20 );
157         }
158         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
159             push @reviews,
160               +{
161                 Summary => $reviews->{Summary},
162                 Comment => $reviews->{Comment},
163               };
164         }
165     }
166     $template->param( SIMILAR_PRODUCTS => \@products );
167     $template->param( AMAZONREVIEWS    => \@reviews );
168 }
169 output_html_with_http_headers $query, $cookie, $template->output;