3 # This file is part of Koha.
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
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.
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
24 use C4::Dates qw/format_date/;
26 use C4::Serials; #uses getsubscriptionfrom biblionumber
35 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
36 use C4::External::Amazon;
38 # use Smart::Comments;
40 my $query = CGI->new();
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43 template_name => "catalogue/detail.tmpl",
47 flagsrequired => { catalogue => 1 },
51 my $biblionumber = $query->param('biblionumber');
52 my $fw = GetFrameworkCode($biblionumber);
54 ## get notes and subjects from MARC record
55 my $marcflavour = C4::Context->preference("marcflavour");
56 my $record = GetMarcBiblio($biblionumber);
58 # some useful variables for enhanced content;
59 # in each case, we're grabbing the first value we find in
60 # the record and normalizing it
61 my $upc = GetNormalizedUPC($record,$marcflavour);
62 my $ean = GetNormalizedEAN($record,$marcflavour);
63 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
64 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
67 normalized_upc => $upc,
68 normalized_ean => $ean,
69 normalized_oclc => $oclc,
70 normalized_isbn => $isbn,
73 unless (defined($record)) {
74 print $query->redirect("/cgi-bin/koha/errors/404.pl");
78 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
79 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
80 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
81 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
82 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
83 my $subtitle = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
85 # Get Branches, Itemtypes and Locations
86 my $branches = GetBranches();
87 my $itemtypes = GetItemTypes();
88 my $dbh = C4::Context->dbh;
90 # change back when ive fixed request.pl
91 my @items = &GetItemsInfo( $biblionumber, 'intra' );
92 my $dat = &GetBiblioData($biblionumber);
94 #coping with subscriptions
95 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
96 my @subscriptions = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
98 $dat->{'serial'}=1 if $subscriptionsnumber;
99 foreach my $subscription (@subscriptions) {
101 my $serials_to_display;
102 $cell{subscriptionid} = $subscription->{subscriptionid};
103 $cell{subscriptionnotes} = $subscription->{notes};
104 $cell{branchcode} = $subscription->{branchcode};
105 $cell{hasalert} = $subscription->{hasalert};
106 #get the three latest serials.
107 $serials_to_display = $subscription->{staffdisplaycount};
108 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
109 $cell{staffdisplaycount} = $serials_to_display;
110 $cell{latestserials} =
111 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
115 if ( defined $dat->{'itemtype'} ) {
116 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
118 $dat->{'count'} = scalar @items;
119 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
120 my $collections = GetKohaAuthorisedValues('items.ccode' , $fw);
121 my (@itemloop, %itemfields);
123 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
124 my $authvalcode_items_damaged = GetAuthValCode('items.damaged', $fw);
125 foreach my $item (@items) {
127 # can place holds defaults to yes
128 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
130 # format some item fields for display
131 if ( defined $item->{'publictype'} ) {
132 $item->{ $item->{'publictype'} } = 1;
134 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
137 foreach (qw(datedue datelastseen onloan)) {
138 $item->{$_} = format_date($item->{$_});
140 # item damaged, lost, withdrawn loops
141 $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
142 if ($item->{damaged}) {
143 $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
145 #get shelf location and collection code description if they are authorised value.
146 my $shelfcode = $item->{'location'};
147 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
148 my $ccode = $item->{'ccode'};
149 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
150 foreach (qw(ccode enumchron copynumber)) {
151 $itemfields{$_} = 1 if ( $item->{$_} );
155 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
156 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
158 if ( defined $reservedate ) {
159 $item->{backgroundcolor} = 'reserved';
160 $item->{reservedate} = format_date($reservedate);
161 $item->{ReservedForBorrowernumber} = $reservedfor;
162 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
163 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
164 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
167 # Check the transit status
168 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
169 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
170 $item->{transfertwhen} = format_date($transfertwhen);
171 $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
172 $item->{transfertto} = $branches->{$transfertto}{branchname};
173 $item->{nocancel} = 1;
176 # FIXME: move this to a pm, check waiting status for holds
177 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
178 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
179 while (my $wait_hashref = $sth2->fetchrow_hashref) {
180 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
183 push @itemloop, $item;
186 $template->param( norequests => $norequests );
188 MARCNOTES => $marcnotesarray,
189 MARCSUBJCTS => $marcsubjctsarray,
190 MARCAUTHORS => $marcauthorsarray,
191 MARCSERIES => $marcseriesarray,
192 MARCURLS => $marcurlsarray,
193 subtitle => $subtitle,
194 itemdata_ccode => $itemfields{ccode},
195 itemdata_enumchron => $itemfields{enumchron},
196 itemdata_copynumber => $itemfields{copynumber},
197 volinfo => $itemfields{enumchron} || $dat->{'serial'} ,
200 my @results = ( $dat, );
201 foreach ( keys %{$dat} ) {
202 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
206 itemloop => \@itemloop,
207 biblionumber => $biblionumber,
209 subscriptions => \@subs,
210 subscriptionsnumber => $subscriptionsnumber,
211 subscriptiontitle => $dat->{title},
214 # $debug and $template->param(debug_display => 1);
217 if (C4::Context->preference("FRBRizeEditions")==1) {
220 XISBNS => get_xisbns($isbn)
223 if ($@) { warn "XISBN Failed $@"; }
225 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
226 my $similar_products_exist;
227 my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour );
228 my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
229 my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
230 my @similar_products;
231 for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
232 # do we have any of these isbns in our collection?
233 my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
234 # verify that there is at least one similar item
235 if (scalar(@$similar_biblionumbers)){
236 $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
237 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} };
240 my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
241 my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
242 $template->param( AmazonSimilarItems => $similar_products_exist );
243 $template->param( amazon_average_rating => $average_rating * 20);
244 $template->param( AMAZON_CUSTOMER_REVIEWS => $customer_reviews );
245 $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
246 $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
248 output_html_with_http_headers $query, $cookie, $template->output;