BUG8446: Adds Shibboleth authentication
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI;
24
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reserves;
29 use C4::Members;
30 use C4::Members::AttributeTypes;
31 use C4::Members::Attributes qw/GetBorrowerAttributeValue/;
32 use C4::Output;
33 use C4::Biblio;
34 use C4::Items;
35 use C4::Letters;
36 use C4::Branch; # GetBranches
37 use Koha::DateUtils;
38
39 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
40
41 use Date::Calc qw(
42   Today
43   Add_Delta_Days
44   Date_to_Days
45 );
46
47 my $query = new CGI;
48
49 BEGIN {
50     if (C4::Context->preference('BakerTaylorEnabled')) {
51         require C4::External::BakerTaylor;
52         import C4::External::BakerTaylor qw(&image_url &link_url);
53     }
54 }
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "opac-user.tt",
59         query           => $query,
60         type            => "opac",
61         authnotrequired => 0,
62         flagsrequired   => { borrow => 1 },
63         debug           => 1,
64     }
65 );
66
67 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
68
69 my $show_priority;
70 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
71     m/priority/ and $show_priority = 1;
72 }
73
74 my $patronupdate = $query->param('patronupdate');
75 my $canrenew = 1;
76
77 my $shibbolethAuthentication = C4::Context->preference('shibbolethAuthentication');
78 $template->param( shibbolethAuthentication => $shibbolethAuthentication );
79
80 # get borrower information ....
81 my ( $borr ) = GetMemberDetails( $borrowernumber );
82
83 my (  $today_year,   $today_month,   $today_day) = Today();
84 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
85
86 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
87
88 my $debar = $borr->{'debarred'};
89 my $userdebarred;
90
91 if ($debar) {
92     $userdebarred = 1;
93     $template->param( 'userdebarred' => $userdebarred );
94     if ( $debar ne "9999-12-31" ) {
95         $borr->{'userdebarreddate'} = $debar;
96     }
97 }
98
99 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
100     $borr->{'flagged'} = 1;
101     $canrenew = 0;
102 }
103
104 if ( $borr->{'amountoutstanding'} > 5 ) {
105     $borr->{'amountoverfive'} = 1;
106 }
107 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
108     $borr->{'amountoverzero'} = 1;
109 }
110 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
111 $no_renewal_amt ||= 0;
112
113 if (  C4::Context->preference( 'OpacRenewalAllowed' ) && $borr->{amountoutstanding} > $no_renewal_amt ) {
114     $borr->{'flagged'} = 1;
115     $canrenew = 0;
116     $template->param(
117         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
118         renewal_blocked_fines_amountoutstanding => sprintf( '%.02f', $borr->{amountoutstanding} ),
119     );
120 }
121
122 if ( $borr->{'amountoutstanding'} < 0 ) {
123     $borr->{'amountlessthanzero'} = 1;
124     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
125 }
126
127 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
128
129 my @bordat;
130 $bordat[0] = $borr;
131
132 # Warningdate is the date that the warning starts appearing
133 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
134     my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
135     if ( $days_to_expiry < 0 ) {
136         #borrower card has expired, warn the borrower
137         $borr->{'warnexpired'} = $borr->{'dateexpiry'};
138     } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
139         # borrower card soon to expire, warn the borrower
140         $borr->{'warndeparture'} = $borr->{dateexpiry};
141         if (C4::Context->preference('ReturnBeforeExpiry')){
142             $borr->{'returnbeforeexpiry'} = 1;
143         }
144     }
145 }
146
147 # pass on any renew errors to the template for displaying
148 my $renew_error = $query->param('renew_error');
149
150 $template->param(   BORROWER_INFO     => \@bordat,
151                     borrowernumber    => $borrowernumber,
152                     patron_flagged    => $borr->{flagged},
153                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
154                     surname           => $borr->{surname},
155                     showname          => $borr->{showname},
156                     RENEW_ERROR       => $renew_error,
157                     borrower          => $borr,
158                 );
159
160 #get issued items ....
161
162 my $count          = 0;
163 my $overdues_count = 0;
164 my @overdues;
165 my @issuedat;
166 my $itemtypes = GetItemTypes();
167 my $issues = GetPendingIssues($borrowernumber);
168 if ($issues){
169     foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
170         # check for reserves
171         my $restype = GetReserveStatus( $issue->{'itemnumber'} );
172         if ( $restype ) {
173             $issue->{'reserved'} = 1;
174         }
175
176         my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
177         my $charges = 0;
178         foreach my $ac (@$accts) {
179             if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
180                 $charges += $ac->{'amountoutstanding'}
181                   if $ac->{'accounttype'} eq 'F';
182                 $charges += $ac->{'amountoutstanding'}
183                   if $ac->{'accounttype'} eq 'FU';
184                 $charges += $ac->{'amountoutstanding'}
185                   if $ac->{'accounttype'} eq 'L';
186             }
187         }
188         $issue->{'charges'} = $charges;
189         $issue->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($issue->{'biblionumber'}), GetFrameworkCode($issue->{'biblionumber'}));
190         # check if item is renewable
191         my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
192         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
193         if($status && C4::Context->preference("OpacRenewalAllowed")){
194             $issue->{'status'} = $status;
195         }
196
197         $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
198
199         if ($renewerror) {
200             $issue->{'too_many'}       = 1 if $renewerror eq 'too_many';
201             $issue->{'on_reserve'}     = 1 if $renewerror eq 'on_reserve';
202             $issue->{'auto_renew'}     = 1 if $renewerror eq 'auto_renew';
203             $issue->{'auto_too_soon'}  = 1 if $renewerror eq 'auto_too_soon';
204
205             if ( $renewerror eq 'too_soon' ) {
206                 $issue->{'too_soon'}         = 1;
207                 $issue->{'soonestrenewdate'} = output_pref(
208                     C4::Circulation::GetSoonestRenewDate(
209                         $issue->{borrowernumber},
210                         $issue->{itemnumber}
211                     )
212                 );
213             }
214         }
215
216         if ( $issue->{'overdue'} ) {
217             push @overdues, $issue;
218             $overdues_count++;
219             $issue->{'overdue'} = 1;
220         }
221         else {
222             $issue->{'issued'} = 1;
223         }
224         # imageurl:
225         my $itemtype = $issue->{'itemtype'};
226         if ( $itemtype ) {
227             $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
228             $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
229         }
230         push @issuedat, $issue;
231         $count++;
232
233         my $isbn = GetNormalizedISBN($issue->{'isbn'});
234         $issue->{normalized_isbn} = $isbn;
235
236                 # My Summary HTML
237                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
238                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
239                     $issue->{title} =~ s/\/+$//; # remove trailing slash
240                     $issue->{title} =~ s/\s+$//; # remove trailing space
241                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
242                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
243                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
244                     $issue->{MySummaryHTML} = $my_summary_html;
245                 }
246     }
247 }
248 $template->param( ISSUES       => \@issuedat );
249 $template->param( issues_count => $count );
250 $template->param( canrenew     => $canrenew );
251 $template->param( OVERDUES       => \@overdues );
252 $template->param( overdues_count => $overdues_count );
253
254 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
255 if ($show_barcode) {
256     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
257     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
258 }
259 $template->param( show_barcode => 1 ) if $show_barcode;
260
261 # load the branches
262 my $branches = GetBranches();
263 my @branch_loop;
264 for my $branch_hash ( sort keys %{$branches} ) {
265     my $selected;
266     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
267         $selected =
268           ( C4::Context->userenv
269               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
270     }
271     push @branch_loop,
272       { value      => "branch: $branch_hash",
273         branchname => $branches->{$branch_hash}->{'branchname'},
274         selected   => $selected,
275       };
276 }
277 $template->param( branchloop => \@branch_loop );
278
279 # now the reserved items....
280 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
281 foreach my $res (@reserves) {
282
283     if ( $res->{'expirationdate'} eq '0000-00-00' ) {
284       $res->{'expirationdate'} = '';
285     }
286     $res->{'subtitle'} = GetRecordValue('subtitle', GetMarcBiblio($res->{'biblionumber'}), GetFrameworkCode($res->{'biblionumber'}));
287     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
288     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
289     my $biblioData = GetBiblioData($res->{'biblionumber'});
290     $res->{'reserves_title'} = $biblioData->{'title'};
291     $res->{'author'} = $biblioData->{'author'};
292
293     if ($show_priority) {
294         $res->{'priority'} ||= '';
295     }
296     $res->{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref") if ( $res->{'suspend_until'} );
297 }
298
299 # use Data::Dumper;
300 # warn Dumper(@reserves);
301
302 $template->param( RESERVES       => \@reserves );
303 $template->param( reserves_count => $#reserves+1 );
304 $template->param( showpriority=>$show_priority );
305
306 my @waiting;
307 my $wcount = 0;
308 foreach my $res (@reserves) {
309     if ( $res->{'itemnumber'} ) {
310         my $item = GetItem( $res->{'itemnumber'});
311         $res->{'holdingbranch'} =
312           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
313         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
314         # get document reserve status
315         my $biblioData = GetBiblioData($res->{'biblionumber'});
316         $res->{'waiting_title'} = $biblioData->{'title'};
317         if ( ( $res->{'found'} eq 'W' ) ) {
318             my $item = $res->{'itemnumber'};
319             $item = GetBiblioFromItemNumber($item,undef);
320             $res->{'wait'}= 1;
321             $res->{'holdingbranch'}=$item->{'holdingbranch'};
322             $res->{'biblionumber'}=$item->{'biblionumber'};
323             $res->{'barcode'} = $item->{'barcode'};
324             $res->{'wbrcode'} = $res->{'branchcode'};
325             $res->{'itemnumber'}    = $res->{'itemnumber'};
326             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
327             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
328                 $res->{'atdestination'} = 1;
329             }
330             # set found to 1 if reserve is waiting for patron pickup
331             $res->{'found'} = 1 if $res->{'found'} eq 'W';
332         } else {
333             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
334             if ($transfertwhen) {
335                 $res->{intransit} = 1;
336                 $res->{datesent}   = $transfertwhen;
337                 $res->{frombranch} = GetBranchName($transfertfrom);
338             }
339         }
340         push @waiting, $res;
341         $wcount++;
342     }
343     # can be cancelled
344     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
345     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
346
347 }
348
349 $template->param( WAITING => \@waiting );
350
351 # current alert subscriptions
352 my $alerts = getalert($borrowernumber);
353 foreach ( @$alerts ) {
354     $_->{ $_->{type} } = 1;
355     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
356 }
357
358 if (C4::Context->preference('BakerTaylorEnabled')) {
359     $template->param(
360         BakerTaylorEnabled  => 1,
361         BakerTaylorImageURL => &image_url(),
362         BakerTaylorLinkURL  => &link_url(),
363         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
364     );
365 }
366
367 if (C4::Context->preference("OPACAmazonCoverImages") or 
368     C4::Context->preference("GoogleJackets") or
369     C4::Context->preference("BakerTaylorEnabled") or
370     C4::Context->preference("SyndeticsCoverImages")) {
371         $template->param(JacketImages=>1);
372 }
373
374 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
375     $template->param( bor_messages => 1 );
376 }
377
378 if ( $borr->{'opacnote'} ) {
379   $template->param( 
380     bor_messages => 1,
381     opacnote => $borr->{'opacnote'},
382   );
383 }
384
385 $template->param(
386     bor_messages_loop    => GetMessages( $borrowernumber, 'B', 'NONE' ),
387     waiting_count      => $wcount,
388     patronupdate => $patronupdate,
389     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
390     userview => 1,
391 );
392
393 $template->param(
394     SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
395     AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
396     OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
397 );
398
399 output_html_with_http_headers $query, $cookie, $template->output;
400