|
Line 0
Link Here
|
| 0 |
- |
1 |
package Koha::Objects::Mixin::ExtendedAttributes; |
|
|
2 |
|
| 3 |
# Copyright 2024 PTFS Europe Ltd |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
=head1 NAME |
| 23 |
|
| 24 |
Koha::Objects::Mixin::ExtendedAttributes |
| 25 |
|
| 26 |
=head2 Class methods |
| 27 |
|
| 28 |
=head3 _build_extended_attributes_relations |
| 29 |
|
| 30 |
Method to dynamically add has_many relations for Koha classes that support extended_attributes. |
| 31 |
|
| 32 |
Used in the API to allow for advanced joins. |
| 33 |
|
| 34 |
Returns a list of relation accessor names. |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
sub _build_extended_attributes_relations { |
| 39 |
my ( $self, $types ) = @_; |
| 40 |
|
| 41 |
my $ea_config = $self->extended_attributes_config; |
| 42 |
|
| 43 |
my $result_source = $self->_resultset->result_source; |
| 44 |
for my $type ( @{$types} ) { |
| 45 |
$result_source->add_relationship( |
| 46 |
"extended_attributes_$type", |
| 47 |
"$ea_config->{schema_class}", |
| 48 |
sub { |
| 49 |
my $args = shift; |
| 50 |
|
| 51 |
return { |
| 52 |
"$args->{foreign_alias}.$ea_config->{id_field}" => |
| 53 |
{ -ident => "$args->{self_alias}.$ea_config->{id_field}" }, |
| 54 |
"$args->{foreign_alias}.$ea_config->{key_field}" => { '=', $type }, |
| 55 |
}; |
| 56 |
}, |
| 57 |
{ |
| 58 |
accessor => 'multi', |
| 59 |
join_type => 'LEFT', |
| 60 |
cascade_copy => 0, |
| 61 |
cascade_delete => 0, |
| 62 |
is_depends_on => 0 |
| 63 |
}, |
| 64 |
); |
| 65 |
|
| 66 |
} |
| 67 |
return map { 'extended_attributes_' . $_ } @{$types}; |
| 68 |
} |
| 69 |
|
| 70 |
1; |