Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / Patron / Modifications.pm
index 5e7ed9e..f6665e8 100644 (file)
@@ -29,7 +29,8 @@ use C4::Context;
 use Koha::Patron::Attribute;
 use Koha::Patron::Modification;
 
-use JSON;
+use JSON qw( from_json );
+use List::Util qw( none );
 
 use base qw(Koha::Objects);
 
@@ -52,17 +53,26 @@ sub pending_count {
         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
     ";
 
-    my @params;
-    if ($branchcode) {
-        $query .= " AND borrowers.branchcode = ? ";
-        push( @params, $branchcode );
+    my $userenv = C4::Context->userenv;
+    my @branchcodes;
+    if ( $userenv and $userenv->{number} ) {
+        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+        if ($branchcode) {
+            return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
+            @branchcodes = ( $branchcode );
+        }
+        else {
+            @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
+        }
+    }
+    my @sql_params;
+    if ( @branchcodes ) {
+        $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
+        push( @sql_params, @branchcodes );
     }
 
-    my $sth = $dbh->prepare($query);
-    $sth->execute(@params);
-    my $result = $sth->fetchrow_hashref();
-
-    return $result->{count};
+    my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
+    return $count;
 }
 
 =head2 pending
@@ -84,18 +94,35 @@ sub pending {
         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
     ";
 
-    my @params;
-    if ($branchcode) {
-        $query .= " AND borrowers.branchcode = ? ";
-        push( @params, $branchcode );
+    my $userenv = C4::Context->userenv;
+    my @branchcodes;
+    if ( $userenv ) {
+        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+        if ($branchcode) {
+            return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
+            @branchcodes = ( $branchcode );
+        }
+        else {
+            @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
+        }
+    }
+    my @sql_params;
+    if ( @branchcodes ) {
+        $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
+        push( @sql_params, @branchcodes );
     }
     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
     my $sth = $dbh->prepare($query);
-    $sth->execute(@params);
+    $sth->execute(@sql_params);
 
     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;
@@ -104,14 +131,16 @@ sub pending {
                         Koha::Patron::Attribute->new(
                         {   borrowernumber => $row->{borrowernumber},
                             code           => $attr->{code},
-                            attribute      => $attr->{value}
+                            attribute      => exists $attr->{attribute} ? $attr->{attribute} : $attr->{value},
                         }
                         );
                 }
 
                 $row->{$key} = \@pending_attributes;
             }
-            delete $row->{$key} unless defined $row->{$key};
+            if (none { $_ eq $key } @changed_keys) {
+                delete $row->{$key} unless defined $row->{$key};
+            }
         }
 
         push( @m, $row );