Now fetches the number of reviews, and passes it through to the template to be displayed
[koha.git] / opac / opac-detail.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5 use C4::Search;
6 use C4::Auth;
7 use C4::Bull; #uses getsubscriptionfrom biblionumber
8 use C4::Interface::CGI::Output;
9 use HTML::Template;
10 use C4::Biblio;
11 use C4::SearchMarc;
12 use C4::Amazon;
13 use C4::Review;
14
15 my $query=new CGI;
16 my ($template, $borrowernumber, $cookie) 
17     = get_template_and_user({template_name => "opac-detail.tmpl",
18                              query => $query,
19                              type => "opac",
20                              authnotrequired => 1,
21                              flagsrequired => {borrow => 1},
22                              });
23
24 my $biblionumber=$query->param('bib');
25 $template->param(biblionumber => $biblionumber);
26
27
28 # change back when ive fixed request.pl
29 my @items                                 = &ItemInfo(undef, $biblionumber, 'opac');
30 my $dat                                   = &bibdata($biblionumber);
31 my ($authorcount, $addauthor)             = &addauthor($biblionumber);
32 my ($webbiblioitemcount, @webbiblioitems) = &getwebbiblioitems($biblionumber);
33 my ($websitecount, @websites)             = &getwebsites($biblionumber);
34 my $subscriptionsnumber = getsubscriptionfrombiblionumber($biblionumber);
35
36 my @title;
37 $dat->{'count'}=@items;
38 my @author;
39 if ($dat->{'author'}){
40         my %authorpush; 
41         $authorpush{author}=$dat->{'author'};
42         push @author, \%authorpush
43 }
44 $dat->{'additional'}=$addauthor->[0]->{'author'};
45 if ($dat->{'additional'}){
46         my %authorpush;
47         $authorpush{author} =$addauthor->[0]->{'author'};
48         push @author, \%authorpush
49 }
50
51 foreach my $word (split(" ", $dat->{'title'})){
52         unless (length($word) == 4){
53                 $word =~s/\%//g
54         }
55         unless (C4::Context->stopwords->{uc($word)} or length($word)==1) {
56         my %titlepush;
57         $titlepush{title} =$word;
58                 push @title, \%titlepush;
59         }#it's NOT a stopword => use it. Otherwise, ignore
60 }
61
62 for (my $i = 1; $i < $authorcount; $i++) {
63         $dat->{'additional'} .= " ; " . $addauthor->[$i]->{'author'};
64     
65         my %authorpush;
66         $authorpush{author}=$addauthor->[$i]->{'author'};
67         push @author, \%authorpush
68 } # for
69
70 my $norequests = 1;
71 foreach my $itm (@items) {
72     $norequests = 0 unless $itm->{'notforloan'};
73     $itm->{$itm->{'publictype'}} = 1;
74 }
75
76 $template->param(norequests => $norequests);
77
78   ## get notes and subjects from MARC record
79 my $marc = C4::Context->preference("marc");
80 my @results = ($dat,);
81 if (C4::Boolean::true_p($marc)) {
82         my $dbh = C4::Context->dbh;
83         my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
84         my $marcflavour = C4::Context->preference("marcflavour");
85         my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
86         $results[0]->{MARCNOTES} = $marcnotesarray;
87         my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
88         $results[0]->{MARCSUBJCTS} = $marcsubjctsarray;
89 #       $template->param(MARCNOTES => $marcnotesarray);
90 #       $template->param(MARCSUBJCTS => $marcsubjctsarray);
91 }
92
93 # get the number of reviews
94 my $reviewcount=numberofreviews($biblionumber);
95 $dat->{'reviews'}=$reviewcount;
96
97 my @results = ($dat,);
98 my $resultsarray=\@results;
99 my $itemsarray=\@items;
100 my $webarray=\@webbiblioitems;
101 my $sitearray=\@websites;
102 my $titlewords=\@title;
103 my $authorwords=\@author;
104
105
106 #coping with subscriptions
107 my $subscriptionsnumber = getsubscriptionfrombiblionumber($biblionumber);
108 my @subscriptions = getsubscriptions($dat->{title},$dat->{issn},$biblionumber);
109 my @subs;
110 foreach my $subscription (@subscriptions){
111         warn "subsid :".$subscription->{subscriptionid};
112         my %cell;
113         $cell{subscriptionid}= $subscription->{subscriptionid};
114         $cell{subscriptionnotes}= $subscription->{notes};
115         #get the three latest serials.
116         $cell{latestserials}=getlatestserials($subscription->{subscriptionid},3);
117         push @subs, \%cell;
118 }
119
120 $template->param(BIBLIO_RESULTS => $resultsarray,
121                                 ITEM_RESULTS => $itemsarray,
122                                 WEB_RESULTS => $webarray,
123                                 SITE_RESULTS => $sitearray,
124                                 subscriptionsnumber => $subscriptionsnumber,
125                              LibraryName => C4::Context->preference("LibraryName"),
126                                 suggestion => C4::Context->preference("suggestion"),
127                                 virtualshelves => C4::Context->preference("virtualshelves"),
128         titlewords => $titlewords,
129         authorwords => $authorwords,
130 );
131   ## Amazon.com stuff
132 =head
133 my $isbn=$dat->{'isbn'};
134 my $amazon_details = &get_amazon_details($isbn);
135 foreach my $result (@{$amazon_details->{Details}}){
136         $template->param(item_description => $result->{ProductDescription});
137         $template->param(image => $result->{ImageUrlMedium});
138         $template->param(list_price => $result->{ListPrice});
139         $template->param(amazon_url => $result->{url});
140                                 }
141
142 my @products;
143 my @reviews;
144 for my $details( @{ $amazon_details->{ Details } } ) {
145         next unless $details->{ SimilarProducts };
146         for my $product ( @{ $details->{ SimilarProducts }->{ Product } } ) {
147                 push @products, +{ Product => $product };
148         }
149         next unless $details->{ Reviews };
150         for my $product ( @{ $details->{ Reviews }->{ AvgCustomerRating } } ) {
151                 $template->param(rating => $product);
152         }
153         for my $reviews ( @{ $details->{ Reviews }->{ CustomerReview } } ) {
154                 push @reviews, +{ Summary => $reviews->{ Summary }, Comment => $reviews->{ Comment }, };
155         }
156 }
157 $template->param( SIMILAR_PRODUCTS => \@products );
158 $template->param( REVIEWS => \@reviews );
159   ## End of Amazon Stuff
160 =cut
161 output_html_with_http_headers $query, $cookie, $template->output;
162