Bug 7460: Add template toolkit's FILTER html_line_break on opac details description...
[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 with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA  02111-1307 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::Overdues qw/CheckBorrowerDebarred/;
34 use C4::Biblio;
35 use C4::Items;
36 use C4::Dates qw/format_date/;
37 use C4::Letters;
38 use C4::Branch; # GetBranches
39
40 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
41
42 use Date::Calc qw(
43   Today
44   Add_Delta_Days
45   Date_to_Days
46 );
47
48 my $query = new CGI;
49
50 BEGIN {
51     if (C4::Context->preference('BakerTaylorEnabled')) {
52         require C4::External::BakerTaylor;
53         import C4::External::BakerTaylor qw(&image_url &link_url);
54     }
55 }
56
57 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58     {
59         template_name   => "opac-user.tmpl",
60         query           => $query,
61         type            => "opac",
62         authnotrequired => 0,
63         flagsrequired   => { borrow => 1 },
64         debug           => 1,
65     }
66 );
67
68 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
69 my $patronupdate = $query->param('patronupdate');
70
71 # get borrower information ....
72 my ( $borr ) = GetMemberDetails( $borrowernumber );
73
74 my (  $today_year,   $today_month,   $today_day) = Today();
75 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
76
77 for (qw(dateenrolled dateexpiry dateofbirth)) {
78     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
79 }
80 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
81
82 my $debar = CheckBorrowerDebarred($borrowernumber);
83 my $userdebarred;
84
85 if ($debar) {
86     $userdebarred = 1;
87     $template->param( 'userdebarred' => $userdebarred );
88     if ( $debar ne "9999-12-31" ) {
89         $borr->{'userdebarreddate'} = C4::Dates::format_date($debar);
90     }
91 }
92
93 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
94     $borr->{'flagged'} = 1;
95 }
96
97 if ( $borr->{'amountoutstanding'} > 5 ) {
98     $borr->{'amountoverfive'} = 1;
99 }
100 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
101     $borr->{'amountoverzero'} = 1;
102 }
103 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
104 $no_renewal_amt ||= 0;
105
106 if ( $borr->{amountoutstanding} > $no_renewal_amt ) {
107     $borr->{'flagged'} = 1;
108     $template->param(
109         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
110     );
111 }
112
113 if ( $borr->{'amountoutstanding'} < 0 ) {
114     $borr->{'amountlessthanzero'} = 1;
115     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
116 }
117
118 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
119
120 my @bordat;
121 $bordat[0] = $borr;
122
123 # Warningdate is the date that the warning starts appearing
124 if ( C4::Context->preference('NotifyBorrowerDeparture') &&
125     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
126     Date_to_Days( $today_year, $today_month, $today_day ) ) 
127 {
128     # borrower card soon to expire, warn the borrower
129     $borr->{'warndeparture'} = $borr->{dateexpiry};
130     if (C4::Context->preference('ReturnBeforeExpiry')){
131         $borr->{'returnbeforeexpiry'} = 1;
132     }
133 }
134
135 $template->param(   BORROWER_INFO     => \@bordat,
136                     borrowernumber    => $borrowernumber,
137                     patron_flagged    => $borr->{flagged},
138                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
139                     surname           => $borr->{surname},
140                     showname          => $borr->{showname},
141
142                 );
143
144 #get issued items ....
145
146 my $count          = 0;
147 my $toggle = 0;
148 my $overdues_count = 0;
149 my @overdues;
150 my @issuedat;
151 my $itemtypes = GetItemTypes();
152 my ($issues) = GetPendingIssues($borrowernumber);
153 my $canrenew = 0;
154 if ($issues){
155         foreach my $issue ( sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues ) {
156                 # check for reserves
157                 my ( $restype, $res, undef ) = CheckReserves( $issue->{'itemnumber'} );
158                 if ( $restype ) {
159                         $issue->{'reserved'} = 1;
160                 }
161                 
162                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
163                 my $charges = 0;
164                 foreach my $ac (@$accts) {
165                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
166                                 $charges += $ac->{'amountoutstanding'}
167                                   if $ac->{'accounttype'} eq 'F';
168                                 $charges += $ac->{'amountoutstanding'}
169                                   if $ac->{'accounttype'} eq 'L';
170                         }
171                 }
172                 $issue->{'charges'} = $charges;
173
174                 # get publictype for icon
175
176                 my $publictype = $issue->{'publictype'};
177                 $issue->{$publictype} = 1;
178
179                 # check if item is renewable
180                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
181                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
182         if($status && C4::Context->preference("OpacRenewalAllowed")){
183             $issue->{'status'} = $status;
184             $canrenew = 1;
185         }
186                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
187                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
188
189                 if ( $issue->{'overdue'} ) {
190                         push @overdues, $issue;
191                         $overdues_count++;
192                         $issue->{'overdue'} = 1;
193                 }
194                 else {
195                         $issue->{'issued'} = 1;
196                 }
197                 # imageurl:
198                 my $itemtype = $issue->{'itemtype'};
199                 if ( $itemtype ) {
200                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
201                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
202                 }
203                 $issue->{date_due} = format_date($issue->{date_due});
204                 push @issuedat, $issue;
205                 $count++;
206                 
207                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
208                 $issue->{normalized_isbn} = $isbn;
209
210                 # My Summary HTML
211                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
212                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
213                     $issue->{title} =~ s/\/+$//; # remove trailing slash
214                     $issue->{title} =~ s/\s+$//; # remove trailing space
215                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
216                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
217                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
218                     $issue->{MySummaryHTML} = $my_summary_html;
219                 }
220         }
221 }
222 $template->param( ISSUES       => \@issuedat );
223 $template->param( issues_count => $count );
224 $template->param( canrenew     => $canrenew );
225 $template->param( OVERDUES       => \@overdues );
226 $template->param( overdues_count => $overdues_count );
227
228 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
229 if ($show_barcode) {
230     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
231     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
232 }
233 $template->param( show_barcode => 1 ) if $show_barcode;
234
235 # load the branches
236 my $branches = GetBranches();
237 my @branch_loop;
238 for my $branch_hash ( sort keys %{$branches} ) {
239     my $selected;
240     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
241         $selected =
242           ( C4::Context->userenv
243               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
244     }
245     push @branch_loop,
246       { value      => "branch: $branch_hash",
247         branchname => $branches->{$branch_hash}->{'branchname'},
248         selected   => $selected,
249       };
250 }
251 $template->param( branchloop => \@branch_loop );
252
253 # now the reserved items....
254 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
255 foreach my $res (@reserves) {
256     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
257
258     if ( $res->{'expirationdate'} ne '0000-00-00' ) {
259       $res->{'expirationdate'} = format_date( $res->{'expirationdate'} ) 
260     } else {
261       $res->{'expirationdate'} = '';
262     }
263     
264     my $publictype = $res->{'publictype'};
265     $res->{$publictype} = 1;
266     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
267     $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
268     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
269     my $biblioData = GetBiblioData($res->{'biblionumber'});
270     $res->{'reserves_title'} = $biblioData->{'title'};
271     if ($OPACDisplayRequestPriority) {
272         $res->{'priority'} = '' if $res->{'priority'} eq '0';
273     }
274 }
275
276 # use Data::Dumper;
277 # warn Dumper(@reserves);
278
279 $template->param( RESERVES       => \@reserves );
280 $template->param( reserves_count => $#reserves+1 );
281 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
282
283 my @waiting;
284 my $wcount = 0;
285 foreach my $res (@reserves) {
286     if ( $res->{'itemnumber'} ) {
287         my $item = GetItem( $res->{'itemnumber'});
288         $res->{'holdingbranch'} =
289           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
290         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
291         # get document reserve status
292         my $biblioData = GetBiblioData($res->{'biblionumber'});
293         $res->{'waiting_title'} = $biblioData->{'title'};
294         if ( ( $res->{'found'} eq 'W' ) ) {
295             my $item = $res->{'itemnumber'};
296             $item = GetBiblioFromItemNumber($item,undef);
297             $res->{'wait'}= 1; 
298             $res->{'holdingbranch'}=$item->{'holdingbranch'};
299             $res->{'biblionumber'}=$item->{'biblionumber'};
300             $res->{'barcode'} = $item->{'barcode'};
301             $res->{'wbrcode'} = $res->{'branchcode'};
302             $res->{'itemnumber'}    = $res->{'itemnumber'};
303             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
304             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
305                 $res->{'atdestination'} = 1;
306             }
307             # set found to 1 if reserve is waiting for patron pickup
308             $res->{'found'} = 1 if $res->{'found'} eq 'W';
309         } else {
310             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
311             if ($transfertwhen) {
312                 $res->{intransit} = 1;
313                 $res->{datesent}   = format_date($transfertwhen);
314                 $res->{frombranch} = GetBranchName($transfertfrom);
315             }
316         }
317         push @waiting, $res;
318         $wcount++;
319     }
320     # can be cancelled
321     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
322     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
323     
324 }
325
326 $template->param( WAITING => \@waiting );
327
328 # current alert subscriptions
329 my $alerts = getalert($borrowernumber);
330 foreach ( @$alerts ) {
331     $_->{ $_->{type} } = 1;
332     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
333 }
334
335 if (C4::Context->preference('BakerTaylorEnabled')) {
336     $template->param(
337         BakerTaylorEnabled  => 1,
338         BakerTaylorImageURL => &image_url(),
339         BakerTaylorLinkURL  => &link_url(),
340         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
341     );
342 }
343
344 if (C4::Context->preference("OPACAmazonCoverImages") or 
345     C4::Context->preference("GoogleJackets") or
346     C4::Context->preference("BakerTaylorEnabled") or
347         C4::Context->preference("SyndeticsCoverImages")) {
348         $template->param(JacketImages=>1);
349 }
350
351 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
352         $template->param( bor_messages => 1 );
353 }
354
355 if ( $borr->{'opacnote'} ) {
356   $template->param( 
357     bor_messages => 1,
358     opacnote => $borr->{'opacnote'},
359   );
360 }
361
362 $template->param(
363     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
364     waiting_count      => $wcount,
365     textmessaging      => $borr->{textmessaging},
366     patronupdate => $patronupdate,
367     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
368     userview => 1,
369     dateformat    => C4::Context->preference("dateformat"),
370 );
371
372 output_html_with_http_headers $query, $cookie, $template->output;
373