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