Bug 5161: Keep patron's attributes on warning/duplicate
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 21 Apr 2020 12:30:19 +0000 (14:30 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 27 Apr 2020 10:28:46 +0000 (11:28 +0100)
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 <david@davidnind.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

members/memberentry.pl

index 66c3266..72cbda5 100755 (executable)
@@ -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++;