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