Bug 16701: Translatability: Fix problem with isolated ' in currency.tt
[koha.git] / opac / opac-memberentry.pl
blobde157efbf4229630d20096c8b2915bea69ab64cc
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
23 use WWW::CSRF qw(generate_csrf_token check_csrf_token CSRF_OK);
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Form::MessagingPreferences;
29 use Koha::Patrons;
30 use Koha::Patron::Modifications;
31 use C4::Branch qw(GetBranchesLoop);
32 use C4::Scrubber;
33 use Email::Valid;
34 use Koha::DateUtils;
35 use Koha::Patron::Images;
37 my $cgi = new CGI;
38 my $dbh = C4::Context->dbh;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42 template_name => "opac-memberentry.tt",
43 type => "opac",
44 query => $cgi,
45 authnotrequired => 1,
49 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
51 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
52 exit;
55 my $action = $cgi->param('action') || q{};
56 if ( $action eq q{} ) {
57 if ($borrowernumber) {
58 $action = 'edit';
60 else {
61 $action = 'new';
65 my $mandatory = GetMandatoryFields($action);
67 $template->param(
68 action => $action,
69 hidden => GetHiddenFields( $mandatory, 'registration' ),
70 mandatory => $mandatory,
71 member_titles => GetTitles() || undef,
72 branches => GetBranchesLoop(),
73 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
76 if ( $action eq 'create' ) {
78 my %borrower = ParseCgiForBorrower($cgi);
80 %borrower = DelEmptyFields(%borrower);
82 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
83 my $invalidformfields = CheckForInvalidFields(\%borrower);
84 delete $borrower{'password2'};
85 my $cardnumber_error_code;
86 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
87 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
88 # spurious length warning.
89 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
92 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
93 if ( $cardnumber_error_code == 1 ) {
94 $template->param( cardnumber_already_exists => 1 );
95 } elsif ( $cardnumber_error_code == 2 ) {
96 $template->param( cardnumber_wrong_length => 1 );
99 $template->param(
100 empty_mandatory_fields => \@empty_mandatory_fields,
101 invalid_form_fields => $invalidformfields,
102 borrower => \%borrower
105 elsif (
106 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
108 $template->param(
109 failed_captcha => 1,
110 borrower => \%borrower
113 else {
114 if (
115 C4::Context->boolean_preference(
116 'PatronSelfRegistrationVerifyByEmail')
119 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
121 template_name => "opac-registration-email-sent.tt",
122 type => "opac",
123 query => $cgi,
124 authnotrequired => 1,
127 $template->param( 'email' => $borrower{'email'} );
129 my $verification_token = md5_hex( \%borrower );
130 $borrower{'password'} = random_string("..........");
132 Koha::Patron::Modifications->new(
133 verification_token => $verification_token )
134 ->AddModifications(\%borrower);
136 #Send verification email
137 my $letter = C4::Letters::GetPreparedLetter(
138 module => 'members',
139 letter_code => 'OPAC_REG_VERIFY',
140 tables => {
141 borrower_modifications => $verification_token,
145 C4::Letters::EnqueueLetter(
147 letter => $letter,
148 message_transport_type => 'email',
149 to_address => $borrower{'email'},
150 from_address =>
151 C4::Context->preference('KohaAdminEmailAddress'),
155 else {
156 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
158 template_name => "opac-registration-confirmation.tt",
159 type => "opac",
160 query => $cgi,
161 authnotrequired => 1,
165 $template->param( OpacPasswordChange =>
166 C4::Context->preference('OpacPasswordChange') );
168 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
169 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
171 $template->param( password_cleartext => $password );
172 $template->param(
173 borrower => GetMember( borrowernumber => $borrowernumber ) );
174 $template->param(
175 PatronSelfRegistrationAdditionalInstructions =>
176 C4::Context->preference(
177 'PatronSelfRegistrationAdditionalInstructions')
182 elsif ( $action eq 'update' ) {
184 my $borrower = GetMember( borrowernumber => $borrowernumber );
185 my $csrf_status = check_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')), scalar $cgi->param('csrf_token'));
186 die "Wrong CSRF token" unless ($csrf_status == CSRF_OK);
188 my %borrower = ParseCgiForBorrower($cgi);
190 my %borrower_changes = DelEmptyFields(%borrower);
191 my @empty_mandatory_fields =
192 CheckMandatoryFields( \%borrower_changes, $action );
193 my $invalidformfields = CheckForInvalidFields(\%borrower);
195 if (@empty_mandatory_fields || @$invalidformfields) {
196 $template->param(
197 empty_mandatory_fields => \@empty_mandatory_fields,
198 invalid_form_fields => $invalidformfields,
199 borrower => \%borrower,
200 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass'))),
203 $template->param( action => 'edit' );
205 else {
206 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
207 if (%borrower_changes) {
208 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
210 template_name => "opac-memberentry-update-submitted.tt",
211 type => "opac",
212 query => $cgi,
213 authnotrequired => 1,
217 my $m =
218 Koha::Patron::Modifications->new(
219 borrowernumber => $borrowernumber );
221 $m->DelModifications;
222 $m->AddModifications(\%borrower_changes);
223 $template->param(
224 borrower => GetMember( borrowernumber => $borrowernumber ),
227 else {
228 $template->param(
229 action => 'edit',
230 nochanges => 1,
231 borrower => GetMember( borrowernumber => $borrowernumber ),
232 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')))
237 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
238 my $borrower = GetMember( borrowernumber => $borrowernumber );
240 if (C4::Context->preference('ExtendedPatronAttributes')) {
241 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
242 if (scalar(@$attributes) > 0) {
243 $borrower->{ExtendedPatronAttributes} = 1;
244 $borrower->{patron_attributes} = $attributes;
248 $template->param(
249 borrower => $borrower,
250 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
251 hidden => GetHiddenFields( $mandatory, 'modification' ),
252 csrf_token => generate_csrf_token($borrower->{userid}, md5_base64(C4::Context->config('pass')))
255 if (C4::Context->preference('OPACpatronimages')) {
256 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
257 $template->param( display_patron_image => 1 ) if $patron_image;
262 my $captcha = random_string("CCCCC");
264 $template->param(
265 captcha => $captcha,
266 captcha_digest => md5_base64($captcha)
269 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
271 sub GetHiddenFields {
272 my ( $mandatory, $action ) = @_;
273 my %hidden_fields;
275 my $BorrowerUnwantedField = $action eq 'modification' ?
276 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
277 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
279 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
280 foreach (@fields) {
281 next unless m/\w/o;
282 #Don't hide mandatory fields
283 next if $mandatory->{$_};
284 $hidden_fields{$_} = 1;
287 return \%hidden_fields;
290 sub GetMandatoryFields {
291 my ($action) = @_;
293 my %mandatory_fields;
295 my $BorrowerMandatoryField =
296 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
298 my @fields = split( /\|/, $BorrowerMandatoryField );
300 foreach (@fields) {
301 $mandatory_fields{$_} = 1;
304 if ( $action eq 'create' || $action eq 'new' ) {
305 $mandatory_fields{'email'} = 1
306 if C4::Context->boolean_preference(
307 'PatronSelfRegistrationVerifyByEmail');
310 return \%mandatory_fields;
313 sub CheckMandatoryFields {
314 my ( $borrower, $action ) = @_;
316 my @empty_mandatory_fields;
318 my $mandatory_fields = GetMandatoryFields($action);
319 delete $mandatory_fields->{'cardnumber'};
321 foreach my $key ( keys %$mandatory_fields ) {
322 push( @empty_mandatory_fields, $key )
323 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
326 return @empty_mandatory_fields;
329 sub CheckForInvalidFields {
330 my $minpw = C4::Context->preference('minPasswordLength');
331 my $borrower = shift;
332 my @invalidFields;
333 if ($borrower->{'email'}) {
334 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
336 if ($borrower->{'emailpro'}) {
337 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
339 if ($borrower->{'B_email'}) {
340 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
342 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
343 push(@invalidFields, "password_match");
345 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
346 push(@invalidFields, "password_invalid");
348 if ( $borrower->{'password'} ) {
349 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
352 return \@invalidFields;
355 sub ParseCgiForBorrower {
356 my ($cgi) = @_;
358 my $scrubber = C4::Scrubber->new();
359 my %borrower;
361 foreach ( $cgi->param ) {
362 if ( $_ =~ '^borrower_' ) {
363 my ($key) = substr( $_, 9 );
364 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
368 my $dob_dt;
369 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
370 if ( $borrower{'dateofbirth'} );
372 if ( $dob_dt ) {
373 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
375 else {
376 # Trigger validation
377 $borrower{'dateofbirth'} = undef;
380 return %borrower;
383 sub DelUnchangedFields {
384 my ( $borrowernumber, %new_data ) = @_;
386 my $current_data = GetMember( borrowernumber => $borrowernumber );
388 foreach my $key ( keys %new_data ) {
389 if ( $current_data->{$key} eq $new_data{$key} ) {
390 delete $new_data{$key};
394 return %new_data;
397 sub DelEmptyFields {
398 my (%borrower) = @_;
400 foreach my $key ( keys %borrower ) {
401 delete $borrower{$key} unless $borrower{$key};
404 return %borrower;