Improving comments style and markup, adding highlighting for comments made by logged...
[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::Dates qw/format_date/;
32 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn);
33 use C4::Amazon;
34 use C4::Review;
35 use C4::Serials;
36 use C4::Members;
37 use C4::XSLT;
38
39 my $query = new CGI;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name   => "opac-detail.tmpl",
43         query           => $query,
44         type            => "opac",
45         authnotrequired => 1,
46         flagsrequired   => { borrow => 1 },
47     }
48 );
49
50 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
51 $template->param( biblionumber => $biblionumber );
52 # XSLT processing of some stuff
53 if (C4::Context->preference("XSLTResultsDisplay") ) {
54     my $newxmlrecord = XSLTParse4Display($biblionumber,'Detail');
55     $template->param('XSLTBloc' => $newxmlrecord);
56 }
57
58 # change back when ive fixed request.pl
59 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
60 my @items;
61 @items = @all_items unless C4::Context->preference('hidelostitems');
62
63 if (C4::Context->preference('hidelostitems')) {
64     # Hide host items
65     for my $itm (@all_items) {
66         push @items, $itm unless $itm->{itemlost};
67     }
68 }
69 my $dat = &GetBiblioData($biblionumber);
70
71 if (!$dat) {
72     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
73     exit;
74 }
75 my $imgdir = getitemtypeimagesrc();
76 my $itemtypes = GetItemTypes();
77 # imageurl:
78 my $itemtype = $dat->{'itemtype'};
79 if ( $itemtype ) {
80     $dat->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
81     $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
82 }
83
84 #coping with subscriptions
85 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
86 my @subscriptions       =
87   GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
88 my @subs;
89 $dat->{'serial'}=1 if $subscriptionsnumber;
90 foreach my $subscription (@subscriptions) {
91     my %cell;
92     $cell{subscriptionid}    = $subscription->{subscriptionid};
93     $cell{subscriptionnotes} = $subscription->{notes};
94     $cell{branchcode}        = $subscription->{branchcode};
95     $cell{hasalert}          = $subscription->{hasalert};
96     #get the three latest serials.
97     $cell{latestserials} =
98       GetLatestSerials( $subscription->{subscriptionid}, 3 );
99     push @subs, \%cell;
100 }
101
102 $dat->{'count'} = scalar(@items);
103
104 #adding RequestOnOpac filter to allow or not the display of plce reserve button
105 # FIXME - use me or delete me.
106 my $RequestOnOpac;
107 if (C4::Context->preference("RequestOnOpac")) {
108     $RequestOnOpac = 1;
109 }
110
111 my $norequests = 1;
112 foreach my $itm (@items) {
113      $norequests = 0 && $norequests
114        if ( (not $itm->{'wthdrawn'} )
115          || (not $itm->{'itemlost'} )
116          || (not $itm->{'itemnotforloan'} )
117          || ($itm->{'itemnumber'} ) );
118         $itm->{ $itm->{'publictype'} } = 1;
119     $itm->{datedue} = format_date($itm->{datedue});
120     $itm->{datelastseen} = format_date($itm->{datelastseen});
121
122     #get collection code description, too
123     $itm->{'ccode'}  = GetAuthorisedValueDesc('','',   $itm->{'ccode'} ,'','','CCODE');
124     $itm->{'location_description'} = GetAuthorisedValueDesc('','',   $itm->{'location'} ,'','','LOC');
125     $itm->{'imageurl'}    = $imgdir."/".$itemtypes->{ $itm->{itype} }->{'imageurl'};     
126     $itm->{'description'} = $itemtypes->{$itemtype}->{'description'};
127
128 }
129
130 $template->param( norequests => $norequests, RequestOnOpac=>$RequestOnOpac );
131
132 ## get notes and subjects from MARC record
133     my $dbh              = C4::Context->dbh;
134     my $marcflavour      = C4::Context->preference("marcflavour");
135     my $record           = GetMarcBiblio($biblionumber);
136     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
137     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
138     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
139     my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
140     my $marcurlsarray   = GetMarcUrls($record,$marcflavour);
141
142     $template->param(
143         MARCNOTES   => $marcnotesarray,
144         MARCSUBJCTS => $marcsubjctsarray,
145         MARCAUTHORS => $marcauthorsarray,
146         MARCSERIES  => $marcseriesarray,
147         MARCURLS    => $marcurlsarray,
148     );
149
150 foreach ( keys %{$dat} ) {
151     $template->param( "$_" => $dat->{$_} . "" );
152 }
153
154 # COinS format FIXME: for books Only
155 my $coins_format;
156 my $fmt = substr $record->leader(), 6,2;
157 my $fmts;
158 $fmts->{'am'} = 'book';
159 $coins_format = $fmts->{$fmt};
160 $template->param(
161         ocoins_format => $coins_format,
162 );
163
164 my $reviews = getreviews( $biblionumber, 1 );
165 my $loggedincommenter;
166 foreach ( @$reviews ) {
167     my $borrower_number_review = $_->{borrowernumber};
168     my $borrowerData           = GetMember($borrower_number_review,'borrowernumber');
169     # setting some borrower info into this hash
170     $_->{title}     = $borrowerData->{'title'};
171     $_->{surname}   = $borrowerData->{'surname'};
172     $_->{firstname} = $borrowerData->{'firstname'};
173     $_->{userid} = $borrowerData->{'userid'};
174     $_->{datereviewed} = format_date($_->{datereviewed});
175     if($borrowerData->{'borrowernumber'} eq $borrowernumber){ $loggedincommenter = 1; }
176 }
177
178
179 if(C4::Context->preference("ISBD")) {
180         $template->param(ISBD => 1);
181 }
182
183 $template->param(
184     ITEM_RESULTS        => \@items,
185     subscriptionsnumber => $subscriptionsnumber,
186     biblionumber        => $biblionumber,
187     subscriptions       => \@subs,
188     subscriptionsnumber => $subscriptionsnumber,
189     reviews             => $reviews,
190     loggedincommenter => $loggedincommenter
191 );
192
193 # XISBN Stuff
194 my $xisbn=$dat->{'isbn'};
195 $xisbn =~ s/(p|-| |:)//g;
196 $template->param(amazonisbn => $xisbn);
197 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
198     eval {
199         $template->param(
200             xisbn => $xisbn,
201             XISBNS => get_xisbns($xisbn)
202         );
203     };
204     if ($@) { warn "XISBN Failed $@"; }
205 }
206 # Amazon.com Stuff
207 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
208     my $similar_products_exist;
209     my $amazon_details = &get_amazon_details( $xisbn );
210     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
211     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
212     for my $one_review (@$customer_reviews) {
213         $one_review->{Date} = format_date($one_review->{Date});
214     }
215     my @similar_products;
216     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
217         # do we have any of these isbns in our collection?
218         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
219         # verify that there is at least one similar item
220         $similar_products_exist++ if ${@$similar_biblionumbers}[0];
221         push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
222     }
223     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
224     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
225     $template->param( OPACAmazonSimilarItems => $similar_products_exist );
226     $template->param( amazon_average_rating => $average_rating * 20);
227     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
228     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
229     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
230 }
231 # Shelf Browser Stuff
232 if (C4::Context->preference("OPACShelfBrowser")) {
233 # pick the first itemnumber unless one was selected by the user
234 my $starting_itemnumber = $query->param('shelfbrowse_itemnumber'); # || $items[0]->{itemnumber};
235 $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
236 # find the right cn_sort value for this item
237 my ($starting_cn_sort, $starting_homebranch, $starting_location);
238 my $sth_get_cn_sort = $dbh->prepare("SELECT cn_sort,homebranch,location from items where itemnumber=?");
239 $sth_get_cn_sort->execute($starting_itemnumber);
240 my $branches = GetBranches();
241 while (my $result = $sth_get_cn_sort->fetchrow_hashref()) {
242     $starting_cn_sort = $result->{'cn_sort'};
243     $starting_homebranch->{code} = $result->{'homebranch'};
244     $starting_homebranch->{description} = $branches->{$result->{'homebranch'}}{branchname};
245     $starting_location->{code} = $result->{'location'};
246     $starting_location->{description} = GetAuthorisedValueDesc('','',   $result->{'location'} ,'','','LOC');
247
248 }
249
250 ## List of Previous Items
251 # order by cn_sort, which should include everything we need for ordering purposes (though not
252 # for limits, those need to be handled separately
253 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");
254 $sth_shelfbrowse_previous->execute($starting_cn_sort.$starting_itemnumber, $starting_homebranch->{code}, $starting_location->{code});
255 my @previous_items;
256 while (my $this_item = $sth_shelfbrowse_previous->fetchrow_hashref()) {
257     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=?");
258     $sth_get_biblio->execute($this_item->{biblionumber});
259     while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
260         $this_item->{'title'} = $this_biblio->{'title'};
261         $this_item->{'isbn'} = $this_biblio->{'isbn'};
262     }
263     unshift @previous_items, $this_item;
264 }
265 my $throwaway = pop @previous_items;
266 ## List of Next Items
267 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");
268 $sth_shelfbrowse_next->execute($starting_cn_sort.$starting_itemnumber, $starting_homebranch->{code}, $starting_location->{code});
269 my @next_items;
270 while (my $this_item = $sth_shelfbrowse_next->fetchrow_hashref()) {
271     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=?");
272     $sth_get_biblio->execute($this_item->{biblionumber});
273     while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
274         $this_item->{'title'} = $this_biblio->{'title'};
275         $this_item->{'isbn'} = $this_biblio->{'isbn'};
276     }
277     push @next_items, $this_item;
278 }
279
280 # alas, these won't auto-vivify, see http://www.perlmonks.org/?node_id=508481
281 my $shelfbrowser_next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
282 my $shelfbrowser_next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
283
284 $template->param(
285     starting_homebranch => $starting_homebranch->{description},
286     starting_location => $starting_location->{description},
287     shelfbrowser_prev_itemnumber => $previous_items[0]->{itemnumber},
288     shelfbrowser_next_itemnumber => $shelfbrowser_next_itemnumber,
289     shelfbrowser_prev_biblionumber => $previous_items[0]->{biblionumber},
290     shelfbrowser_next_biblionumber => $shelfbrowser_next_biblionumber,
291     PREVIOUS_SHELF_BROWSE => \@previous_items,
292     NEXT_SHELF_BROWSE => \@next_items,
293 );
294 }
295
296 output_html_with_http_headers $query, $cookie, $template->output;