From f121d5ff72668d374ae54fbe3e7fb372c532cd9f Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 12 Jul 2024 14:00:49 +0000
Subject: [PATCH] Bug 37339: Set messaging preferences from default on self
 registration

The change on bug 30318 prevented setting the default messaging preferences when none were passed.
Self registration relied on this for setting borrower preferences from the defaults.

This patch removes the call to handle_form_action and replaces it with a call to:
SetMessagingPreferencesFromDefault

To test:
 1 - Ensure self registration is enabled
 2 - Set default messaging preferences for the self registration category
 3 - Self register a patron from the OPAC, confirm defaults not set
 4 - Apply patch, restart all
 5 - Self register a patron
 6 - Confirm defaults have been set
 7 - Enable  PatronSelfRegistrationVerifyByEmail
 8 - Self register a patron on OPAC with an email
 9 - Find the email and grab the verification link (unless you have emails enabled)
     SELECT * FROM message_queue;
10 - Visit the link and confirm registration
11 - Confirm messaging defaults are set

Signed-off-by: David Nind <david@davidnind.com>
---
 opac/opac-memberentry.pl         | 12 +++---------
 opac/opac-registration-verify.pl |  4 +++-
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/opac/opac-memberentry.pl b/opac/opac-memberentry.pl
index c40d09d512..b671dde7c0 100755
--- a/opac/opac-memberentry.pl
+++ b/opac/opac-memberentry.pl
@@ -29,6 +29,7 @@ use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
 use C4::Form::MessagingPreferences;
+use C4::Members::Messaging qw( SetMessagingPreferencesFromDefaults );
 use Koha::AuthUtils;
 use Koha::Patrons;
 use Koha::Patron::Consent;
@@ -230,6 +231,8 @@ if ( $op eq 'cud-create' ) {
             try {
                 $patron = Koha::Patron->new( \%borrower )->store;
                 Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
+                C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
+                    { borrowernumber => $patron->borrowernumber, categorycode => $patron->categorycode } );
             } catch {
                 my $type = ref($_);
                 my $info = "$_";
@@ -249,15 +252,6 @@ if ( $op eq 'cud-create' ) {
             if ( $patron ) {
                 $patron->extended_attributes->filter_by_branch_limitations->delete;
                 $patron->extended_attributes($attributes);
-                if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
-                    C4::Form::MessagingPreferences::handle_form_action(
-                        $cgi,
-                        { borrowernumber => $patron->borrowernumber },
-                        $template,
-                        1,
-                        $PatronSelfRegistrationDefaultCategory
-                    );
-                }
 
                 $template->param( password_cleartext => $patron->plain_text_password );
                 $template->param( borrower => $patron->unblessed );
diff --git a/opac/opac-registration-verify.pl b/opac/opac-registration-verify.pl
index 129d51cbb9..d3ae183384 100755
--- a/opac/opac-registration-verify.pl
+++ b/opac/opac-registration-verify.pl
@@ -25,6 +25,7 @@ use C4::Context;
 use C4::Output qw( output_html_with_http_headers );
 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
 use C4::Members;
+use C4::Members::Messaging qw( SetMessagingPreferencesFromDefaults );
 use C4::Form::MessagingPreferences;
 use Koha::AuthUtils;
 use Koha::Patrons;
@@ -90,6 +91,8 @@ elsif ( $rego_found
     try {
         $patron = Koha::Patron->new( $patron_attrs )->store;
         Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $patron && $consent_dt;
+        C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
+            { borrowernumber => $patron->borrowernumber, categorycode => $patron->categorycode } );
     } catch {
         $error_type = ref($_);
         $error_info = "$_";
@@ -112,7 +115,6 @@ elsif ( $rego_found
             }
         );
         $template->param( "confirmed" => 1 );
-        C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
 
         $template->param( password_cleartext => $patron->plain_text_password );
         $template->param( borrower => $patron );
-- 
2.39.2