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