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