From 099b2532ecaad9aee274a84501a4d7fb6a214557 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Wed, 26 Feb 2025 08:23:09 +0200 Subject: [PATCH] Bug 34776: Restore patron messaging preferences if error occurs during new account creation Content-Type: text/plain; charset=utf-8 If error occurs while creating a new patron messaging preferences are either lost or reseted as default fro patron category. This patch adds new method restore_form_values to restore already added messaging preferences. To test: 1. Enable syspref EnhancedMessagingPreferences. 2. Create a new patron but cause an error to prevent it from saving (e.g. add wrong age). 3. Add messaging preferences for patron (if there are default preferences, add also new ones or remove defaults). 4. Attempt to save. => Note that messaging preferences were lost and you need to readd them. 5. Apply this patch. 6. Repeat steps from 2 to 4. => Note that messaging preferences which you added are kept even if error occurs. Sponsored-by: Koha-Suomi Oy Signed-off-by: Jesse Maseto Signed-off-by: David Nind Signed-off-by: Marcel de Rooy --- C4/Form/MessagingPreferences.pm | 45 +++++++++++++++++++++++++++++++++ members/memberentry.pl | 14 +++++++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/C4/Form/MessagingPreferences.pm b/C4/Form/MessagingPreferences.pm index 88c8e08fe1..51540f1d78 100644 --- a/C4/Form/MessagingPreferences.pm +++ b/C4/Form/MessagingPreferences.pm @@ -148,6 +148,51 @@ PREF: foreach my $option (@$messaging_options) { $template->param( messaging_preferences => $messaging_options ); } +=head2 restore_form_values + + C4::Form::MessagingPreferences::restore_form_values({ borrowernumber => 51 }, $template, $input); + +Restores patron message preferences if error occurs while creating a patron. + +C<$input> is the CGI query object. + +C<$template> is the Template::Toolkit object for the response. + +=cut + +sub restore_form_values { + my ( $input, $template ) = @_; + my $messaging_options = C4::Members::Messaging::GetMessagingOptions(); + foreach my $option (@$messaging_options) { + $option->{ $option->{'message_name'} } = 1; + + my $message_attribute_id = $option->{'message_attribute_id'}; + if ( $option->{'takes_days'} ) { + my $selected_value = $input->param( $message_attribute_id . '-DAYS' ); + my $days_in_advance = $selected_value ? $selected_value : 0; + $option->{days_in_advance} = $days_in_advance; + @{ $option->{'select_days'} } = map { + { + day => $_, + selected => $_ == $days_in_advance + } + } ( 0 .. MAX_DAYS_IN_ADVANCE ); + } + + my @transport_types = $input->multi_param($message_attribute_id); + foreach my $transport_type (@transport_types) { + $option->{ 'transports_' . $transport_type } = 1; + } + + if ( $option->{'has_digest'} ) { + if ( List::Util::first { $_ == $message_attribute_id } $input->multi_param('digest') ) { + $option->{'digest'} = 1; + } + } + } + $template->param( messaging_preferences => $messaging_options ); +} + =head1 TODO =over 4 diff --git a/members/memberentry.pl b/members/memberentry.pl index 5846b3b1c6..348c90019e 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -838,10 +838,18 @@ if ( C4::Context->preference('ExtendedPatronAttributes') ) { } if ( C4::Context->preference('EnhancedMessagingPreferences') ) { - if ( $op eq 'add_form' ) { - C4::Form::MessagingPreferences::set_form_values( { categorycode => $categorycode }, $template ); + unless ( $nok && $input->param('setting_messaging_prefs') ) { + if ( $op eq 'add_form' ) { + C4::Form::MessagingPreferences::set_form_values( { categorycode => $categorycode }, $template ); + } else { + C4::Form::MessagingPreferences::set_form_values( { borrowernumber => $borrowernumber }, $template ); + } } else { - C4::Form::MessagingPreferences::set_form_values( { borrowernumber => $borrowernumber }, $template ); + if ( $op eq 'add_form' ) { + C4::Form::MessagingPreferences::restore_form_values( $input, $template ); + } else { + C4::Form::MessagingPreferences::restore_form_values( $input, $template ); + } } $template->param( SMSSendDriver => C4::Context->preference("SMSSendDriver") ); $template->param( SMSnumber => $data{'smsalertnumber'} ); -- 2.39.5