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