From bfdecc12defc600528874714db61f72579e12cc3 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 16 Jul 2024 14:02:45 +0100 Subject: [PATCH] Bug 30657: Dynamically add attribute accessors This patch adds dynamic creation of accessors for extended patron attributes based on attribute code. It should work for Koha::Patron objects resulting from calls to Koha::Patron->new(), Koha::Patrons->search, Koha::Patrons->find and any relations that return a Koha::Patron object. We return a single value for non-repeatable attributes and an arrayref of values for repeatable ones. This should make it simple to access such attributes in Koha Notice Templates. Simply use the 'code' in your variables.. example: You've added a 'smartcard' extended attribute to your system, you would refer to it in your notice as [% patron.smartcard | html %] --- Koha/Patron.pm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index ec7f6704983..b4a734d6ffa 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -108,14 +108,68 @@ Koha::Patron - Koha Patron Object class =head2 Class Methods -=head3 new +=head3 new, _new_from_dbic, _add_dynamic_methods + +These methods are used internally. They dynamically add accessors to Koha::Patron objects resulting from +calls to ->new, ->find and ->search for any defined extended patron attributes. Said accessors will be named +after the 'code' of the attribute type and will return either the single value or an array of values for +attribute types that are repeatable. =cut sub new { my ( $class, $params ) = @_; - return $class->SUPER::new($params); + my $self = $class->next::method($params); + + $self->_add_dynamic_methods; + + return $self; +} + +sub _new_from_dbic { + my $self = shift; + my ( $source, $data, $prefetch ) = @_; + + my $obj = $self->next::method(@_); + + $obj->_add_dynamic_methods; + + return $obj; +} + +sub _add_dynamic_methods { + my $self = shift; + + # Fetch attributes dynamically + my $attribute_types = Koha::Patron::Attribute::Types->search(); + while ( my $attr = $attribute_types->next ) { + my $code = $attr->code; + + # Dynamically create accessor method + no strict 'refs'; + if ( !defined *{ ref($self) . "::$code" }{CODE} ) { + if ( $attr->repeatable ) { + *{ ref($self) . "::$code" } = sub { + my ($self) = @_; + return $self->_result->get_column($code) if $self->_result->has_column($code); + my $attributes = $self->_result->borrower_attributes->search( { code => $code } ); + my $values = []; + while ( my $attribute = $attributes->next ) { + push @$values, $attribute->attribute; + } + return $values; + }; + } else { + *{ ref($self) . "::$code" } = sub { + my ($self) = @_; + return $self->_result->get_column($code) if $self->_result->has_column($code); + my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first; + return $attribute ? $attribute->attribute : undef; + }; + } + } + } } =head3 fixup_cardnumber -- 2.48.1