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 );
23 use List
::MoreUtils
qw( any each_array uniq );
24 use String
::Random
qw( random_string );
29 use C4
::Members
::Attributes
qw( GetBorrowerAttributes );
30 use C4
::Form
::MessagingPreferences
;
35 use Koha
::Patron
::Attribute
::Types
;
36 use Koha
::Patron
::Attributes
;
37 use Koha
::Patron
::Images
;
38 use Koha
::Patron
::Modification
;
39 use Koha
::Patron
::Modifications
;
44 my $dbh = C4
::Context
->dbh;
46 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
48 template_name
=> "opac-memberentry.tt",
55 unless ( C4
::Context
->preference('PatronSelfRegistration') || $borrowernumber )
57 print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
61 my $action = $cgi->param('action') || q{};
62 if ( $action eq q{} ) {
63 if ($borrowernumber) {
71 my $mandatory = GetMandatoryFields
($action);
73 my @libraries = Koha
::Libraries
->search;
74 if ( my @libraries_to_display = split '\|', C4
::Context
->preference('PatronSelfRegistrationLibraryList') ) {
75 @libraries = map { my $b = $_; my $branchcode = $_->branchcode; grep( /^$branchcode$/, @libraries_to_display ) ?
$b : () } @libraries;
77 my ( $min, $max ) = C4
::Members
::get_cardnumber_length
();
80 minlength_cardnumber
=> $min,
81 maxlength_cardnumber
=> $max
87 hidden
=> GetHiddenFields
( $mandatory, 'registration' ),
88 mandatory
=> $mandatory,
89 libraries
=> \
@libraries,
90 OPACPatronDetails
=> C4
::Context
->preference('OPACPatronDetails'),
93 my $attributes = ParsePatronAttributes
($borrowernumber,$cgi);
94 my $conflicting_attribute = 0;
96 foreach my $attr (@
$attributes) {
97 unless ( C4
::Members
::Attributes
::CheckUniqueness
($attr->{code
}, $attr->{value
}, $borrowernumber) ) {
98 my $attr_info = C4
::Members
::AttributeTypes
->fetch($attr->{code
});
100 extended_unique_id_failed_code
=> $attr->{code
},
101 extended_unique_id_failed_value
=> $attr->{value
},
102 extended_unique_id_failed_description
=> $attr_info->description()
104 $conflicting_attribute = 1;
108 if ( $action eq 'create' ) {
110 my %borrower = ParseCgiForBorrower
($cgi);
112 %borrower = DelEmptyFields
(%borrower);
114 my @empty_mandatory_fields = CheckMandatoryFields
( \
%borrower, $action );
115 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
116 delete $borrower{'password2'};
117 my $cardnumber_error_code;
118 if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
119 # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
120 # spurious length warning.
121 $cardnumber_error_code = checkcardnumber
( $borrower{cardnumber
}, $borrower{borrowernumber
} );
124 if ( @empty_mandatory_fields || @
$invalidformfields || $cardnumber_error_code || $conflicting_attribute ) {
125 if ( $cardnumber_error_code == 1 ) {
126 $template->param( cardnumber_already_exists
=> 1 );
127 } elsif ( $cardnumber_error_code == 2 ) {
128 $template->param( cardnumber_wrong_length
=> 1 );
132 empty_mandatory_fields
=> \
@empty_mandatory_fields,
133 invalid_form_fields
=> $invalidformfields,
134 borrower
=> \
%borrower
136 $template->param( patron_attribute_classes
=> GeneratePatronAttributesForm
( undef, $attributes ) );
139 md5_base64
( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
143 borrower
=> \
%borrower
145 $template->param( patron_attribute_classes
=> GeneratePatronAttributesForm
( undef, $attributes ) );
149 C4
::Context
->boolean_preference(
150 'PatronSelfRegistrationVerifyByEmail')
153 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
155 template_name
=> "opac-registration-email-sent.tt",
158 authnotrequired
=> 1,
161 $template->param( 'email' => $borrower{'email'} );
163 my $verification_token = md5_hex
( time().{}.rand().{}.$$ );
164 while ( Koha
::Patron
::Modifications
->search( { verification_token
=> $verification_token } )->count() ) {
165 $verification_token = md5_hex
( time().{}.rand().{}.$$ );
168 $borrower{password
} = random_string
("..........");
169 $borrower{verification_token
} = $verification_token;
171 Koha
::Patron
::Modification
->new( \
%borrower )->store();
173 #Send verification email
174 my $letter = C4
::Letters
::GetPreparedLetter
(
176 letter_code
=> 'OPAC_REG_VERIFY',
178 borrower_modifications
=> $verification_token,
182 C4
::Letters
::EnqueueLetter
(
185 message_transport_type
=> 'email',
186 to_address
=> $borrower{'email'},
188 C4
::Context
->preference('KohaAdminEmailAddress'),
193 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
195 template_name
=> "opac-registration-confirmation.tt",
198 authnotrequired
=> 1,
202 $template->param( OpacPasswordChange
=>
203 C4
::Context
->preference('OpacPasswordChange') );
205 my ( $borrowernumber, $password ) = AddMember_Opac
(%borrower);
206 C4
::Members
::Attributes
::SetBorrowerAttributes
( $borrowernumber, $attributes );
207 C4
::Form
::MessagingPreferences
::handle_form_action
($cgi, { borrowernumber
=> $borrowernumber }, $template, 1, C4
::Context
->preference('PatronSelfRegistrationDefaultCategory') ) if $borrowernumber && C4
::Context
->preference('EnhancedMessagingPreferences');
209 $template->param( password_cleartext
=> $password );
211 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ) );
213 PatronSelfRegistrationAdditionalInstructions
=>
214 C4
::Context
->preference(
215 'PatronSelfRegistrationAdditionalInstructions')
220 elsif ( $action eq 'update' ) {
222 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
223 die "Wrong CSRF token"
224 unless Koha
::Token
->new->check_csrf({
225 session_id
=> scalar $cgi->cookie('CGISESSID'),
226 token
=> scalar $cgi->param('csrf_token'),
229 my %borrower = ParseCgiForBorrower
($cgi);
231 my %borrower_changes = DelEmptyFields
(%borrower);
232 my @empty_mandatory_fields =
233 CheckMandatoryFields
( \
%borrower_changes, $action );
234 my $invalidformfields = CheckForInvalidFields
(\
%borrower);
236 # Send back the data to the template
237 %borrower = ( %$borrower, %borrower );
239 if (@empty_mandatory_fields || @
$invalidformfields) {
241 empty_mandatory_fields
=> \
@empty_mandatory_fields,
242 invalid_form_fields
=> $invalidformfields,
243 borrower
=> \
%borrower,
244 csrf_token
=> Koha
::Token
->new->generate_csrf({
245 session_id
=> scalar $cgi->cookie('CGISESSID'),
248 $template->param( patron_attribute_classes
=> GeneratePatronAttributesForm
( $borrowernumber, $attributes ) );
250 $template->param( action
=> 'edit' );
253 my %borrower_changes = DelUnchangedFields
( $borrowernumber, %borrower );
254 my $extended_attributes_changes = FilterUnchangedAttributes
( $borrowernumber, $attributes );
256 if ( %borrower_changes || scalar @
{$extended_attributes_changes} > 0 ) {
257 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
259 template_name
=> "opac-memberentry-update-submitted.tt",
262 authnotrequired
=> 1,
266 $borrower_changes{borrowernumber
} = $borrowernumber;
267 $borrower_changes{extended_attributes
} = to_json
($extended_attributes_changes);
269 # FIXME update the following with
270 # Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber })->delete;
271 # when bug 17091 will be pushed
272 my $patron_modifications = Koha
::Patron
::Modifications
->search({ borrowernumber
=> $borrowernumber });
273 while ( my $patron_modification = $patron_modifications->next ) {
274 $patron_modification->delete;
277 my $m = Koha
::Patron
::Modification
->new( \
%borrower_changes )->store();
280 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
287 borrower
=> GetMember
( borrowernumber
=> $borrowernumber ),
288 patron_attribute_classes
=> GeneratePatronAttributesForm
( $borrowernumber, $attributes ),
289 csrf_token
=> Koha
::Token
->new->generate_csrf({
290 session_id
=> scalar $cgi->cookie('CGISESSID'),
296 elsif ( $action eq 'edit' ) { #Display logged in borrower's data
297 my $borrower = GetMember
( borrowernumber
=> $borrowernumber );
300 borrower
=> $borrower,
301 guarantor
=> scalar Koha
::Patrons
->find($borrowernumber)->guarantor(),
302 hidden
=> GetHiddenFields
( $mandatory, 'modification' ),
303 csrf_token
=> Koha
::Token
->new->generate_csrf({
304 session_id
=> scalar $cgi->cookie('CGISESSID'),
308 if (C4
::Context
->preference('OPACpatronimages')) {
309 my $patron_image = Koha
::Patron
::Images
->find($borrower->{borrowernumber
});
310 $template->param( display_patron_image
=> 1 ) if $patron_image;
313 $template->param( patron_attribute_classes
=> GeneratePatronAttributesForm
( $borrowernumber ) );
315 # Render self-registration page
316 $template->param( patron_attribute_classes
=> GeneratePatronAttributesForm
() );
319 my $captcha = random_string
("CCCCC");
323 captcha_digest
=> md5_base64
($captcha)
326 output_html_with_http_headers
$cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 };
328 sub GetHiddenFields
{
329 my ( $mandatory, $action ) = @_;
332 my $BorrowerUnwantedField = $action eq 'modification' ?
333 C4
::Context
->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
334 C4
::Context
->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
336 my @fields = split( /\|/, $BorrowerUnwantedField || q
|| );
339 #Don't hide mandatory fields
340 next if $mandatory->{$_};
341 $hidden_fields{$_} = 1;
344 return \
%hidden_fields;
347 sub GetMandatoryFields
{
350 my %mandatory_fields;
352 my $BorrowerMandatoryField =
353 C4
::Context
->preference("PatronSelfRegistrationBorrowerMandatoryField");
355 my @fields = split( /\|/, $BorrowerMandatoryField );
358 $mandatory_fields{$_} = 1;
361 if ( $action eq 'create' || $action eq 'new' ) {
362 $mandatory_fields{'email'} = 1
363 if C4
::Context
->boolean_preference(
364 'PatronSelfRegistrationVerifyByEmail');
367 return \
%mandatory_fields;
370 sub CheckMandatoryFields
{
371 my ( $borrower, $action ) = @_;
373 my @empty_mandatory_fields;
375 my $mandatory_fields = GetMandatoryFields
($action);
376 delete $mandatory_fields->{'cardnumber'};
378 foreach my $key ( keys %$mandatory_fields ) {
379 push( @empty_mandatory_fields, $key )
380 unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
383 return @empty_mandatory_fields;
386 sub CheckForInvalidFields
{
387 my $minpw = C4
::Context
->preference('minPasswordLength');
388 my $borrower = shift;
390 if ($borrower->{'email'}) {
391 unless ( Email
::Valid
->address($borrower->{'email'}) ) {
392 push(@invalidFields, "email");
393 } elsif ( C4
::Context
->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
394 my $patrons_with_same_email = Koha
::Patrons
->search( { email
=> $borrower->{email
} })->count;
395 if ( $patrons_with_same_email ) {
396 push @invalidFields, "duplicate_email";
400 if ($borrower->{'emailpro'}) {
401 push(@invalidFields, "emailpro") if (!Email
::Valid
->address($borrower->{'emailpro'}));
403 if ($borrower->{'B_email'}) {
404 push(@invalidFields, "B_email") if (!Email
::Valid
->address($borrower->{'B_email'}));
406 if ( defined $borrower->{'password'}
407 and $borrower->{'password'} ne $borrower->{'password2'} )
409 push( @invalidFields, "password_match" );
411 if ( $borrower->{'password'} && $minpw && (length($borrower->{'password'}) < $minpw) ) {
412 push(@invalidFields, "password_invalid");
414 if ( $borrower->{'password'} ) {
415 push(@invalidFields, "password_spaces") if ($borrower->{'password'} =~ /^\s/ or $borrower->{'password'} =~ /\s$/);
418 return \
@invalidFields;
421 sub ParseCgiForBorrower
{
424 my $scrubber = C4
::Scrubber
->new();
427 foreach ( $cgi->param ) {
428 if ( $_ =~ '^borrower_' ) {
429 my ($key) = substr( $_, 9 );
430 $borrower{$key} = $scrubber->scrub( scalar $cgi->param($_) );
435 $dob_dt = eval { dt_from_string
( $borrower{'dateofbirth'} ); }
436 if ( $borrower{'dateofbirth'} );
439 $borrower{'dateofbirth'} = output_pref
( { dt
=> $dob_dt, dateonly
=> 1, dateformat
=> 'iso' } );
443 $borrower{'dateofbirth'} = undef;
449 sub DelUnchangedFields
{
450 my ( $borrowernumber, %new_data ) = @_;
452 my $current_data = GetMember
( borrowernumber
=> $borrowernumber );
454 foreach my $key ( keys %new_data ) {
455 if ( $current_data->{$key} eq $new_data{$key} ) {
456 delete $new_data{$key};
466 foreach my $key ( keys %borrower ) {
467 delete $borrower{$key} unless $borrower{$key};
473 sub FilterUnchangedAttributes
{
474 my ( $borrowernumber, $entered_attributes ) = @_;
476 my @patron_attributes = grep {$_->opac_editable} Koha
::Patron
::Attributes
->search({ borrowernumber
=> $borrowernumber })->as_list;
478 my $patron_attribute_types;
479 foreach my $attr (@patron_attributes) {
480 $patron_attribute_types->{ $attr->code } += 1;
483 my $passed_attribute_types;
484 foreach my $attr (@
{ $entered_attributes }) {
485 $passed_attribute_types->{ $attr->{ code
} } += 1;
488 my @changed_attributes;
490 # Loop through the current patron attributes
491 foreach my $attribute_type ( keys %{ $patron_attribute_types } ) {
492 if ( $patron_attribute_types->{ $attribute_type } != $passed_attribute_types->{ $attribute_type } ) {
493 # count differs, overwrite all attributes for given type
494 foreach my $attr (@
{ $entered_attributes }) {
495 push @changed_attributes, $attr
496 if $attr->{ code
} eq $attribute_type;
499 # count matches, check values
501 foreach my $attr (grep { $_->code eq $attribute_type } @patron_attributes) {
503 unless any
{ $_->{ value
} eq $attr->attribute } @
{ $entered_attributes };
508 foreach my $attr (@
{ $entered_attributes }) {
509 push @changed_attributes, $attr
510 if $attr->{ code
} eq $attribute_type;
516 # Loop through passed attributes, looking for new ones
517 foreach my $attribute_type ( keys %{ $passed_attribute_types } ) {
518 if ( !defined $patron_attribute_types->{ $attribute_type } ) {
520 foreach my $attr (grep { $_->{code
} eq $attribute_type } @
{ $entered_attributes }) {
521 push @changed_attributes, $attr;
526 return \
@changed_attributes;
529 sub GeneratePatronAttributesForm
{
530 my ( $borrowernumber, $entered_attributes ) = @_;
532 # Get all attribute types and the values for this patron (if applicable)
533 my @types = grep { $_->opac_editable() or $_->opac_display }
534 Koha
::Patron
::Attribute
::Types
->search()->as_list();
535 if ( scalar(@types) == 0 ) {
539 my @displayable_attributes = grep { $_->opac_display }
540 Koha
::Patron
::Attributes
->search({ borrowernumber
=> $borrowernumber })->as_list;
542 my %attr_values = ();
544 # Build the attribute values list either from the passed values
545 # or taken from the patron itself
546 if ( defined $entered_attributes ) {
547 foreach my $attr (@
$entered_attributes) {
548 push @
{ $attr_values{ $attr->{code
} } }, $attr->{value
};
551 elsif ( defined $borrowernumber ) {
552 my @editable_attributes = grep { $_->opac_editable } @displayable_attributes;
553 foreach my $attr (@editable_attributes) {
554 push @
{ $attr_values{ $attr->code } }, $attr->attribute;
558 # Add the non-editable attributes (that don't come from the form)
559 foreach my $attr ( grep { !$_->opac_editable } @displayable_attributes ) {
560 push @
{ $attr_values{ $attr->code } }, $attr->attribute;
563 # Find all existing classes
564 my @classes = sort( uniq
( map { $_->class } @types ) );
567 foreach my $attr_type (@types) {
568 push @
{ $items_by_class{ $attr_type->class() } }, {
570 # If editable, make sure there's at least one empty entry,
571 # to make the template's job easier
572 values => $attr_values{ $attr_type->code() } || ['']
574 unless !defined $attr_values{ $attr_type->code() }
575 and !$attr_type->opac_editable;
578 # Finally, build a list of containing classes
580 foreach my $class (@classes) {
581 next unless ( $items_by_class{$class} );
583 my $av = Koha
::AuthorisedValues
->search(
584 { category
=> 'PA_CLASS', authorised_value
=> $class } );
586 my $lib = $av->count ?
$av->next->opac_description : $class;
591 items
=> $items_by_class{$class},
599 sub ParsePatronAttributes
{
600 my ( $borrowernumber, $cgi ) = @_;
602 my @codes = $cgi->multi_param('patron_attribute_code');
603 my @values = $cgi->multi_param('patron_attribute_value');
605 my @editable_attribute_types
606 = map { $_->code } Koha
::Patron
::Attribute
::Types
->search({ opac_editable
=> 1 });
608 my $ea = each_array
( @codes, @values );
611 my $delete_candidates = {};
613 while ( my ( $code, $value ) = $ea->() ) {
614 if ( any
{ $_ eq $code } @editable_attribute_types ) {
615 # It is an editable attribute
616 if ( !defined($value) or $value eq '' ) {
617 $delete_candidates->{$code} = 1
618 unless $delete_candidates->{$code};
622 push @attributes, { code
=> $code, value
=> $value };
624 # 'code' is no longer a delete candidate
625 delete $delete_candidates->{$code}
626 if defined $delete_candidates->{$code};
631 foreach my $code ( keys %{$delete_candidates} ) {
632 if ( Koha
::Patron
::Attributes
->search({
633 borrowernumber
=> $borrowernumber, code
=> $code })->count > 0 )
635 push @attributes, { code
=> $code, value
=> '' }
636 unless any
{ $_->{code
} eq $code } @attributes;