Bug 20443: Fix patron modification approval
[koha-ffzg.git] / Koha / Patron / Modification.pm
index e3573ca..51f182a 100644 (file)
@@ -4,18 +4,18 @@ package Koha::Patron::Modification;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
@@ -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);
@@ -54,8 +54,7 @@ sub store {
             )->count()
             )
         {
-            Koha::Exceptions::Patron::Modification::DuplicateVerificationToken
-                ->throw(
+            Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
                 "Duplicate verification token " . $self->verification_token );
         }
     }
@@ -74,7 +73,6 @@ sub store {
     return $self->SUPER::store();
 }
 
-
 =head2 approve
 
 $m->approve();
@@ -93,21 +91,24 @@ sub approve {
     delete $data->{timestamp};
     delete $data->{verification_token};
     delete $data->{extended_attributes};
-
-    foreach my $key ( keys %$data ) {
-        delete $data->{$key} unless ( defined( $data->{$key} ) );
-    }
+    my $changed_fields = $data->{changed_fields};
+    delete $data->{changed_fields};
 
     my $patron = Koha::Patrons->find( $self->borrowernumber );
-
     return unless $patron;
 
+    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);
 
     # Take care of extended attributes
     if ( $self->extended_attributes ) {
-        $extended_attributes
-            = try { decode_json( $self->extended_attributes ) }
+        $extended_attributes = try { from_json( $self->extended_attributes ) }
         catch {
             Koha::Exceptions::Patron::Modification::InvalidData->throw(
                 'The passed extended_attributes is not valid JSON');
@@ -130,21 +131,27 @@ sub approve {
                     );
                 }
                 foreach my $attr ( @{$extended_attributes} ) {
+                    $attr->{attribute} = exists $attr->{attribute} ? $attr->{attribute} : $attr->{value};
                     Koha::Patron::Attribute->new(
                         {   borrowernumber => $patron->borrowernumber,
                             code           => $attr->{code},
-                            attribute      => $attr->{value}
+                            attribute      => $attr->{attribute},
                         }
-                    )->store;
+                    )->store
+                        if $attr->{attribute} # there's a value
+                           or
+                          (    defined $attr->{attribute} # there's a value that is 0, and not
+                            && $attr->{attribute} ne ""   # the empty string which means delete
+                            && $attr->{attribute} == 0
+                          );
                 }
             }
             catch {
                 if ( $_->isa('DBIx::Class::Exception') ) {
-                    Koha::Exceptions::Patron::Modification->throw(
-                        $_->{msg} );
+                    Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
                 }
                 else {
-                    Koha::Exceptions::Patron::Modification->throw($@);
+                    Koha::Exceptions::Patron::Modification->throw($_);
                 }
             };
         }
@@ -153,7 +160,6 @@ sub approve {
     return $self->delete();
 }
 
-
 =head3 type
 
 =cut