Bug 17785: Fix OAI-PMH's XSLT-generated URLs under Plack
[koha.git] / opac / opac-user.pl
blob04a94c0818bb9d8c9ff6c3f20f0230c37a9ab033
1 #!/usr/bin/perl
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
23 use CGI qw ( -utf8 );
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 Koha::DateUtils;
37 use Koha::Holds;
38 use Koha::Database;
39 use Koha::Patron::Messages;
40 use Koha::Patron::Discharge;
41 use Koha::Patrons;
43 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
45 use Scalar::Util qw(looks_like_number);
46 use Date::Calc qw(
47 Today
48 Add_Delta_Days
49 Date_to_Days
52 my $query = new CGI;
54 BEGIN {
55 if (C4::Context->preference('BakerTaylorEnabled')) {
56 require C4::External::BakerTaylor;
57 import C4::External::BakerTaylor qw(&image_url &link_url);
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
63 template_name => "opac-user.tt",
64 query => $query,
65 type => "opac",
66 authnotrequired => 0,
67 debug => 1,
71 my %renewed = map { $_ => 1 } split( ':', $query->param('renewed') );
73 my $show_priority;
74 for ( C4::Context->preference("OPACShowHoldQueueDetails") ) {
75 m/priority/ and $show_priority = 1;
78 my $patronupdate = $query->param('patronupdate');
79 my $canrenew = 1;
81 $template->param( shibbolethAuthentication => C4::Context->config('useshibboleth') );
83 if (!$borrowernumber) {
84 $template->param( adminWarning => 1 );
87 # get borrower information ....
88 my ( $borr ) = GetMember( borrowernumber => $borrowernumber );
90 my ( $today_year, $today_month, $today_day) = Today();
91 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
93 my $debar = Koha::Patrons->find( $borrowernumber )->is_debarred;
94 my $userdebarred;
96 if ($debar) {
97 $userdebarred = 1;
98 $template->param( 'userdebarred' => $userdebarred );
99 if ( $debar ne "9999-12-31" ) {
100 $borr->{'userdebarreddate'} = $debar;
102 # FIXME looks like $available is not needed
103 # If a patron is discharged he has a validated discharge available
104 my $available = Koha::Patron::Discharge::count({
105 borrowernumber => $borrowernumber,
106 validated => 1,
108 $template->param( 'discharge_available' => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $borrowernumber}) );
111 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
112 $borr->{'flagged'} = 1;
113 $canrenew = 0;
116 my ( $amountoutstanding ) = GetMemberAccountRecords($borrowernumber);
117 if ( $amountoutstanding > 5 ) {
118 $borr->{'amountoverfive'} = 1;
120 if ( 5 >= $amountoutstanding && $amountoutstanding > 0 ) {
121 $borr->{'amountoverzero'} = 1;
123 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
124 $no_renewal_amt = undef unless looks_like_number( $no_renewal_amt );
126 if ( C4::Context->preference('OpacRenewalAllowed')
127 && defined($no_renewal_amt)
128 && $amountoutstanding > $no_renewal_amt )
130 $borr->{'flagged'} = 1;
131 $canrenew = 0;
132 $template->param(
133 renewal_blocked_fines => $no_renewal_amt,
134 renewal_blocked_fines_amountoutstanding => $amountoutstanding,
138 if ( $amountoutstanding < 0 ) {
139 $borr->{'amountlessthanzero'} = 1;
140 $amountoutstanding = -1 * ( $amountoutstanding );
143 # Warningdate is the date that the warning starts appearing
144 if ( $borr->{'dateexpiry'} && C4::Context->preference('NotifyBorrowerDeparture') ) {
145 my $days_to_expiry = Date_to_Days( $warning_year, $warning_month, $warning_day ) - Date_to_Days( $today_year, $today_month, $today_day );
146 if ( $days_to_expiry < 0 ) {
147 #borrower card has expired, warn the borrower
148 $borr->{'warnexpired'} = $borr->{'dateexpiry'};
149 } elsif ( $days_to_expiry < C4::Context->preference('NotifyBorrowerDeparture') ) {
150 # borrower card soon to expire, warn the borrower
151 $borr->{'warndeparture'} = $borr->{dateexpiry};
152 if (C4::Context->preference('ReturnBeforeExpiry')){
153 $borr->{'returnbeforeexpiry'} = 1;
158 # pass on any renew errors to the template for displaying
159 my $renew_error = $query->param('renew_error');
161 $template->param( BORROWER_INFO => $borr,
162 amountoutstanding => $amountoutstanding,
163 borrowernumber => $borrowernumber,
164 patron_flagged => $borr->{flagged},
165 OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
166 surname => $borr->{surname},
167 RENEW_ERROR => $renew_error,
168 borrower => $borr,
171 #get issued items ....
173 my $count = 0;
174 my $overdues_count = 0;
175 my @overdues;
176 my @issuedat;
177 my $itemtypes = GetItemTypes();
178 my $issues = GetPendingIssues($borrowernumber);
179 if ($issues){
180 foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
181 # check for reserves
182 my $restype = GetReserveStatus( $issue->{'itemnumber'} );
183 if ( $restype ) {
184 $issue->{'reserved'} = 1;
187 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
188 my $charges = 0;
189 foreach my $ac (@$accts) {
190 if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
191 $charges += $ac->{'amountoutstanding'}
192 if $ac->{'accounttype'} eq 'F';
193 $charges += $ac->{'amountoutstanding'}
194 if $ac->{'accounttype'} eq 'FU';
195 $charges += $ac->{'amountoutstanding'}
196 if $ac->{'accounttype'} eq 'L';
199 $issue->{'charges'} = $charges;
200 my $marcrecord = GetMarcBiblio( $issue->{'biblionumber'} );
201 $issue->{'subtitle'} = GetRecordValue('subtitle', $marcrecord, GetFrameworkCode($issue->{'biblionumber'}));
202 # check if item is renewable
203 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
204 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
205 if($status && C4::Context->preference("OpacRenewalAllowed")){
206 $issue->{'status'} = $status;
209 $issue->{'renewed'} = $renewed{ $issue->{'itemnumber'} };
211 if ($renewerror) {
212 $issue->{'too_many'} = 1 if $renewerror eq 'too_many';
213 $issue->{'on_reserve'} = 1 if $renewerror eq 'on_reserve';
214 $issue->{'norenew_overdue'} = 1 if $renewerror eq 'overdue';
215 $issue->{'auto_renew'} = 1 if $renewerror eq 'auto_renew';
216 $issue->{'auto_too_soon'} = 1 if $renewerror eq 'auto_too_soon';
217 $issue->{'auto_too_late'} = 1 if $renewerror eq 'auto_too_late';
219 if ( $renewerror eq 'too_soon' ) {
220 $issue->{'too_soon'} = 1;
221 $issue->{'soonestrenewdate'} = output_pref(
222 C4::Circulation::GetSoonestRenewDate(
223 $issue->{borrowernumber},
224 $issue->{itemnumber}
230 if ( $issue->{'overdue'} ) {
231 push @overdues, $issue;
232 $overdues_count++;
233 $issue->{'overdue'} = 1;
235 else {
236 $issue->{'issued'} = 1;
238 # imageurl:
239 my $itemtype = $issue->{'itemtype'};
240 if ( $itemtype ) {
241 $issue->{'imageurl'} = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
242 $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
244 push @issuedat, $issue;
245 $count++;
247 my $isbn = GetNormalizedISBN($issue->{'isbn'});
248 $issue->{normalized_isbn} = $isbn;
249 $issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
251 # My Summary HTML
252 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
253 $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
254 $issue->{title} =~ s/\/+$//; # remove trailing slash
255 $issue->{title} =~ s/\s+$//; # remove trailing space
256 $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
257 $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
258 $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
259 $issue->{MySummaryHTML} = $my_summary_html;
263 my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
264 $canrenew = 0 if ($overduesblockrenewing ne 'allow' and $overdues_count == $count);
265 $template->param( ISSUES => \@issuedat );
266 $template->param( issues_count => $count );
267 $template->param( canrenew => $canrenew );
268 $template->param( OVERDUES => \@overdues );
269 $template->param( overdues_count => $overdues_count );
271 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
272 if ($show_barcode) {
273 my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
274 undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
276 $template->param( show_barcode => 1 ) if $show_barcode;
278 # now the reserved items....
279 my $reserves = Koha::Holds->search( { borrowernumber => $borrowernumber } );
281 $template->param(
282 RESERVES => $reserves,
283 showpriority => $show_priority,
286 # current alert subscriptions
287 my $alerts = getalert($borrowernumber);
288 foreach ( @$alerts ) {
289 $_->{ $_->{type} } = 1;
290 $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
293 if (C4::Context->preference('BakerTaylorEnabled')) {
294 $template->param(
295 BakerTaylorEnabled => 1,
296 BakerTaylorImageURL => &image_url(),
297 BakerTaylorLinkURL => &link_url(),
298 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
302 if (C4::Context->preference("OPACAmazonCoverImages") or
303 C4::Context->preference("GoogleJackets") or
304 C4::Context->preference("BakerTaylorEnabled") or
305 C4::Context->preference("SyndeticsCoverImages")) {
306 $template->param(JacketImages=>1);
309 my $patron_messages = Koha::Patron::Messages->search(
311 borrowernumber => $borrowernumber,
312 message_type => 'B',
316 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor')
317 || C4::Context->preference('AllowStaffToSetCheckoutsVisibilityForGuarantor') )
319 my @relatives =
320 Koha::Database->new()->schema()->resultset("Borrower")->search(
322 privacy_guarantor_checkouts => 1,
323 'me.guarantorid' => $borrowernumber
325 { prefetch => [ { 'issues' => { 'item' => 'biblio' } } ] }
327 $template->param( relatives => \@relatives );
330 $template->param(
331 borrower => Koha::Patrons->find($borrowernumber),
332 patron_messages => $patron_messages,
333 opacnote => $borr->{opacnote},
334 patronupdate => $patronupdate,
335 OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
336 userview => 1,
337 SuspendHoldsOpac => C4::Context->preference('SuspendHoldsOpac'),
338 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
339 OpacHoldNotes => C4::Context->preference('OpacHoldNotes'),
340 failed_holds => scalar $query->param('failed_holds'),
343 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };