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

(-)a/C4/Members/Attributes.pm (-22 / +2 lines)
Lines 30-36 our ($csv, $AttributeTypes); Link Here
30
BEGIN {
30
BEGIN {
31
    @ISA = qw(Exporter);
31
    @ISA = qw(Exporter);
32
    @EXPORT_OK = qw(CheckUniqueness SetBorrowerAttributes
32
    @EXPORT_OK = qw(CheckUniqueness SetBorrowerAttributes
33
                    DeleteBorrowerAttribute UpdateBorrowerAttribute
33
                    UpdateBorrowerAttribute
34
                    extended_attributes_code_value_arrayref extended_attributes_merge
34
                    extended_attributes_code_value_arrayref extended_attributes_merge
35
                    SearchIdMatchingAttribute);
35
                    SearchIdMatchingAttribute);
36
    %EXPORT_TAGS = ( all => \@EXPORT_OK );
36
    %EXPORT_TAGS = ( all => \@EXPORT_OK );
Lines 147-172 sub SetBorrowerAttributes { Link Here
147
    return 1; # borrower attributes successfully set
147
    return 1; # borrower attributes successfully set
148
}
148
}
149
149
150
=head2 DeleteBorrowerAttribute
151
152
  DeleteBorrowerAttribute($borrowernumber, $attribute);
153
154
Delete a borrower attribute for the patron identified by C<$borrowernumber> and the attribute code of C<$attribute>
155
156
=cut
157
158
sub DeleteBorrowerAttribute {
159
    my ( $borrowernumber, $attribute ) = @_;
160
161
    my $dbh = C4::Context->dbh;
162
    my $sth = $dbh->prepare(qq{
163
        DELETE FROM borrower_attributes
164
            WHERE borrowernumber = ?
165
            AND code = ?
166
    } );
167
    $sth->execute( $borrowernumber, $attribute->{code} );
168
}
169
170
=head2 UpdateBorrowerAttribute
150
=head2 UpdateBorrowerAttribute
171
151
172
  UpdateBorrowerAttribute($borrowernumber, $attribute );
152
  UpdateBorrowerAttribute($borrowernumber, $attribute );
Lines 178-184 Update a borrower attribute C<$attribute> for the patron identified by C<$borrow Link Here
178
sub UpdateBorrowerAttribute {
158
sub UpdateBorrowerAttribute {
179
    my ( $borrowernumber, $attribute ) = @_;
159
    my ( $borrowernumber, $attribute ) = @_;
180
160
181
    DeleteBorrowerAttribute $borrowernumber, $attribute;
161
    Koha::Patrons->find($borrowernumber)->get_extended_attributes->search({ 'me.code' => $attribute->{code} })->filter_by_branch_limitations->delete;
182
162
183
    my $dbh = C4::Context->dbh;
163
    my $dbh = C4::Context->dbh;
184
    my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
164
    my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
(-)a/Koha/Patron.pm (-7 / +7 lines)
Lines 1556-1566 sub add_guarantor { Link Here
1556
    )->store();
1556
    )->store();
1557
}
1557
}
1558
1558
1559
=head3 get_extended_attribute_value
1559
=head3 get_extended_attribute
1560
1560
1561
my $attribute_value = $patron->get_extended_attribute_value( $code );
1561
my $attribute_value = $patron->get_extended_attribute( $code );
1562
1562
1563
Return the attribute's value for the code passed in parameter.
1563
Return the attribute for the code passed in parameter.
1564
1564
1565
It not exist it returns undef
1565
It not exist it returns undef
1566
1566
Lines 1571-1583 Maybe you certainly not want to use this method, it is actually only used for SH Link Here
1571
1571
1572
=cut
1572
=cut
1573
1573
1574
sub get_extended_attribute_value {
1574
sub get_extended_attribute {
1575
    my ( $self, $code ) = @_;
1575
    my ( $self, $code, $value ) = @_;
1576
    my $rs = $self->_result->borrower_attributes;
1576
    my $rs = $self->_result->borrower_attributes;
1577
    return unless $rs;
1577
    return unless $rs;
1578
    my $attribute = $rs->search( { code => $code } );
1578
    my $attribute = $rs->search({ code => $code, ( $value ? ( attribute => $value ) : () ) });
1579
    return unless $attribute->count;
1579
    return unless $attribute->count;
1580
    return $attribute->next->attribute;
1580
    return $attribute->next;
1581
}
1581
}
1582
1582
1583
=head3 to_api
1583
=head3 to_api
(-)a/opac/opac-user.pl (-2 / +2 lines)
Lines 289-296 $template->param( overdues_count => $overdues_count ); Link Here
289
my $show_barcode = Koha::Patron::Attribute::Types->search( # FIXME we should not need this search
289
my $show_barcode = Koha::Patron::Attribute::Types->search( # FIXME we should not need this search
290
    { code => ATTRIBUTE_SHOW_BARCODE } )->count;
290
    { code => ATTRIBUTE_SHOW_BARCODE } )->count;
291
if ($show_barcode) {
291
if ($show_barcode) {
292
    my $patron_show_barcode = $patron->get_extended_attribute_value(ATTRIBUTE_SHOW_BARCODE);
292
    my $patron_show_barcode = $patron->get_extended_attribute(ATTRIBUTE_SHOW_BARCODE);
293
    undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
293
    undef $show_barcode if $patron_show_barcode and not $patron_show_barcode->attribute;
294
}
294
}
295
$template->param( show_barcode => 1 ) if $show_barcode;
295
$template->param( show_barcode => 1 ) if $show_barcode;
296
296
(-)a/t/db_dependent/Auth_with_ldap.t (-1 / +1 lines)
Lines 210-216 subtest 'checkpw_ldap tests' => sub { Link Here
210
            'Extended attributes are not deleted'
210
            'Extended attributes are not deleted'
211
        );
211
        );
212
212
213
        is( $patron->get_extended_attribute_value( $attr_type2->{code} ), 'BAR', 'Mapped attribute is BAR' );
213
        is( $patron->get_extended_attribute( $attr_type2->{code} )->attribute, 'BAR', 'Mapped attribute is BAR' );
214
        $auth->unmock('update_local');
214
        $auth->unmock('update_local');
215
        $auth->unmock('ldap_entry_2_hash');
215
        $auth->unmock('ldap_entry_2_hash');
216
216
(-)a/t/db_dependent/Koha/Patrons.t (-8 / +8 lines)
Lines 2085-2096 subtest 'get_extended_attributes' => sub { Link Here
2085
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
2085
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
2086
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
2086
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
2087
2087
2088
    $attribute_12 = $patron_2->get_extended_attribute_value( $attribute_type1->code );
2088
    $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code );
2089
    is( $attribute_12, 'my attribute12', 'Koha::Patron->get_extended_attribute_value should return the correct attribute value' );
2089
    is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' );
2090
2090
2091
    # TODO - What about multiple?
2091
    # TODO - What about multiple? POD explains the problem
2092
    my $non_existent = $patron_2->get_extended_attribute_value( 'not_exist' );
2092
    my $non_existent = $patron_2->get_extended_attribute( 'not_exist' );
2093
    is( $non_existent, undef, 'Koha::Patron->get_extended_attribute_value must return undef if the attribute does not exist' );
2093
    is( $non_existent, undef, 'Koha::Patron->get_extended_attribute must return undef if the attribute does not exist' );
2094
2094
2095
    # Test branch limitations
2095
    # Test branch limitations
2096
    set_logged_in_user($patron_2);
2096
    set_logged_in_user($patron_2);
Lines 2103-2113 subtest 'get_extended_attributes' => sub { Link Here
2103
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
2103
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
2104
2104
2105
    # Not filtered
2105
    # Not filtered
2106
    my $limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
2106
    my $limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
2107
    is( $limited_value, 'my attribute limited', );
2107
    is( $limited_value->attribute, 'my attribute limited', );
2108
2108
2109
    ## Do we need a filtered?
2109
    ## Do we need a filtered?
2110
    #$limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
2110
    #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
2111
    #is( $limited_value, undef, );
2111
    #is( $limited_value, undef, );
2112
2112
2113
    $schema->storage->txn_rollback;
2113
    $schema->storage->txn_rollback;
(-)a/t/db_dependent/Members/Attributes.t (-23 / +13 lines)
Lines 26-32 use Koha::Database; Link Here
26
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
27
use t::lib::Mocks;
28
28
29
use Test::More tests => 41;
29
use Test::More tests => 38;
30
30
31
use_ok('C4::Members::Attributes');
31
use_ok('C4::Members::Attributes');
32
32
Lines 124-136 $patron = Koha::Patrons->find($borrowernumber); Link Here
124
my $extended_attributes = $patron->get_extended_attributes;
124
my $extended_attributes = $patron->get_extended_attributes;
125
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
125
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
126
is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
126
is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
127
$attribute_value = $patron->get_extended_attribute_value('my invalid code');
127
$attribute_value = $patron->get_extended_attribute('my invalid code');
128
is( $attribute_value, undef, 'non existent attribute should undef');
128
is( $attribute_value, undef, 'non existent attribute should undef');
129
129
130
$attribute_value = $patron->get_extended_attribute_value($attributes->[0]->{code});
130
$attribute_value = $patron->get_extended_attribute($attributes->[0]->{code});
131
is( $attribute_value, $attributes->[0]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
131
is( $attribute_value->attribute, $attributes->[0]->{value}, 'get_extended_attribute returns the correct attribute value' );
132
$attribute_value = $patron->get_extended_attribute_value($attributes->[1]->{code});
132
$attribute_value = $patron->get_extended_attribute($attributes->[1]->{code});
133
is( $attribute_value, $attributes->[1]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
133
is( $attribute_value->attribute, $attributes->[1]->{value}, 'get_extended_attribute returns the correct attribute value' );
134
134
135
135
136
my $attribute = {
136
my $attribute = {
Lines 174-200 for my $attr( split(' ', $attributes->[1]->{value}) ) { Link Here
174
}
174
}
175
175
176
176
177
C4::Members::Attributes::DeleteBorrowerAttribute();
177
$patron->get_extended_attribute($attribute->{code})->delete;
178
$borrower_attributes = $patron->get_extended_attributes;
178
$borrower_attributes = $patron->get_extended_attributes;
179
is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
179
is( $borrower_attributes->count, 2, 'delete attribute by code' );
180
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
181
$borrower_attributes = $patron->get_extended_attributes;
182
is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
183
C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
184
$borrower_attributes = $patron->get_extended_attributes;
185
is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
186
187
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
188
$borrower_attributes = $patron->get_extended_attributes;
189
is( $borrower_attributes->count, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
190
$attr_0 = $borrower_attributes->next;
180
$attr_0 = $borrower_attributes->next;
191
is( $attr_0->code, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
181
is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code');
192
is( $attr_0->type->description, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
182
is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code');
193
is( $attr_0->attribute, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
183
is( $attr_0->attribute, $attributes->[1]->{value}, 'delete attribute by code');
194
184
195
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
185
$patron->get_extended_attribute($attributes->[1]->{code})->delete;
196
$borrower_attributes = $patron->get_extended_attributes;
186
$borrower_attributes = $patron->get_extended_attributes;
197
is( $borrower_attributes->count, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
187
is( $borrower_attributes->count, 1, 'delete attribute by code' );
198
188
199
# Regression tests for bug 16504
189
# Regression tests for bug 16504
200
t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
190
t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
(-)a/tools/modborrowers.pl (-2 / +1 lines)
Lines 388-394 if ( $op eq 'do' ) { Link Here
388
            if ( grep { $_ eq $valuename } @disabled ) {
388
            if ( grep { $_ eq $valuename } @disabled ) {
389
                # The attribute is disabled, we remove it for this borrower !
389
                # The attribute is disabled, we remove it for this borrower !
390
                eval {
390
                eval {
391
                    C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
391
                    $patron->get_extended_attribute($attribute->{code})->delete;
392
                };
392
                };
393
                push @errors, { error => $@ } if $@;
393
                push @errors, { error => $@ } if $@;
394
            } else {
394
            } else {
395
- 

Return to bug 20443