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