REFACTOR: correcting indirect object method syntax
[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 get_biblio_from_xisbn);
36 use C4::Amazon;
37
38 # use Smart::Comments;
39
40 my $query = CGI->new();
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "catalogue/detail.tmpl",
44         query           => $query,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { catalogue => 1 },
48     }
49 );
50
51 my $biblionumber = $query->param('biblionumber');
52 my $fw = GetFrameworkCode($biblionumber);
53
54 ## get notes and subjects from MARC record
55 my $marcflavour      = C4::Context->preference("marcflavour");
56 my $record           = GetMarcBiblio($biblionumber);
57
58 unless (defined($record)) {
59     print $query->redirect("/cgi-bin/koha/errors/404.pl");
60         exit;
61 }
62
63 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
64 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
65 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
66 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
67 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
68 my $subtitle         = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
69
70 # Get Branches, Itemtypes and Locations
71 my $branches = GetBranches();
72 my $itemtypes = GetItemTypes();
73
74 # FIXME: move this to a pm, check waiting status for holds
75 my $dbh = C4::Context->dbh;
76
77 # change back when ive fixed request.pl
78 my @items = &GetItemsInfo( $biblionumber, 'intra' );
79 my $dat = &GetBiblioData($biblionumber);
80
81 #coping with subscriptions
82 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
83 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
84 my @subs;
85 $dat->{'serial'}=1 if $subscriptionsnumber;
86 foreach my $subscription (@subscriptions) {
87     my %cell;
88     $cell{subscriptionid}    = $subscription->{subscriptionid};
89     $cell{subscriptionnotes} = $subscription->{notes};
90         $cell{branchcode}        = $subscription->{branchcode};
91         $cell{hasalert}          = $subscription->{hasalert};
92     #get the three latest serials.
93     $cell{latestserials} =
94       GetLatestSerials( $subscription->{subscriptionid}, 3 );
95     push @subs, \%cell;
96 }
97
98 if ( defined $dat->{'itemtype'} ) {
99     $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
100 }
101 $dat->{'count'} = scalar @items;
102 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
103 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
104 my (@itemloop, %itemfields);
105 my $norequests = 1;
106 foreach my $item (@items) {
107
108     # can place holds defaults to yes
109     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
110
111     # format some item fields for display
112     if ( defined $item->{'publictype'} ) {
113         $item->{ $item->{'publictype'} } = 1;
114     }
115     $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
116         foreach (qw(datedue datelastseen onloan)) {
117                 $item->{$_} = format_date($item->{$_});
118         }
119     # item damaged, lost, withdrawn loops
120     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
121     if ($item->{damaged}) {
122         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
123     }
124     #get shelf location and collection code description if they are authorised value.
125     my $shelfcode = $item->{'location'};
126     $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
127     my $ccode = $item->{'ccode'};
128     $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
129     foreach (qw(ccode enumchron copynumber)) {
130         $itemfields{$_} = 1 if ( $item->{$_} );
131     }
132
133     # checking for holds
134     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
135     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
136
137     if ( defined $reservedate ) {
138         $item->{backgroundcolor} = 'reserved';
139         $item->{reservedate}     = format_date($reservedate);
140         $item->{ReservedForBorrowernumber}     = $reservedfor;
141         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
142         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
143         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
144     }
145
146         # Check the transit status
147     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
148     if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
149         $item->{transfertwhen} = format_date($transfertwhen);
150         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
151         $item->{transfertto}   = $branches->{$transfertto}{branchname};
152         $item->{nocancel} = 1;
153     }
154
155     # FIXME: move this to a pm, check waiting status for holds
156     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
157     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
158     while (my $wait_hashref = $sth2->fetchrow_hashref) {
159         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
160     }
161
162     push @itemloop, $item;
163 }
164
165 $template->param( norequests => $norequests );
166 $template->param(
167         MARCNOTES   => $marcnotesarray,
168         MARCSUBJCTS => $marcsubjctsarray,
169         MARCAUTHORS => $marcauthorsarray,
170         MARCSERIES  => $marcseriesarray,
171         MARCURLS => $marcurlsarray,
172         subtitle    => $subtitle,
173         itemdata_ccode      => $itemfields{ccode},
174         itemdata_enumchron  => $itemfields{enumchron},
175         itemdata_copynumber => $itemfields{copynumber},
176         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
177 );
178
179 my @results = ( $dat, );
180 foreach ( keys %{$dat} ) {
181     $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
182 }
183
184 $template->param(
185     itemloop        => \@itemloop,
186     biblionumber        => $biblionumber,
187     detailview => 1,
188     subscriptions       => \@subs,
189     subscriptionsnumber => $subscriptionsnumber,
190     subscriptiontitle   => $dat->{title},
191 );
192
193 # $debug and $template->param(debug_display => 1);
194
195 # XISBN Stuff
196 my $xisbn=$dat->{'isbn'};
197 $xisbn =~ /(\d*[X]*)/ if ( $xisbn );
198 $template->param(amazonisbn => $1);             # FIXME: so it is OK if the ISBN = 'XXXXX' ?
199 if (C4::Context->preference("FRBRizeEditions")==1) {
200     eval {
201         $template->param(
202             xisbn => $xisbn,
203             XISBNS => get_xisbns($xisbn)
204         );
205     };
206     if ($@) { warn "XISBN Failed $@"; }
207 }
208 if ( C4::Context->preference("AmazonContent") == 1 ) {
209     my $similar_products_exist;
210     my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour );
211     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
212     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
213     my @similar_products;
214     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
215         # do we have any of these isbns in our collection?
216         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
217         # verify that there is at least one similar item
218                 if (scalar(@$similar_biblionumbers)){            
219                         $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
220             push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
221         }
222     }
223     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
224     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
225     $template->param( AmazonSimilarItems => $similar_products_exist );
226     $template->param( amazon_average_rating => $average_rating * 20);
227     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
228     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
229     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
230 }
231 output_html_with_http_headers $query, $cookie, $template->output;