Merge remote branch 'kc/new/bug_4885' into kcmaster
[koha.git] / opac / opac-ISBDdetail.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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 =head1 NAME
22
23 opac-ISBDdetail.pl - script to show a biblio in ISBD format
24
25 =head1 DESCRIPTION
26
27 This script needs a biblionumber as parameter 
28
29 It shows the biblio
30
31 The template is in <templates_dir>/catalogue/ISBDdetail.tmpl.
32 this template must be divided into 11 "tabs".
33
34 The first 10 tabs present the biblio, the 11th one presents
35 the items attached to the biblio
36
37 =head1 FUNCTIONS
38
39 =cut
40
41 use strict;
42 use warnings;
43
44 use C4::Auth;
45 use C4::Context;
46 use C4::Output;
47 use CGI;
48 use MARC::Record;
49 use C4::Biblio;
50 use C4::Items;
51 use C4::Acquisition;
52 use C4::Review;
53 use C4::Serials;    # uses getsubscriptionfrom biblionumber
54 use C4::Koha;
55 use C4::Members;    # GetMember
56 use C4::External::Amazon;
57
58 my $query = CGI->new();
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => "opac-ISBDdetail.tmpl",
62         query           => $query,
63         type            => "opac",
64         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
65         debug           => 1,
66     }
67 );
68
69 my $biblionumber = $query->param('biblionumber');
70
71 $template->param( 'AllowOnShelfHolds' => C4::Context->preference('AllowOnShelfHolds') );
72 $template->param( 'ItemsIssued' => CountItemsIssued( $biblionumber ) );
73
74 my $marcflavour      = C4::Context->preference("marcflavour");
75 my $record = GetMarcBiblio($biblionumber);
76 if ( ! $record ) {
77     print $query->redirect("/cgi-bin/koha/errors/404.pl");
78     exit;
79 }
80 # some useful variables for enhanced content;
81 # in each case, we're grabbing the first value we find in
82 # the record and normalizing it
83 my $upc = GetNormalizedUPC($record,$marcflavour);
84 my $ean = GetNormalizedEAN($record,$marcflavour);
85 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
86 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
87 my $content_identifier_exists = 1 if ($isbn or $ean or $oclc or $upc);
88 $template->param(
89     normalized_upc => $upc,
90     normalized_ean => $ean,
91     normalized_oclc => $oclc,
92     normalized_isbn => $isbn,
93         content_identifier_exists => $content_identifier_exists,
94 );
95
96 #coping with subscriptions
97 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
98 my $dbh = C4::Context->dbh;
99 my $dat                 = TransformMarcToKoha( $dbh, $record );
100 my @subscriptions       =
101   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
102 my @subs;
103 foreach my $subscription (@subscriptions) {
104     my %cell;
105         my $serials_to_display;
106     $cell{subscriptionid}    = $subscription->{subscriptionid};
107     $cell{subscriptionnotes} = $subscription->{notes};
108     $cell{branchcode}        = $subscription->{branchcode};
109
110     #get the three latest serials.
111         $serials_to_display = $subscription->{opacdisplaycount};
112         $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
113         $cell{opacdisplaycount} = $serials_to_display;
114     $cell{latestserials} =
115       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
116     push @subs, \%cell;
117 }
118
119 $template->param(
120     subscriptions       => \@subs,
121     subscriptionsnumber => $subscriptionsnumber,
122 );
123
124 my $norequests = 1;
125 my $res = GetISBDView($biblionumber, "opac");
126 my @items = &GetItemsInfo($biblionumber, 'opac');
127
128 my $itemtypes = GetItemTypes();
129 for my $itm (@items) {
130     $norequests = 0
131        if ( (not $itm->{'wthdrawn'} )
132          && (not $itm->{'itemlost'} )
133          && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'} )
134                  && (not $itemtypes->{$itm->{'itype'}}->{notforloan} )
135          && ($itm->{'itemnumber'} ) );
136 }
137
138 my $reviews = getreviews( $biblionumber, 1 );
139 foreach ( @$reviews ) {
140     my $borrower_number_review = $_->{borrowernumber};
141     my $borrowerData           = GetMember('borrowernumber' =>$borrower_number_review);
142     # setting some borrower info into this hash
143     $_->{title}     = $borrowerData->{'title'};
144     $_->{surname}   = $borrowerData->{'surname'};
145     $_->{firstname} = $borrowerData->{'firstname'};
146 }
147
148
149 $template->param(
150     RequestOnOpac       => C4::Context->preference("RequestOnOpac"),
151     AllowOnShelfHolds   => C4::Context->preference('AllowOnShelfHolds'),
152     norequests   => $norequests,
153     ISBD         => $res,
154     biblionumber => $biblionumber,
155     reviews             => $reviews,
156 );
157
158 #Search for title in links
159 if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
160     $dat->{author} ? $search_for_title =~ s/{AUTHOR}/$dat->{author}/g : $search_for_title =~ s/{AUTHOR}//g;
161     $dat->{title} =~ s/\/+$//; # remove trailing slash
162     $dat->{title} =~ s/\s+$//; # remove trailing space
163     $dat->{title} ? $search_for_title =~ s/{TITLE}/$dat->{title}/g : $search_for_title =~ s/{TITLE}//g;
164     $isbn ? $search_for_title =~ s/{ISBN}/$isbn/g : $search_for_title =~ s/{ISBN}//g;
165  $template->param('OPACSearchForTitleIn' => $search_for_title);
166 }
167
168 ## Amazon.com stuff
169 #not used unless preference set
170 if ( C4::Context->preference("OPACAmazonEnabled") == 1 ) {
171
172     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
173
174     foreach my $result ( @{ $amazon_details->{Details} } ) {
175         $template->param( item_description => $result->{ProductDescription} );
176         $template->param( image            => $result->{ImageUrlMedium} );
177         $template->param( list_price       => $result->{ListPrice} );
178         $template->param( amazon_url       => $result->{url} );
179     }
180
181     my @products;
182     my @reviews;
183     for my $details ( @{ $amazon_details->{Details} } ) {
184         next unless $details->{SimilarProducts};
185         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
186             push @products, +{ Product => $product };
187         }
188         next unless $details->{Reviews};
189         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
190             $template->param( rating => $product * 20 );
191         }
192         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
193             push @reviews,
194               +{
195                 Summary => $reviews->{Summary},
196                 Comment => $reviews->{Comment},
197               };
198         }
199     }
200     $template->param( SIMILAR_PRODUCTS => \@products );
201     $template->param( AMAZONREVIEWS    => \@reviews );
202 }
203
204 output_html_with_http_headers $query, $cookie, $template->output;