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