BEGIN {
@ISA = qw(Exporter);
@EXPORT_OK = qw(CheckUniqueness SetBorrowerAttributes
- DeleteBorrowerAttribute UpdateBorrowerAttribute
+ UpdateBorrowerAttribute
extended_attributes_code_value_arrayref extended_attributes_merge
SearchIdMatchingAttribute);
%EXPORT_TAGS = ( all => \@EXPORT_OK );
return 1; # borrower attributes successfully set
}
-=head2 DeleteBorrowerAttribute
-
- DeleteBorrowerAttribute($borrowernumber, $attribute);
-
-Delete a borrower attribute for the patron identified by C<$borrowernumber> and the attribute code of C<$attribute>
-
-=cut
-
-sub DeleteBorrowerAttribute {
- my ( $borrowernumber, $attribute ) = @_;
-
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(qq{
- DELETE FROM borrower_attributes
- WHERE borrowernumber = ?
- AND code = ?
- } );
- $sth->execute( $borrowernumber, $attribute->{code} );
-}
-
=head2 UpdateBorrowerAttribute
UpdateBorrowerAttribute($borrowernumber, $attribute );
sub UpdateBorrowerAttribute {
my ( $borrowernumber, $attribute ) = @_;
- DeleteBorrowerAttribute $borrowernumber, $attribute;
+ Koha::Patrons->find($borrowernumber)->get_extended_attributes->search({ 'me.code' => $attribute->{code} })->filter_by_branch_limitations->delete;
my $dbh = C4::Context->dbh;
my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
)->store();
}
-=head3 get_extended_attribute_value
+=head3 get_extended_attribute
-my $attribute_value = $patron->get_extended_attribute_value( $code );
+my $attribute_value = $patron->get_extended_attribute( $code );
-Return the attribute's value for the code passed in parameter.
+Return the attribute for the code passed in parameter.
It not exist it returns undef
=cut
-sub get_extended_attribute_value {
- my ( $self, $code ) = @_;
+sub get_extended_attribute {
+ my ( $self, $code, $value ) = @_;
my $rs = $self->_result->borrower_attributes;
return unless $rs;
- my $attribute = $rs->search( { code => $code } );
+ my $attribute = $rs->search({ code => $code, ( $value ? ( attribute => $value ) : () ) });
return unless $attribute->count;
- return $attribute->next->attribute;
+ return $attribute->next;
}
=head3 to_api
my $show_barcode = Koha::Patron::Attribute::Types->search( # FIXME we should not need this search
{ code => ATTRIBUTE_SHOW_BARCODE } )->count;
if ($show_barcode) {
- my $patron_show_barcode = $patron->get_extended_attribute_value(ATTRIBUTE_SHOW_BARCODE);
- undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
+ my $patron_show_barcode = $patron->get_extended_attribute(ATTRIBUTE_SHOW_BARCODE);
+ undef $show_barcode if $patron_show_barcode and not $patron_show_barcode->attribute;
}
$template->param( show_barcode => 1 ) if $show_barcode;
'Extended attributes are not deleted'
);
- is( $patron->get_extended_attribute_value( $attr_type2->{code} ), 'BAR', 'Mapped attribute is BAR' );
+ is( $patron->get_extended_attribute( $attr_type2->{code} )->attribute, 'BAR', 'Mapped attribute is BAR' );
$auth->unmock('update_local');
$auth->unmock('ldap_entry_2_hash');
my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
- $attribute_12 = $patron_2->get_extended_attribute_value( $attribute_type1->code );
- is( $attribute_12, 'my attribute12', 'Koha::Patron->get_extended_attribute_value should return the correct attribute value' );
+ $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code );
+ is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' );
- # TODO - What about multiple?
- my $non_existent = $patron_2->get_extended_attribute_value( 'not_exist' );
- is( $non_existent, undef, 'Koha::Patron->get_extended_attribute_value must return undef if the attribute does not exist' );
+ # TODO - What about multiple? POD explains the problem
+ my $non_existent = $patron_2->get_extended_attribute( 'not_exist' );
+ is( $non_existent, undef, 'Koha::Patron->get_extended_attribute must return undef if the attribute does not exist' );
# Test branch limitations
set_logged_in_user($patron_2);
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, 'my attribute limited', );
+ my $limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
+ is( $limited_value->attribute, 'my attribute limited', );
## Do we need a filtered?
- #$limited_value = $patron_1->get_extended_attribute_value( $attribute_type_limited->code );
+ #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
#is( $limited_value, undef, );
$schema->storage->txn_rollback;
use t::lib::TestBuilder;
use t::lib::Mocks;
-use Test::More tests => 41;
+use Test::More tests => 38;
use_ok('C4::Members::Attributes');
my $extended_attributes = $patron->get_extended_attributes;
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
-$attribute_value = $patron->get_extended_attribute_value('my invalid code');
+$attribute_value = $patron->get_extended_attribute('my invalid code');
is( $attribute_value, undef, 'non existent attribute should undef');
-$attribute_value = $patron->get_extended_attribute_value($attributes->[0]->{code});
-is( $attribute_value, $attributes->[0]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
-$attribute_value = $patron->get_extended_attribute_value($attributes->[1]->{code});
-is( $attribute_value, $attributes->[1]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
+$attribute_value = $patron->get_extended_attribute($attributes->[0]->{code});
+is( $attribute_value->attribute, $attributes->[0]->{value}, 'get_extended_attribute returns the correct attribute value' );
+$attribute_value = $patron->get_extended_attribute($attributes->[1]->{code});
+is( $attribute_value->attribute, $attributes->[1]->{value}, 'get_extended_attribute returns the correct attribute value' );
my $attribute = {
}
-C4::Members::Attributes::DeleteBorrowerAttribute();
+$patron->get_extended_attribute($attribute->{code})->delete;
$borrower_attributes = $patron->get_extended_attributes;
-is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
-C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
-$borrower_attributes = $patron->get_extended_attributes;
-is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
-C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
-$borrower_attributes = $patron->get_extended_attributes;
-is( $borrower_attributes->count, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
-
-C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
-$borrower_attributes = $patron->get_extended_attributes;
-is( $borrower_attributes->count, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
+is( $borrower_attributes->count, 2, 'delete attribute by code' );
$attr_0 = $borrower_attributes->next;
-is( $attr_0->code, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
-is( $attr_0->type->description, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
-is( $attr_0->attribute, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
+is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code');
+is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code');
+is( $attr_0->attribute, $attributes->[1]->{value}, 'delete attribute by code');
-C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
+$patron->get_extended_attribute($attributes->[1]->{code})->delete;
$borrower_attributes = $patron->get_extended_attributes;
-is( $borrower_attributes->count, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
+is( $borrower_attributes->count, 1, 'delete attribute by code' );
# Regression tests for bug 16504
t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
if ( grep { $_ eq $valuename } @disabled ) {
# The attribute is disabled, we remove it for this borrower !
eval {
- C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
+ $patron->get_extended_attribute($attribute->{code})->delete;
};
push @errors, { error => $@ } if $@;
} else {