Bug 29408: Add JSDoc documentation for kohaTable function
[koha-ffzg.git] / opac / opac-memberentry.pl
index 6b7afc0..5fab449 100755 (executable)
@@ -19,14 +19,14 @@ use Modern::Perl;
 
 use CGI qw ( -utf8 );
 use Digest::MD5 qw( md5_base64 md5_hex );
-use JSON;
+use JSON qw( to_json );
 use List::MoreUtils qw( any each_array uniq );
 use String::Random qw( random_string );
 
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
-use C4::Members;
+use C4::Members qw( checkcardnumber );
 use C4::Form::MessagingPreferences;
 use Koha::AuthUtils;
 use Koha::Patrons;
@@ -34,8 +34,8 @@ use Koha::Patron::Consent;
 use Koha::Patron::Modification;
 use Koha::Patron::Modifications;
 use C4::Scrubber;
-use Email::Valid;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Email;
 use Koha::Libraries;
 use Koha::Patron::Attribute::Types;
 use Koha::Patron::Attributes;
@@ -44,7 +44,7 @@ use Koha::Patron::Modification;
 use Koha::Patron::Modifications;
 use Koha::Patron::Categories;
 use Koha::Token;
-
+use Koha::AuthorisedValues;
 my $cgi = CGI->new;
 my $dbh = C4::Context->dbh;
 
@@ -64,6 +64,11 @@ unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
 }
 
 my $action = $cgi->param('action') || q{};
+if ( $borrowernumber && ( $action eq 'create' || $action eq 'new' ) ) {
+    print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
+    exit;
+}
+
 if ( $action eq q{} ) {
     if ($borrowernumber) {
         $action = 'edit';
@@ -75,16 +80,14 @@ if ( $action eq q{} ) {
 
 my $mandatory = GetMandatoryFields($action);
 
-my @libraries = Koha::Libraries->search;
-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 $params = {};
+if ( $action eq 'create' || $action eq 'new' ) {
+    my @PatronSelfRegistrationLibraryList = split '\|', C4::Context->preference('PatronSelfRegistrationLibraryList');
+    $params = { branchcode => { -in => \@PatronSelfRegistrationLibraryList } }
+      if @PatronSelfRegistrationLibraryList;
 }
+my @libraries = Koha::Libraries->search($params);
+
 my ( $min, $max ) = C4::Members::get_cardnumber_length();
 if ( defined $min ) {
      $template->param(
@@ -93,12 +96,15 @@ if ( defined $min ) {
      );
  }
 
+my $defaultCategory = Koha::Patron::Categories->find(C4::Context->preference('PatronSelfRegistrationDefaultCategory'));
+
 $template->param(
     action            => $action,
     hidden            => GetHiddenFields( $mandatory, $action ),
     mandatory         => $mandatory,
     libraries         => \@libraries,
     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
+    defaultCategory  => $defaultCategory,
 );
 
 my $attributes = ParsePatronAttributes($borrowernumber,$cgi);
@@ -106,8 +112,7 @@ my $conflicting_attribute = 0;
 
 foreach my $attr (@$attributes) {
     my $attribute = Koha::Patron::Attribute->new($attr);
-    eval {$attribute->check_unique_id};
-    if ( $@ ) {
+    if ( !$attribute->unique_ok ) {
         my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
         $template->param(
             extended_unique_id_failed_code => $attr->{code},
@@ -123,6 +128,7 @@ if ( $action eq 'create' ) {
     my %borrower = ParseCgiForBorrower($cgi);
 
     %borrower = DelEmptyFields(%borrower);
+    $borrower{categorycode} ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
 
     my @empty_mandatory_fields = (CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
@@ -156,10 +162,12 @@ if ( $action eq 'create' ) {
             borrower       => \%borrower
         );
         $template->param( patron_attribute_classes => GeneratePatronAttributesForm( undef, $attributes ) );
+    } elsif ( ! grep { $borrower{branchcode} eq $_->branchcode } @libraries ) {
+        die "Branchcode not allowed"; # They hack the form
     }
     else {
         if (
-            C4::Context->boolean_preference(
+            C4::Context->preference(
                 'PatronSelfRegistrationVerifyByEmail')
           )
         {
@@ -193,7 +201,7 @@ if ( $action eq 'create' ) {
                 },
             );
 
-            C4::Letters::EnqueueLetter(
+            my $message_id = C4::Letters::EnqueueLetter(
                 {
                     letter                 => $letter,
                     message_transport_type => 'email',
@@ -202,9 +210,7 @@ if ( $action eq 'create' ) {
                       C4::Context->preference('KohaAdminEmailAddress'),
                 }
             );
-            my $num_letters_attempted = C4::Letters::SendQueuedMessages( {
-                    letter_code => 'OPAC_REG_VERIFY'
-                    } );
+            C4::Letters::SendQueuedMessages({ message_id => $message_id });
         }
         else {
             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
@@ -216,7 +222,6 @@ if ( $action eq 'create' ) {
                 }
             );
 
-            $borrower{categorycode}     ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
             $borrower{password}         ||= Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($borrower{categorycode}));
             my $consent_dt = delete $borrower{gdpr_proc_consent};
             my $patron = Koha::Patron->new( \%borrower )->store;
@@ -259,7 +264,7 @@ elsif ( $action eq 'update' ) {
     my %borrower = ParseCgiForBorrower($cgi);
     $borrower{borrowernumber} = $borrowernumber;
 
-    my @empty_mandatory_fields =
+    my @empty_mandatory_fields = grep { $_ ne 'password' } # password is not required when editing personal details
       ( CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
 
@@ -398,7 +403,7 @@ sub GetMandatoryFields {
 
     if ( $action eq 'create' || $action eq 'new' ) {
         $mandatory_fields{'email'} = 1
-          if C4::Context->boolean_preference(
+          if C4::Context->preference(
             'PatronSelfRegistrationVerifyByEmail');
     }
 
@@ -439,7 +444,7 @@ sub CheckForInvalidFields {
     my $borrower = shift;
     my @invalidFields;
     if ($borrower->{'email'}) {
-        unless ( Email::Valid->address($borrower->{'email'}) ) {
+        unless ( Koha::Email->is_valid($borrower->{email}) ) {
             push(@invalidFields, "email");
         } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
             my $patrons_with_same_email = Koha::Patrons->search( # FIXME Should be search_limited?
@@ -465,10 +470,10 @@ sub CheckForInvalidFields {
         delete $borrower->{'repeat_email'};
     }
     if ($borrower->{'emailpro'}) {
-        push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
+        push(@invalidFields, "emailpro") unless Koha::Email->is_valid($borrower->{'emailpro'});
     }
     if ($borrower->{'B_email'}) {
-        push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
+        push(@invalidFields, "B_email") unless Koha::Email->is_valid($borrower->{'B_email'});
     }
     if ( defined $borrower->{'password'}
         and $borrower->{'password'} ne $borrower->{'password2'} )
@@ -476,7 +481,7 @@ sub CheckForInvalidFields {
         push( @invalidFields, "password_match" );
     }
     if ( $borrower->{'password'} ) {
-        my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $borrower->{password}, Koha::Patron::Categories->find($borrower->{categorycode}) );
+        my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $borrower->{password}, Koha::Patron::Categories->find($borrower->{categorycode}||C4::Context->preference('PatronSelfRegistrationDefaultCategory')) );
           unless ( $is_valid ) {
               push @invalidFields, 'password_too_short' if $error eq 'too_short';
               push @invalidFields, 'password_too_weak' if $error eq 'too_weak';
@@ -522,6 +527,10 @@ sub ParseCgiForBorrower {
     # Replace checkbox 'agreed' by datetime in gdpr_proc_consent
     $borrower{gdpr_proc_consent} = dt_from_string if  $borrower{gdpr_proc_consent} && $borrower{gdpr_proc_consent} eq 'agreed';
 
+    delete $borrower{$_} for qw/borrowernumber date_renewed debarred debarredcomment flags privacy privacy_guarantor_fines privacy_guarantor_checkouts checkprevcheckout updated_on lastseen lang login_attempts overdrive_auth_token anonymized/; # See also members/memberentry.pl
+    delete $borrower{$_} for qw/dateenrolled dateexpiry borrowernotes opacnote sort1 sort2 sms_provider_id autorenew_checkouts gonenoaddress lost relationship/; # On OPAC only
+    delete $borrower{$_} for split( /\s*\|\s*/, C4::Context->preference('PatronSelfRegistrationBorrowerUnwantedField') || q{} );
+
     return %borrower;
 }