Bug 17393: Fix non-Latin chars handling in self reg
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 use C4::Auth;
24 use C4::Output;
25 use C4::Members;
26 use C4::Form::MessagingPreferences;
27 use Koha::Borrower::Modifications;
28 use C4::Branch qw(GetBranchesLoop);
29 use C4::Scrubber;
30 use Email::Valid;
31 use Koha::DateUtils;
32
33 my $cgi = new CGI;
34 my $dbh = C4::Context->dbh;
35
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-memberentry.tt",
39         type            => "opac",
40         query           => $cgi,
41         authnotrequired => 1,
42     }
43 );
44
45 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
46 {
47     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
48     exit;
49 }
50
51 my $action = $cgi->param('action') || q{};
52 if ( $action eq q{} ) {
53     if ($borrowernumber) {
54         $action = 'edit';
55     }
56     else {
57         $action = 'new';
58     }
59 }
60
61 my $mandatory = GetMandatoryFields($action);
62 my $hidden = GetHiddenFields($mandatory);
63
64 $template->param(
65     action            => $action,
66     hidden            => $hidden,
67     mandatory         => $mandatory,
68     member_titles     => GetTitles() || undef,
69     branches          => GetBranchesLoop(),
70     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
71 );
72
73 if ( $action eq 'create' ) {
74
75     my %borrower = ParseCgiForBorrower($cgi);
76
77     %borrower = DelEmptyFields(%borrower);
78
79     my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
80     my $invalidformfields = CheckForInvalidFields(\%borrower);
81
82     if (@empty_mandatory_fields || @$invalidformfields) {
83         $template->param(
84             empty_mandatory_fields => \@empty_mandatory_fields,
85             invalid_form_fields    => $invalidformfields,
86             borrower               => \%borrower
87         );
88     }
89     elsif (
90         md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
91     {
92         $template->param(
93             failed_captcha => 1,
94             borrower       => \%borrower
95         );
96     }
97     else {
98         if (
99             C4::Context->boolean_preference(
100                 'PatronSelfRegistrationVerifyByEmail')
101           )
102         {
103             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
104                 {
105                     template_name   => "opac-registration-email-sent.tt",
106                     type            => "opac",
107                     query           => $cgi,
108                     authnotrequired => 1,
109                 }
110             );
111             $template->param( 'email' => $borrower{'email'} );
112
113             my $verification_token = md5_hex( \%borrower );
114             $borrower{'password'} = random_string("..........");
115
116             Koha::Borrower::Modifications->new(
117                 verification_token => $verification_token )
118               ->AddModifications(\%borrower);
119
120             #Send verification email
121             my $letter = C4::Letters::GetPreparedLetter(
122                 module      => 'members',
123                 letter_code => 'OPAC_REG_VERIFY',
124                 tables      => {
125                     borrower_modifications => $verification_token,
126                 },
127             );
128
129             C4::Letters::EnqueueLetter(
130                 {
131                     letter                 => $letter,
132                     message_transport_type => 'email',
133                     to_address             => $borrower{'email'},
134                     from_address =>
135                       C4::Context->preference('KohaAdminEmailAddress'),
136                 }
137             );
138         }
139         else {
140             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
141                 {
142                     template_name   => "opac-registration-confirmation.tt",
143                     type            => "opac",
144                     query           => $cgi,
145                     authnotrequired => 1,
146                 }
147             );
148
149             $template->param( OpacPasswordChange =>
150                   C4::Context->preference('OpacPasswordChange') );
151
152             my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
153             C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
154
155             $template->param( password_cleartext => $password );
156             $template->param(
157                 borrower => GetMember( borrowernumber => $borrowernumber ) );
158             $template->param(
159                 PatronSelfRegistrationAdditionalInstructions =>
160                   C4::Context->preference(
161                     'PatronSelfRegistrationAdditionalInstructions')
162             );
163         }
164     }
165 }
166 elsif ( $action eq 'update' ) {
167     my $borrower = GetMember( borrowernumber => $borrowernumber );
168
169     my %borrower = ParseCgiForBorrower($cgi);
170
171     my %borrower_changes = DelEmptyFields(%borrower);
172     my @empty_mandatory_fields =
173       CheckMandatoryFields( \%borrower_changes, $action );
174     my $invalidformfields = CheckForInvalidFields(\%borrower);
175
176     # Send back the data to the template
177     %borrower = ( %$borrower, %borrower );
178
179     if (@empty_mandatory_fields || @$invalidformfields) {
180         $template->param(
181             empty_mandatory_fields => \@empty_mandatory_fields,
182             invalid_form_fields    => $invalidformfields,
183             borrower               => \%borrower
184         );
185
186         $template->param( action => 'edit' );
187     }
188     else {
189         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
190         if (%borrower_changes) {
191             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
192                 {
193                     template_name   => "opac-memberentry-update-submitted.tt",
194                     type            => "opac",
195                     query           => $cgi,
196                     authnotrequired => 1,
197                 }
198             );
199
200             my $m =
201               Koha::Borrower::Modifications->new(
202                 borrowernumber => $borrowernumber );
203
204             $m->DelModifications;
205             $m->AddModifications(\%borrower_changes);
206             $template->param(
207                 borrower => GetMember( borrowernumber => $borrowernumber ),
208             );
209         }
210         else {
211             $template->param(
212                 action => 'edit',
213                 nochanges => 1,
214                 borrower => GetMember( borrowernumber => $borrowernumber ),
215             );
216         }
217     }
218 }
219 elsif ( $action eq 'edit' ) {    #Display logged in borrower's data
220     my $borrower = GetMember( borrowernumber => $borrowernumber );
221
222     if (C4::Context->preference('ExtendedPatronAttributes')) {
223         my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
224         if (scalar(@$attributes) > 0) {
225             $borrower->{ExtendedPatronAttributes} = 1;
226             $borrower->{patron_attributes} = $attributes;
227         }
228     }
229
230     $template->param(
231         borrower => $borrower, );
232
233     if (C4::Context->preference('OPACpatronimages')) {
234         my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
235         if ($image) {
236             $template->param(
237                 display_patron_image => 1
238             );
239         }
240     }
241
242 }
243
244 my $captcha = random_string("CCCCC");
245
246 $template->param(
247     captcha        => $captcha,
248     captcha_digest => md5_base64($captcha)
249 );
250
251 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
252
253 sub GetHiddenFields {
254     my ($mandatory) = @_;
255     my %hidden_fields;
256
257     my $BorrowerUnwantedField =
258       C4::Context->preference("PatronSelfRegistrationBorrowerUnwantedField");
259
260     my @fields = split( /\|/, $BorrowerUnwantedField );
261     foreach (@fields) {
262         next unless m/\w/o;
263         #Don't hide mandatory fields
264         next if $mandatory->{$_};
265         $hidden_fields{$_} = 1;
266     }
267
268     return \%hidden_fields;
269 }
270
271 sub GetMandatoryFields {
272     my ($action) = @_;
273
274     my %mandatory_fields;
275
276     my $BorrowerMandatoryField =
277       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
278
279     my @fields = split( /\|/, $BorrowerMandatoryField );
280
281     foreach (@fields) {
282         $mandatory_fields{$_} = 1;
283     }
284
285     if ( $action eq 'create' || $action eq 'new' ) {
286         $mandatory_fields{'email'} = 1
287           if C4::Context->boolean_preference(
288             'PatronSelfRegistrationVerifyByEmail');
289     }
290
291     return \%mandatory_fields;
292 }
293
294 sub CheckMandatoryFields {
295     my ( $borrower, $action ) = @_;
296
297     my @empty_mandatory_fields;
298
299     my $mandatory_fields = GetMandatoryFields($action);
300     delete $mandatory_fields->{'cardnumber'};
301
302     foreach my $key ( keys %$mandatory_fields ) {
303         push( @empty_mandatory_fields, $key )
304           unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
305     }
306
307     return @empty_mandatory_fields;
308 }
309
310 sub CheckForInvalidFields {
311     my $borrower = shift;
312     my @invalidFields;
313     if ($borrower->{'email'}) {
314         push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
315     }
316     if ($borrower->{'emailpro'}) {
317         push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
318     }
319     if ($borrower->{'B_email'}) {
320         push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
321     }
322     return \@invalidFields;
323 }
324
325 sub ParseCgiForBorrower {
326     my ($cgi) = @_;
327
328     my $scrubber = C4::Scrubber->new();
329     my %borrower;
330
331     foreach ( $cgi->param ) {
332         if ( $_ =~ '^borrower_' ) {
333             my ($key) = substr( $_, 9 );
334             $borrower{$key} = $scrubber->scrub( scalar $cgi->param($_) );
335         }
336     }
337
338     my $dob_dt;
339     $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
340         if ( $borrower{'dateofbirth'} );
341
342     if ( $dob_dt ) {
343         $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
344     }
345     else {
346         # Trigger validation
347         $borrower{'dateofbirth'} = undef;
348     }
349
350     return %borrower;
351 }
352
353 sub DelUnchangedFields {
354     my ( $borrowernumber, %new_data ) = @_;
355
356     my $current_data = GetMember( borrowernumber => $borrowernumber );
357
358     foreach my $key ( keys %new_data ) {
359         if ( $current_data->{$key} eq $new_data{$key} ) {
360             delete $new_data{$key};
361         }
362     }
363
364     return %new_data;
365 }
366
367 sub DelEmptyFields {
368     my (%borrower) = @_;
369
370     foreach my $key ( keys %borrower ) {
371         delete $borrower{$key} unless $borrower{$key};
372     }
373
374     return %borrower;
375 }