GetSubscription... renamed to CountSubscription...
[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 # $Id$
21
22 use strict;
23 require Exporter;
24 use CGI;
25 use C4::Auth;
26 use C4::Serials;    #uses getsubscriptionfrom biblionumber
27 use C4::Interface::CGI::Output;
28 use HTML::Template;
29 use C4::Biblio;
30 use C4::SearchMarc;
31 use C4::Amazon;
32 use C4::Review;
33
34 my $query = new CGI;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-detail.tmpl",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => 1,
41         flagsrequired   => { borrow => 1 },
42     }
43 );
44
45 my $biblionumber = $query->param('bib');
46 $template->param( biblionumber => $biblionumber );
47
48 # change back when ive fixed request.pl
49 my @items = &ItemInfo( undef, $biblionumber, 'opac' );
50 my $dat = &bibdata($biblionumber);
51 my ( $authorcount,        $addauthor )      = &getaddauthor($biblionumber);
52 my ( $webbiblioitemcount, @webbiblioitems ) = &getwebbiblioitems($biblionumber);
53 my ( $websitecount,       @websites )       = &getwebsites($biblionumber);
54 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
55
56 #coping with subscriptions
57 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
58 my @subscriptions       =
59   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
60 my @subs;
61 foreach my $subscription (@subscriptions) {
62     my %cell;
63     $cell{subscriptionid}    = $subscription->{subscriptionid};
64     $cell{subscriptionnotes} = $subscription->{notes};
65
66     #get the three latest serials.
67     $cell{latestserials} =
68       GetLatestSerials( $subscription->{subscriptionid}, 3 );
69     push @subs, \%cell;
70 }
71
72 $dat->{'count'} = @items;
73 my @author;
74 if ( $dat->{'author'} ) {
75     my %authorpush;
76     $authorpush{author} = $dat->{'author'};
77     push @author, \%authorpush;
78 }
79 $dat->{'additional'} = $addauthor->[0]->{'author'};
80 if ( $dat->{'additional'} ) {
81     my %authorpush;
82     $authorpush{author} = $addauthor->[0]->{'author'};
83     push @author, \%authorpush;
84 }
85 my @title;
86 foreach my $word ( split( " ", $dat->{'title'} ) ) {
87     unless ( length($word) == 4 ) {
88         $word =~ s/\%//g;
89     }
90     unless ( C4::Context->stopwords->{ uc($word) } or length($word) == 1 ) {
91         my %titlepush;
92         $titlepush{title} = $word;
93         push @title, \%titlepush;
94     }    #it's NOT a stopword => use it. Otherwise, ignore
95 }
96
97 for ( my $i = 1 ; $i < $authorcount ; $i++ ) {
98     $dat->{'additional'} .= " ; " . $addauthor->[$i]->{'author'};
99
100     my %authorpush;
101     $authorpush{author} = $addauthor->[$i]->{'author'};
102     push @author, \%authorpush;
103 }    # for
104
105 my $norequests = 1;
106 foreach my $itm (@items) {
107     $norequests = 0
108       unless ( ( $itm->{'wthdrawn'} )
109         || ( $itm->{'itemlost'} )
110         || ( $itm->{'notforloan'} )
111         || ( $itm->{'itemnotforloan'} )
112         || ( !$itm->{'itemnumber'} ) );
113     $itm->{ $itm->{'publictype'} } = 1;
114 }
115
116 $template->param( norequests => $norequests );
117
118 ## get notes and subjects from MARC record
119 my $marc    = C4::Context->preference("marc");
120 my @results = ( $dat, );
121 if ( C4::Boolean::true_p($marc) ) {
122     my $dbh = C4::Context->dbh;
123     my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber( $dbh, $biblionumber );
124     my $marcflavour = C4::Context->preference("marcflavour");
125     my $marcnotesarray = &getMARCnotes( $dbh, $bibid, $marcflavour );
126     $results[0]->{MARCNOTES} = $marcnotesarray;
127     my $marcsubjctsarray = &getMARCsubjects( $dbh, $bibid, $marcflavour );
128     $results[0]->{MARCSUBJCTS} = $marcsubjctsarray;
129
130     #   $template->param(MARCNOTES => $marcnotesarray);
131     #   $template->param(MARCSUBJCTS => $marcsubjctsarray);
132 }
133
134 # get the number of reviews
135 my $reviewcount = numberofreviews($biblionumber);
136 $dat->{'reviews'} = $reviewcount;
137
138 my @results      = ( $dat, );
139 my $resultsarray = \@results;
140 my $itemsarray   = \@items;
141 my $webarray     = \@webbiblioitems;
142 my $sitearray    = \@websites;
143 my $titlewords   = \@title;
144 my $authorwords  = \@author;
145
146 #coping with subscriptions
147 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
148 my @subscriptions       =
149   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
150 my @subs;
151 foreach my $subscription (@subscriptions) {
152     warn "subsid :" . $subscription->{subscriptionid};
153     my %cell;
154     $cell{subscriptionid}    = $subscription->{subscriptionid};
155     $cell{subscriptionnotes} = $subscription->{notes};
156
157     #get the three latest serials.
158     $cell{latestserials} =
159       GetLatestSerials( $subscription->{subscriptionid}, 3 );
160     push @subs, \%cell;
161 }
162
163 $template->param(
164     BIBLIO_RESULTS      => $resultsarray,
165     ITEM_RESULTS        => $itemsarray,
166     WEB_RESULTS         => $webarray,
167     SITE_RESULTS        => $sitearray,
168     subscriptionsnumber => $subscriptionsnumber,
169     LibraryName         => C4::Context->preference("LibraryName"),
170     suggestion          => C4::Context->preference("suggestion"),
171     virtualshelves      => C4::Context->preference("virtualshelves"),
172     titlewords          => $titlewords,
173     authorwords         => $authorwords,
174     reviewson           => C4::Context->preference("marc"),
175 );
176 ## Amazon.com stuff
177 #not used unless preference set
178 if ( C4::Context->preference("AmazonContent") == 1 ) {
179     use C4::Amazon;
180     $dat->{'amazonisbn'} = $dat->{'isbn'};
181     $dat->{'amazonisbn'} =~ s|-||g;
182
183     $template->param( amazonisbn => $dat->{amazonisbn} );
184
185     my $amazon_details = &get_amazon_details( $dat->{amazonisbn} );
186
187     foreach my $result ( @{ $amazon_details->{Details} } ) {
188         $template->param( item_description => $result->{ProductDescription} );
189         $template->param( image            => $result->{ImageUrlMedium} );
190         $template->param( list_price       => $result->{ListPrice} );
191         $template->param( amazon_url       => $result->{url} );
192     }
193
194     my @products;
195     my @reviews;
196     for my $details ( @{ $amazon_details->{Details} } ) {
197         next unless $details->{SimilarProducts};
198         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
199             push @products, +{ Product => $product };
200         }
201         next unless $details->{Reviews};
202         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
203             $template->param( rating => $product * 20 );
204         }
205         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
206             push @reviews,
207               +{
208                 Summary => $reviews->{Summary},
209                 Comment => $reviews->{Comment},
210               };
211         }
212     }
213     $template->param( SIMILAR_PRODUCTS => \@products );
214     $template->param( REVIEWS          => \@reviews );
215 }
216 output_html_with_http_headers $query, $cookie, $template->output;