16cf460f3560ad472e968464580ede48153a484c
[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 use C4::Search;         # enabled_staff_search_views
38 use C4::VirtualShelves;
39
40 # use Smart::Comments;
41
42 my $query = CGI->new();
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {
45         template_name   => "catalogue/detail.tmpl",
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1 },
50     }
51 );
52
53 my $biblionumber = $query->param('biblionumber');
54 my $fw = GetFrameworkCode($biblionumber);
55
56 ## get notes and subjects from MARC record
57 my $marcflavour      = C4::Context->preference("marcflavour");
58 my $record           = GetMarcBiblio($biblionumber);
59
60 # some useful variables for enhanced content;
61 # in each case, we're grabbing the first value we find in
62 # the record and normalizing it
63 my $upc = GetNormalizedUPC($record,$marcflavour);
64 my $ean = GetNormalizedEAN($record,$marcflavour);
65 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
66 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
67
68 $template->param(
69     normalized_upc => $upc,
70     normalized_ean => $ean,
71     normalized_oclc => $oclc,
72     normalized_isbn => $isbn,
73 );
74
75 unless (defined($record)) {
76     print $query->redirect("/cgi-bin/koha/errors/404.pl");
77         exit;
78 }
79
80 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
81 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
82 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
83 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
84 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
85 my $subtitle         = GetRecordValue('subtitle', $record, $fw);
86
87 # Get Branches, Itemtypes and Locations
88 my $branches = GetBranches();
89 my $itemtypes = GetItemTypes();
90 my $dbh = C4::Context->dbh;
91
92 # change back when ive fixed request.pl
93 my @items = &GetItemsInfo( $biblionumber, 'intra' );
94 my $dat = &GetBiblioData($biblionumber);
95
96 #coping with subscriptions
97 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
98 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
99 my @subs;
100 $dat->{'serial'}=1 if $subscriptionsnumber;
101 foreach my $subscription (@subscriptions) {
102     my %cell;
103         my $serials_to_display;
104     $cell{subscriptionid}    = $subscription->{subscriptionid};
105     $cell{subscriptionnotes} = $subscription->{notes};
106     $cell{branchcode}        = $subscription->{branchcode};
107     $cell{branchname}        = GetBranchName($subscription->{branchcode});
108     $cell{hasalert}          = $subscription->{hasalert};
109     #get the three latest serials.
110         $serials_to_display = $subscription->{staffdisplaycount};
111         $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
112         $cell{staffdisplaycount} = $serials_to_display;
113     $cell{latestserials} =
114       GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
115     push @subs, \%cell;
116 }
117
118 if ( defined $dat->{'itemtype'} ) {
119     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
120 }
121 $dat->{'count'} = scalar @items;
122 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
123 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
124 my (@itemloop, %itemfields);
125 my $norequests = 1;
126 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
127 my $authvalcode_items_damaged  = GetAuthValCode('items.damaged', $fw);
128 foreach my $item (@items) {
129
130     # can place holds defaults to yes
131     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
132
133     # format some item fields for display
134     if ( defined $item->{'publictype'} ) {
135         $item->{ $item->{'publictype'} } = 1;
136     }
137     $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
138                                                : '';
139
140         foreach (qw(datedue datelastseen onloan)) {
141                 $item->{$_} = format_date($item->{$_});
142         }
143     # item damaged, lost, withdrawn loops
144     $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
145     if ($item->{damaged}) {
146         $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
147     }
148     #get shelf location and collection code description if they are authorised value.
149     my $shelfcode = $item->{'location'};
150     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
151     my $ccode = $item->{'ccode'};
152     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
153     foreach (qw(ccode enumchron copynumber uri)) {
154         $itemfields{$_} = 1 if ( $item->{$_} );
155     }
156
157     # checking for holds
158     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
159     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
160     
161     if (C4::Context->preference('HidePatronName')){
162         $item->{'hidepatronname'} = 1;
163     }
164
165     if ( defined $reservedate ) {
166         $item->{backgroundcolor} = 'reserved';
167         $item->{reservedate}     = format_date($reservedate);
168         $item->{ReservedForBorrowernumber}     = $reservedfor;
169         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
170         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
171         $item->{ExpectedAtLibrary}      = $branches->{$expectedAt}{branchname};
172         $item->{cardnumber}             = $ItemBorrowerReserveInfo->{'cardnumber'};
173     }
174
175         # Check the transit status
176     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
177     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
178         $item->{transfertwhen} = format_date($transfertwhen);
179         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
180         $item->{transfertto}   = $branches->{$transfertto}{branchname};
181         $item->{nocancel} = 1;
182     }
183
184     # FIXME: move this to a pm, check waiting status for holds
185     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
186     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
187     while (my $wait_hashref = $sth2->fetchrow_hashref) {
188         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
189     }
190
191     push @itemloop, $item;
192 }
193
194 $template->param( norequests => $norequests );
195 $template->param(
196         MARCNOTES   => $marcnotesarray,
197         MARCSUBJCTS => $marcsubjctsarray,
198         MARCAUTHORS => $marcauthorsarray,
199         MARCSERIES  => $marcseriesarray,
200         MARCURLS => $marcurlsarray,
201         subtitle    => $subtitle,
202         itemdata_ccode      => $itemfields{ccode},
203         itemdata_enumchron  => $itemfields{enumchron},
204         itemdata_uri        => $itemfields{uri},
205         itemdata_copynumber => $itemfields{copynumber},
206         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
207         z3950_search_params     => C4::Search::z3950_search_args($dat),
208         C4::Search::enabled_staff_search_views,
209 );
210
211 my @results = ( $dat, );
212 foreach ( keys %{$dat} ) {
213     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
214 }
215
216 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
217 # method query not found?!?!
218
219 $template->param(
220     itemloop        => \@itemloop,
221     biblionumber        => $biblionumber,
222     detailview => 1,
223     subscriptions       => \@subs,
224     subscriptionsnumber => $subscriptionsnumber,
225     subscriptiontitle   => $dat->{title},
226 );
227
228 # $debug and $template->param(debug_display => 1);
229
230 # Lists
231
232 if (C4::Context->preference("virtualshelves") ) {
233    $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
234 }
235
236 # XISBN Stuff
237 if (C4::Context->preference("FRBRizeEditions")==1) {
238     eval {
239         $template->param(
240             XISBNS => get_xisbns($isbn)
241         );
242     };
243     if ($@) { warn "XISBN Failed $@"; }
244 }
245 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
246     $template->param( AmazonTld => get_amazon_tld() );
247     my $amazon_reviews  = C4::Context->preference("AmazonReviews");
248     my $amazon_similars = C4::Context->preference("AmazonSimilarItems");
249     my @services;
250     if ( $amazon_reviews ) {
251         $template->param( AmazonReviews => 1 );
252         push( @services, 'EditorialReview' );
253     }
254     if ( $amazon_similars ) {
255         $template->param( AmazonSimilarItems => 1 );
256         push( @services, 'Similarities' );
257     }
258     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
259     if ( $amazon_similars ) {
260         my $similar_products_exist;
261         my @similar_products;
262         for my $similar_product (@{$amazon_details->{Items}->{Item}->[0]->{SimilarProducts}->{SimilarProduct}}) {
263             # do we have any of these isbns in our collection?
264             my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
265             # verify that there is at least one similar item
266                     if (scalar(@$similar_biblionumbers)){            
267                             $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
268                 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
269             }
270         }
271         $template->param( AmazonSimilarItems       => $similar_products_exist );
272         $template->param( AMAZON_SIMILAR_PRODUCTS  => \@similar_products      );
273     }
274     if ( $amazon_reviews ) {
275         my $item = $amazon_details->{Items}->{Item}->[0];
276         my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
277         #my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{Review}};
278         #my $average_rating = $amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{AverageRating} || 0;
279         #$template->param( amazon_average_rating    => $average_rating * 20    );
280         #$template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews       );
281         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews      );
282     }
283 }
284
285 # Get OPAC URL
286 if (C4::Context->preference('OPACBaseURL')){
287      $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
288 }
289
290 output_html_with_http_headers $query, $cookie, $template->output;