Bug 23151: Tweak to use the new database structure
authorMark Tompsett <mtompset@hotmail.com>
Wed, 19 Jun 2019 16:29:59 +0000 (16:29 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 19 Jul 2019 08:39:11 +0000 (09:39 +0100)
This will use changed_fields to know whether a
borrower field is actually modified.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/Patron/Modification.pm
Koha/Patron/Modifications.pm
opac/opac-memberentry.pl

index b5d5ade..1e577b1 100644 (file)
@@ -28,7 +28,7 @@ use Koha::Patron::Attributes;
 use Koha::Patron::Modifications;
 
 use JSON;
-use List::MoreUtils qw( uniq );
+use List::MoreUtils qw( uniq any );
 use Try::Tiny;
 
 use base qw(Koha::Object);
@@ -91,17 +91,17 @@ sub approve {
     delete $data->{timestamp};
     delete $data->{verification_token};
     delete $data->{extended_attributes};
+    my $changed_fields = $data->{changed_fields};
+    delete $data->{changed_fields};
 
     my $patron = Koha::Patrons->find( $self->borrowernumber );
     return unless $patron;
 
-    foreach my $key ( keys %$data ) {
-        next # Unset it!
-          if $key eq 'dateofbirth'
-          && $patron->dateofbirth
-          && not defined $data->{$key};
-
-        delete $data->{$key} unless defined $data->{$key};
+    my @keep_keys = split /,/, $changed_fields;
+    my @all_keys = keys %$data;
+    foreach my $key ( @all_keys ) {
+        next if (any { $_ eq $key } @keep_keys);
+        delete $data->{$key};
     }
 
     $patron->set($data);
index 646d112..e29079c 100644 (file)
@@ -30,6 +30,7 @@ use Koha::Patron::Attribute;
 use Koha::Patron::Modification;
 
 use JSON;
+use List::Util qw /any none/;
 
 use base qw(Koha::Objects);
 
@@ -116,7 +117,12 @@ sub pending {
 
     my @m;
     while ( my $row = $sth->fetchrow_hashref() ) {
+        my @changed_keys = split /,/, $row->{changed_fields};
         foreach my $key ( keys %$row ) {
+            if ($key eq 'changed_fields') {
+                delete $row->{$key};
+                next;
+            }
             if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
                 my $attributes = from_json( $row->{$key} );
                 my @pending_attributes;
@@ -132,9 +138,7 @@ sub pending {
 
                 $row->{$key} = \@pending_attributes;
             }
-            if ( $key eq 'dateofbirth' and not defined $row->{$key}) {
-                $row->{$key} = '';
-            } else {
+            if (none { $_ eq $key } @changed_keys) {
                 delete $row->{$key} unless defined $row->{$key};
             }
         }
index f76a30c..db8a6ac 100755 (executable)
@@ -247,9 +247,8 @@ elsif ( $action eq 'update' ) {
     my %borrower = ParseCgiForBorrower($cgi);
     $borrower{borrowernumber} = $borrowernumber;
 
-    my %borrower_changes = DelEmptyFields(%borrower);
     my @empty_mandatory_fields =
-      CheckMandatoryFields( \%borrower_changes, $action );
+      CheckMandatoryFields( \%borrower, $action );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
 
     # Send back the data to the template
@@ -270,6 +269,7 @@ elsif ( $action eq 'update' ) {
     }
     else {
         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
+        $borrower_changes{'changed_fields'} = join ',', keys %borrower_changes;
         my $extended_attributes_changes = FilterUnchangedAttributes( $borrowernumber, $attributes );
 
         if ( %borrower_changes || scalar @{$extended_attributes_changes} > 0 ) {