From 9e467f542bc70440f94aa62c1f84923c86267a49 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 30 Nov 2023 15:43:02 -0100 Subject: [PATCH] Bug 30645: Consider multiple instances of extended_attributes in query params This is a POC but its fully functional afaik. Im not 100% happy with the code as Im addressing the 3 possible related metadata tables individually. Im also not sure if this is the best place to put this code, please advise. Testing in the UI, this is fixing the patron search and the licenses additional fields search (requires bug 35287 applied) ILL is still not working, but I think its because the query builder in the javascript is not breaking apart multiple search words, in other words, this fix should also fix ILL illrequestattributes once the javascript is looked at. Joubu can you please rebase your test patch? --- Koha/Objects.pm | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index dc3d044bd3..a4dce1618d 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp qw( carp ); use List::MoreUtils qw( none ); use Class::Inspector; +use Scalar::Util qw( reftype ); use Koha::Database; use Koha::Exceptions::Object; @@ -136,12 +137,86 @@ based on the query I<$params> and I<$attributes> that are passed (like in DBIC). sub search { my ( $self, $params, $attributes ) = @_; + if ( reftype( $attributes->{prefetch} ) + && reftype( $attributes->{prefetch} ) eq 'ARRAY' + && grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) ) + { + my $ea_entries = $self->extended_attributes_entries( $params, 0 ); + my $ea_queries = $ea_entries / 2; + while ( $ea_entries > 0 ) { + push( @{ $attributes->{join} }, 'extended_attributes' ); + $ea_entries--; + } + } + my $class = ref($self) ? ref($self) : $self; my $rs = $self->_resultset()->search($params, $attributes); return $class->_new_from_dbic($rs); } +sub extended_attributes_entries { + my ( $self, $params, $extended_attributes_search_instances ) = @_; + + if ( reftype($params) && reftype($params) eq 'HASH' ) { + + # rewrite additional_field_values table params + if ( $params->{'extended_attributes.field_id'} ) { + $extended_attributes_search_instances++; + if ( $extended_attributes_search_instances > 1 ) { + my $old_key_value = delete $params->{'extended_attributes.field_id'}; + my $new_key_value = "extended_attributes_$extended_attributes_search_instances" . ".field_id"; + $params->{$new_key_value} = $old_key_value; + + my $old_value_value = delete $params->{'extended_attributes.value'}; + my $new_value_value = "extended_attributes_$extended_attributes_search_instances" . ".value"; + $params->{$new_value_value} = $old_value_value; + } + } + + # rewrite borrower_attributes table params + if ( $params->{'extended_attributes.code'} ) { + $extended_attributes_search_instances++; + if ( $extended_attributes_search_instances > 1 ) { + my $old_key_value = delete $params->{'extended_attributes.code'}; + my $new_key_value = "extended_attributes_$extended_attributes_search_instances" . ".code"; + $params->{$new_key_value} = $old_key_value; + + my $old_value_value = delete $params->{'extended_attributes.attribute'}; + my $new_value_value = "extended_attributes_$extended_attributes_search_instances" . ".attribute"; + $params->{$new_value_value} = $old_value_value; + } + } + + # rewrite illrequestattributes table params + if ( $params->{'extended_attributes.type'} ) { + $extended_attributes_search_instances++; + if ( $extended_attributes_search_instances > 1 ) { + my $old_key_value = delete $params->{'extended_attributes.type'}; + my $new_key_value = "extended_attributes_$extended_attributes_search_instances" . ".type"; + $params->{$new_key_value} = $old_key_value; + + my $old_value_value = delete $params->{'extended_attributes.value'}; + my $new_value_value = "extended_attributes_$extended_attributes_search_instances" . ".value"; + $params->{$new_value_value} = $old_value_value; + } + } + + foreach my $key ( keys %{$params} ) { + return $self->extended_attributes_entries( $params->{$key}, $extended_attributes_search_instances ); + } + return $extended_attributes_search_instances; + } elsif ( reftype($params) && reftype($params) eq 'ARRAY' ) { + foreach my $ea_instance (@$params) { + $extended_attributes_search_instances = + +$self->extended_attributes_entries( $ea_instance, $extended_attributes_search_instances ); + } + return $extended_attributes_search_instances; + } else { + return $extended_attributes_search_instances; + } +} + =head3 search_related my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? ); -- 2.30.2