From 9bd6738b01c97edf38ae7782cb8671176209e984 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 21 Apr 2020 14:30:19 +0200 Subject: [PATCH] Bug 5161: Keep patron's attributes on warning/duplicate When a patron is added or modified and a warning appears (duplicate, inconsistent data, etc.) the form lost the patron's attributes. Test plan: Create some attribute types for patrons Create a new patron, use an userid that already exists and fill the attributes => You get a warning and the attributes are kept Modify the userid and save again Edit the same patron Modify the attributes, as well as the userid (to get the duplicate warning) => You get a warning and the attributes are kept with the modified values Modify the userid and save again => The new values are saved Edit the attributes from the detail page (so not with the full edit form) Modify them and save => The new values are saved Signed-off-by: David Nind --- members/memberentry.pl | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/members/memberentry.pl b/members/memberentry.pl index 66c32660a0..72cbda5895 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -415,6 +415,9 @@ if ($op eq 'save' || $op eq 'insert'){ } } } +elsif ( $borrowernumber ) { + $extended_patron_attributes = Koha::Patrons->find($borrowernumber)->extended_attributes->unblessed; +} if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){ unless ($newdata{'dateexpiry'}){ @@ -767,8 +770,8 @@ foreach (qw(dateenrolled dateexpiry dateofbirth)) { $template->param( $_ => $data{$_}); } -if (C4::Context->preference('ExtendedPatronAttributes')) { - patron_attributes_form($template, $borrowernumber, $op); +if ( C4::Context->preference('ExtendedPatronAttributes') ) { + patron_attributes_form( $template, $extended_patron_attributes, $op ); } if (C4::Context->preference('EnhancedMessagingPreferences')) { @@ -861,7 +864,7 @@ sub parse_extended_patron_attributes { sub patron_attributes_form { my $template = shift; - my $borrowernumber = shift; + my $attributes = shift; my $op = shift; my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef; @@ -870,16 +873,11 @@ sub patron_attributes_form { $template->param(no_patron_attribute_types => 1); return; } - my @attributes; - if ( $borrowernumber ) { - my $patron = Koha::Patrons->find($borrowernumber); # Already fetched but outside of this sub - @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved! - } # map patron's attributes into a more convenient structure my %attr_hash = (); - foreach my $attr (@attributes) { - push @{ $attr_hash{$attr->code} }, $attr; + foreach my $attr (@$attributes) { + push @{ $attr_hash{$attr->{code}} }, $attr; } my @attribute_loop = (); @@ -897,16 +895,16 @@ sub patron_attributes_form { if (exists $attr_hash{$attr_type->code()}) { foreach my $attr (@{ $attr_hash{$attr_type->code()} }) { my $newentry = { %$entry }; - $newentry->{value} = $attr->attribute; + $newentry->{value} = $attr->{attribute}; $newentry->{use_dropdown} = 0; if ($attr_type->authorised_value_category()) { $newentry->{use_dropdown} = 1; - $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->attribute); + $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{attribute}); } $i++; undef $newentry->{value} if ($attr_type->unique_id() && $op eq 'duplicate'); $newentry->{form_id} = "patron_attr_$i"; - push @{$items_by_class{$attr_type->class()}}, $newentry; + push @{$items_by_class{$attr_type->{class}}}, $newentry; } } else { $i++; -- 2.11.0