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