View | Details | Raw Unified | Return to bug 20443
Collapse All | Expand All

(-)a/Koha/Patron/Attributes.pm (-22 / +45 lines)
Lines 18-23 package Koha::Patron::Attributes; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Patron::Attribute;
20
use Koha::Patron::Attribute;
21
use Koha::Patron::Attribute::Types;
21
22
22
use base qw(Koha::Objects);
23
use base qw(Koha::Objects);
23
24
Lines 33-67 Koha::Patron::Attributes - Koha Patron Attributes Object set class Link Here
33
34
34
=head3 search
35
=head3 search
35
36
36
my $attributes-> Koha::Patron::Attributes->search( $params );
37
my $attributes = Koha::Patron::Attributes->search( $params );
37
38
38
=cut
39
=cut
39
40
40
sub search {
41
sub search {
41
    my ( $self, $params, $attributes ) = @_;
42
    my ( $self, $params, $attributes ) = @_;
42
43
43
    my $branchcode = $params->{branchcode};
44
    delete( $params->{branchcode} );
45
46
    my $or =
47
        $branchcode
48
        ? {
49
            '-or' => [
50
                'borrower_attribute_types_branches.b_branchcode' => undef,
51
                'borrower_attribute_types_branches.b_branchcode' => $branchcode,
52
            ]
53
        } : {};
54
55
    my $join =
56
        $branchcode
57
        ? {
58
            join => {
59
                'code' => 'borrower_attribute_types_branches'
60
            },
61
        } : {};
62
    $attributes //= {};
63
    unless ( exists $attributes->{order_by} ) { $attributes->{order_by} = ['code', 'attribute'] }
44
    unless ( exists $attributes->{order_by} ) { $attributes->{order_by} = ['code', 'attribute'] }
64
    return $self->SUPER::search( { %$params, %$or }, { %$attributes, %$join } );
45
46
    return $self->SUPER::search( $params, $attributes );
47
}
48
49
=head3 filter_by_branch_limitations
50
51
my $attributes = Koha::Patron::Attributes->filter_by_branch_limitations([$branchcode]);
52
53
Search patron attributes filtered by a library
54
55
If $branchcode exists it will be used to filter the result set.
56
57
Otherwise it will be the library of the logged in user.
58
59
=cut
60
61
sub filter_by_branch_limitations {
62
    my ( $self, $branchcode ) = @_;
63
64
    # Maybe we should not limit if logged in user is superlibrarian?
65
    my $branch_limit =
66
        $branchcode          ? $branchcode
67
        # Do we raise an exception if no userenv defined?
68
      : C4::Context->userenv ? C4::Context->userenv->{"branch"}
69
      :                        undef;
70
71
    my $or = $branch_limit
72
      ? {
73
        '-or' => [
74
            'borrower_attribute_types_branches.b_branchcode' => undef,
75
            'borrower_attribute_types_branches.b_branchcode' => $branch_limit,
76
        ]
77
      }
78
      : {};
79
80
    my $join = $branch_limit
81
      ? {
82
        join => {
83
            code => 'borrower_attribute_types_branches'
84
        },
85
      }
86
      : {};
87
    return $self->search( $or, $join );
65
}
88
}
66
89
67
=head3 _type
90
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-5 / +13 lines)
Lines 2020-2026 subtest 'anonymize' => sub { Link Here
2020
$schema->storage->txn_rollback;
2020
$schema->storage->txn_rollback;
2021
2021
2022
subtest 'get_extended_attributes' => sub {
2022
subtest 'get_extended_attributes' => sub {
2023
    plan tests => 9;
2023
    plan tests => 10;
2024
    my $schema = Koha::Database->new->schema;
2024
    my $schema = Koha::Database->new->schema;
2025
    $schema->storage->txn_begin;
2025
    $schema->storage->txn_begin;
2026
2026
Lines 2094-2105 subtest 'get_extended_attributes' => sub { Link Here
2094
2094
2095
    # Test branch limitations
2095
    # Test branch limitations
2096
    set_logged_in_user($patron_2);
2096
    set_logged_in_user($patron_2);
2097
    C4::Members::Attributes::SetBorrowerAttributes($patron_1->borrowernumber, $attributes_for_1);
2097
    # Return all
2098
    $extended_attributes_for_1 = $patron_1->get_extended_attributes;
2098
    $extended_attributes_for_1 = $patron_1->get_extended_attributes;
2099
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should not be returned');
2099
    is( $extended_attributes_for_1->count, 3, 'There should be 2 attributes for patron 1, the limited one should be returned');
2100
2100
2101
    # Return filtered
2102
    $extended_attributes_for_1 = $patron_1->get_extended_attributes->filter_by_branch_limitations;
2103
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
2104
2105
    # Not filtered
2101
    my $limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
2106
    my $limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
2102
    is( $limited_value, undef, );
2107
    is( $limited_value, 'my attribute limited', );
2108
2109
    ## Do we need a filtered?
2110
    #$limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
2111
    #is( $limited_value, undef, );
2103
2112
2104
    $schema->storage->txn_rollback;
2113
    $schema->storage->txn_rollback;
2105
};
2114
};
2106
- 

Return to bug 20443