fix for bug 1771: Template errors with remote itemtype image
[koha.git] / opac / opac-user.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
21 use CGI;
22
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Members;
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Dates qw/format_date/;
32 use C4::Letters;
33 use C4::Branch; # GetBranches
34
35 my $query = new CGI;
36
37 BEGIN {
38         if (C4::Context->preference('BakerTaylorEnabled')) {
39                 require C4::External::BakerTaylor;
40                 import C4::External::BakerTaylor qw(&image_url &link_url);
41         }
42 }
43
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45     {
46         template_name   => "opac-user.tmpl",
47         query           => $query,
48         type            => "opac",
49         authnotrequired => 0,
50         flagsrequired   => { borrow => 1 },
51         debug           => 1,
52     }
53 );
54
55 my $patronupdate = $query->param('patronupdate');
56
57 # get borrower information ....
58 my ( $borr ) = GetMemberDetails( $borrowernumber );
59
60 for (qw(dateenrolled dateexpiry dateofbirth)) {
61     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
62 }
63 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
64
65 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
66     $borr->{'flagged'} = 1;
67 }
68 # $make flagged available everywhere in the template
69 my $patron_flagged = $borr->{'flagged'};
70 if ( $borr->{'amountoutstanding'} > 5 ) {
71     $borr->{'amountoverfive'} = 1;
72 }
73 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
74     $borr->{'amountoverzero'} = 1;
75 }
76 if ( $borr->{'amountoutstanding'} < 0 ) {
77     $borr->{'amountlessthanzero'} = 1;
78     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
79 }
80
81 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
82
83 my @bordat;
84 $bordat[0] = $borr;
85
86 $template->param(   BORROWER_INFO  => \@bordat,
87                     borrowernumber => $borrowernumber,
88                     patron_flagged => $patron_flagged,
89                 );
90
91 #get issued items ....
92 my ($issues) = GetPendingIssues($borrowernumber);
93 my @issue_list = sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues;
94
95 my $count          = 0;
96 my $toggle = 0;
97 my $overdues_count = 0;
98 my @overdues;
99 my @issuedat;
100 my $itemtypes = GetItemTypes();
101 foreach my $issue ( @issue_list ) {
102         if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
103     # check for reserves
104     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
105     if ( $restype ) {
106         $issue->{'reserved'} = 1;
107     }
108     
109     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
110     my $charges = 0;
111     foreach my $ac (@$accts) {
112         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
113             $charges += $ac->{'amountoutstanding'}
114               if $ac->{'accounttype'} eq 'F';
115             $charges += $ac->{'amountoutstanding'}
116               if $ac->{'accounttype'} eq 'L';
117         }
118     }
119     $issue->{'charges'} = $charges;
120
121     # get publictype for icon
122
123     my $publictype = $issue->{'publictype'};
124     $issue->{$publictype} = 1;
125
126     # check if item is renewable
127     my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
128     ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
129     $issue->{'status'} = $status && C4::Context->preference("OpacRenewalAllowed");
130     $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
131     $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
132
133     if ( $issue->{'overdue'} ) {
134         push @overdues, $issue;
135         $overdues_count++;
136         $issue->{'overdue'} = 1;
137     }
138     else {
139         $issue->{'issued'} = 1;
140     }
141     # imageurl:
142     my $itemtype = $issue->{'itemtype'};
143     if ( $itemtype ) {
144         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
145         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
146     }
147     $issue->{date_due} = format_date($issue->{date_due});
148     push @issuedat, $issue;
149     $count++;
150         
151                 # XISBN Stuff
152         my $xisbn=$issue->{'isbn'};
153         $xisbn =~ /(\d*[X]*)/;
154         $issue->{amazonisbn} = $1;              # FIXME: so it is OK if the ISBN = 'XXXXX' ?
155         my ($clean, $amazonisbn);
156         $amazonisbn = $1;
157         # these might be overkill, but they are better than the regexp above.
158         if (
159                 $amazonisbn =~ /\b(\d{13})\b/ or
160                 $amazonisbn =~ /\b(\d{10})\b/ or 
161                 $amazonisbn =~ /\b(\d{9}X)\b/i
162         ) {
163                 $clean = $1;
164                 $issue->{clean_isbn} = $1;
165         }
166         
167 }
168
169 $template->param( ISSUES       => \@issuedat );
170 $template->param( issues_count => $count );
171
172 $template->param( OVERDUES       => \@overdues );
173 $template->param( overdues_count => $overdues_count );
174
175 # load the branches
176 my $branches = GetBranches();
177 my @branch_loop;
178 for my $branch_hash (sort keys %$branches ) {
179     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
180     push @branch_loop,
181       {
182         value      => "branch: $branch_hash",
183         branchname => $branches->{$branch_hash}->{'branchname'},
184         selected => $selected
185       };
186 }
187 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
188
189 # now the reserved items....
190 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
191 foreach my $res (@reserves) {
192     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
193     my $publictype = $res->{'publictype'};
194     $res->{$publictype} = 1;
195     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
196     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
197     my $biblioData = GetBiblioData($res->{'biblionumber'});
198     $res->{'reserves_title'} = $biblioData->{'title'};
199 }
200
201 # use Data::Dumper;
202 # warn Dumper(@reserves);
203
204 $template->param( RESERVES       => \@reserves );
205 $template->param( reserves_count => $#reserves+1 );
206
207 my @waiting;
208 my $wcount = 0;
209 foreach my $res (@reserves) {
210     if ( $res->{'itemnumber'} ) {
211         my $item = GetItem( $res->{'itemnumber'});
212         $res->{'holdingbranch'} =
213           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
214         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
215         # get document reserve status
216         my $biblioData = GetBiblioData($res->{'biblionumber'});
217         $res->{'waiting_title'} = $biblioData->{'title'};
218         if ( ( $res->{'found'} eq 'W' ) ) {
219             my $item = $res->{'itemnumber'};
220             $item = GetBiblioFromItemNumber($item,undef);
221             $res->{'wait'}= 1; 
222             $res->{'holdingbranch'}=$item->{'holdingbranch'};
223             $res->{'biblionumber'}=$item->{'biblionumber'};
224             $res->{'barcodenumber'}     = $item->{'barcode'};
225             $res->{'wbrcode'} = $res->{'branchcode'};
226             $res->{'itemnumber'}        = $res->{'itemnumber'};
227             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
228             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
229                 $res->{'atdestination'} = 1;
230             }
231             # set found to 1 if reserve is waiting for patron pickup
232             $res->{'found'} = 1 if $res->{'found'} eq 'W';
233         } else {
234             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
235             if ($transfertwhen) {
236                 $res->{intransit} = 1;
237                 $res->{datesent}   = format_date($transfertwhen);
238                 $res->{frombranch} = GetBranchName($transfertfrom);
239             }
240         }
241         push @waiting, $res;
242         $wcount++;
243     }
244 }
245
246 $template->param( WAITING => \@waiting );
247
248 # current alert subscriptions
249 my $alerts = getalert($borrowernumber);
250 foreach ( @$alerts ) {
251     $_->{ $_->{type} } = 1;
252     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
253 }
254
255 if (C4::Context->preference('BakerTaylorEnabled')) {
256         $template->param(
257                 BakerTaylorEnabled  => 1,
258                 BakerTaylorImageURL => &image_url(),
259                 BakerTaylorLinkURL  => &link_url(),
260                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
261         );
262 }
263
264 if (C4::Context->preference("AmazonContent"     ) or 
265         C4::Context->preference("GoogleJackets"     ) or
266         C4::Context->preference("BakerTaylorEnabled")   ) {
267                 $template->param(JacketImages=>1);
268 }
269
270 $template->param(
271     waiting_count      => $wcount,
272     textmessaging      => $borr->{textmessaging},
273         patronupdate => $patronupdate,
274         OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
275         userview => 1,
276         dateformat    => C4::Context->preference("dateformat"),
277 );
278
279 output_html_with_http_headers $query, $cookie, $template->output;
280