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