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