BUGFIXING mail alert on issue arrival
[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
21 use strict;
22 require Exporter;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Serials;    #uses getsubscriptionfrom biblionumber
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Dates qw/format_date/;
31 use C4::XISBN qw(get_xisbns get_biblio_from_xisbn);
32 use C4::Amazon;
33 use C4::Review;
34 use C4::Serials;
35 use C4::Members;
36
37 my $query = new CGI;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {
40         template_name   => "opac-detail.tmpl",
41         query           => $query,
42         type            => "opac",
43         authnotrequired => 1,
44         flagsrequired   => { borrow => 1 },
45     }
46 );
47
48 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
49 $template->param( biblionumber => $biblionumber );
50
51 # change back when ive fixed request.pl
52 my @items = &GetItemsInfo( $biblionumber, 'opac' );
53 my $dat = &GetBiblioData($biblionumber);
54
55 if (!$dat) {
56     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
57     exit;
58 }
59
60 #coping with subscriptions
61 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
62 my @subscriptions       =
63   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
64 my @subs;
65 $dat->{'serial'}=1 if $subscriptionsnumber;
66 foreach my $subscription (@subscriptions) {
67     my %cell;
68     $cell{subscriptionid}    = $subscription->{subscriptionid};
69     $cell{subscriptionnotes} = $subscription->{notes};
70     $cell{branchcode}        = $subscription->{branchcode};
71     $cell{hasalert}          = $subscription->{hasalert};
72     #get the three latest serials.
73     $cell{latestserials} =
74       GetLatestSerials( $subscription->{subscriptionid}, 3 );
75     push @subs, \%cell;
76 }
77
78 $dat->{'count'} = scalar(@items);
79
80 #adding RequestOnOpac filter to allow or not the display of plce reserve button
81 # FIXME - use me or delete me.
82 my $RequestOnOpac;
83 if (C4::Context->preference("RequestOnOpac")) {
84     $RequestOnOpac = 1;
85 }
86
87 my $norequests = 1;
88 foreach my $itm (@items) {
89      $norequests = 0 && $norequests
90        if ( (not $itm->{'wthdrawn'} )
91          || (not $itm->{'itemlost'} )
92          || (not $itm->{'itemnotforloan'} )
93          || ($itm->{'itemnumber'} ) );
94         $itm->{ $itm->{'publictype'} } = 1;
95
96         #get collection code description, too
97         $itm->{'ccode'}  = GetAuthorisedValueDesc('','',   $itm->{'ccode'} ,'','','CCODE');
98 }
99
100 $template->param( norequests => $norequests, RequestOnOpac=>$RequestOnOpac );
101
102 ## get notes and subjects from MARC record
103     my $dbh              = C4::Context->dbh;
104     my $marcflavour      = C4::Context->preference("marcflavour");
105     my $record           = GetMarcBiblio($biblionumber);
106     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
107     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
108     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
109     my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
110     my $marcurlsarray   = GetMarcUrls($record,$marcflavour);
111
112     $template->param(
113         MARCNOTES   => $marcnotesarray,
114         MARCSUBJCTS => $marcsubjctsarray,
115         MARCAUTHORS => $marcauthorsarray,
116         MARCSERIES  => $marcseriesarray,
117         MARCURLS    => $marcurlsarray,
118     );
119
120 foreach ( keys %{$dat} ) {
121     $template->param( "$_" => $dat->{$_} . "" );
122 }
123
124 # COinS format FIXME: for books Only
125 my $coins_format;
126 my $fmt = substr $record->leader(), 6,2;
127 my $fmts;
128 $fmts->{'am'} = 'book';
129 $coins_format = $fmts->{$fmt};
130 $template->param(
131         ocoins_format => $coins_format,
132 );
133
134 my $reviews = getreviews( $biblionumber, 1 );
135 foreach ( @$reviews ) {
136     my $borrower_number_review = $_->{borrowernumber};
137     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
138     # setting some borrower info into this hash
139     $_->{title}     = $borrowerData->{'title'};
140     $_->{surname}   = $borrowerData->{'surname'};
141     $_->{firstname} = $borrowerData->{'firstname'};
142     $_->{datereviewed} = format_date($_->{datereviewed});
143 }
144
145 $template->param(
146     ITEM_RESULTS        => \@items,
147     subscriptionsnumber => $subscriptionsnumber,
148     biblionumber        => $biblionumber,
149     subscriptions       => \@subs,
150     subscriptionsnumber => $subscriptionsnumber,
151     reviews             => $reviews
152 );
153
154 # XISBN Stuff
155 my $xisbn=$dat->{'isbn'};
156 $xisbn =~ s/(p|-| |:)//g;
157 $template->param(amazonisbn => $xisbn);
158 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
159     eval {
160         $template->param(
161             xisbn => $xisbn,
162             XISBNS => get_xisbns($xisbn)
163         );
164     };
165     if ($@) { warn "XISBN Failed $@"; }
166 }
167 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
168     my $amazon_details = &get_amazon_details( $xisbn );
169     foreach my $result ( @{ $amazon_details->{Details} } ) {
170         $template->param( item_description => $result->{ProductDescription} );
171         $template->param( image            => $result->{ImageUrlMedium} );
172         $template->param( list_price       => $result->{ListPrice} );
173         $template->param( amazon_url       => $result->{url} );
174     }
175
176     my @products;
177     my @reviews;
178     for my $details ( @{ $amazon_details->{Details} } ) {
179         next unless $details->{SimilarProducts};
180         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
181             if (C4::Context->preference("OPACAmazonSimilarItems") ) {
182                 my $xbiblios;
183                 my @xisbns;
184
185                 if (C4::Context->preference("OPACXISBNAmazonSimilarItems") ) {
186                     my $xbiblio = get_biblio_from_xisbn($product);
187                     push @xisbns, $xbiblio;
188                     $xbiblios = \@xisbns;
189                 }
190                 else {
191                     $xbiblios = get_xisbns($product);
192                 }
193                 push @products, +{ product => $xbiblios };
194             }
195         }
196         next unless $details->{Reviews};
197         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
198             $template->param( rating => $product * 20 );
199         }
200         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
201             push @reviews,
202               +{
203                 summary => $reviews->{Summary},
204                 comment => $reviews->{Comment},
205               };
206         }
207     }
208     $template->param( SIMILAR_PRODUCTS => \@products );
209     $template->param( AMAZONREVIEWS    => \@reviews );
210 }
211
212 output_html_with_http_headers $query, $cookie, $template->output;