From 9fc7f8bb200f0c42f202e1e27d9e1cbb7ae6358d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 9 Mar 2023 12:43:05 +0000 Subject: [PATCH] Bug 33161: (follow-up) Consistent use of db fields throughout We should stick to DB field names as long as possible to allow re-use and only convert to api field names via to_api_mapping at the last moment inside the parent to_api function. Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 04736dd0bafbef608e56e8cd4dae63bb4eb93fe6) Signed-off-by: Martin Renvoize --- Koha/Item.pm | 17 +---------------- Koha/Object.pm | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 4c7cec845c..42c8959f45 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -2090,17 +2090,6 @@ sub api_strings_mapping { foreach my $col ( keys %{$columns_info} ) { - # Skip columns not in public read list - next - unless !$params->{public} - || any { $col eq $_ } $public_read_list; - - # Skip columns that are not exposed on the API by to_api_mapping - # i.e. mapping exists but points to undef - next - if $col eq 'more_subfields_xml' # not dealt with as a regular field - || ( exists $to_api_mapping->{$col} && !defined $to_api_mapping->{$col} ); - # By now, we are done with known columns, now check the framework for mappings my $field = $self->_result->result_source->name . '.' . $col; @@ -2114,11 +2103,7 @@ sub api_strings_mapping { my $str = C4::Biblio::GetAuthorisedValueDesc( $itemtagfield, $subfield->{tagsubfield}, $self->$col, '', $tagslib, undef, $params->{public} ); my $type = exists $code_to_type->{$code} ? $code_to_type->{$code} : 'av'; - - # The _strings entry should match the API attribute name - my $mapped_attr = exists $to_api_mapping->{$col} ? $to_api_mapping->{$col} : $col; - - $strings->{$mapped_attr} = { + $strings->{$col} = { str => $str, type => $type, ( $type eq 'av' ? ( category => $code ) : () ), diff --git a/Koha/Object.pm b/Koha/Object.pm index b4b5717c34..102d7154db 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -570,13 +570,14 @@ sub to_api { # Remove forbidden attributes if required (including their coded values) if ( $params->{public} ) { for my $field ( keys %{$json_object} ) { - delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list }; + delete $json_object->{$field} + unless any { $_ eq $field } @{ $self->public_read_list }; } - if ( $strings ) { - foreach my $field (keys %{$avs}) { + if ($strings) { + foreach my $field ( keys %{$avs} ) { delete $avs->{$field} - unless any { $_ eq $field } @{ $self->public_read_list }; + unless any { $_ eq $field } @{ $self->public_read_list }; } } } @@ -588,15 +589,18 @@ sub to_api { foreach my $column ( keys %{ $self->to_api_mapping } ) { my $mapped_column = $self->to_api_mapping->{$column}; if ( exists $json_object->{$column} - && defined $mapped_column ) { + && defined $mapped_column ) + { # key != undef $json_object->{$mapped_column} = delete $json_object->{$column}; $avs->{$mapped_column} = delete $avs->{$column} if exists $avs->{$column}; - } elsif ( exists $json_object->{$column} - && !defined $mapped_column ) { + } + elsif ( exists $json_object->{$column} + && !defined $mapped_column ) + { # key == undef delete $json_object->{$column}; @@ -610,8 +614,9 @@ sub to_api { if ($embeds) { foreach my $embed ( keys %{$embeds} ) { - if ( $embed =~ m/^(?.*)_count$/ - and $embeds->{$embed}->{is_count} ) { + if ( $embed =~ m/^(?.*)_count$/ + and $embeds->{$embed}->{is_count} ) + { my $relation = $+{relation}; $json_object->{$embed} = $self->$relation->count; @@ -671,6 +676,27 @@ sub to_api_mapping { return {}; } +=head3 api_strings_mapping + + my $params = { is_public => 1 }; + my $string_map = $object->api_strings_mapping($params); + +Generic method that returns the string map for coded attributes. + +Return should be a hashref keyed on database field name with the values +being hashrefs containing 'str', 'type' and optionally 'category'. + +This is then use in to_api to render the _strings embed when requested. + +Note: this only returns an empty I. Each class should have its +own mapping returned. + +=cut + +sub api_strings_mapping { + return {}; +} + =head3 public_read_list -- 2.39.5