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