91aa87d9d8ec315bf691e23693c470d89e4b3f3e
[koha.git] / opac / opac-detail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 use strict;
22 require Exporter;
23 use CGI;
24 use C4::Auth;
25 use C4::Branch;
26 use C4::Koha;
27 use C4::Serials;    #uses getsubscriptionfrom biblionumber
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Tags qw(get_tags);
32 use C4::Dates qw/format_date/;
33 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn);
34 use C4::Amazon;
35 use C4::Review;
36 use C4::Serials;
37 use C4::Members;
38 use C4::XSLT;
39
40 BEGIN {
41         if (C4::Context->preference('BakerTaylorEnabled')) {
42                 require C4::External::BakerTaylor;
43                 import C4::External::BakerTaylor qw(&image_url &link_url);
44         }
45 }
46
47 my $query = new CGI;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49     {
50         template_name   => "opac-detail.tmpl",
51         query           => $query,
52         type            => "opac",
53         authnotrequired => 1,
54         flagsrequired   => { borrow => 1 },
55     }
56 );
57
58 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
59 $template->param( biblionumber => $biblionumber );
60 # XSLT processing of some stuff
61 if (C4::Context->preference("XSLTResultsDisplay") ) {
62     my $newxmlrecord = XSLTParse4Display($biblionumber,C4::Context->config('opachtdocs')."/prog/en/xslt/MARC21slim2OPACDetail.xsl");
63     $template->param('XSLTBloc' => $newxmlrecord);
64 }
65
66 # change back when ive fixed request.pl
67 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
68 my @items;
69 @items = @all_items unless C4::Context->preference('hidelostitems');
70
71 if (C4::Context->preference('hidelostitems')) {
72     # Hide host items
73     for my $itm (@all_items) {
74         push @items, $itm unless $itm->{itemlost};
75     }
76 }
77 my $dat = &GetBiblioData($biblionumber);
78
79 if (!$dat) {
80     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
81     exit;
82 }
83 my $imgdir = getitemtypeimagesrc();
84 my $itemtypes = GetItemTypes();
85 # imageurl:
86 my $itemtype = $dat->{'itemtype'};
87 if ( $itemtype ) {
88     $dat->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
89     $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
90 }
91 my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'});
92 my $collections =  GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'} );
93
94 #coping with subscriptions
95 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
96 my @subscriptions       =
97   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
98 my @subs;
99 $dat->{'serial'}=1 if $subscriptionsnumber;
100 foreach my $subscription (@subscriptions) {
101     my %cell;
102     $cell{subscriptionid}    = $subscription->{subscriptionid};
103     $cell{subscriptionnotes} = $subscription->{notes};
104     $cell{branchcode}        = $subscription->{branchcode};
105     $cell{hasalert}          = $subscription->{hasalert};
106     #get the three latest serials.
107     $cell{latestserials} =
108       GetLatestSerials( $subscription->{subscriptionid}, 3 );
109     push @subs, \%cell;
110 }
111
112 $dat->{'count'} = scalar(@items);
113
114 #adding RequestOnOpac filter to allow or not the display of plce reserve button
115 # FIXME - use me or delete me.
116 my $RequestOnOpac;
117 if (C4::Context->preference("RequestOnOpac")) {
118     $RequestOnOpac = 1;
119 }
120
121 my $norequests = 1;
122 my %itemfields;
123 for my $itm (@items) {
124      $norequests = 0 && $norequests
125        if ( (not $itm->{'wthdrawn'} )
126          || (not $itm->{'itemlost'} )
127          || (not $itm->{'itemnotforloan'} )
128          || ($itm->{'itemnumber'} ) );
129         $itm->{ $itm->{'publictype'} } = 1;
130     $itm->{datedue} = format_date($itm->{datedue});
131     $itm->{datelastseen} = format_date($itm->{datelastseen});
132
133     #get collection code description, too
134         my $ccode= $itm->{'ccode'};
135         $itm->{'ccode'} = $collections->{$ccode} if(defined($collections) && exists($collections->{$ccode}));
136     $itm->{'location_description'} = $shelflocations->{$itm->{'location'} };
137     $itm->{'imageurl'}    = $imgdir."/".$itemtypes->{ $itm->{itype} }->{'imageurl'};     
138     $itm->{'description'} = $itemtypes->{$itemtype}->{'description'};
139         $itemfields{ccode} = 1 if($itm->{ccode});
140         $itemfields{enumchron} = 1 if($itm->{enumchron});
141         $itemfields{copynumber} = 1 if($itm->{copynumber});
142 }
143
144 ## get notes and subjects from MARC record
145     my $dbh              = C4::Context->dbh;
146     my $marcflavour      = C4::Context->preference("marcflavour");
147     my $record           = GetMarcBiblio($biblionumber);
148     my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
149     my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
150     my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
151     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
152     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
153
154     $template->param(
155         MARCNOTES   => $marcnotesarray,
156         MARCSUBJCTS => $marcsubjctsarray,
157         MARCAUTHORS => $marcauthorsarray,
158         MARCSERIES  => $marcseriesarray,
159         MARCURLS    => $marcurlsarray,
160                 norequests => $norequests,
161                 RequestOnOpac=>$RequestOnOpac,
162                 itemdata_ccode => $itemfields{ccode},
163                 itemdata_enumchron => $itemfields{enumchron},
164                 itemdata_copynumber => $itemfields{copynumber},
165     );
166
167 foreach ( keys %{$dat} ) {
168     $template->param( "$_" => $dat->{$_} . "" );
169 }
170
171 # COinS format FIXME: for books Only
172 my $coins_format;
173 my $fmt = substr $record->leader(), 6,2;
174 my $fmts;
175 $fmts->{'am'} = 'book';
176 $coins_format = $fmts->{$fmt};
177 $template->param(
178         ocoins_format => $coins_format,
179 );
180
181 my $reviews = getreviews( $biblionumber, 1 );
182 my $loggedincommenter;
183 foreach ( @$reviews ) {
184     my $borrower_number_review = $_->{borrowernumber};
185     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
186     # setting some borrower info into this hash
187     $_->{title}     = $borrowerData->{'title'};
188     $_->{surname}   = $borrowerData->{'surname'};
189     $_->{firstname} = $borrowerData->{'firstname'};
190     $_->{userid} = $borrowerData->{'userid'};
191     $_->{datereviewed} = format_date($_->{datereviewed});
192     if($borrowerData->{'borrowernumber'} eq $borrowernumber){ $loggedincommenter = 1; }
193 }
194
195
196 if(C4::Context->preference("ISBD")) {
197         $template->param(ISBD => 1);
198 }
199
200 $template->param(
201     ITEM_RESULTS        => \@items,
202     subscriptionsnumber => $subscriptionsnumber,
203     biblionumber        => $biblionumber,
204     subscriptions       => \@subs,
205     subscriptionsnumber => $subscriptionsnumber,
206     reviews             => $reviews,
207     loggedincommenter => $loggedincommenter
208 );
209
210 # XISBN Stuff
211 my $xisbn=$dat->{'isbn'};
212 $xisbn =~ /(\d*[X]*)/;
213 $template->param(amazonisbn => $1);             # FIXME: so it is OK if the ISBN = 'XXXXX' ?
214 my ($clean, $amazonisbn);
215 $amazonisbn = $1;
216 # these might be overkill, but they are better than the regexp above.
217 if (
218         $amazonisbn =~ /\b(\d{13})\b/ or
219         $amazonisbn =~ /\b(\d{10})\b/ or 
220         $amazonisbn =~ /\b(\d{9}X)\b/i
221 ) {
222         $clean = $1;
223         $template->param(clean_isbn => $1);
224 }
225
226 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
227     eval {
228         $template->param(
229             xisbn => $xisbn,
230             XISBNS => get_xisbns($xisbn)
231         );
232     };
233     if ($@) { warn "XISBN Failed $@"; }
234 }
235 # Amazon.com Stuff
236 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
237     my $similar_products_exist;
238     my $amazon_details = &get_amazon_details( $xisbn );
239     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
240     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
241     for my $one_review (@$customer_reviews) {
242         $one_review->{Date} = format_date($one_review->{Date});
243     }
244     my @similar_products;
245     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
246         # do we have any of these isbns in our collection?
247         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
248         # verify that there is at least one similar item
249         $similar_products_exist++ if ${@$similar_biblionumbers}[0];
250         push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
251     }
252     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
253     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
254     $template->param( OPACAmazonSimilarItems => $similar_products_exist );
255     $template->param( amazon_average_rating => $average_rating * 20);
256     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
257     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
258     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
259 }
260 # Shelf Browser Stuff
261 if (C4::Context->preference("OPACShelfBrowser")) {
262 # pick the first itemnumber unless one was selected by the user
263 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber'); # || $items[0]->{itemnumber};
264 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
265 # find the right cn_sort value for this item
266 my ($starting_cn_sort, $starting_homebranch, $starting_location);
267 my $sth_get_cn_sort = $dbh->prepare("SELECT cn_sort,homebranch,location from items where itemnumber=?");
268 $sth_get_cn_sort->execute($starting_itemnumber);
269 my $branches = GetBranches();
270 while (my $result = $sth_get_cn_sort->fetchrow_hashref()) {
271     $starting_cn_sort = $result->{'cn_sort'};
272     $starting_homebranch->{code} = $result->{'homebranch'};
273     $starting_homebranch->{description} = $branches->{$result->{'homebranch'}}{branchname};
274     $starting_location->{code} = $result->{'location'};
275     $starting_location->{description} = GetAuthorisedValueDesc('','',   $result->{'location'} ,'','','LOC');
276
277 }
278
279 ## List of Previous Items
280 # order by cn_sort, which should include everything we need for ordering purposes (though not
281 # for limits, those need to be handled separately
282 my $sth_shelfbrowse_previous = $dbh->prepare("SELECT * FROM items WHERE CONCAT(cn_sort,itemnumber) <= ? AND homebranch=? AND location=? ORDER BY CONCAT(cn_sort,itemnumber) DESC LIMIT 3");
283 $sth_shelfbrowse_previous->execute($starting_cn_sort.$starting_itemnumber, $starting_homebranch->{code}, $starting_location->{code});
284 my @previous_items;
285 while (my $this_item = $sth_shelfbrowse_previous->fetchrow_hashref()) {
286     my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
287     $sth_get_biblio->execute($this_item->{biblionumber});
288     while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
289         $this_item->{'title'} = $this_biblio->{'title'};
290         $this_item->{'isbn'} = $this_biblio->{'isbn'};
291     }
292     unshift @previous_items, $this_item;
293 }
294 my $throwaway = pop @previous_items;
295 ## List of Next Items
296 my $sth_shelfbrowse_next = $dbh->prepare("SELECT * FROM items WHERE CONCAT(cn_sort,itemnumber) >= ? AND homebranch=? AND location=? ORDER BY CONCAT(cn_sort,itemnumber) ASC LIMIT 3");
297 $sth_shelfbrowse_next->execute($starting_cn_sort.$starting_itemnumber, $starting_homebranch->{code}, $starting_location->{code});
298 my @next_items;
299 while (my $this_item = $sth_shelfbrowse_next->fetchrow_hashref()) {
300     my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
301     $sth_get_biblio->execute($this_item->{biblionumber});
302     while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
303         $this_item->{'title'} = $this_biblio->{'title'};
304         $this_item->{'isbn'} = $this_biblio->{'isbn'};
305     }
306     push @next_items, $this_item;
307 }
308
309 # alas, these won't auto-vivify, see http://www.perlmonks.org/?node_id=508481
310 my $shelfbrowser_next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
311 my $shelfbrowser_next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
312
313 $template->param(
314     starting_homebranch => $starting_homebranch->{description},
315     starting_location => $starting_location->{description},
316     shelfbrowser_prev_itemnumber => $previous_items[0]->{itemnumber},
317     shelfbrowser_next_itemnumber => $shelfbrowser_next_itemnumber,
318     shelfbrowser_prev_biblionumber => $previous_items[0]->{biblionumber},
319     shelfbrowser_next_biblionumber => $shelfbrowser_next_biblionumber,
320     PREVIOUS_SHELF_BROWSE => \@previous_items,
321     NEXT_SHELF_BROWSE => \@next_items,
322 );
323 }
324
325 if (C4::Context->preference("BakerTaylorEnabled")) {
326         $template->param(
327                 BakerTaylorEnabled  => 1,
328                 BakerTaylorImageURL => &image_url(),
329                 BakerTaylorLinkURL  => &link_url(),
330                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
331         );
332         my ($bt_user, $bt_pass);
333         if ($clean and
334                 $bt_user = C4::Context->preference('BakerTaylorUsername') and
335                 $bt_pass = C4::Context->preference('BakerTaylorPassword')    )
336         {
337                 $template->param(
338                 BakerTaylorContentURL   =>
339                 sprintf("http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
340                                 $bt_user,$bt_pass,$clean)
341                 );
342         }
343 }
344
345 my $tag_quantity;
346 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
347         $template->param(
348                 TagsEnabled => 1,
349                 TagsShowOnDetail => $tag_quantity,
350                 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
351         );
352         $template->param(TagLoop => get_tags({biblionumber=>$biblionumber,
353                                                                 'sort'=>'-weight', limit=>$tag_quantity}));
354 }
355
356 output_html_with_http_headers $query, $cookie, $template->output;