Bugfixing interface issues. Also enabling contextual menu highlighting for catalogue...
[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         detailview => 1,
102     subscriptions       => \@subs,
103     subscriptionsnumber => $subscriptionsnumber,
104     subscriptiontitle   => $dat->{title},
105 );
106
107 # XISBN Stuff
108 my $xisbn=$dat->{'isbn'};
109 $xisbn =~ s/(p|-| |:)//g;
110 $template->param(amazonisbn => $xisbn);
111 if (C4::Context->preference("FRBRizeEditions")==1) {
112         eval {
113                 $template->param(
114                         xisbn => $xisbn,
115                         XISBNS => get_xisbns($xisbn)
116                 );
117         };
118         if ($@) { warn "XISBN Failed $@"; }
119 }
120 if ( C4::Context->preference("AmazonContent") == 1 ) {
121     my $amazon_details = &get_amazon_details( $xisbn );
122     foreach my $result ( @{ $amazon_details->{Details} } ) {
123         $template->param( item_description => $result->{ProductDescription} );
124         $template->param( image            => $result->{ImageUrlMedium} );
125         $template->param( list_price       => $result->{ListPrice} );
126         $template->param( amazon_url       => $result->{url} );
127     }
128
129     my @products;
130     my @reviews;
131     for my $details ( @{ $amazon_details->{Details} } ) {
132         next unless $details->{SimilarProducts};
133         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
134                         if (C4::Context->preference("AmazonSimilarItems") ) {
135                                 my $xbiblios;
136                                 my @xisbns;
137
138                                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
139                                         my $xbiblio = get_biblio_from_xisbn($product);
140                                         push @xisbns, $xbiblio;
141                                         $xbiblios = \@xisbns;
142                                 }
143                                 else {
144                                         $xbiblios = get_xisbns($product);
145                                 }
146                 push @products, +{ product => $xbiblios };
147                         }
148         }
149         next unless $details->{Reviews};
150         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
151             $template->param( rating => $product * 20 );
152         }
153         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
154             push @reviews,
155               +{
156                 summary => $reviews->{Summary},
157                 comment => $reviews->{Comment},
158               };
159         }
160     }
161     $template->param( SIMILAR_PRODUCTS => \@products );
162     $template->param( AMAZONREVIEWS    => \@reviews );
163 }
164
165 output_html_with_http_headers $query, $cookie, $template->output;