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