RT#870 fix sendmail encoding
[koha.git] / opac / opac-userupdate.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Mail::Sendmail;
25
26 use C4::Auth;    # checkauth, getborrowernumber.
27 use C4::Context;
28 use C4::Koha;
29 use C4::Circulation;
30 use C4::Output;
31 use C4::Dates qw/format_date/;
32 use C4::Members;
33 use C4::Members::Attributes;
34 use C4::Branch;
35 use Encode;
36
37 my $query = new CGI;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "opac-userupdate.tmpl",
42         query           => $query,
43         type            => "opac",
44         authnotrequired => 0,
45         flagsrequired   => { borrow => 1 },
46         debug           => 1,
47     }
48 );
49
50 # get borrower information ....
51 my ( $borr ) = GetMemberDetails( $borrowernumber );
52 my $lib = GetBranchDetail($borr->{'branchcode'});
53
54 # handle the new information....
55 # collect the form values and send an email.
56 my @fields = (
57     'surname','firstname','othernames','streetnumber','address','address2','city','zipcode','country','phone','mobile','fax','phonepro', 'email','emailpro','B_streetnumber','B_address','B_address2','B_city','B_zipcode','B_country','B_phone','B_email','dateofbirth','sex'
58 );
59 my $update;
60 my $updateemailaddress = $lib->{'branchemail'};
61 $updateemailaddress = C4::Context->preference('KohaAdminEmailAddress') unless( $updateemailaddress =~ /\w+@\w+/);
62 if ( !$updateemailaddress || $updateemailaddress eq '' ) {
63     warn
64 "KohaAdminEmailAddress system preference not set.  Couldn't send patron update information for $borr->{'firstname'} $borr->{'surname'} (#$borrowernumber)\n";
65     my ($template) = get_template_and_user(
66         {
67             template_name   => "kohaerror.tmpl",
68             query           => $query,
69             type            => "opac",
70             authnotrequired => 1,
71             flagsrequired   => { borrow => 1 },
72             debug           => 1,
73         }
74     );
75
76     $template->param(
77         noadminemail => 1,
78     );
79
80     output_html_with_http_headers $query, $cookie, $template->output;
81     exit;
82 }
83
84 if ( $query->param('modify') ) {
85
86     # get all the fields:
87     my $message = <<"EOF";
88 Patron $borr->{'cardnumber'}
89
90 has requested to change her/his personal details.
91 Please check these new details and make the changes:
92 EOF
93
94     my $streetnumber = $borr->{'streetnumber'} || '';
95     my $address = $borr->{'address'} || '';
96     my $address2 = $borr->{'address2'} || '';
97     my $B_streetnumber = $borr->{'B_streetnumber'} || '';
98     my $B_address = $borr->{'B_address'} || '';
99     my $B_address2 = $borr->{'B_address2'} || '';
100
101     foreach my $field (@fields) {
102         my $newfield = $query->param($field) || '';
103         my $borrowerfield = '';
104         if($borr->{$field}) {
105             $borrowerfield = $borr->{$field};
106         }
107         
108         if($field eq "dateofbirth") {
109            $borrowerfield  = format_date( $borr->{'dateofbirth'} ) || '';
110         }
111
112         if($borrowerfield eq $newfield) {
113             $message .= "$field : $borrowerfield  -->  $newfield\n";
114         } else {
115             $message .= uc($field) . " : $borrowerfield  -->  $newfield\n";
116         }
117     }
118     $message .= "\n\nThanks,\nKoha\n\n";
119     my %mail = (
120         To      => $updateemailaddress,
121         From    => $updateemailaddress,
122         Subject => "User Request for update of Record.",
123         Message => $message,
124         'Content-Type' => 'text/plain; charset="utf8"',
125     );
126
127         Encode::_utf8_off($mail{$_}) foreach keys %mail;
128
129     if ( sendmail %mail ) {
130
131         # do something if it works....
132         print $query->redirect('/cgi-bin/koha/opac-user.pl?patronupdate=sent');
133         exit;
134     }
135     else {
136
137         # do something if it doesnt work....
138         warn "Error sending mail: $Mail::Sendmail::error \n";
139     }
140 }
141
142 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
143 $borr->{'dateexpiry'}   = format_date( $borr->{'dateexpiry'} );
144 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
145 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
146 $borr->{'branchname'}   = GetBranchName($borr->{'branchcode'});
147
148 if (C4::Context->preference('ExtendedPatronAttributes')) {
149     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
150     if (scalar(@$attributes) > 0) {
151         $borr->{ExtendedPatronAttributes} = 1;
152         $borr->{patron_attributes} = $attributes;
153     }
154 }
155
156 my $checkin_prefs  = C4::Members::Messaging::GetMessagingPreferences({
157     borrowernumber => $borrowernumber,
158     message_name   => 'Item Checkout'
159 });
160 for (@{ $checkin_prefs->{transports} }) {
161     $borr->{"items_returned_$_"} = 1;
162 }
163 my $checkout_prefs = C4::Members::Messaging::GetMessagingPreferences({
164     borrowernumber => $borrowernumber,
165     message_name   => 'Item Check-in'
166 });
167 for (@{ $checkout_prefs->{transports} }) {
168     $borr->{"items_borrowed_$_"} = 1;
169 }
170
171 my @bordat;
172 $bordat[0] = $borr;
173
174 $template->param( 
175     BORROWER_INFO => \@bordat,
176     userupdateview => 1,
177 );
178
179 output_html_with_http_headers $query, $cookie, $template->output;