From 6b0ea859f0114c291a3836813c1a7c5055bf8788 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 8 Nov 2023 11:31:18 -0100 Subject: [PATCH] Bug 35287: Preparation: Extend AdditionalFields Mixin The mixin now implements the extended_attributes method to be utilized by any Koha class that implements the AdditionalFields Mixin and is exposed through the REST API It also implements a strings_map to be utilized in the same fashion as described above. This is useful because additional fields may be an authorised value Signed-off-by: Edith Speller Signed-off-by: Michaela Sieber Signed-off-by: Paul Derscheid --- Koha/Object/Mixin/AdditionalFields.pm | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Koha/Object/Mixin/AdditionalFields.pm b/Koha/Object/Mixin/AdditionalFields.pm index 90f8ab087f..18e5c5efc7 100644 --- a/Koha/Object/Mixin/AdditionalFields.pm +++ b/Koha/Object/Mixin/AdditionalFields.pm @@ -225,6 +225,85 @@ sub additional_field_values { return Koha::AdditionalFieldValues->_new_from_dbic( $afv_rs ); } +=head3 extended_attributes + +REST API embed of additional_field_values + +=cut + +sub extended_attributes { + my ($self, $extended_attributes) = @_; + + if ($extended_attributes) { + $self->set_additional_fields($extended_attributes); + } + my $afv_rs = $self->_result->extended_attributes; + return Koha::AdditionalFieldValues->_new_from_dbic($afv_rs); +} + +=head3 strings_map + +Returns a map of column name to string representations including the string, +the mapping type and the mapping category where appropriate. + +Currently handles additional fields values mappings. + +Accepts a param hashref where the 'public' key denotes whether we want the public +or staff client strings. + +=cut + +sub strings_map { + my ( $self, $params ) = @_; + + my $afvs = $self->get_additional_field_values_for_template; + my $strings = {}; + + foreach my $af_id ( keys %{$afvs} ) { + + my $additional_field = Koha::AdditionalFields->find($af_id); + my $av_cat = $additional_field->effective_authorised_value_category; + my @af_value_arr; + my $af_value_str; + my $value_to_push; + + foreach my $av_value ( @{ $afvs->{$af_id} } ) { + if ($av_cat) { + my $av = Koha::AuthorisedValues->search( + { + category => $av_cat, + authorised_value => $av_value, + } + ); + + $value_to_push = + $av->count ? $params->{public} ? $av->next->opac_description : $av->next->lib : $av_value; + } else { + $value_to_push = $av_value; + } + push @af_value_arr, $value_to_push if $value_to_push; + } + + $af_value_str = join( ", ", @af_value_arr ); + + push( + @{ $strings->{additional_field_values} }, + { + field_label => $additional_field->name, + value_str => $af_value_str, + type => $av_cat ? 'av' : 'text', + field_id => $af_id, + } + ); + } + + my @sorted = sort { $a->{field_id} <=> $b->{field_id} } @{ $strings->{additional_field_values} }; + my @non_empty = grep { $_->{value_str} ne "" } @sorted; + $strings->{additional_field_values} = \@non_empty; + + return $strings; +} + =head1 AUTHOR Koha Development Team -- 2.47.0