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