Syndetics and Amazon bugfix enhancements
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 use warnings;
21
22 use CGI;
23 use C4::Auth;
24 use C4::Dates qw/format_date/;
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::Circulation;
31 use C4::Branch;
32 use C4::Reserves;
33 use C4::Members;
34 use C4::Serials;
35 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
36 use C4::External::Amazon;
37
38 # use Smart::Comments;
39
40 my $query = CGI->new();
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "catalogue/detail.tmpl",
44         query           => $query,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { catalogue => 1 },
48     }
49 );
50
51 my $biblionumber = $query->param('biblionumber');
52 my $fw = GetFrameworkCode($biblionumber);
53
54 ## get notes and subjects from MARC record
55 my $marcflavour      = C4::Context->preference("marcflavour");
56 my $record           = GetMarcBiblio($biblionumber);
57
58 # some useful variables for enhanced content;
59 # in each case, we're grabbing the first value we find in
60 # the record and normalizing it
61 my $upc = GetNormalizedUPC($record,$marcflavour);
62 my $ean = GetNormalizedEAN($record,$marcflavour);
63 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
64 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
65
66 $template->param(
67     normalized_upc => $upc,
68     normalized_ean => $ean,
69     normalized_oclc => $oclc,
70     normalized_isbn => $isbn,
71 );
72
73 unless (defined($record)) {
74     print $query->redirect("/cgi-bin/koha/errors/404.pl");
75         exit;
76 }
77
78 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
79 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
80 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
81 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
82 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
83 my $subtitle         = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
84
85 # Get Branches, Itemtypes and Locations
86 my $branches = GetBranches();
87 my $itemtypes = GetItemTypes();
88 my $dbh = C4::Context->dbh;
89
90 # change back when ive fixed request.pl
91 my @items = &GetItemsInfo( $biblionumber, 'intra' );
92 my $dat = &GetBiblioData($biblionumber);
93
94 #coping with subscriptions
95 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
96 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
97 my @subs;
98 $dat->{'serial'}=1 if $subscriptionsnumber;
99 foreach my $subscription (@subscriptions) {
100     my %cell;
101     $cell{subscriptionid}    = $subscription->{subscriptionid};
102     $cell{subscriptionnotes} = $subscription->{notes};
103         $cell{branchcode}        = $subscription->{branchcode};
104         $cell{hasalert}          = $subscription->{hasalert};
105     #get the three latest serials.
106     $cell{latestserials} =
107       GetLatestSerials( $subscription->{subscriptionid}, 3 );
108     push @subs, \%cell;
109 }
110
111 if ( defined $dat->{'itemtype'} ) {
112     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
113 }
114 $dat->{'count'} = scalar @items;
115 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
116 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
117 my (@itemloop, %itemfields);
118 my $norequests = 1;
119 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
120 my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
121 foreach my $item (@items) {
122
123     # can place holds defaults to yes
124     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
125
126     # format some item fields for display
127     if ( defined $item->{'publictype'} ) {
128         $item->{ $item->{'publictype'} } = 1;
129     }
130     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
131                                                : '';
132
133         foreach (qw(datedue datelastseen onloan)) {
134                 $item->{$_} = format_date($item->{$_});
135         }
136     # item damaged, lost, withdrawn loops
137     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
138     if ($item->{damaged}) {
139         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
140     }
141     #get shelf location and collection code description if they are authorised value.
142     my $shelfcode = $item->{'location'};
143     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
144     my $ccode = $item->{'ccode'};
145     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
146     foreach (qw(ccode enumchron copynumber)) {
147         $itemfields{$_} = 1 if ( $item->{$_} );
148     }
149
150     # checking for holds
151     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
152     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
153
154     if ( defined $reservedate ) {
155         $item->{backgroundcolor} = 'reserved';
156         $item->{reservedate}     = format_date($reservedate);
157         $item->{ReservedForBorrowernumber}     = $reservedfor;
158         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
159         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
160         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
161     }
162
163         # Check the transit status
164     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
165     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
166         $item->{transfertwhen} = format_date($transfertwhen);
167         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
168         $item->{transfertto}   = $branches->{$transfertto}{branchname};
169         $item->{nocancel} = 1;
170     }
171
172     # FIXME: move this to a pm, check waiting status for holds
173     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
174     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
175     while (my $wait_hashref = $sth2->fetchrow_hashref) {
176         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
177     }
178
179     push @itemloop, $item;
180 }
181
182 $template->param( norequests => $norequests );
183 $template->param(
184         MARCNOTES   => $marcnotesarray,
185         MARCSUBJCTS => $marcsubjctsarray,
186         MARCAUTHORS => $marcauthorsarray,
187         MARCSERIES  => $marcseriesarray,
188         MARCURLS => $marcurlsarray,
189         subtitle    => $subtitle,
190         itemdata_ccode      => $itemfields{ccode},
191         itemdata_enumchron  => $itemfields{enumchron},
192         itemdata_copynumber => $itemfields{copynumber},
193         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
194 );
195
196 my @results = ( $dat, );
197 foreach ( keys %{$dat} ) {
198     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
199 }
200
201 $template->param(
202     itemloop        => \@itemloop,
203     biblionumber        => $biblionumber,
204     detailview => 1,
205     subscriptions       => \@subs,
206     subscriptionsnumber => $subscriptionsnumber,
207     subscriptiontitle   => $dat->{title},
208 );
209
210 # $debug and $template->param(debug_display => 1);
211
212 # XISBN Stuff
213 if (C4::Context->preference("FRBRizeEditions")==1) {
214     eval {
215         $template->param(
216             XISBNS => get_xisbns($isbn)
217         );
218     };
219     if ($@) { warn "XISBN Failed $@"; }
220 }
221 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
222     my $similar_products_exist;
223     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
224     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
225     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
226     my @similar_products;
227     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
228         # do we have any of these isbns in our collection?
229         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
230         # verify that there is at least one similar item
231                 if (scalar(@$similar_biblionumbers)){            
232                         $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
233             push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
234         }
235     }
236     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
237     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
238     $template->param( AmazonSimilarItems => $similar_products_exist );
239     $template->param( amazon_average_rating => $average_rating * 20);
240     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
241     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
242     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
243 }
244 output_html_with_http_headers $query, $cookie, $template->output;