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 );
27 use C4::Form::MessagingPreferences;
29 use Koha::Borrower::Modifications;
30 use C4::Branch qw(GetBranchesLoop);
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 member_titles => GetTitles() || undef,
70 branches => GetBranchesLoop(),
71 OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
74 if ( $action eq 'create' ) {
76 my %borrower = ParseCgiForBorrower($cgi);
78 %borrower = DelEmptyFields(%borrower);
80 my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
81 my $invalidformfields = CheckForInvalidFields(\%borrower);
82 delete $borrower{'password2'};
83 my $cardnumber_error_code;
84 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
85 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
86 # spurious length warning.
87 $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
90 if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
91 if ( $cardnumber_error_code == 1 ) {
92 $template->param( cardnumber_already_exists => 1 );
93 } elsif ( $cardnumber_error_code == 2 ) {
94 $template->param( cardnumber_wrong_length => 1 );
98 empty_mandatory_fields => \@empty_mandatory_fields,
99 invalid_form_fields => $invalidformfields,
100 borrower => \%borrower
104 md5_base64( $cgi->param('captcha') ) ne $cgi->param('captcha_digest') )
108 borrower => \%borrower
113 C4::Context->boolean_preference(
114 'PatronSelfRegistrationVerifyByEmail')
117 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
119 template_name => "opac-registration-email-sent.tt",
122 authnotrequired => 1,
125 $template->param( 'email' => $borrower{'email'} );
127 my $verification_token = md5_hex( \%borrower );
128 $borrower{'password'} = random_string("..........");
129 Koha::Borrower::Modifications->new(
130 verification_token => $verification_token )
131 ->AddModifications(\%borrower);
133 #Send verification email
134 my $letter = C4::Letters::GetPreparedLetter(
136 letter_code => 'OPAC_REG_VERIFY',
138 borrower_modifications => $verification_token,
142 C4::Letters::EnqueueLetter(
145 message_transport_type => 'email',
146 to_address => $borrower{'email'},
148 C4::Context->preference('KohaAdminEmailAddress'),
153 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
155 template_name => "opac-registration-confirmation.tt",
158 authnotrequired => 1,
162 $template->param( OpacPasswordChange =>
163 C4::Context->preference('OpacPasswordChange') );
165 my ( $borrowernumber, $password ) = AddMember_Opac(%borrower);
166 C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4::Context->preference('EnhancedMessagingPreferences');
168 $template->param( password_cleartext => $password );
170 borrower => GetMember( borrowernumber => $borrowernumber ) );
172 PatronSelfRegistrationAdditionalInstructions =>
173 C4::Context->preference(
174 'PatronSelfRegistrationAdditionalInstructions')
179 elsif ( $action eq 'update' ) {
181 my %borrower = ParseCgiForBorrower($cgi);
183 my %borrower_changes = DelEmptyFields(%borrower);
184 my @empty_mandatory_fields =
185 CheckMandatoryFields( \%borrower_changes, $action );
186 my $invalidformfields = CheckForInvalidFields(\%borrower);
188 if (@empty_mandatory_fields || @$invalidformfields) {
190 empty_mandatory_fields => \@empty_mandatory_fields,
191 invalid_form_fields => $invalidformfields,
192 borrower => \%borrower
195 $template->param( action => 'edit' );
198 my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
199 if (%borrower_changes) {
200 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
202 template_name => "opac-memberentry-update-submitted.tt",
205 authnotrequired => 1,
210 Koha::Borrower::Modifications->new(
211 borrowernumber => $borrowernumber );
213 $m->DelModifications;
214 $m->AddModifications(\%borrower_changes);
216 borrower => GetMember( borrowernumber => $borrowernumber ),
223 borrower => GetMember( borrowernumber => $borrowernumber ),
228 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
229 my $borrower = GetMember( borrowernumber => $borrowernumber );
231 if (C4::Context->preference('ExtendedPatronAttributes')) {
232 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 'opac');
233 if (scalar(@$attributes) > 0) {
234 $borrower->{ExtendedPatronAttributes} = 1;
235 $borrower->{patron_attributes} = $attributes;
240 borrower => $borrower,
241 guarantor => scalar Koha::Borrowers->find($borrowernumber)->guarantor(),
242 hidden => GetHiddenFields( $mandatory, 'modification' ),
245 if (C4::Context->preference('OPACpatronimages')) {
246 my ($image, $dberror) = GetPatronImage($borrower->{borrowernumber});
249 display_patron_image => 1
256 my $captcha = random_string("CCCCC");
260 captcha_digest => md5_base64($captcha)
263 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
265 sub GetHiddenFields {
266 my ( $mandatory, $action ) = @_;
269 my $BorrowerUnwantedField = $action eq 'modification' ?
270 C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
271 C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
273 my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
276 #Don't hide mandatory fields
277 next if $mandatory->{$_};
278 $hidden_fields{$_} = 1;
281 return \%hidden_fields;
284 sub GetMandatoryFields {
287 my %mandatory_fields;
289 my $BorrowerMandatoryField =
290 C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
292 my @fields = split( /\|/, $BorrowerMandatoryField );
295 $mandatory_fields{$_} = 1;
298 if ( $action eq 'create' || $action eq 'new' ) {
299 $mandatory_fields{'email'} = 1
300 if C4::Context->boolean_preference(
301 'PatronSelfRegistrationVerifyByEmail');
304 return \%mandatory_fields;
307 sub CheckMandatoryFields {
308 my ( $borrower, $action ) = @_;
310 my @empty_mandatory_fields;
312 my $mandatory_fields = GetMandatoryFields($action);
313 delete $mandatory_fields->{'cardnumber'};
315 foreach my $key ( keys %$mandatory_fields ) {
316 push( @empty_mandatory_fields, $key )
317 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
320 return @empty_mandatory_fields;
323 sub CheckForInvalidFields {
324 my $minpw = C4::Context->preference('minPasswordLength');
325 my $borrower = shift;
327 if ($borrower->{'email'}) {
328 push(@invalidFields, "email") if (!Email::Valid->address($borrower->{'email'}));
330 if ($borrower->{'emailpro'}) {
331 push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
333 if ($borrower->{'B_email'}) {
334 push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
336 if ( $borrower->{'password'} ne $borrower->{'password2'} ){
337 push(@invalidFields, "password_match");
339 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
340 push(@invalidFields, "password_invalid");
342 if ( $borrower->{'password'} ) {
343 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
346 return \@invalidFields;
349 sub ParseCgiForBorrower {
352 my $scrubber = C4::Scrubber->new();
355 foreach ( $cgi->param ) {
356 if ( $_ =~ '^borrower_' ) {
357 my ($key) = substr( $_, 9 );
358 $borrower{$key} = $scrubber->scrub( $cgi->param($_) );
363 $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
364 if ( $borrower{'dateofbirth'} );
367 $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
371 $borrower{'dateofbirth'} = undef;
377 sub DelUnchangedFields {
378 my ( $borrowernumber, %new_data ) = @_;
380 my $current_data = GetMember( borrowernumber => $borrowernumber );
382 foreach my $key ( keys %new_data ) {
383 if ( $current_data->{$key} eq $new_data{$key} ) {
384 delete $new_data{$key};
394 foreach my $key ( keys %borrower ) {
395 delete $borrower{$key} unless $borrower{$key};