a8a0cfa3d470ba44724034fc9b5ed5ee596c6d7d
[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 get_biblio_from_xisbn);
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 unless (defined($record)) {
59     print $query->redirect("/cgi-bin/koha/errors/404.pl");
60         exit;
61 }
62
63 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
64 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
65 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
66 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
67 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
68 my $subtitle         = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
69
70 # Get Branches, Itemtypes and Locations
71 my $branches = GetBranches();
72 my $itemtypes = GetItemTypes();
73 my $dbh = C4::Context->dbh;
74
75 # change back when ive fixed request.pl
76 my @items = &GetItemsInfo( $biblionumber, 'intra' );
77 my $dat = &GetBiblioData($biblionumber);
78
79 #coping with subscriptions
80 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
81 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
82 my @subs;
83 $dat->{'serial'}=1 if $subscriptionsnumber;
84 foreach my $subscription (@subscriptions) {
85     my %cell;
86     $cell{subscriptionid}    = $subscription->{subscriptionid};
87     $cell{subscriptionnotes} = $subscription->{notes};
88         $cell{branchcode}        = $subscription->{branchcode};
89         $cell{hasalert}          = $subscription->{hasalert};
90     #get the three latest serials.
91     $cell{latestserials} =
92       GetLatestSerials( $subscription->{subscriptionid}, 3 );
93     push @subs, \%cell;
94 }
95
96 if ( defined $dat->{'itemtype'} ) {
97     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
98 }
99 $dat->{'count'} = scalar @items;
100 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
101 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
102 my (@itemloop, %itemfields);
103 my $norequests = 1;
104 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
105 my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
106 foreach my $item (@items) {
107
108     # can place holds defaults to yes
109     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
110
111     # format some item fields for display
112     if ( defined $item->{'publictype'} ) {
113         $item->{ $item->{'publictype'} } = 1;
114     }
115     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
116                                                : '';
117
118     foreach (qw(datedue datelastseen onloan)) {
119         $item->{$_} = format_date($item->{$_});
120     }
121     # item damaged, lost, withdrawn loops
122     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
123     if ($item->{damaged}) {
124         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
125     }
126     #get shelf location and collection code description if they are authorised value.
127     my $shelfcode = $item->{'location'};
128     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
129     my $ccode = $item->{'ccode'};
130     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
131     foreach (qw(ccode enumchron copynumber uri)) {
132         $itemfields{$_} = 1 if ( $item->{$_} );
133     }
134
135     # checking for holds
136     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
137     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
138
139     if ( defined $reservedate ) {
140         $item->{backgroundcolor} = 'reserved';
141         $item->{reservedate}     = format_date($reservedate);
142         $item->{ReservedForBorrowernumber}     = $reservedfor;
143         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
144         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
145         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
146     }
147
148         # Check the transit status
149     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
150     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
151         $item->{transfertwhen} = format_date($transfertwhen);
152         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
153         $item->{transfertto}   = $branches->{$transfertto}{branchname};
154         $item->{nocancel} = 1;
155     }
156
157     # FIXME: move this to a pm, check waiting status for holds
158     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
159     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
160     while (my $wait_hashref = $sth2->fetchrow_hashref) {
161         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
162     }
163
164     push @itemloop, $item;
165 }
166
167 $template->param( norequests => $norequests );
168 $template->param(
169         MARCNOTES   => $marcnotesarray,
170         MARCSUBJCTS => $marcsubjctsarray,
171         MARCAUTHORS => $marcauthorsarray,
172         MARCSERIES  => $marcseriesarray,
173         MARCURLS => $marcurlsarray,
174         subtitle    => $subtitle,
175         itemdata_ccode      => $itemfields{ccode},
176         itemdata_enumchron  => $itemfields{enumchron},
177         itemdata_uri        => $itemfields{uri},
178         itemdata_copynumber => $itemfields{copynumber},
179         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
180 );
181
182 my @results = ( $dat, );
183 foreach ( keys %{$dat} ) {
184     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
185 }
186
187 $template->param(
188     itemloop        => \@itemloop,
189     biblionumber        => $biblionumber,
190     detailview => 1,
191     subscriptions       => \@subs,
192     subscriptionsnumber => $subscriptionsnumber,
193     subscriptiontitle   => $dat->{title},
194 );
195
196 # $debug and $template->param(debug_display => 1);
197
198 # XISBN Stuff
199 my $xisbn=$dat->{'isbn'};
200 $xisbn =~ /(\d*[X]*)/ if ( $xisbn );
201 $template->param(amazonisbn => $1);             # FIXME: so it is OK if the ISBN = 'XXXXX' ?
202 if (C4::Context->preference("FRBRizeEditions")==1) {
203     eval {
204         $template->param(
205             xisbn => $xisbn,
206             XISBNS => get_xisbns($xisbn)
207         );
208     };
209     if ($@) { warn "XISBN Failed $@"; }
210 }
211 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
212     $template->param( AmazonTld => get_amazon_tld() );
213     my $amazon_reviews  = C4::Context->preference("AmazonReviews");
214     my $amazon_similars = C4::Context->preference("AmazonSimilarItems");
215     my @services;
216     if ( $amazon_reviews ) {
217         $template->param( AmazonReviews => 1 );
218         push( @services, 'EditorialReview' );
219     }
220     if ( $amazon_similars ) {
221         $template->param( AmazonSimilarItems => 1 );
222         push( @services, 'Similarities' );
223     }
224     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
225     if ( $amazon_similars ) {
226         my $similar_products_exist;
227         my @similar_products;
228         for my $similar_product (@{$amazon_details->{Items}->{Item}->[0]->{SimilarProducts}->{SimilarProduct}}) {
229             # do we have any of these isbns in our collection?
230             my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
231             # verify that there is at least one similar item
232                     if (scalar(@$similar_biblionumbers)){            
233                             $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
234                 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
235             }
236         }
237         $template->param( AmazonSimilarItems       => $similar_products_exist );
238         $template->param( AMAZON_SIMILAR_PRODUCTS  => \@similar_products      );
239     }
240     if ( $amazon_reviews ) {
241         my $item = $amazon_details->{Items}->{Item}->[0];
242         my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
243         #my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{Review}};
244         #my $average_rating = $amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{AverageRating} || 0;
245         #$template->param( amazon_average_rating    => $average_rating * 20    );
246         #$template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews       );
247         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews      );
248     }
249     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
250     my $average_rating    =    $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0;
251     $template->param( AmazonSimilarItems       => $similar_products_exist );
252     $template->param( amazon_average_rating    => $average_rating * 20    );
253     $template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews       );
254     $template->param( AMAZON_SIMILAR_PRODUCTS  => \@similar_products      );
255     $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews      );
256 }
257 output_html_with_http_headers $query, $cookie, $template->output;