Bug 31577: Use patron category multi-select for OpacHiddenItemsExceptions system...
[koha-ffzg.git] / Koha / Patron / Modifications.pm
index 3ecef11..f6665e8 100644 (file)
@@ -26,9 +26,11 @@ use Modern::Perl;
 
 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);
 
@@ -51,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
@@ -83,22 +94,53 @@ 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' ) {
-                $row->{$key} = from_json($row->{$key});
+                my $attributes = from_json( $row->{$key} );
+                my @pending_attributes;
+                foreach my $attr ( @{$attributes} ) {
+                    push @pending_attributes,
+                        Koha::Patron::Attribute->new(
+                        {   borrowernumber => $row->{borrowernumber},
+                            code           => $attr->{code},
+                            attribute      => exists $attr->{attribute} ? $attr->{attribute} : $attr->{value},
+                        }
+                        );
+                }
+
+                $row->{$key} = \@pending_attributes;
+            }
+            if (none { $_ eq $key } @changed_keys) {
+                delete $row->{$key} unless defined $row->{$key};
             }
-            delete $row->{$key} unless defined $row->{$key};
         }
 
         push( @m, $row );