Adding TagsEnabled to list of known sysprefs, adding 'my tabs'
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 require Exporter;
21 use CGI;
22 use C4::Auth;
23 use C4::Date qw/format_date/;
24 use C4::Koha;
25 use C4::Serials;    #uses getsubscriptionfrom biblionumber
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Circulation;
30 use C4::Branch;
31 use C4::Reserves;
32 use C4::Members;
33 use C4::Serials;
34 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn);
35 use C4::Amazon;
36
37 # use Smart::Comments;
38
39 my $query = new CGI;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name   => "catalogue/detail.tmpl",
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { catalogue => 1 },
47     }
48 );
49
50 my $biblionumber = $query->param('biblionumber');
51 my $fw = GetFrameworkCode($biblionumber);
52
53 ## get notes and subjects from MARC record
54 my $marcflavour      = C4::Context->preference("marcflavour");
55 my $record           = GetMarcBiblio($biblionumber);
56 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
57 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
58 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
59 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
60
61 # Get Branches, Itemtypes and Locations
62 my $branches = GetBranches();
63 my $itemtypes = GetItemTypes();
64
65 # FIXME: move this to a pm, check waiting status for holds
66 my $dbh = C4::Context->dbh;
67
68 # change back when ive fixed request.pl
69 my @items = &GetItemsInfo( $biblionumber, 'intra' );
70 my $dat = &GetBiblioData($biblionumber);
71
72 if (!$dat) { 
73     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
74         exit;
75 }
76
77 #coping with subscriptions
78 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
79 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
80
81
82
83 my @subs;
84 foreach my $subscription (@subscriptions) {
85     my %cell;
86     $cell{subscriptionid}    = $subscription->{subscriptionid};
87     $cell{subscriptionnotes} = $subscription->{notes};
88
89     #get the three latest serials.
90     $cell{latestserials} =
91       GetLatestSerials( $subscription->{subscriptionid}, 3 );
92     push @subs, \%cell;
93 }
94 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
95 $dat->{'count'} = @items;
96 my @itemloop;
97 my $norequests = 1;
98 foreach my $item (@items) {
99
100     # can place holds defaults to yes
101     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
102
103     # format some item fields for display
104     $item->{ $item->{'publictype'} } = 1;
105     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
106     $item->{datedue} = format_date($item->{datedue});
107     $item->{datelastseen} = format_date($item->{datelastseen});
108     $item->{onloan} = format_date($item->{onloan});
109     # item damaged, lost, withdrawn loops
110     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
111     if ($item->{damaged}) {
112         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
113     }
114     #get shelf location and collection code description if they are authorised value.
115         my $shelflocations = GetKohaAuthorisedValues('items.location',$fw );
116         my $shelfcode= $item->{'location'};
117         $item->{'location'} = $shelflocations->{$shelfcode} if(defined($shelflocations) && exists($shelflocations->{$shelfcode})); 
118     my $collections =  GetKohaAuthorisedValues('items.ccode',$fw );
119         my $ccode= $item->{'ccode'};
120         $item->{'ccode'} = $collections->{$ccode} if(defined($collections) && exists($collections->{$ccode})); 
121
122     # checking for holds
123     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
124     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
125
126     if ( defined $reservedate ) {
127         $item->{backgroundcolor} = 'reserved';
128         $item->{reservedate}     = format_date($reservedate);
129         $item->{ReservedForBorrowernumber}     = $reservedfor;
130         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
131         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
132         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
133     }
134
135         # Check the transit status
136     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
137     if ( $transfertwhen ne '' ) {
138         $item->{transfertwhen} = format_date($transfertwhen);
139         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
140         $item->{transfertto} = $branches->{$transfertto}{branchname};
141         $item->{nocancel} = 1;
142     }
143
144     # FIXME: move this to a pm, check waiting status for holds
145     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
146     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
147     while (my $wait_hashref = $sth2->fetchrow_hashref) {
148         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
149     }
150
151     push @itemloop, $item;
152 }
153
154 $template->param( norequests => $norequests );
155
156     $template->param(
157         MARCNOTES   => $marcnotesarray,
158         MARCSUBJCTS => $marcsubjctsarray,
159         MARCAUTHORS => $marcauthorsarray,
160         MARCSERIES  => $marcseriesarray
161     );
162
163 my @results = ( $dat, );
164 foreach ( keys %{$dat} ) {
165     $template->param( "$_" => $dat->{$_} . "" );
166 }
167
168 $template->param(
169     itemloop        => \@itemloop,
170     biblionumber        => $biblionumber,
171     detailview => 1,
172     subscriptions       => \@subs,
173     subscriptionsnumber => $subscriptionsnumber,
174     subscriptiontitle   => $dat->{title},
175 );
176
177 # XISBN Stuff
178 my $xisbn=$dat->{'isbn'};
179 $xisbn =~ s/(p|-| |:)//g;
180 $template->param(amazonisbn => $xisbn);
181 if (C4::Context->preference("FRBRizeEditions")==1) {
182     eval {
183         $template->param(
184             xisbn => $xisbn,
185             XISBNS => get_xisbns($xisbn)
186         );
187     };
188     if ($@) { warn "XISBN Failed $@"; }
189 }
190 if ( C4::Context->preference("AmazonContent") == 1 ) {
191     my $similar_products_exist;
192     my $amazon_details = &get_amazon_details( $xisbn );
193     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
194     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
195     my @similar_products;
196     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
197         # do we have any of these isbns in our collection?
198         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
199         # verify that there is at least one similar item
200         $similar_products_exist++ if ${@$similar_biblionumbers}[0];
201         push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
202     }
203     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
204     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
205     $template->param( AmazonSimilarItems => $similar_products_exist );
206     $template->param( amazon_average_rating => $average_rating * 20);
207     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
208     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
209     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
210 }
211 output_html_with_http_headers $query, $cookie, $template->output;