Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / opac / opac-memberentry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
23
24 use C4::Auth;
25 use C4::Output;
26 use C4::Members;
27 use Koha::Borrower::Modifications;
28 use C4::Branch qw(GetBranchesLoop);
29 use C4::Scrubber;
30
31 my $cgi = new CGI;
32 my $dbh = C4::Context->dbh;
33
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {
36         template_name   => "opac-memberentry.tt",
37         type            => "opac",
38         query           => $cgi,
39         authnotrequired => 1,
40     }
41 );
42
43 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
44 {
45     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
46     exit;
47 }
48
49 my $action = $cgi->param('action') || q{};
50 if ( $action eq q{} ) {
51     if ($borrowernumber) {
52         $action = 'edit';
53     }
54     else {
55         $action = 'new';
56     }
57 }
58
59 $template->param(
60     action            => $action,
61     hidden            => GetHiddenFields(),
62     mandatory         => GetMandatoryFields($action),
63     member_titles     => GetTitles() || undef,
64     branches          => GetBranchesLoop(),
65     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
66 );
67
68 if ( $action eq 'create' ) {
69
70     my %borrower = ParseCgiForBorrower($cgi);
71
72     %borrower = DelEmptyFields(%borrower);
73
74     my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
75
76     if (@empty_mandatory_fields) {
77         $template->param(
78             empty_mandatory_fields => \@empty_mandatory_fields,
79             borrower               => \%borrower
80         );
81     }
82     elsif (
83         md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
84     {
85         $template->param(
86             failed_captcha => 1,
87             borrower       => \%borrower
88         );
89     }
90     else {
91         if (
92             C4::Context->boolean_preference(
93                 'PatronSelfRegistrationVerifyByEmail')
94           )
95         {
96             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
97                 {
98                     template_name   => "opac-registration-email-sent.tt",
99                     type            => "opac",
100                     query           => $cgi,
101                     authnotrequired => 1,
102                 }
103             );
104             $template->param( 'email' => $borrower{'email'} );
105
106             my $verification_token = md5_hex( \%borrower );
107             $borrower{'password'} = random_string("..........");
108
109             Koha::Borrower::Modifications->new(
110                 verification_token => $verification_token )
111               ->AddModifications(\%borrower);
112
113             #Send verification email
114             my $letter = C4::Letters::GetPreparedLetter(
115                 module      => 'members',
116                 letter_code => 'OPAC_REG_VERIFY',
117                 tables      => {
118                     borrower_modifications => $verification_token,
119                 },
120             );
121
122             C4::Letters::EnqueueLetter(
123                 {
124                     letter                 => $letter,
125                     message_transport_type => 'email',
126                     to_address             => $borrower{'email'},
127                     from_address =>
128                       C4::Context->preference('KohaAdminEmailAddress'),
129                 }
130             );
131         }
132         else {
133             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
134                 {
135                     template_name   => "opac-registration-confirmation.tt",
136                     type            => "opac",
137                     query           => $cgi,
138                     authnotrequired => 1,
139                 }
140             );
141
142             $template->param( OpacPasswordChange =>
143                   C4::Context->preference('OpacPasswordChange') );
144
145             my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
146
147             $template->param( password_cleartext => $password );
148             $template->param(
149                 borrower => GetMember( borrowernumber => $borrowernumber ) );
150             $template->param(
151                 PatronSelfRegistrationAdditionalInstructions =>
152                   C4::Context->preference(
153                     'PatronSelfRegistrationAdditionalInstructions')
154             );
155         }
156     }
157 }
158 elsif ( $action eq 'update' ) {
159
160     my %borrower = ParseCgiForBorrower($cgi);
161
162     my %borrower_changes = DelEmptyFields(%borrower);
163     my @empty_mandatory_fields =
164       CheckMandatoryFields( \%borrower_changes, $action );
165
166     if (@empty_mandatory_fields) {
167         $template->param(
168             empty_mandatory_fields => \@empty_mandatory_fields,
169             borrower               => \%borrower
170         );
171
172         $template->param( action => 'edit' );
173     }
174     else {
175         ( $template, $borrowernumber, $cookie ) = get_template_and_user(
176             {
177                 template_name   => "opac-memberentry-update-submitted.tt",
178                 type            => "opac",
179                 query           => $cgi,
180                 authnotrequired => 1,
181             }
182         );
183
184         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
185
186         my $m =
187           Koha::Borrower::Modifications->new(
188             borrowernumber => $borrowernumber );
189
190         $m->DelModifications;
191         $m->AddModifications(\%borrower_changes);
192         $template->param(
193             borrower => GetMember( borrowernumber => $borrowernumber ),
194         );
195     }
196 }
197 elsif ( $action eq 'edit' ) {    #Display logged in borrower's data
198     my $borrower = GetMember( borrowernumber => $borrowernumber );
199
200     if (C4::Context->preference('ExtendedPatronAttributes')) {
201         my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
202         if (scalar(@$attributes) > 0) {
203             $borrower->{ExtendedPatronAttributes} = 1;
204             $borrower->{patron_attributes} = $attributes;
205         }
206     }
207
208     $template->param(
209         borrower => $borrower, );
210
211     if (C4::Context->preference('OPACpatronimages')) {
212         my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
213         if ($image) {
214             $template->param(
215                 display_patron_image => 1
216             );
217         }
218     }
219
220 }
221
222 my $captcha = random_string("CCCCC");
223
224 $template->param(
225     captcha        => $captcha,
226     captcha_digest => md5_base64($captcha)
227 );
228
229 output_html_with_http_headers $cgi, $cookie, $template->output;
230
231 sub GetHiddenFields {
232     my %hidden_fields;
233
234     my $BorrowerUnwantedField =
235       C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
236
237     my @fields = split( /\|/, $BorrowerUnwantedField );
238     foreach (@fields) {
239         next unless m/\w/o;
240         $hidden_fields{$_} = 1;
241     }
242
243     return \%hidden_fields;
244 }
245
246 sub GetMandatoryFields {
247     my ($action) = @_;
248
249     my %mandatory_fields;
250
251     my $BorrowerMandatoryField =
252       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
253
254     my @fields = split( /\|/, $BorrowerMandatoryField );
255
256     foreach (@fields) {
257         $mandatory_fields{$_} = 1;
258     }
259
260     if ( $action eq 'create' || $action eq 'new' ) {
261         $mandatory_fields{'email'} = 1
262           if C4::Context->boolean_preference(
263             'PatronSelfRegistrationVerifyByEmail');
264     }
265
266     return \%mandatory_fields;
267 }
268
269 sub CheckMandatoryFields {
270     my ( $borrower, $action ) = @_;
271
272     my @empty_mandatory_fields;
273
274     my $mandatory_fields = GetMandatoryFields($action);
275     delete $mandatory_fields->{'cardnumber'};
276
277     foreach my $key ( keys %$mandatory_fields ) {
278         push( @empty_mandatory_fields, $key )
279           unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
280     }
281
282     return @empty_mandatory_fields;
283 }
284
285 sub ParseCgiForBorrower {
286     my ($cgi) = @_;
287
288     my $scrubber = C4::Scrubber->new();
289     my %borrower;
290
291     foreach ( $cgi->param ) {
292         if ( $_ =~ '^borrower_' ) {
293             my ($key) = substr( $_, 9 );
294             $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
295         }
296     }
297
298     $borrower{'dateofbirth'} =
299       C4::Dates->new( $borrower{'dateofbirth'} )->output("iso")
300       if ( defined( $borrower{'dateofbirth'} ) );
301
302     return %borrower;
303 }
304
305 sub DelUnchangedFields {
306     my ( $borrowernumber, %new_data ) = @_;
307
308     my $current_data = GetMember( borrowernumber => $borrowernumber );
309
310     foreach my $key ( keys %new_data ) {
311         if ( $current_data->{$key} eq $new_data{$key} ) {
312             delete $new_data{$key};
313         }
314     }
315
316     return %new_data;
317 }
318
319 sub DelEmptyFields {
320     my (%borrower) = @_;
321
322     foreach my $key ( keys %borrower ) {
323         delete $borrower{$key} unless $borrower{$key};
324     }
325
326     return %borrower;
327 }