|
Lines 1-234
Link Here
|
| 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 |
use Scalar::Util qw( reftype ); |
| 22 |
|
| 23 |
=head1 NAME |
| 24 |
|
| 25 |
Koha::Objects::Mixin::ExtendedAttributes |
| 26 |
|
| 27 |
=head2 Class methods |
| 28 |
|
| 29 |
|
| 30 |
=head3 search |
| 31 |
|
| 32 |
Overwrites the search method to include extended attributes rewrite and dynamic relation accessors |
| 33 |
|
| 34 |
=cut |
| 35 |
|
| 36 |
sub search { |
| 37 |
my ( $self, $params, $attributes ) = @_; |
| 38 |
|
| 39 |
my $class = ref($self) ? ref($self) : $self; |
| 40 |
|
| 41 |
$self->handle_query_extended_attributes( |
| 42 |
{ |
| 43 |
attributes => $attributes, |
| 44 |
filtered_params => $params, |
| 45 |
} |
| 46 |
); |
| 47 |
|
| 48 |
my $rs = $self->_resultset()->search( $params, $attributes ); |
| 49 |
return $class->_new_from_dbic($rs); |
| 50 |
} |
| 51 |
|
| 52 |
=head3 handle_query_extended_attributes |
| 53 |
|
| 54 |
Checks for the presence of extended_attributes in a query |
| 55 |
If present, it builds the dynamic extended attributes relations and rewrites the query to include the extended_attributes relation |
| 56 |
|
| 57 |
=cut |
| 58 |
|
| 59 |
sub handle_query_extended_attributes { |
| 60 |
my ( $self, $args ) = @_; |
| 61 |
|
| 62 |
my $attributes = $args->{attributes}; |
| 63 |
my $filtered_params = $args->{filtered_params}; |
| 64 |
|
| 65 |
if ( reftype( $attributes->{prefetch} ) |
| 66 |
&& reftype( $attributes->{prefetch} ) eq 'ARRAY' |
| 67 |
&& grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) ) |
| 68 |
{ |
| 69 |
|
| 70 |
my @array = $self->_get_extended_attributes_entries($filtered_params); |
| 71 |
|
| 72 |
# Calling our private method to build the extended attributes relations |
| 73 |
my @joins = $self->_build_extended_attributes_relations( \@array ); |
| 74 |
push @{ $attributes->{join} }, @joins; |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
=head3 _get_extended_attributes_entries |
| 80 |
|
| 81 |
$self->_get_extended_attributes_entries( $filtered_params, 0 ) |
| 82 |
|
| 83 |
Recursive function that returns the rewritten extended_attributes query entries. |
| 84 |
|
| 85 |
Given: |
| 86 |
[ |
| 87 |
'-and', |
| 88 |
[ |
| 89 |
{ |
| 90 |
'extended_attributes.code' => 'CODE_1', |
| 91 |
'extended_attributes.attribute' => { 'like' => '%Bar%' } |
| 92 |
}, |
| 93 |
{ |
| 94 |
'extended_attributes.attribute' => { 'like' => '%Bar%' }, |
| 95 |
'extended_attributes.code' => 'CODE_2' |
| 96 |
} |
| 97 |
] |
| 98 |
]; |
| 99 |
|
| 100 |
Returns : |
| 101 |
|
| 102 |
[ |
| 103 |
'CODE_1', |
| 104 |
'CODE_2' |
| 105 |
] |
| 106 |
|
| 107 |
=cut |
| 108 |
|
| 109 |
sub _get_extended_attributes_entries { |
| 110 |
my ( $self, $params, @array ) = @_; |
| 111 |
|
| 112 |
if ( reftype($params) && reftype($params) eq 'HASH' ) { |
| 113 |
|
| 114 |
# rewrite additional_field_values table query params |
| 115 |
@array = _rewrite_related_metadata_query( $params, 'field_id', 'value', @array ) |
| 116 |
if $params->{'extended_attributes.field_id'}; |
| 117 |
|
| 118 |
# rewrite borrower_attributes table query params |
| 119 |
@array = _rewrite_related_metadata_query( $params, 'code', 'attribute', @array ) |
| 120 |
if $params->{'extended_attributes.code'}; |
| 121 |
|
| 122 |
# rewrite illrequestattributes table query params |
| 123 |
@array = _rewrite_related_metadata_query( $params, 'type', 'value', @array ) |
| 124 |
if $params->{'extended_attributes.type'}; |
| 125 |
|
| 126 |
foreach my $key ( keys %{$params} ) { |
| 127 |
return $self->_get_extended_attributes_entries( $params->{$key}, @array ); |
| 128 |
} |
| 129 |
} elsif ( reftype($params) && reftype($params) eq 'ARRAY' ) { |
| 130 |
foreach my $ea_instance (@$params) { |
| 131 |
@array = $self->_get_extended_attributes_entries( $ea_instance, @array ); |
| 132 |
} |
| 133 |
return @array; |
| 134 |
} else { |
| 135 |
return @array; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
=head3 _rewrite_related_metadata_query |
| 140 |
|
| 141 |
$extended_attributes_entries = |
| 142 |
_rewrite_related_metadata_query( $params, 'field_id', 'value', @array ) |
| 143 |
|
| 144 |
Helper function that rewrites all subsequent extended_attributes queries to match the alias generated by the dbic self left join |
| 145 |
Take the below example (patrons): |
| 146 |
[ |
| 147 |
{ |
| 148 |
"extended_attributes.attribute":{"like":"%123%"}, |
| 149 |
"extended_attributes.code":"CODE_1" |
| 150 |
} |
| 151 |
], |
| 152 |
[ |
| 153 |
{ |
| 154 |
"extended_attributes.attribute":{"like":"%abc%" }, |
| 155 |
"extended_attributes.code":"CODE_2" |
| 156 |
} |
| 157 |
] |
| 158 |
|
| 159 |
It'll be rewritten as: |
| 160 |
[ |
| 161 |
{ |
| 162 |
'extended_attributes_CODE_1.attribute' => { 'like' => '%123%' }, |
| 163 |
'extended_attributes_CODE_1.code' => 'CODE_1' |
| 164 |
} |
| 165 |
], |
| 166 |
[ |
| 167 |
{ |
| 168 |
'extended_attributes_CODE_2.attribute' => { 'like' => '%abc%' }, |
| 169 |
'extended_attributes_CODE_2.code' => 'CODE_2' |
| 170 |
} |
| 171 |
] |
| 172 |
|
| 173 |
=cut |
| 174 |
|
| 175 |
sub _rewrite_related_metadata_query { |
| 176 |
my ( $params, $key, $value, @array ) = @_; |
| 177 |
|
| 178 |
if ( ref \$params->{ 'extended_attributes.' . $key } eq 'SCALAR' ) { |
| 179 |
my $old_key_value = delete $params->{ 'extended_attributes.' . $key }; |
| 180 |
my $new_key_value = "extended_attributes_$old_key_value" . "." . $key; |
| 181 |
$params->{$new_key_value} = $old_key_value; |
| 182 |
|
| 183 |
my $old_value_value = delete $params->{ 'extended_attributes.' . $value }; |
| 184 |
my $new_value_value = "extended_attributes_$old_key_value" . "." . $value; |
| 185 |
$params->{$new_value_value} = $old_value_value; |
| 186 |
push @array, $old_key_value; |
| 187 |
} |
| 188 |
|
| 189 |
return @array; |
| 190 |
} |
| 191 |
|
| 192 |
=head3 _build_extended_attributes_relations |
| 193 |
|
| 194 |
Method to dynamically add has_many relations for Koha classes that support extended_attributes. |
| 195 |
|
| 196 |
Returns a list of relation accessor names. |
| 197 |
|
| 198 |
=cut |
| 199 |
|
| 200 |
sub _build_extended_attributes_relations { |
| 201 |
my ( $self, $types ) = @_; |
| 202 |
|
| 203 |
return if ( !grep ( /extended_attributes/, keys %{ $self->_resultset->result_source->_relationships } ) ); |
| 204 |
|
| 205 |
my $ea_config = $self->extended_attributes_config; |
| 206 |
|
| 207 |
my $result_source = $self->_resultset->result_source; |
| 208 |
for my $type ( @{$types} ) { |
| 209 |
$result_source->add_relationship( |
| 210 |
"extended_attributes_$type", |
| 211 |
"$ea_config->{schema_class}", |
| 212 |
sub { |
| 213 |
my $args = shift; |
| 214 |
|
| 215 |
return { |
| 216 |
"$args->{foreign_alias}.$ea_config->{id_field}->{foreign}" => |
| 217 |
{ -ident => "$args->{self_alias}.$ea_config->{id_field}->{self}" }, |
| 218 |
"$args->{foreign_alias}.$ea_config->{key_field}" => { '=', $type }, |
| 219 |
}; |
| 220 |
}, |
| 221 |
{ |
| 222 |
accessor => 'multi', |
| 223 |
join_type => 'LEFT', |
| 224 |
cascade_copy => 0, |
| 225 |
cascade_delete => 0, |
| 226 |
is_depends_on => 0 |
| 227 |
}, |
| 228 |
); |
| 229 |
|
| 230 |
} |
| 231 |
return map { 'extended_attributes_' . $_ } @{$types}; |
| 232 |
} |
| 233 |
|
| 234 |
1; |