Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / opac / opac-memberentry.pl
index f317ac8..a076cd3 100755 (executable)
@@ -75,8 +75,14 @@ if ( $action eq q{} ) {
 my $mandatory = GetMandatoryFields($action);
 
 my @libraries = Koha::Libraries->search;
-if ( my @libraries_to_display = split '\|', C4::Context->preference('PatronSelfRegistrationLibraryList') ) {
-    @libraries = map { my $b = $_; my $branchcode = $_->branchcode; grep { $_ eq $branchcode } @libraries_to_display ? $b : () } @libraries;
+if ( $action eq 'new'
+    && ( my @libraries_to_display = split '\|', C4::Context->preference('PatronSelfRegistrationLibraryList') )
+) {
+    @libraries = map {
+        my $b          = $_;
+        my $branchcode = $_->branchcode;
+        ( grep { $_ eq $branchcode } @libraries_to_display ) ? $b : ()
+    } @libraries;
 }
 my ( $min, $max ) = C4::Members::get_cardnumber_length();
 if ( defined $min ) {
@@ -101,11 +107,11 @@ foreach my $attr (@$attributes) {
     my $attribute = Koha::Patron::Attribute->new($attr);
     eval {$attribute->check_unique_id};
     if ( $@ ) {
-        my $attr_info = C4::Members::AttributeTypes->fetch($attr->{code});
+        my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
         $template->param(
             extended_unique_id_failed_code => $attr->{code},
             extended_unique_id_failed_value => $attr->{attribute},
-            extended_unique_id_failed_description => $attr_info->description()
+            extended_unique_id_failed_description => $attr_type->description,
         );
         $conflicting_attribute = 1;
     }
@@ -117,7 +123,7 @@ if ( $action eq 'create' ) {
 
     %borrower = DelEmptyFields(%borrower);
 
-    my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
+    my @empty_mandatory_fields = (CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
     delete $borrower{'password2'};
     my $cardnumber_error_code;
@@ -253,7 +259,7 @@ elsif ( $action eq 'update' ) {
     $borrower{borrowernumber} = $borrowernumber;
 
     my @empty_mandatory_fields =
-      CheckMandatoryFields( \%borrower, $action );
+      ( CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
 
     # Send back the data to the template
@@ -406,6 +412,20 @@ sub CheckMandatoryFields {
     return @empty_mandatory_fields;
 }
 
+sub CheckMandatoryAttributes{
+    my ( $borrower, $attributes ) = @_;
+
+    my @empty_mandatory_fields;
+
+    for my $attribute (@$attributes ) {
+        my $attr = Koha::Patron::Attribute::Types->find($attribute->{code});
+        push @empty_mandatory_fields, $attribute->{code}
+            if $attr && $attr->mandatory && $attribute->{attribute} =~ m|^\s*$|;
+    }
+
+    return @empty_mandatory_fields;
+}
+
 sub CheckForInvalidFields {
     my $borrower = shift;
     my @invalidFields;
@@ -427,7 +447,13 @@ sub CheckForInvalidFields {
             if ( $patrons_with_same_email ) {
                 push @invalidFields, "duplicate_email";
             }
+        } elsif ( C4::Context->preference("PatronSelfRegistrationConfirmEmail")
+            && $borrower->{'email'} ne $borrower->{'repeat_email'}
+            && !defined $borrower->{borrowernumber} ) {
+            push @invalidFields, "email_match";
         }
+        # email passed all tests, so prevent attempting to store repeat_email
+        delete $borrower->{'repeat_email'};
     }
     if ($borrower->{'emailpro'}) {
         push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
@@ -470,16 +496,18 @@ sub ParseCgiForBorrower {
         }
     }
 
-    my $dob_dt;
-    $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
-        if ( $borrower{'dateofbirth'} );
+    if ( defined $borrower{'dateofbirth'} ) {
+        my $dob_dt;
+        $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
+            if ( $borrower{'dateofbirth'} );
 
-    if ( $dob_dt ) {
-        $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
-    }
-    else {
-        # Trigger validation
-        $borrower{'dateofbirth'} = undef;
+        if ( $dob_dt ) {
+            $borrower{'dateofbirth'} = output_pref( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
+        }
+        else {
+            # Trigger validation
+            $borrower{'dateofbirth'} = undef;
+        }
     }
 
     # Replace checkbox 'agreed' by datetime in gdpr_proc_consent
@@ -677,7 +705,8 @@ sub ParsePatronAttributes {
     }
 
     foreach my $code ( keys %{$delete_candidates} ) {
-        if ( Koha::Patron::Attributes->search({
+        if ( not $borrowernumber # self-registration
+            || Koha::Patron::Attributes->search({
                 borrowernumber => $borrowernumber, code => $code })->count > 0 )
         {
             push @attributes, { code => $code, attribute => '' }