item rework: moved various accessor functions
[koha_fer] / 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 foreach my $subscription (@subscriptions) {
66     my %cell;
67     $cell{subscriptionid}    = $subscription->{subscriptionid};
68     $cell{subscriptionnotes} = $subscription->{notes};
69
70     #get the three latest serials.
71     $cell{latestserials} =
72       GetLatestSerials( $subscription->{subscriptionid}, 3 );
73     push @subs, \%cell;
74 }
75
76 $dat->{'count'} = @items;
77
78 #adding RequestOnOpac filter to allow or not the display of plce reserve button
79 # FIXME - use me or delete me.
80 my $RequestOnOpac;
81 if (C4::Context->preference("RequestOnOpac")) {
82         $RequestOnOpac = 1;
83 }
84
85 my $norequests = 1;
86 foreach my $itm (@items) {
87      $norequests = 0 && $norequests
88        if ( (not $itm->{'wthdrawn'} )
89          || (not $itm->{'itemlost'} )
90          || (not $itm->{'itemnotforloan'} )
91          || ($itm->{'itemnumber'} ) );
92         $itm->{ $itm->{'publictype'} } = 1;
93
94         #get collection code description, too
95         $itm->{'ccode'}  = GetAuthorisedValueDesc('','',   $itm->{'ccode'} ,'','','CCODE');
96 }
97
98 $template->param( norequests => $norequests, RequestOnOpac=>$RequestOnOpac );
99
100 ## get notes and subjects from MARC record
101     my $dbh              = C4::Context->dbh;
102     my $marcflavour      = C4::Context->preference("marcflavour");
103     my $record           = GetMarcBiblio($biblionumber);
104     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
105     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
106     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
107         my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
108         my $marcurlsarray       = GetMarcUrls($record,$marcflavour);
109
110     $template->param(
111         MARCNOTES   => $marcnotesarray,
112         MARCSUBJCTS => $marcsubjctsarray,
113         MARCAUTHORS => $marcauthorsarray,
114                 MARCSERIES  => $marcseriesarray,
115                 MARCURLS        => $marcurlsarray,
116     );
117
118 foreach ( keys %{$dat} ) {
119     $template->param( "$_" => $dat->{$_} . "" );
120 }
121
122 my $reviews = getreviews( $biblionumber, 1 );
123 foreach ( @$reviews ) {
124     my $borrower_number_review = $_->{borrowernumber};
125     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
126     # setting some borrower info into this hash
127     $_->{title}     = $borrowerData->{'title'};
128     $_->{surname}   = $borrowerData->{'surname'};
129     $_->{firstname} = $borrowerData->{'firstname'};
130     $_->{datereviewed} = format_date($_->{datereviewed});
131 }
132
133 $template->param(
134     ITEM_RESULTS        => \@items,
135     subscriptionsnumber => $subscriptionsnumber,
136     biblionumber        => $biblionumber,
137     subscriptions       => \@subs,
138     subscriptionsnumber => $subscriptionsnumber,
139     reviews             => $reviews
140 );
141
142 # XISBN Stuff
143 my $xisbn=$dat->{'isbn'};
144 $xisbn =~ s/(p|-| |:)//g;
145 $template->param(amazonisbn => $xisbn);
146 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
147     eval {
148         $template->param(
149             xisbn => $xisbn,
150             XISBNS => get_xisbns($xisbn)
151         );
152     };
153     if ($@) { warn "XISBN Failed $@"; }
154 }
155 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
156     my $amazon_details = &get_amazon_details( $xisbn );
157     foreach my $result ( @{ $amazon_details->{Details} } ) {
158         $template->param( item_description => $result->{ProductDescription} );
159         $template->param( image            => $result->{ImageUrlMedium} );
160         $template->param( list_price       => $result->{ListPrice} );
161         $template->param( amazon_url       => $result->{url} );
162     }
163
164     my @products;
165     my @reviews;
166     for my $details ( @{ $amazon_details->{Details} } ) {
167         next unless $details->{SimilarProducts};
168         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
169             if (C4::Context->preference("OPACAmazonSimilarItems") ) {
170                 my $xbiblios;
171                 my @xisbns;
172
173                 if (C4::Context->preference("OPACXISBNAmazonSimilarItems") ) {
174                     my $xbiblio = get_biblio_from_xisbn($product);
175                     push @xisbns, $xbiblio;
176                     $xbiblios = \@xisbns;
177                 }
178                 else {
179                     $xbiblios = get_xisbns($product);
180                 }
181                 push @products, +{ product => $xbiblios };
182             }
183         }
184         next unless $details->{Reviews};
185         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
186             $template->param( rating => $product * 20 );
187         }
188         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
189             push @reviews,
190               +{
191                 summary => $reviews->{Summary},
192                 comment => $reviews->{Comment},
193               };
194         }
195     }
196     $template->param( SIMILAR_PRODUCTS => \@products );
197     $template->param( AMAZONREVIEWS    => \@reviews );
198 }
199
200 output_html_with_http_headers $query, $cookie, $template->output;