Bugfix #3155 Disabling Amazon Reviews
[koha.git] / opac / opac-detail.pl
blob07632db9e0b6d007590f810eb60e7514c4f7d8be
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
23 use CGI;
24 use C4::Auth;
25 use C4::Branch;
26 use C4::Koha;
27 use C4::Serials; #uses getsubscriptionfrom biblionumber
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Circulation;
32 use C4::Tags qw(get_tags);
33 use C4::Dates qw/format_date/;
34 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
35 use C4::External::Amazon;
36 use C4::External::Syndetics qw(get_syndetics_index get_syndetics_summary get_syndetics_toc get_syndetics_excerpt get_syndetics_reviews get_syndetics_anotes );
37 use C4::Review;
38 use C4::Serials;
39 use C4::Members;
40 use C4::XSLT;
42 BEGIN {
43 if (C4::Context->preference('BakerTaylorEnabled')) {
44 require C4::External::BakerTaylor;
45 import C4::External::BakerTaylor qw(&image_url &link_url);
49 my $query = new CGI;
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52 template_name => "opac-detail.tmpl",
53 query => $query,
54 type => "opac",
55 authnotrequired => 1,
56 flagsrequired => { borrow => 1 },
60 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
61 my $record = GetMarcBiblio($biblionumber);
62 $template->param( biblionumber => $biblionumber );
63 # XSLT processing of some stuff
64 if (C4::Context->preference("XSLTDetailsDisplay") ) {
65 my $newxmlrecord = XSLTParse4Display($biblionumber, $record, C4::Context->config('opachtdocs')."/prog/en/xslt/MARC21slim2OPACDetail.xsl");
66 $template->param('XSLTBloc' => $newxmlrecord);
69 $template->param('OPACShowCheckoutName' => C4::Context->preference("OPACShowCheckoutName") );
70 # change back when ive fixed request.pl
71 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
72 my @items;
73 @items = @all_items unless C4::Context->preference('hidelostitems');
75 if (C4::Context->preference('hidelostitems')) {
76 # Hide host items
77 for my $itm (@all_items) {
78 push @items, $itm unless $itm->{itemlost};
81 my $dat = &GetBiblioData($biblionumber);
83 if (!$dat) {
84 print $query->redirect("/cgi-bin/koha/errors/404.pl");
85 exit;
87 my $itemtypes = GetItemTypes();
88 # imageurl:
89 my $itemtype = $dat->{'itemtype'};
90 if ( $itemtype ) {
91 $dat->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
92 $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
94 my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'});
95 my $collections = GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'} );
97 #coping with subscriptions
98 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
99 my @subscriptions = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
101 my @subs;
102 $dat->{'serial'}=1 if $subscriptionsnumber;
103 foreach my $subscription (@subscriptions) {
104 my $serials_to_display;
105 my %cell;
106 $cell{subscriptionid} = $subscription->{subscriptionid};
107 $cell{subscriptionnotes} = $subscription->{notes};
108 $cell{branchcode} = $subscription->{branchcode};
109 $cell{hasalert} = $subscription->{hasalert};
110 #get the three latest serials.
111 $serials_to_display = $subscription->{opacdisplaycount};
112 $serials_to_display = C4::Context->preference('OPACSerialIssueDisplayCount') unless $serials_to_display;
113 $cell{opacdisplaycount} = $serials_to_display;
114 $cell{latestserials} =
115 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
116 push @subs, \%cell;
119 $dat->{'count'} = scalar(@items);
121 my $biblio_authorised_value_images = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $biblionumber, $record ) );
123 my $norequests = 1;
124 my $branches = GetBranches();
125 my %itemfields;
126 for my $itm (@items) {
127 $norequests = 0
128 if ( (not $itm->{'wthdrawn'} )
129 && (not $itm->{'itemlost'} )
130 && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'} )
131 && (not $itemtypes->{$itm->{'itype'}}->{notforloan} )
132 && ($itm->{'itemnumber'} ) );
134 $itm->{ $itm->{'publictype'} } = 1;
135 $itm->{datedue} = format_date($itm->{datedue});
136 $itm->{datelastseen} = format_date($itm->{datelastseen});
138 #get collection code description, too
139 my $ccode= $itm->{'ccode'};
140 $itm->{'ccode'} = $collections->{$ccode} if(defined($collections) && exists($collections->{$ccode}));
141 $itm->{'location_description'} = $shelflocations->{$itm->{'location'} };
142 $itm->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
143 $itm->{'description'} = $itemtypes->{$itemtype}->{'description'};
144 foreach (qw(ccode enumchron copynumber itemnotes)) {
145 $itemfields{$_} = 1 if ($itm->{$_});
148 # walk through the item-level authorised values and populate some images
149 my $item_authorised_value_images = C4::Items::get_authorised_value_images( C4::Items::get_item_authorised_values( $itm->{'itemnumber'} ) );
150 # warn( Data::Dumper->Dump( [ $item_authorised_value_images ], [ 'item_authorised_value_images' ] ) );
152 if ( $itm->{'itemlost'} ) {
153 my $lostimageinfo = List::Util::first { $_->{'category'} eq 'LOST' } @$item_authorised_value_images;
154 $itm->{'lostimageurl'} = $lostimageinfo->{ 'imageurl' };
155 $itm->{'lostimagelabel'} = $lostimageinfo->{ 'label' };
158 if( $itm->{'count_reserves'}){
159 if( $itm->{'count_reserves'} eq "Waiting"){ $itm->{'waiting'} = 1; }
160 if( $itm->{'count_reserves'} eq "Reserved"){ $itm->{'onhold'} = 1; }
163 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
164 if ( $transfertwhen ne '' ) {
165 $itm->{transfertwhen} = format_date($transfertwhen);
166 $itm->{transfertfrom} = $branches->{$transfertfrom}{branchname};
167 $itm->{transfertto} = $branches->{$transfertto}{branchname};
171 ## get notes and subjects from MARC record
172 my $dbh = C4::Context->dbh;
173 my $marcflavour = C4::Context->preference("marcflavour");
174 my $marcnotesarray = GetMarcNotes ($record,$marcflavour);
175 my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
176 my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
177 my $marcseriesarray = GetMarcSeries ($record,$marcflavour);
178 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
179 my $subtitle = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
181 $template->param(
182 MARCNOTES => $marcnotesarray,
183 MARCSUBJCTS => $marcsubjctsarray,
184 MARCAUTHORS => $marcauthorsarray,
185 MARCSERIES => $marcseriesarray,
186 MARCURLS => $marcurlsarray,
187 norequests => $norequests,
188 RequestOnOpac => C4::Context->preference("RequestOnOpac"),
189 itemdata_ccode => $itemfields{ccode},
190 itemdata_enumchron => $itemfields{enumchron},
191 itemdata_copynumber => $itemfields{copynumber},
192 itemdata_itemnotes => $itemfields{itemnotes},
193 authorised_value_images => $biblio_authorised_value_images,
194 subtitle => $subtitle,
197 foreach ( keys %{$dat} ) {
198 $template->param( "$_" => $dat->{$_} . "" );
201 # some useful variables for enhanced content;
202 # in each case, we're grabbing the first value we find in
203 # the record and normalizing it
204 my $upc = GetNormalizedUPC($record,$marcflavour);
205 my $ean = GetNormalizedEAN($record,$marcflavour);
206 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
207 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
208 my $content_identifier_exists = 1 if ($isbn or $ean or $oclc or $upc);
209 $template->param(
210 normalized_upc => $upc,
211 normalized_ean => $ean,
212 normalized_oclc => $oclc,
213 normalized_isbn => $isbn,
214 content_identifier_exists => $content_identifier_exists,
217 # COinS format FIXME: for books Only
218 my $coins_format;
219 my $fmt = substr $record->leader(), 6,2;
220 my $fmts;
221 $fmts->{'am'} = 'book';
222 $coins_format = $fmts->{$fmt};
223 $template->param(
224 ocoins_format => $coins_format,
227 my $reviews = getreviews( $biblionumber, 1 );
228 my $loggedincommenter;
229 foreach ( @$reviews ) {
230 my $borrowerData = GetMember($_->{borrowernumber},'borrowernumber');
231 # setting some borrower info into this hash
232 $_->{title} = $borrowerData->{'title'};
233 $_->{surname} = $borrowerData->{'surname'};
234 $_->{firstname} = $borrowerData->{'firstname'};
235 $_->{userid} = $borrowerData->{'userid'};
236 $_->{datereviewed} = format_date($_->{datereviewed});
237 if ($borrowerData->{'borrowernumber'} eq $borrowernumber) {
238 $_->{your_comment} = 1;
239 $loggedincommenter = 1;
244 if(C4::Context->preference("ISBD")) {
245 $template->param(ISBD => 1);
248 $template->param(
249 ITEM_RESULTS => \@items,
250 subscriptionsnumber => $subscriptionsnumber,
251 biblionumber => $biblionumber,
252 subscriptions => \@subs,
253 subscriptionsnumber => $subscriptionsnumber,
254 reviews => $reviews,
255 loggedincommenter => $loggedincommenter
258 # XISBN Stuff
259 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
260 eval {
261 $template->param(
262 XISBNS => get_xisbns($isbn)
265 if ($@) { warn "XISBN Failed $@"; }
267 # Amazon.com Stuff
268 if ( C4::Context->preference("OPACAmazonEnabled") ) {
269 $template->param( AmazonTld => get_amazon_tld() );
270 $template->param( OPACAmazonReviews => C4::Context->preference("OPACAmazonReviews") );
272 if ( C4::Context->preference("OPACAmazonEnabled") && C4::Context->preference("OPACAmazonSimilarItems") ) {
273 my $similar_products_exist;
274 my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
275 my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
276 my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
277 for my $one_review (@$customer_reviews) {
278 $one_review->{Date} = format_date($one_review->{Date});
280 my @similar_products;
281 for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
282 # do we have any of these isbns in our collection?
283 my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
284 # verify that there is at least one similar item
285 if (scalar(@$similar_biblionumbers)){
286 $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
287 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} };
290 my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
291 my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0;
292 $template->param( OPACAmazonSimilarItems => $similar_products_exist );
293 $template->param( amazon_average_rating => $average_rating * 20);
294 $template->param( AMAZON_CUSTOMER_REVIEWS => $customer_reviews );
295 $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
296 $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
299 my $syndetics_elements;
301 if ( C4::Context->preference("SyndeticsEnabled") ) {
302 eval {
303 $syndetics_elements = &get_syndetics_index($isbn,$upc,$oclc);
304 for my $element (values %$syndetics_elements) {
305 $template->param("Syndetics$element"."Exists" => 1 );
306 #warn "Exists: "."Syndetics$element"."Exists";
309 warn $@ if $@;
312 if ( C4::Context->preference("SyndeticsEnabled")
313 && C4::Context->preference("SyndeticsSummary")
314 && $syndetics_elements->{'SUMMARY'} =~ /SUMMARY/) {
315 eval {
316 my $syndetics_summary = &get_syndetics_summary($isbn,$upc,$oclc);
317 $template->param( SYNDETICS_SUMMARY => $syndetics_summary );
319 warn $@ if $@;
323 if ( C4::Context->preference("SyndeticsEnabled")
324 && C4::Context->preference("SyndeticsTOC")
325 && $syndetics_elements->{'TOC'} =~ /TOC/) {
326 eval {
327 my $syndetics_toc = &get_syndetics_toc($isbn,$upc,$oclc);
328 $template->param( SYNDETICS_TOC => $syndetics_toc );
330 warn $@ if $@;
333 if ( C4::Context->preference("SyndeticsEnabled")
334 && C4::Context->preference("SyndeticsExcerpt")
335 && $syndetics_elements->{'DBCHAPTER'} =~ /DBCHAPTER/ ) {
336 eval {
337 my $syndetics_excerpt = &get_syndetics_excerpt($isbn,$upc,$oclc);
338 $template->param( SYNDETICS_EXCERPT => $syndetics_excerpt );
340 warn $@ if $@;
343 if ( C4::Context->preference("SyndeticsEnabled")
344 && C4::Context->preference("SyndeticsReviews")) {
345 eval {
346 my $syndetics_reviews = &get_syndetics_reviews($isbn,$upc,$oclc,$syndetics_elements);
347 $template->param( SYNDETICS_REVIEWS => $syndetics_reviews );
349 warn $@ if $@;
352 if ( C4::Context->preference("SyndeticsEnabled")
353 && C4::Context->preference("SyndeticsAuthorNotes")
354 && $syndetics_elements->{'ANOTES'} =~ /ANOTES/ ) {
355 eval {
356 my $syndetics_anotes = &get_syndetics_anotes($isbn,$upc,$oclc);
357 $template->param( SYNDETICS_ANOTES => $syndetics_anotes );
359 warn $@ if $@;
362 # LibraryThingForLibraries ID Code and Tabbed View Option
363 if( C4::Context->preference('LibraryThingForLibrariesEnabled') )
365 $template->param(LibraryThingForLibrariesID =>
366 C4::Context->preference('LibraryThingForLibrariesID') );
367 $template->param(LibraryThingForLibrariesTabbedView =>
368 C4::Context->preference('LibraryThingForLibrariesTabbedView') );
372 # Babelthèque
373 if ( C4::Context->preference("Babeltheque") ) {
374 $template->param(
375 Babeltheque => 1,
379 # Shelf Browser Stuff
380 if (C4::Context->preference("OPACShelfBrowser")) {
381 # pick the first itemnumber unless one was selected by the user
382 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber'); # || $items[0]->{itemnumber};
383 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
384 # find the right cn_sort value for this item
385 my ($starting_cn_sort, $starting_homebranch, $starting_location);
386 my $sth_get_cn_sort = $dbh->prepare("SELECT cn_sort,homebranch,location from items where itemnumber=?");
387 $sth_get_cn_sort->execute($starting_itemnumber);
388 while (my $result = $sth_get_cn_sort->fetchrow_hashref()) {
389 $starting_cn_sort = $result->{'cn_sort'};
390 $starting_homebranch->{code} = $result->{'homebranch'};
391 $starting_homebranch->{description} = $branches->{$result->{'homebranch'}}{branchname};
392 $starting_location->{code} = $result->{'location'};
393 $starting_location->{description} = GetAuthorisedValueDesc('','', $result->{'location'} ,'','','LOC');
397 ## List of Previous Items
398 # order by cn_sort, which should include everything we need for ordering purposes (though not
399 # for limits, those need to be handled separately
400 my $sth_shelfbrowse_previous = $dbh->prepare("
401 SELECT *
402 FROM items
403 WHERE
404 ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) AND
405 homebranch = ? AND location = ?
406 ORDER BY cn_sort DESC, itemnumber LIMIT 3
408 $sth_shelfbrowse_previous->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
409 my @previous_items;
410 while (my $this_item = $sth_shelfbrowse_previous->fetchrow_hashref()) {
411 my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
412 $sth_get_biblio->execute($this_item->{biblionumber});
413 while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
414 $this_item->{'title'} = $this_biblio->{'title'};
415 my $this_record = GetMarcBiblio($this_biblio->{'biblionumber'});
416 $this_item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
417 $this_item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
418 $this_item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
420 unshift @previous_items, $this_item;
423 ## List of Next Items; this also intentionally catches the current item
424 my $sth_shelfbrowse_next = $dbh->prepare("
425 SELECT *
426 FROM items
427 WHERE
428 ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) AND
429 homebranch = ? AND location = ?
430 ORDER BY cn_sort, itemnumber LIMIT 3
432 $sth_shelfbrowse_next->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
433 my @next_items;
434 while (my $this_item = $sth_shelfbrowse_next->fetchrow_hashref()) {
435 my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
436 $sth_get_biblio->execute($this_item->{biblionumber});
437 while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
438 $this_item->{'title'} = $this_biblio->{'title'};
439 my $this_record = GetMarcBiblio($this_biblio->{'biblionumber'});
440 $this_item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
441 $this_item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
442 $this_item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
444 push @next_items, $this_item;
447 # alas, these won't auto-vivify, see http://www.perlmonks.org/?node_id=508481
448 my $shelfbrowser_next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
449 my $shelfbrowser_next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
451 $template->param(
452 starting_homebranch => $starting_homebranch->{description},
453 starting_location => $starting_location->{description},
454 starting_itemnumber => $starting_itemnumber,
455 shelfbrowser_prev_itemnumber => (@previous_items ? $previous_items[0]->{itemnumber} : 0),
456 shelfbrowser_next_itemnumber => $shelfbrowser_next_itemnumber,
457 shelfbrowser_prev_biblionumber => (@previous_items ? $previous_items[0]->{biblionumber} : 0),
458 shelfbrowser_next_biblionumber => $shelfbrowser_next_biblionumber,
459 PREVIOUS_SHELF_BROWSE => \@previous_items,
460 NEXT_SHELF_BROWSE => \@next_items,
464 if (C4::Context->preference("BakerTaylorEnabled")) {
465 $template->param(
466 BakerTaylorEnabled => 1,
467 BakerTaylorImageURL => &image_url(),
468 BakerTaylorLinkURL => &link_url(),
469 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
471 my ($bt_user, $bt_pass);
472 if ($isbn and
473 $bt_user = C4::Context->preference('BakerTaylorUsername') and
474 $bt_pass = C4::Context->preference('BakerTaylorPassword') )
476 $template->param(
477 BakerTaylorContentURL =>
478 sprintf("http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
479 $bt_user,$bt_pass,$isbn)
484 my $tag_quantity;
485 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
486 $template->param(
487 TagsEnabled => 1,
488 TagsShowOnDetail => $tag_quantity,
489 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
491 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
492 'sort'=>'-weight', limit=>$tag_quantity}));
495 output_html_with_http_headers $query, $cookie, $template->output;