Bug 13757: (followup) Only touch opac_editable attributes
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Dec 2016 19:19:06 +0000 (16:19 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 24 Mar 2017 18:45:01 +0000 (18:45 +0000)
As reported by Owen, the members-update.pl was showing every attributes
the patron has (display issue) instead of showing only those affected by
the changes.

This patch fixes this by filtering the patron's attributes by opac
editability.

It also fixes Koha::Patron::Modification->approve so it only clears the
attributes with the updating 'code' and leaves the others untouched.
As its been coded so far (until someone refactors it all) the
Koha::Patron::Modification object needs to contain all the attributes
for a specific code. And it comes from parsing the UI's input.

Tests for Koha::Patron::Modification->approve to come.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Patron/Modification.pm
members/members-update.pl

index 696f7e8..e3573ca 100644 (file)
@@ -23,11 +23,12 @@ use Carp;
 
 use Koha::Database;
 use Koha::Exceptions::Patron::Modification;
+use Koha::Patron::Attribute;
+use Koha::Patron::Attributes;
 use Koha::Patron::Modifications;
-# TODO: Remove once Koha::Patron::Attribute(s) is implemented
-use C4::Members::Attributes qw( SetBorrowerAttributes );
 
 use JSON;
+use List::MoreUtils qw( uniq );
 use Try::Tiny;
 
 use base qw(Koha::Object);
@@ -87,6 +88,7 @@ sub approve {
     my ($self) = @_;
 
     my $data = $self->unblessed();
+    my $extended_attributes;
 
     delete $data->{timestamp};
     delete $data->{verification_token};
@@ -104,7 +106,7 @@ sub approve {
 
     # Take care of extended attributes
     if ( $self->extended_attributes ) {
-        our $extended_attributes
+        $extended_attributes
             = try { decode_json( $self->extended_attributes ) }
         catch {
             Koha::Exceptions::Patron::Modification::InvalidData->throw(
@@ -117,12 +119,23 @@ sub approve {
             try {
                 $patron->store();
 
-                # Take care of extended attributes
-                if ( $self->extended_attributes ) {
-                    my $extended_attributes
-                        = decode_json( $self->extended_attributes );
-                    SetBorrowerAttributes( $patron->borrowernumber,
-                        $extended_attributes );
+                # Deal with attributes
+                my @codes
+                    = uniq( map { $_->{code} } @{$extended_attributes} );
+                foreach my $code (@codes) {
+                    map { $_->delete } Koha::Patron::Attributes->search(
+                        {   borrowernumber => $patron->borrowernumber,
+                            code           => $code
+                        }
+                    );
+                }
+                foreach my $attr ( @{$extended_attributes} ) {
+                    Koha::Patron::Attribute->new(
+                        {   borrowernumber => $patron->borrowernumber,
+                            code           => $attr->{code},
+                            attribute      => $attr->{value}
+                        }
+                    )->store;
                 }
             }
             catch {
@@ -141,8 +154,6 @@ sub approve {
 }
 
 
-
-
 =head3 type
 
 =cut
index 24179e6..851af14 100755 (executable)
@@ -24,7 +24,6 @@ use C4::Auth;
 use C4::Output;
 use C4::Context;
 use C4::Members;
-use C4::Members::Attributes qw( GetBorrowerAttributes );
 use Koha::Patron::Attributes;
 use Koha::Patron::Modifications;
 
@@ -55,10 +54,12 @@ my $borrowers;
 foreach my $pm (@$pending_modifications) {
     $borrowers->{ $pm->{borrowernumber} }
         = GetMember( borrowernumber => $pm->{borrowernumber} );
-    my $patron_attributes = Koha::Patron::Attributes->search(
+    my @patron_attributes
+        = grep { $_->opac_editable }
+        Koha::Patron::Attributes->search(
         { borrowernumber => $pm->{borrowernumber} } );
     $borrowers->{ $pm->{'borrowernumber'} }->{extended_attributes}
-        = $patron_attributes;
+        = \@patron_attributes;
 }
 
 $template->param(