Bug 32445: (follow-up) Fix availability display on opac-course-details
[srvgit] / opac / opac-memberentry.pl
index 015c56a..0115d78 100755 (executable)
@@ -19,14 +19,15 @@ 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::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
+use C4::Members qw( checkcardnumber );
 use C4::Form::MessagingPreferences;
 use Koha::AuthUtils;
 use Koha::Patrons;
@@ -34,17 +35,15 @@ 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 );
+use Koha::Email;
 use Koha::Libraries;
 use Koha::Patron::Attribute::Types;
 use Koha::Patron::Attributes;
 use Koha::Patron::Images;
-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 +63,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';
@@ -81,7 +85,7 @@ if ( $action eq 'create' || $action eq 'new' ) {
     $params = { branchcode => { -in => \@PatronSelfRegistrationLibraryList } }
       if @PatronSelfRegistrationLibraryList;
 }
-my @libraries = Koha::Libraries->search($params);
+my $libraries = Koha::Libraries->search($params);
 
 my ( $min, $max ) = C4::Members::get_cardnumber_length();
 if ( defined $min ) {
@@ -97,7 +101,7 @@ $template->param(
     action            => $action,
     hidden            => GetHiddenFields( $mandatory, $action ),
     mandatory         => $mandatory,
-    libraries         => \@libraries,
+    libraries         => $libraries,
     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
     defaultCategory  => $defaultCategory,
 );
@@ -107,7 +111,7 @@ my $conflicting_attribute = 0;
 
 foreach my $attr (@$attributes) {
     my $attribute = Koha::Patron::Attribute->new($attr);
-    if ( !$$attribute->unique_ok ) {
+    if ( !$attribute->unique_ok ) {
         my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
         $template->param(
             extended_unique_id_failed_code => $attr->{code},
@@ -157,7 +161,7 @@ if ( $action eq 'create' ) {
             borrower       => \%borrower
         );
         $template->param( patron_attribute_classes => GeneratePatronAttributesForm( undef, $attributes ) );
-    } elsif ( ! grep { $borrower{branchcode} eq $_->branchcode } @libraries ) {
+    } elsif ( !$libraries->find($borrower{branchcode}) ) {
         die "Branchcode not allowed"; # They hack the form
     }
     else {
@@ -184,6 +188,7 @@ if ( $action eq 'create' ) {
             $borrower{password}          = Koha::AuthUtils::generate_password(Koha::Patron::Categories->find($borrower{categorycode})) unless $borrower{password};
             $borrower{verification_token} = $verification_token;
 
+            $borrower{extended_attributes} = to_json($attributes);
             Koha::Patron::Modification->new( \%borrower )->store();
 
             #Send verification email
@@ -236,6 +241,45 @@ if ( $action eq 'create' ) {
 
                 $template->param( password_cleartext => $patron->plain_text_password );
                 $template->param( borrower => $patron->unblessed );
+
+                # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
+                if ( C4::Context->preference("AutoEmailNewUser") ) {
+                    #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
+                    my $emailaddr = $patron->notice_email_address;
+                    # if we manage to find a valid email address, send notice
+                    if ($emailaddr) {
+                        eval {
+                            my $letter = GetPreparedLetter(
+                                module      => 'members',
+                                letter_code => 'WELCOME',
+                                branchcode  => $patron->branchcode,,
+                                lang        => $patron->lang || 'default',
+                                tables      => {
+                                    'branches'  => $patron->branchcode,
+                                    'borrowers' => $patron->borrowernumber,
+                                },
+                                want_librarian => 1,
+                            ) or return;
+
+                            my $message_id = EnqueueLetter(
+                                {
+                                    letter                 => $letter,
+                                    borrowernumber         => $patron->id,
+                                    to_address             => $emailaddr,
+                                    message_transport_type => 'email'
+                                }
+                            );
+                            SendQueuedMessages({ message_id => $message_id });
+                        };
+                    }
+                }
+
+                # Notify library of new patron registration
+                my $notify_library = C4::Context->preference('EmailPatronRegistrations');
+                if ($notify_library) {
+                    $patron->notify_library_of_registration($notify_library);
+                }
+
             } else {
                 # FIXME Handle possible errors here
             }
@@ -386,7 +430,8 @@ sub GetMandatoryFields {
 
     my %mandatory_fields;
 
-    my $BorrowerMandatoryField =
+    my $BorrowerMandatoryField = $action eq 'edit' || $action eq 'update' ?
+      C4::Context->preference("PatronSelfModificationMandatoryField") :
       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
 
     my @fields = split( /\|/, $BorrowerMandatoryField );
@@ -439,7 +484,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 +510,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'} )
@@ -505,23 +550,13 @@ sub ParseCgiForBorrower {
         }
     }
 
-    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;
-        }
-    }
-
     # 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;
 }
 
@@ -615,7 +650,7 @@ sub GeneratePatronAttributesForm {
     my ( $borrowernumber, $entered_attributes ) = @_;
 
     # Get all attribute types and the values for this patron (if applicable)
-    my @types = grep { $_->opac_editable() or $_->opac_display }
+    my @types = grep { $_->opac_editable() or $_->opac_display } # FIXME filter using DBIC
         Koha::Patron::Attribute::Types->search()->as_list();
     if ( scalar(@types) == 0 ) {
         return [];
@@ -688,13 +723,14 @@ sub ParsePatronAttributes {
     my @values = $cgi->multi_param('patron_attribute_value');
 
     my @editable_attribute_types
-        = map { $_->code } Koha::Patron::Attribute::Types->search({ opac_editable => 1 });
+        = map { $_->code } Koha::Patron::Attribute::Types->search({ opac_editable => 1 })->as_list;
 
     my $ea = each_array( @codes, @values );
     my @attributes;
 
     my $delete_candidates = {};
 
+    my $scrubber = C4::Scrubber->new();
     while ( my ( $code, $value ) = $ea->() ) {
         if ( any { $_ eq $code } @editable_attribute_types ) {
             # It is an editable attribute
@@ -704,7 +740,7 @@ sub ParsePatronAttributes {
             }
             else {
                 # we've got a value
-                push @attributes, { code => $code, attribute => $value };
+                push @attributes, { code => $code, attribute => $scrubber->scrub( $value ) };
 
                 # 'code' is no longer a delete candidate
                 delete $delete_candidates->{$code}