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