fix for #988
[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         my $serials_to_display;
102     $cell{subscriptionid}    = $subscription->{subscriptionid};
103     $cell{subscriptionnotes} = $subscription->{notes};
104         $cell{branchcode}        = $subscription->{branchcode};
105         $cell{hasalert}          = $subscription->{hasalert};
106     #get the three latest serials.
107         $serials_to_display = $subscription->{staffdisplaycount};
108         $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
109         $cell{staffdisplaycount} = $serials_to_display;
110     $cell{latestserials} =
111       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
112     push @subs, \%cell;
113 }
114
115 if ( defined $dat->{'itemtype'} ) {
116     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
117 }
118 $dat->{'count'} = scalar @items;
119 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
120 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
121 my (@itemloop, %itemfields);
122 my $norequests = 1;
123 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
124 my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
125 foreach my $item (@items) {
126
127     # can place holds defaults to yes
128     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
129
130     # format some item fields for display
131     if ( defined $item->{'publictype'} ) {
132         $item->{ $item->{'publictype'} } = 1;
133     }
134     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
135                                                : '';
136
137         foreach (qw(datedue datelastseen onloan)) {
138                 $item->{$_} = format_date($item->{$_});
139         }
140     # item damaged, lost, withdrawn loops
141     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
142     if ($item->{damaged}) {
143         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
144     }
145     #get shelf location and collection code description if they are authorised value.
146     my $shelfcode = $item->{'location'};
147     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
148     my $ccode = $item->{'ccode'};
149     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
150     foreach (qw(ccode enumchron copynumber)) {
151         $itemfields{$_} = 1 if ( $item->{$_} );
152     }
153
154     # checking for holds
155     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
156     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
157
158     if ( defined $reservedate ) {
159         $item->{backgroundcolor} = 'reserved';
160         $item->{reservedate}     = format_date($reservedate);
161         $item->{ReservedForBorrowernumber}     = $reservedfor;
162         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
163         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
164         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
165     }
166
167         # Check the transit status
168     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
169     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
170         $item->{transfertwhen} = format_date($transfertwhen);
171         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
172         $item->{transfertto}   = $branches->{$transfertto}{branchname};
173         $item->{nocancel} = 1;
174     }
175
176     # FIXME: move this to a pm, check waiting status for holds
177     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
178     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
179     while (my $wait_hashref = $sth2->fetchrow_hashref) {
180         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
181     }
182
183     push @itemloop, $item;
184 }
185
186 $template->param( norequests => $norequests );
187 $template->param(
188         MARCNOTES   => $marcnotesarray,
189         MARCSUBJCTS => $marcsubjctsarray,
190         MARCAUTHORS => $marcauthorsarray,
191         MARCSERIES  => $marcseriesarray,
192         MARCURLS => $marcurlsarray,
193         subtitle    => $subtitle,
194         itemdata_ccode      => $itemfields{ccode},
195         itemdata_enumchron  => $itemfields{enumchron},
196         itemdata_copynumber => $itemfields{copynumber},
197         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
198 );
199
200 my @results = ( $dat, );
201 foreach ( keys %{$dat} ) {
202     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
203 }
204
205 $template->param(
206     itemloop        => \@itemloop,
207     biblionumber        => $biblionumber,
208     detailview => 1,
209     subscriptions       => \@subs,
210     subscriptionsnumber => $subscriptionsnumber,
211     subscriptiontitle   => $dat->{title},
212 );
213
214 # $debug and $template->param(debug_display => 1);
215
216 # XISBN Stuff
217 if (C4::Context->preference("FRBRizeEditions")==1) {
218     eval {
219         $template->param(
220             XISBNS => get_xisbns($isbn)
221         );
222     };
223     if ($@) { warn "XISBN Failed $@"; }
224 }
225 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
226     $template->param( AmazonTld => get_amazon_tld() );
227     my $amazon_reviews  = C4::Context->preference("AmazonReviews");
228     my $amazon_similars = C4::Context->preference("AmazonSimilarItems");
229     my @services;
230     if ( $amazon_reviews ) {
231         $template->param( AmazonReviews => 1 );
232         push( @services, 'EditorialReview' );
233     }
234     if ( $amazon_similars ) {
235         $template->param( AmazonSimilarItems => 1 );
236         push( @services, 'Similarities' );
237     }
238     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
239     if ( $amazon_similars ) {
240         my $similar_products_exist;
241         my @similar_products;
242         for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
243             # do we have any of these isbns in our collection?
244             my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
245             # verify that there is at least one similar item
246                     if (scalar(@$similar_biblionumbers)){            
247                             $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
248                 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
249             }
250         }
251         $template->param( AmazonSimilarItems       => $similar_products_exist );
252         $template->param( AMAZON_SIMILAR_PRODUCTS  => \@similar_products      );
253     }
254     if ( $amazon_reviews ) {
255         my $item = $amazon_details->{Items}->{Item};
256         my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
257         #my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
258         #my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0;
259         #$template->param( amazon_average_rating    => $average_rating * 20    );
260         #$template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews       );
261         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews      );
262     }
263 }
264 output_html_with_http_headers $query, $cookie, $template->output;