Merge branch 'bug_9850' into 3.14-master
[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 use Encode;
26
27 use C4::Auth;    # checkauth, getborrowernumber.
28 use C4::Context;
29 use C4::Koha;
30 use C4::Circulation;
31 use C4::Output;
32 use C4::Dates qw/format_date/;
33 use C4::Members;
34 use C4::Members::Attributes;
35 use C4::Branch;
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 ( $patronemail ) = GetFirstValidEmailAddress($borrowernumber);
53 my $lib = GetBranchDetail($borr->{'branchcode'});
54
55 # handle the new information....
56 # collect the form values and send an email.
57 my @fields = (
58     'surname','firstname','othernames','streetnumber','address','address2','city','state','zipcode','country','phone','mobile','fax','phonepro', 'emailaddress','emailpro','B_streetnumber','B_address','B_address2','B_city','B_state','B_zipcode','B_country','B_phone','B_email','dateofbirth','sex'
59 );
60 my $update;
61 my $updateemailaddress = $lib->{'branchemail'};
62 $updateemailaddress = C4::Context->preference('KohaAdminEmailAddress') unless( $updateemailaddress =~ /\w+@\w+/);
63 if ( !$updateemailaddress || $updateemailaddress eq '' ) {
64     warn
65 "KohaAdminEmailAddress system preference not set.  Couldn't send patron update information for $borr->{'firstname'} $borr->{'surname'} (#$borrowernumber)\n";
66     my ($template) = get_template_and_user(
67         {
68             template_name   => "kohaerror.tmpl",
69             query           => $query,
70             type            => "opac",
71             authnotrequired => 1,
72             flagsrequired   => { borrow => 1 },
73             debug           => 1,
74         }
75     );
76
77     $template->param(
78         noadminemail => 1,
79     );
80
81     output_html_with_http_headers $query, $cookie, $template->output;
82     exit;
83 }
84
85 if ( !$patronemail || $patronemail eq '' ) {
86         $patronemail = $updateemailaddress;
87 };
88
89 if ( $query->param('modify') ) {
90
91     # get all the fields:
92     my $message = <<"EOF";
93 Patron $borr->{'cardnumber'} has requested to change her/his personal details.
94 Please check these new details and make the changes to these fields:
95
96 EOF
97
98     my $streetnumber = $borr->{'streetnumber'} || '';
99     my $address = $borr->{'address'} || '';
100     my $address2 = $borr->{'address2'} || '';
101     my $B_streetnumber = $borr->{'B_streetnumber'} || '';
102     my $B_address = $borr->{'B_address'} || '';
103     my $B_address2 = $borr->{'B_address2'} || '';
104
105     foreach my $field (@fields) {
106         my $newfield = decode('utf-8',$query->param($field)) || '';
107         my $borrowerfield = '';
108         if($borr->{$field}) {
109             $borrowerfield = $borr->{$field};
110         }
111         
112         if($field eq "dateofbirth") {
113            $borrowerfield  = format_date( $borr->{'dateofbirth'} ) || '';
114         }
115
116         if($borrowerfield ne $newfield) {
117             $message .= $field . " : $borrowerfield  -->  $newfield\n";
118         }
119     }
120
121     $message .= "\nEdit this patron's record: http://".C4::Context->preference('staffClientBaseURL ')."/cgi-bin/koha/members/memberentry.pl?op=modify&borrowernumber=".$borr->{'borrowernumber'}."&categorycode=".$borr->{'categorycode'} if C4::Context->preference('staffClientBaseURL ');
122
123     $message .= "\n\nThanks,\nKoha\n\n";
124     my %mail = (
125         To      => $updateemailaddress,
126         From    => $patronemail,
127         Subject => "User Request for update of Record.",
128         Message => encode('utf-8', $message), # Mail::Sendmail doesn't like wide characters
129         'Content-Type' => 'text/plain; charset="utf-8"',
130     );
131
132     if ( sendmail %mail ) {
133
134         # do something if it works....
135         print $query->redirect('/cgi-bin/koha/opac-user.pl?patronupdate=sent');
136         exit;
137     }
138     else {
139
140         # do something if it doesnt work....
141         warn "Error sending mail: $Mail::Sendmail::error \n";
142     }
143 }
144
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 ( keys %{ $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 ( keys %{ $checkout_prefs->{transports} }) {
168     $borr->{"items_borrowed_$_"} = 1;
169 }
170
171 if (C4::Context->preference('OPACpatronimages')) {
172     my ($image, $dberror) = GetPatronImage($borr->{'cardnumber'});
173     if ($image) {
174         $template->param(
175             display_patron_image => 1
176         );
177     }
178 }
179
180 my @bordat;
181 $bordat[0] = $borr;
182
183 $template->param( 
184     BORROWER_INFO => \@bordat,
185     userupdateview => 1,
186 );
187
188 output_html_with_http_headers $query, $cookie, $template->output;