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