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