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