catalogue - exit after redirect in detail.pl and updateitem.pl
[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         exit;
71 }
72
73 #coping with subscriptions
74 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
75 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
76
77 my @subs;
78 foreach my $subscription (@subscriptions) {
79     my %cell;
80     $cell{subscriptionid}    = $subscription->{subscriptionid};
81     $cell{subscriptionnotes} = $subscription->{notes};
82
83     #get the three latest serials.
84     $cell{latestserials} =
85       GetLatestSerials( $subscription->{subscriptionid}, 3 );
86     push @subs, \%cell;
87 }
88 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
89 $dat->{'count'} = @items;
90 my @itemloop;
91 my $norequests = 1;
92 foreach my $item (@items) {
93
94     # can place holds defaults to yes
95     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
96
97     # format some item fields for display
98     $item->{ $item->{'publictype'} } = 1;
99     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
100     $item->{datedue} = format_date($item->{datedue});
101     $item->{datelastseen} = format_date($item->{datelastseen});
102     $item->{onloan} = format_date($item->{onloan});
103     $item->{locationname} = $locations{$item->{location}};
104     # item damaged, lost, withdrawn loops
105     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
106     if ($item->{damaged}) {
107         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
108     }
109
110     # checking for holds
111     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
112     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
113
114     if ( defined $reservedate ) {
115         $item->{backgroundcolor} = 'reserved';
116         $item->{reservedate}     = format_date($reservedate);
117         $item->{ReservedForBorrowernumber}     = $reservedfor;
118         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
119         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
120         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
121     }
122
123         # Check the transit status
124     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
125     if ( $transfertwhen ne '' ) {
126         $item->{transfertwhen} = format_date($transfertwhen);
127         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
128         $item->{transfertto} = $branches->{$transfertto}{branchname};
129         $item->{nocancel} = 1;
130     }
131
132     # FIXME: move this to a pm, check waiting status for holds
133     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
134     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
135     while (my $wait_hashref = $sth2->fetchrow_hashref) {
136         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
137     }
138
139     push @itemloop, $item;
140 }
141
142 $template->param( norequests => $norequests );
143
144 ## get notes and subjects from MARC record
145     my $marcflavour      = C4::Context->preference("marcflavour");
146     my $record           = GetMarcBiblio($biblionumber);
147     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
148     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
149     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
150
151     $template->param(
152         MARCNOTES   => $marcnotesarray,
153         MARCSUBJCTS => $marcsubjctsarray,
154         MARCAUTHORS => $marcauthorsarray
155     );
156
157 my @results = ( $dat, );
158 foreach ( keys %{$dat} ) {
159     $template->param( "$_" => $dat->{$_} . "" );
160 }
161
162 $template->param(
163     itemloop        => \@itemloop,
164     biblionumber        => $biblionumber,
165     detailview => 1,
166     subscriptions       => \@subs,
167     subscriptionsnumber => $subscriptionsnumber,
168     subscriptiontitle   => $dat->{title},
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("FRBRizeEditions")==1) {
176     eval {
177         $template->param(
178             xisbn => $xisbn,
179             XISBNS => get_xisbns($xisbn)
180         );
181     };
182     if ($@) { warn "XISBN Failed $@"; }
183 }
184 if ( C4::Context->preference("AmazonContent") == 1 ) {
185     my $amazon_details = &get_amazon_details( $xisbn );
186     foreach my $result ( @{ $amazon_details->{Details} } ) {
187         $template->param( item_description => $result->{ProductDescription} );
188         $template->param( image            => $result->{ImageUrlMedium} );
189         $template->param( list_price       => $result->{ListPrice} );
190         $template->param( amazon_url       => $result->{url} );
191     }
192
193     my @products;
194     my @reviews;
195     for my $details ( @{ $amazon_details->{Details} } ) {
196
197         next unless $details->{SimilarProducts};
198         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
199             if (C4::Context->preference("AmazonSimilarItems") ) {
200                 my @xisbns;
201                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
202                     @xisbns = @{get_xisbns($product)};
203                 }
204                 else {
205                     push @xisbns, get_biblio_from_xisbn($product);
206                 }
207                 push @products, +{ product => \@xisbns };
208             }
209         }
210         next unless $details->{Reviews};
211         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
212             $template->param( rating => $product * 20 );
213         }
214         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
215             push @reviews,
216               +{
217                 summary => $reviews->{Summary},
218                 comment => $reviews->{Comment},
219               };
220         }
221     }
222     $template->param( SIMILAR_PRODUCTS => \@products );
223     $template->param( AMAZONREVIEWS    => \@reviews );
224 }
225
226 output_html_with_http_headers $query, $cookie, $template->output;