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