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>.
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use String::Random qw( random_string );
26 use C4::Form::MessagingPreferences;
28 use Koha::Patron::Modifications;
29 use C4::Branch qw(GetBranchesLoop);
33 use Koha::Patron::Images;
36 my $dbh = C4::Context->dbh;
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "opac-memberentry.tt",
47 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
49 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
53 my $action = $cgi->param('action') || q{};
54 if ( $action eq q{} ) {
55 if ($borrowernumber) {
63 my $mandatory = GetMandatoryFields($action);
67 hidden => GetHiddenFields( $mandatory, 'registration' ),
68 mandatory => $mandatory,
69 branches => GetBranchesLoop(),
70 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
73 if ( $action eq 'create' ) {
75 my %borrower = ParseCgiForBorrower($cgi);
77 %borrower = DelEmptyFields(%borrower);
79 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
80 my $invalidformfields = CheckForInvalidFields(\%borrower);
81 delete $borrower{'password2'};
82 my $cardnumber_error_code;
83 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
84 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
85 # spurious length warning.
86 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
89 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
90 if ( $cardnumber_error_code == 1 ) {
91 $template->param( cardnumber_already_exists => 1 );
92 } elsif ( $cardnumber_error_code == 2 ) {
93 $template->param( cardnumber_wrong_length => 1 );
97 empty_mandatory_fields => \@empty_mandatory_fields,
98 invalid_form_fields => $invalidformfields,
99 borrower => \%borrower
103 md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
107 borrower => \%borrower
112 C4::Context->boolean_preference(
113 'PatronSelfRegistrationVerifyByEmail')
116 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
118 template_name => "opac-registration-email-sent.tt",
121 authnotrequired => 1,
124 $template->param( 'email' => $borrower{'email'} );
126 my $verification_token = md5_hex( time().{}.rand().{}.$$ );
128 while ( Koha::Patron::Modifications->search( { verification_token => $verification_token } )->count() ) {
129 $verification_token = md5_hex( time().{}.rand().{}.$$ );
132 $borrower{'password'} = random_string("..........");
134 Koha::Patron::Modifications->new(
135 verification_token => $verification_token )
136 ->AddModifications(\%borrower);
138 #Send verification email
139 my $letter = C4::Letters::GetPreparedLetter(
141 letter_code => 'OPAC_REG_VERIFY',
143 borrower_modifications => $verification_token,
147 C4::Letters::EnqueueLetter(
150 message_transport_type => 'email',
151 to_address => $borrower{'email'},
153 C4::Context->preference('KohaAdminEmailAddress'),
158 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
160 template_name => "opac-registration-confirmation.tt",
163 authnotrequired => 1,
167 $template->param( OpacPasswordChange =>
168 C4::Context->preference('OpacPasswordChange') );
170 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
171 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
173 $template->param( password_cleartext => $password );
175 borrower => GetMember( borrowernumber => $borrowernumber ) );
177 PatronSelfRegistrationAdditionalInstructions =>
178 C4::Context->preference(
179 'PatronSelfRegistrationAdditionalInstructions')
184 elsif ( $action eq 'update' ) {
186 my $borrower = GetMember( borrowernumber => $borrowernumber );
187 my %borrower = ParseCgiForBorrower($cgi);
189 my %borrower_changes = DelEmptyFields(%borrower);
190 my @empty_mandatory_fields =
191 CheckMandatoryFields( \%borrower_changes, $action );
192 my $invalidformfields = CheckForInvalidFields(\%borrower);
194 # Send back the data to the template
195 %borrower = ( %$borrower, %borrower );
197 if (@empty_mandatory_fields || @$invalidformfields) {
199 empty_mandatory_fields => \@empty_mandatory_fields,
200 invalid_form_fields => $invalidformfields,
201 borrower => \%borrower
204 $template->param( action => 'edit' );
207 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
208 if (%borrower_changes) {
209 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
211 template_name => "opac-memberentry-update-submitted.tt",
214 authnotrequired => 1,
219 Koha::Patron::Modifications->new(
220 borrowernumber => $borrowernumber );
222 $m->DelModifications;
223 $m->AddModifications(\%borrower_changes);
225 borrower => GetMember( borrowernumber => $borrowernumber ),
232 borrower => GetMember( borrowernumber => $borrowernumber ),
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;
249 borrower => $borrower,
250 guarantor => scalar Koha::Patrons->find($borrowernumber)->guarantor(),
251 hidden => GetHiddenFields( $mandatory, 'modification' ),
254 if (C4::Context->preference('OPACpatronimages')) {
255 my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
256 $template->param( display_patron_image => 1 ) if $patron_image;
261 my $captcha = random_string("CCCCC");
265 captcha_digest => md5_base64($captcha)
268 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
270 sub GetHiddenFields {
271 my ( $mandatory, $action ) = @_;
274 my $BorrowerUnwantedField = $action eq 'modification' ?
275 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
276 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
278 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
281 #Don't hide mandatory fields
282 next if $mandatory->{$_};
283 $hidden_fields{$_} = 1;
286 return \%hidden_fields;
289 sub GetMandatoryFields {
292 my %mandatory_fields;
294 my $BorrowerMandatoryField =
295 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
297 my @fields = split( /\|/, $BorrowerMandatoryField );
300 $mandatory_fields{$_} = 1;
303 if ( $action eq 'create' || $action eq 'new' ) {
304 $mandatory_fields{'email'} = 1
305 if C4::Context->boolean_preference(
306 'PatronSelfRegistrationVerifyByEmail');
309 return \%mandatory_fields;
312 sub CheckMandatoryFields {
313 my ( $borrower, $action ) = @_;
315 my @empty_mandatory_fields;
317 my $mandatory_fields = GetMandatoryFields($action);
318 delete $mandatory_fields->{'cardnumber'};
320 foreach my $key ( keys %$mandatory_fields ) {
321 push( @empty_mandatory_fields, $key )
322 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
325 return @empty_mandatory_fields;
328 sub CheckForInvalidFields {
329 my $minpw = C4::Context->preference('minPasswordLength');
330 my $borrower = shift;
332 if ($borrower->{'email'}) {
333 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
335 if ($borrower->{'emailpro'}) {
336 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
338 if ($borrower->{'B_email'}) {
339 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
341 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
342 push(@invalidFields, "password_match");
344 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
345 push(@invalidFields, "password_invalid");
347 if ( $borrower->{'password'} ) {
348 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
351 return \@invalidFields;
354 sub ParseCgiForBorrower {
357 my $scrubber = C4::Scrubber->new();
360 foreach ( $cgi->param ) {
361 if ( $_ =~ '^borrower_' ) {
362 my ($key) = substr( $_, 9 );
363 $borrower{$key} = $scrubber->scrub( scalar $cgi->param($_) );
368 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
369 if ( $borrower{'dateofbirth'} );
372 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
376 $borrower{'dateofbirth'} = undef;
382 sub DelUnchangedFields {
383 my ( $borrowernumber, %new_data ) = @_;
385 my $current_data = GetMember( borrowernumber => $borrowernumber );
387 foreach my $key ( keys %new_data ) {
388 if ( $current_data->{$key} eq $new_data{$key} ) {
389 delete $new_data{$key};
399 foreach my $key ( keys %borrower ) {
400 delete $borrower{$key} unless $borrower{$key};