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

(-)a/Koha/Patron.pm (-3 / +56 lines)
Lines 108-121 Koha::Patron - Koha Patron Object class Link Here
108
108
109
=head2 Class Methods
109
=head2 Class Methods
110
110
111
=head3 new
111
=head3 new, _new_from_dbic, _add_dynamic_methods
112
113
These methods are used internally. They dynamically add accessors to Koha::Patron objects resulting from 
114
calls to ->new, ->find and ->search for any defined extended patron attributes. Said accessors will be named
115
after the 'code' of the attribute type and will return either the single value or an array of values for
116
attribute types that are repeatable.
112
117
113
=cut
118
=cut
114
119
115
sub new {
120
sub new {
116
    my ( $class, $params ) = @_;
121
    my ( $class, $params ) = @_;
117
122
118
    return $class->SUPER::new($params);
123
    my $self = $class->next::method($params);
124
125
    $self->_add_dynamic_methods;
126
127
    return $self;
128
}
129
130
sub _new_from_dbic {
131
    my $self = shift;
132
    my ( $source, $data, $prefetch ) = @_;
133
134
    my $obj = $self->next::method(@_);
135
136
    $obj->_add_dynamic_methods;
137
138
    return $obj;
139
}
140
141
sub _add_dynamic_methods {
142
    my $self = shift;
143
144
    # Fetch attributes dynamically
145
    my $attribute_types = Koha::Patron::Attribute::Types->search();
146
    while ( my $attr = $attribute_types->next ) {
147
        my $code = $attr->code;
148
149
        # Dynamically create accessor method
150
        no strict 'refs';
151
        if ( !defined *{ ref($self) . "::$code" }{CODE} ) {
152
            if ( $attr->repeatable ) {
153
                *{ ref($self) . "::$code" } = sub {
154
                    my ($self) = @_;
155
                    return $self->_result->get_column($code) if $self->_result->has_column($code);
156
                    my $attributes = $self->_result->borrower_attributes->search( { code => $code } );
157
                    my $values     = [];
158
                    while ( my $attribute = $attributes->next ) {
159
                        push @$values, $attribute->attribute;
160
                    }
161
                    return $values;
162
                };
163
            } else {
164
                *{ ref($self) . "::$code" } = sub {
165
                    my ($self) = @_;
166
                    return $self->_result->get_column($code) if $self->_result->has_column($code);
167
                    my $attribute = $self->_result->borrower_attributes->search( { code => $code } )->first;
168
                    return $attribute ? $attribute->attribute : undef;
169
                };
170
            }
171
        }
172
    }
119
}
173
}
120
174
121
=head3 fixup_cardnumber
175
=head3 fixup_cardnumber
122
- 

Return to bug 30657