help templates and includes - cleanup for XHTML validation, error display
[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_biblio_from_xisbn);
35 use C4::Amazon;
36
37 my $query = new CGI;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {
40         template_name   => "catalogue/detail.tmpl",
41         query           => $query,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => { catalogue => 1 },
45     }
46 );
47
48 my $biblionumber = $query->param('biblionumber');
49 my $fw = GetFrameworkCode($biblionumber);
50
51 # Get Branches, Itemtypes and Locations
52 my $branches = GetBranches();
53 my $itemtypes = GetItemTypes();
54
55 my %locations;
56 # FIXME: move this to a pm, check waiting status for holds
57 my $dbh = C4::Context->dbh;
58 my $lsch = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category = 'LOC'");
59 $lsch->execute();
60 while (my $ldata = $lsch->fetchrow_hashref ) {
61     $locations{ $ldata->{'authorised_value'} } = $ldata->{'lib'};
62 }
63
64 # change back when ive fixed request.pl
65 my @items = &GetItemsInfo( $biblionumber, 'intra' );
66 my $dat = &GetBiblioData($biblionumber);
67
68 if (!$dat) { 
69     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
70 }
71
72 #coping with subscriptions
73 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
74 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
75
76 my @subs;
77 foreach my $subscription (@subscriptions) {
78     my %cell;
79     $cell{subscriptionid}    = $subscription->{subscriptionid};
80     $cell{subscriptionnotes} = $subscription->{notes};
81
82     #get the three latest serials.
83     $cell{latestserials} =
84       GetLatestSerials( $subscription->{subscriptionid}, 3 );
85     push @subs, \%cell;
86 }
87 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
88 $dat->{'count'} = @items;
89 my @itemloop;
90 my $norequests = 1;
91 foreach my $item (@items) {
92
93     # can place holds defaults to yes
94     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
95
96     # format some item fields for display
97     $item->{ $item->{'publictype'} } = 1;
98     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
99     $item->{datedue} = format_date($item->{datedue});
100     $item->{datelastseen} = format_date($item->{datelastseen});
101     $item->{onloan} = format_date($item->{onloan});
102     $item->{locationname} = $locations{$item->{location}};
103     # item damaged, lost, withdrawn loops
104     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
105     if ($item->{damaged}) {
106         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
107     }
108
109     # checking for holds
110     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
111     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
112
113     if ( defined $reservedate ) {
114         $item->{backgroundcolor} = 'reserved';
115         $item->{reservedate}     = format_date($reservedate);
116         $item->{ReservedForBorrowernumber}     = $reservedfor;
117         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
118         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
119         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
120     }
121
122         # Check the transit status
123     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
124     if ( $transfertwhen ne '' ) {
125         $item->{transfertwhen} = format_date($transfertwhen);
126         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
127         $item->{transfertto} = $branches->{$transfertto}{branchname};
128         $item->{nocancel} = 1;
129     }
130
131     # FIXME: move this to a pm, check waiting status for holds
132     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
133     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
134     while (my $wait_hashref = $sth2->fetchrow_hashref) {
135         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
136     }
137
138     push @itemloop, $item;
139 }
140
141 $template->param( norequests => $norequests );
142
143 ## get notes and subjects from MARC record
144     my $marcflavour      = C4::Context->preference("marcflavour");
145     my $record           = GetMarcBiblio($biblionumber);
146     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
147     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
148     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
149
150     $template->param(
151         MARCNOTES   => $marcnotesarray,
152         MARCSUBJCTS => $marcsubjctsarray,
153         MARCAUTHORS => $marcauthorsarray
154     );
155
156 my @results = ( $dat, );
157 foreach ( keys %{$dat} ) {
158     $template->param( "$_" => $dat->{$_} . "" );
159 }
160
161 $template->param(
162     itemloop        => \@itemloop,
163     biblionumber        => $biblionumber,
164     detailview => 1,
165     subscriptions       => \@subs,
166     subscriptionsnumber => $subscriptionsnumber,
167     subscriptiontitle   => $dat->{title},
168 );
169
170 # XISBN Stuff
171 my $xisbn=$dat->{'isbn'};
172 $xisbn =~ s/(p|-| |:)//g;
173 $template->param(amazonisbn => $xisbn);
174 if (C4::Context->preference("FRBRizeEditions")==1) {
175     eval {
176         $template->param(
177             xisbn => $xisbn,
178             XISBNS => get_xisbns($xisbn)
179         );
180     };
181     if ($@) { warn "XISBN Failed $@"; }
182 }
183 if ( C4::Context->preference("AmazonContent") == 1 ) {
184     my $amazon_details = &get_amazon_details( $xisbn );
185     foreach my $result ( @{ $amazon_details->{Details} } ) {
186         $template->param( item_description => $result->{ProductDescription} );
187         $template->param( image            => $result->{ImageUrlMedium} );
188         $template->param( list_price       => $result->{ListPrice} );
189         $template->param( amazon_url       => $result->{url} );
190     }
191
192     my @products;
193     my @reviews;
194     for my $details ( @{ $amazon_details->{Details} } ) {
195
196         next unless $details->{SimilarProducts};
197         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
198             if (C4::Context->preference("AmazonSimilarItems") ) {
199                 my @xisbns;
200                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
201                     @xisbns = @{get_xisbns($product)};
202                 }
203                 else {
204                     push @xisbns, get_biblio_from_xisbn($product);
205                 }
206                 push @products, +{ product => \@xisbns };
207             }
208         }
209         next unless $details->{Reviews};
210         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
211             $template->param( rating => $product * 20 );
212         }
213         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
214             push @reviews,
215               +{
216                 summary => $reviews->{Summary},
217                 comment => $reviews->{Comment},
218               };
219         }
220     }
221     $template->param( SIMILAR_PRODUCTS => \@products );
222     $template->param( AMAZONREVIEWS    => \@reviews );
223 }
224
225 output_html_with_http_headers $query, $cookie, $template->output;