f0815b520686fdafc7b0212df3498d3e7329fcb1
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 require Exporter;
21 use CGI;
22 use C4::Auth;
23 use C4::Serials;    #uses getsubscriptionfrom biblionumber
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Serials;
27 use C4::XISBN qw(get_xisbns get_biblio_from_xisbn);
28 use C4::Amazon;
29
30 my $query = new CGI;
31 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
32     {
33         template_name   => "catalogue/detail.tmpl",
34         query           => $query,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { catalogue => 1 },
38     }
39 );
40
41 my $biblionumber = $query->param('biblionumber');
42
43 # change back when ive fixed request.pl
44 my @items = &GetItemsInfo( $biblionumber, 'intra' );
45 my $dat = &GetBiblioData($biblionumber);
46
47 if (!$dat) { 
48         print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
49 }
50
51 #coping with subscriptions
52 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
53 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
54
55 my @subs;
56 foreach my $subscription (@subscriptions) {
57     my %cell;
58     $cell{subscriptionid}    = $subscription->{subscriptionid};
59     $cell{subscriptionnotes} = $subscription->{notes};
60
61     #get the three latest serials.
62     $cell{latestserials} =
63       GetLatestSerials( $subscription->{subscriptionid}, 3 );
64     push @subs, \%cell;
65 }
66
67 $dat->{'count'} = @items;
68
69 my $norequests = 1;
70 foreach my $itm (@items) {
71     $norequests = 0
72       unless ( ( $itm->{'notforloan'} > 0 )
73         || ( $itm->{'itemnotforloan'} > 0 ) );
74     $itm->{ $itm->{'publictype'} } = 1;
75 }
76
77 $template->param( norequests => $norequests );
78
79 ## get notes and subjects from MARC record
80     my $dbh              = C4::Context->dbh;
81     my $marcflavour      = C4::Context->preference("marcflavour");
82     my $record           = GetMarcBiblio($biblionumber);
83     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
84     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
85     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
86
87     $template->param(
88         MARCNOTES   => $marcnotesarray,
89         MARCSUBJCTS => $marcsubjctsarray,
90         MARCAUTHORS => $marcauthorsarray
91     );
92
93 my @results = ( $dat, );
94 foreach ( keys %{$dat} ) {
95     $template->param( "$_" => $dat->{$_} . "" );
96 }
97
98 $template->param(
99     ITEM_RESULTS        => \@items,
100     biblionumber        => $biblionumber,
101     subscriptions       => \@subs,
102     subscriptionsnumber => $subscriptionsnumber,
103     subscriptiontitle   => $dat->{title},
104 );
105
106 # XISBN Stuff
107 my $xisbn=$dat->{'isbn'};
108 $xisbn =~ s/(p|-| |:)//g;
109 $template->param(amazonisbn => $xisbn);
110 if (C4::Context->preference("FRBRizeEditions")==1) {
111         eval {
112                 $template->param(
113                         xisbn => $xisbn,
114                         XISBNS => get_xisbns($xisbn)
115                 );
116         };
117         if ($@) { warn "XISBN Failed $@"; }
118 }
119 if ( C4::Context->preference("AmazonContent") == 1 ) {
120     my $amazon_details = &get_amazon_details( $xisbn );
121     foreach my $result ( @{ $amazon_details->{Details} } ) {
122         $template->param( item_description => $result->{ProductDescription} );
123         $template->param( image            => $result->{ImageUrlMedium} );
124         $template->param( list_price       => $result->{ListPrice} );
125         $template->param( amazon_url       => $result->{url} );
126     }
127
128     my @products;
129     my @reviews;
130     for my $details ( @{ $amazon_details->{Details} } ) {
131         next unless $details->{SimilarProducts};
132         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
133                         if (C4::Context->preference("AmazonSimilarItems") ) {
134                                 my $xbiblios;
135                                 my @xisbns;
136
137                                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
138                                         my $xbiblio = get_biblio_from_xisbn($product);
139                                         push @xisbns, $xbiblio;
140                                         $xbiblios = \@xisbns;
141                                 }
142                                 else {
143                                         $xbiblios = get_xisbns($product);
144                                 }
145                 push @products, +{ product => $xbiblios };
146                         }
147         }
148         next unless $details->{Reviews};
149         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
150             $template->param( rating => $product * 20 );
151         }
152         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
153             push @reviews,
154               +{
155                 summary => $reviews->{Summary},
156                 comment => $reviews->{Comment},
157               };
158         }
159     }
160     $template->param( SIMILAR_PRODUCTS => \@products );
161     $template->param( AMAZONREVIEWS    => \@reviews );
162 }
163
164 output_html_with_http_headers $query, $cookie, $template->output;