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