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);
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
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;
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 );