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