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