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