Browse Source

Bug 20443: Improve filtering method

Koha::Patron::Attributes->search mimicks what is done in
Koha::AuthorisedValues->search.
But actually it should be more explicit when the caller use it.
For instance filter_by_branch_limitation (see discussion on bug 11983).

This will be useful for the following patches as we will need a way to
replace the $no_branch_limit flag.
When the $no_branch_limit flag is called, a simple ->search call should
be done.
When we want to limit on a specific library we can pass the branchcode
in paramter of filter_by_branch_limitation (this is not used yet).
If not passed the logged-in user library will be used by default.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
20.05.x
Jonathan Druart 6 years ago
committed by Martin Renvoize
parent
commit
52949f88e0
Signed by: martin.renvoize GPG Key ID: 422B469130441A0F
  1. 67
      Koha/Patron/Attributes.pm
  2. 17
      t/db_dependent/Koha/Patrons.t

67
Koha/Patron/Attributes.pm

@ -18,6 +18,7 @@ package Koha::Patron::Attributes;
use Modern::Perl;
use Koha::Patron::Attribute;
use Koha::Patron::Attribute::Types;
use base qw(Koha::Objects);
@ -33,35 +34,57 @@ Koha::Patron::Attributes - Koha Patron Attributes Object set class
=head3 search
my $attributes-> Koha::Patron::Attributes->search( $params );
my $attributes = Koha::Patron::Attributes->search( $params );
=cut
sub search {
my ( $self, $params, $attributes ) = @_;
my $branchcode = $params->{branchcode};
delete( $params->{branchcode} );
my $or =
$branchcode
? {
'-or' => [
'borrower_attribute_types_branches.b_branchcode' => undef,
'borrower_attribute_types_branches.b_branchcode' => $branchcode,
]
} : {};
my $join =
$branchcode
? {
join => {
'code' => 'borrower_attribute_types_branches'
},
} : {};
$attributes //= {};
unless ( exists $attributes->{order_by} ) { $attributes->{order_by} = ['code', 'attribute'] }
return $self->SUPER::search( { %$params, %$or }, { %$attributes, %$join } );
return $self->SUPER::search( $params, $attributes );
}
=head3 filter_by_branch_limitations
my $attributes = Koha::Patron::Attributes->filter_by_branch_limitations([$branchcode]);
Search patron attributes filtered by a library
If $branchcode exists it will be used to filter the result set.
Otherwise it will be the library of the logged in user.
=cut
sub filter_by_branch_limitations {
my ( $self, $branchcode ) = @_;
# Maybe we should not limit if logged in user is superlibrarian?
my $branch_limit =
$branchcode ? $branchcode
# Do we raise an exception if no userenv defined?
: C4::Context->userenv ? C4::Context->userenv->{"branch"}
: undef;
my $or = $branch_limit
? {
'-or' => [
'borrower_attribute_types_branches.b_branchcode' => undef,
'borrower_attribute_types_branches.b_branchcode' => $branch_limit,
]
}
: {};
my $join = $branch_limit
? {
join => {
code => 'borrower_attribute_types_branches'
},
}
: {};
return $self->search( $or, $join );
}
=head3 _type

17
t/db_dependent/Koha/Patrons.t

@ -2020,7 +2020,7 @@ subtest 'anonymize' => sub {
$schema->storage->txn_rollback;
subtest 'get_extended_attributes' => sub {
plan tests => 9;
plan tests => 10;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
@ -2094,12 +2094,21 @@ subtest 'get_extended_attributes' => sub {
# Test branch limitations
set_logged_in_user($patron_2);
C4::Members::Attributes::SetBorrowerAttributes($patron_1->borrowernumber, $attributes_for_1);
# Return all
$extended_attributes_for_1 = $patron_1->get_extended_attributes;
is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should not be returned');
is( $extended_attributes_for_1->count, 3, 'There should be 2 attributes for patron 1, the limited one should be returned');
# Return filtered
$extended_attributes_for_1 = $patron_1->get_extended_attributes->filter_by_branch_limitations;
is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
# Not filtered
my $limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
is( $limited_value, undef, );
is( $limited_value, 'my attribute limited', );
## Do we need a filtered?
#$limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
#is( $limited_value, undef, );
$schema->storage->txn_rollback;
};

Loading…
Cancel
Save