Bug 7367 - One "the" too many
[koha.git] / catalogue / detail.pl
blobd110740ee8f6a062dff5dab3cc5f6f2e07972bf0
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
19 use strict;
20 use warnings;
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; # to use GetMember
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::Tags qw(get_tags);
39 use C4::VirtualShelves;
40 use C4::XSLT;
41 use C4::Images;
42 use Koha::DateUtils;
44 # use Smart::Comments;
46 my $query = CGI->new();
48 my $analyze = $query->param('analyze');
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52 template_name => 'catalogue/detail.tmpl',
53 query => $query,
54 type => "intranet",
55 authnotrequired => 0,
56 flagsrequired => { catalogue => 1 },
60 my $biblionumber = $query->param('biblionumber');
61 my $record = GetMarcBiblio($biblionumber);
63 if ( not defined $record ) {
64 # biblionumber invalid -> report and exit
65 $template->param( unknownbiblionumber => 1,
66 biblionumber => $biblionumber );
67 output_html_with_http_headers $query, $cookie, $template->output;
68 exit;
71 if($query->cookie("holdfor")){
72 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
73 $template->param(
74 holdfor => $query->cookie("holdfor"),
75 holdfor_surname => $holdfor_patron->{'surname'},
76 holdfor_firstname => $holdfor_patron->{'firstname'},
77 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
81 my $fw = GetFrameworkCode($biblionumber);
82 my $showallitems = $query->param('showallitems');
83 my $marcflavour = C4::Context->preference("marcflavour");
85 # XSLT processing of some stuff
86 if (C4::Context->preference("XSLTDetailsDisplay") ) {
87 $template->param('XSLTDetailsDisplay' =>'1',
88 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
91 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
92 $template->param( ocoins => GetCOinSBiblio($record) );
94 # some useful variables for enhanced content;
95 # in each case, we're grabbing the first value we find in
96 # the record and normalizing it
97 my $upc = GetNormalizedUPC($record,$marcflavour);
98 my $ean = GetNormalizedEAN($record,$marcflavour);
99 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
100 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
102 $template->param(
103 normalized_upc => $upc,
104 normalized_ean => $ean,
105 normalized_oclc => $oclc,
106 normalized_isbn => $isbn,
109 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
110 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
111 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
112 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
113 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
114 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
115 my $marchostsarray = GetMarcHosts($record,$marcflavour);
116 my $subtitle = GetRecordValue('subtitle', $record, $fw);
118 # Get Branches, Itemtypes and Locations
119 my $branches = GetBranches();
120 my $itemtypes = GetItemTypes();
121 my $dbh = C4::Context->dbh;
123 my @all_items = GetItemsInfo( $biblionumber );
124 my @items;
125 for my $itm (@all_items) {
126 push @items, $itm unless ( $itm->{itemlost} && GetHideLostItemsPreference($borrowernumber) && !$showallitems);
129 # flag indicating existence of at least one item linked via a host record
130 my $hostrecords;
131 # adding items linked via host biblios
132 my @hostitems = GetHostItemsInfo($record);
133 if (@hostitems){
134 $hostrecords =1;
135 push (@items,@hostitems);
138 my $dat = &GetBiblioData($biblionumber);
140 # get count of holds
141 my ( $holdcount, $holds ) = GetReservesFromBiblionumber($biblionumber,1);
143 #coping with subscriptions
144 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
145 my @subscriptions = GetSubscriptions( $dat->{title}, $dat->{issn}, undef, $biblionumber );
146 my @subs;
148 foreach my $subscription (@subscriptions) {
149 my %cell;
150 my $serials_to_display;
151 $cell{subscriptionid} = $subscription->{subscriptionid};
152 $cell{subscriptionnotes} = $subscription->{notes};
153 $cell{branchcode} = $subscription->{branchcode};
154 $cell{branchname} = GetBranchName($subscription->{branchcode});
155 $cell{hasalert} = $subscription->{hasalert};
156 #get the three latest serials.
157 $serials_to_display = $subscription->{staffdisplaycount};
158 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
159 $cell{staffdisplaycount} = $serials_to_display;
160 $cell{latestserials} =
161 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
162 push @subs, \%cell;
165 if ( defined $dat->{'itemtype'} ) {
166 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
169 $dat->{'count'} = scalar @all_items + @hostitems;
170 $dat->{'showncount'} = scalar @items + @hostitems;
171 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
173 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
174 my $collections = GetKohaAuthorisedValues('items.ccode' , $fw);
175 my $copynumbers = GetKohaAuthorisedValues('items.copynumber', $fw);
176 my (@itemloop, %itemfields);
177 my $norequests = 1;
178 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
179 my $authvalcode_items_damaged = GetAuthValCode('items.damaged', $fw);
181 my $analytics_flag;
182 my $materials_flag; # set this if the items have anything in the materials field
183 foreach my $item (@items) {
185 $item->{homebranch} = GetBranchName($item->{homebranch});
187 # can place holds defaults to yes
188 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
190 # format some item fields for display
191 if ( defined $item->{'publictype'} ) {
192 $item->{ $item->{'publictype'} } = 1;
194 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
195 : '';
197 foreach (qw(datelastseen onloan)) {
198 $item->{$_} = format_date($item->{$_});
200 $item->{datedue} = format_sqldatetime($item->{datedue});
201 # item damaged, lost, withdrawn loops
202 $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
203 if ($item->{damaged}) {
204 $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
206 #get shelf location and collection code description if they are authorised value.
207 # same thing for copy number
208 my $shelfcode = $item->{'location'};
209 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
210 my $ccode = $item->{'ccode'};
211 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
212 my $copynumber = $item->{'copynumber'};
213 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
214 foreach (qw(ccode enumchron copynumber itemnotes uri)) {
215 $itemfields{$_} = 1 if ( $item->{$_} );
218 # checking for holds
219 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
220 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
222 if (C4::Context->preference('HidePatronName')){
223 $item->{'hidepatronname'} = 1;
226 if ( defined $reservedate ) {
227 $item->{backgroundcolor} = 'reserved';
228 $item->{reservedate} = format_date($reservedate);
229 $item->{ReservedForBorrowernumber} = $reservedfor;
230 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
231 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
232 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
233 $item->{Reservedcardnumber} = $ItemBorrowerReserveInfo->{'cardnumber'};
236 # Check the transit status
237 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
238 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
239 $item->{transfertwhen} = format_date($transfertwhen);
240 $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
241 $item->{transfertto} = $branches->{$transfertto}{branchname};
242 $item->{nocancel} = 1;
245 # FIXME: move this to a pm, check waiting status for holds
246 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
247 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
248 while (my $wait_hashref = $sth2->fetchrow_hashref) {
249 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
252 # item has a host number if its biblio number does not match the current bib
253 if ($item->{biblionumber} ne $biblionumber){
254 $item->{hostbiblionumber} = $item->{biblionumber};
255 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
258 #count if item is used in analytical bibliorecords
259 my $countanalytics= GetAnalyticsCount($item->{itemnumber});
260 if ($countanalytics > 0){
261 $analytics_flag=1;
262 $item->{countanalytics} = $countanalytics;
264 if ($item->{'materials'} ne ''){
265 $materials_flag = 1;
267 push @itemloop, $item;
270 $template->param( norequests => $norequests );
271 $template->param(
272 MARCNOTES => $marcnotesarray,
273 MARCSUBJCTS => $marcsubjctsarray,
274 MARCAUTHORS => $marcauthorsarray,
275 MARCSERIES => $marcseriesarray,
276 MARCURLS => $marcurlsarray,
277 MARCISBNS => $marcisbnsarray,
278 MARCHOSTS => $marchostsarray,
279 subtitle => $subtitle,
280 itemdata_ccode => $itemfields{ccode},
281 itemdata_enumchron => $itemfields{enumchron},
282 itemdata_uri => $itemfields{uri},
283 itemdata_copynumber => $itemfields{copynumber},
284 volinfo => $itemfields{enumchron},
285 itemdata_itemnotes => $itemfields{itemnotes},
286 z3950_search_params => C4::Search::z3950_search_args($dat),
287 holdcount => $holdcount,
288 hostrecords => $hostrecords,
289 analytics_flag => $analytics_flag,
290 C4::Search::enabled_staff_search_views,
291 materials => $materials_flag,
294 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
295 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
296 my $subfields = substr $fieldspec, 3;
297 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
298 my @alternateholdingsinfo = ();
299 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
301 for my $field (@holdingsfields) {
302 my %holding = ( holding => '' );
303 my $havesubfield = 0;
304 for my $subfield ($field->subfields()) {
305 if ((index $subfields, $$subfield[0]) >= 0) {
306 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
307 $holding{'holding'} .= $$subfield[1];
308 $havesubfield++;
311 if ($havesubfield) {
312 push(@alternateholdingsinfo, \%holding);
316 $template->param(
317 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
321 my @results = ( $dat, );
322 foreach ( keys %{$dat} ) {
323 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
326 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
327 # method query not found?!?!
329 $template->param(
330 itemloop => \@itemloop,
331 biblionumber => $biblionumber,
332 ($analyze? 'analyze':'detailview') =>1,
333 subscriptions => \@subs,
334 subscriptionsnumber => $subscriptionsnumber,
335 subscriptiontitle => $dat->{title},
338 # $debug and $template->param(debug_display => 1);
340 # Lists
342 if (C4::Context->preference("virtualshelves") ) {
343 $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
346 # XISBN Stuff
347 if (C4::Context->preference("FRBRizeEditions")==1) {
348 eval {
349 $template->param(
350 XISBNS => get_xisbns($isbn)
353 if ($@) { warn "XISBN Failed $@"; }
355 if ( C4::Context->preference("AmazonEnabled") == 1 ) {
356 $template->param( AmazonTld => get_amazon_tld() );
357 my $amazon_reviews = C4::Context->preference("AmazonReviews");
358 my $amazon_similars = C4::Context->preference("AmazonSimilarItems");
359 my @services;
360 if ( $amazon_reviews ) {
361 $template->param( AmazonReviews => 1 );
362 push( @services, 'EditorialReview' );
364 if ( $amazon_similars ) {
365 $template->param( AmazonSimilarItems => 1 );
366 push( @services, 'Similarities' );
368 my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
369 if ( $amazon_similars ) {
370 my $similar_products_exist;
371 my @similar_products;
372 for my $similar_product (@{$amazon_details->{Items}->{Item}->[0]->{SimilarProducts}->{SimilarProduct}}) {
373 # do we have any of these isbns in our collection?
374 my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
375 # verify that there is at least one similar item
376 if (scalar(@$similar_biblionumbers)){
377 $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
378 push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} };
381 $template->param( AmazonSimilarItems => $similar_products_exist );
382 $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
384 if ( $amazon_reviews ) {
385 my $item = $amazon_details->{Items}->{Item}->[0];
386 my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
387 #my $customer_reviews = \@{$amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{Review}};
388 #my $average_rating = $amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{AverageRating} || 0;
389 #$template->param( amazon_average_rating => $average_rating * 20 );
390 #$template->param( AMAZON_CUSTOMER_REVIEWS => $customer_reviews );
391 $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
395 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
396 my @images = ListImagesForBiblio($biblionumber);
397 $template->{VARS}->{localimages} = \@images;
400 # Get OPAC URL
401 if (C4::Context->preference('OPACBaseURL')){
402 $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
405 # Displaying tags
407 my $tag_quantity;
408 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
409 $template->param(
410 TagsEnabled => 1,
411 TagsShowOnDetail => $tag_quantity
413 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
414 'sort'=>'-weight', limit=>$tag_quantity}));
417 output_html_with_http_headers $query, $cookie, $template->output;