X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=Koha%2FPatron%2FModifications.pm;h=f6665e8ddbcf90d873ffb6be9551e6dc70e91bc2;hb=9d6d641d1f8b77271800f43bc027b651f9aea52b;hp=5e7ed9efa51869ae2c0ad70891ef385dc6364830;hpb=475c294b292eadcc7e8d20307c16591fdf8b9b71;p=srvgit diff --git a/Koha/Patron/Modifications.pm b/Koha/Patron/Modifications.pm index 5e7ed9efa5..f6665e8ddb 100644 --- a/Koha/Patron/Modifications.pm +++ b/Koha/Patron/Modifications.pm @@ -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 );