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