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