Lines 832-876
Returns the passed params, converted from API naming into the model.
Link Here
|
832 |
|
832 |
|
833 |
=cut |
833 |
=cut |
834 |
|
834 |
|
835 |
sub attributes_from_api { |
835 |
sub _recursive_fixup { |
836 |
my ( $self, $from_api_params ) = @_; |
836 |
my ( $self, $key, $value, $column_info ) = @_; |
837 |
|
|
|
838 |
my $from_api_mapping = $self->from_api_mapping; |
839 |
|
837 |
|
840 |
my $params; |
838 |
if ( ref($value) && ref($value) eq 'HASH' ) { |
841 |
my $columns_info = $self->_result->result_source->columns_info; |
839 |
my $hash; |
842 |
my $dtf = $self->_result->result_source->storage->datetime_parser; |
840 |
for my $k ( keys %$value ) { |
|
|
841 |
$hash->{$k} = $self->_recursive_fixup( $key, $value->{$k}, $column_info ); |
842 |
} |
843 |
return $hash; |
843 |
|
844 |
|
844 |
while (my ($key, $value) = each %{ $from_api_params } ) { |
845 |
} elsif ( ref($value) && ref($value) eq 'ARRAY' ) { |
845 |
my $koha_field_name = |
846 |
return [ map { $self->_recursive_fixup( $key, $_, $column_info ) } @$value ]; |
846 |
exists $from_api_mapping->{$key} |
847 |
} else { |
847 |
? $from_api_mapping->{$key} |
848 |
if ( $column_info->{is_boolean} ) { |
848 |
: $key; |
|
|
849 |
|
849 |
|
850 |
if ( $columns_info->{$koha_field_name}->{is_boolean} ) { |
|
|
851 |
# TODO: Remove when D8 is formally deprecated |
850 |
# TODO: Remove when D8 is formally deprecated |
852 |
# Handle booleans gracefully |
851 |
# Handle booleans gracefully |
853 |
$value = ( $value ) ? 1 : 0; |
852 |
$value = ($value) ? 1 : 0; |
854 |
} |
853 |
} elsif ( _date_or_datetime_column_type( $column_info->{data_type} ) ) { |
855 |
elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) { |
854 |
if ( defined $value ) { |
856 |
if (defined $value) { |
|
|
857 |
try { |
855 |
try { |
858 |
if ( $columns_info->{$koha_field_name}->{data_type} eq 'date' ) { |
856 |
my $dtf = $self->_result->result_source->storage->datetime_parser; |
|
|
857 |
if ( $column_info->{data_type} eq 'date' ) { |
859 |
my $dt = DateTime::Format::MySQL->parse_date($value); |
858 |
my $dt = DateTime::Format::MySQL->parse_date($value); |
860 |
$value = $dtf->format_date($dt); |
859 |
$value = $dtf->format_date($dt); |
861 |
} |
860 |
} else { |
862 |
else { |
|
|
863 |
my $dt = Koha::DateTime::Format::RFC3339->parse_datetime($value); |
861 |
my $dt = Koha::DateTime::Format::RFC3339->parse_datetime($value); |
864 |
$value = $dtf->format_datetime($dt); |
862 |
$value = $dtf->format_datetime($dt); |
865 |
} |
863 |
} |
866 |
} |
864 |
} catch { |
867 |
catch { |
|
|
868 |
Koha::Exceptions::BadParameter->throw( parameter => $key ); |
865 |
Koha::Exceptions::BadParameter->throw( parameter => $key ); |
869 |
}; |
866 |
}; |
870 |
} |
867 |
} |
871 |
} |
868 |
} |
|
|
869 |
return $value; |
870 |
} |
871 |
} |
872 |
|
873 |
sub attributes_from_api { |
874 |
my ( $self, $from_api_params ) = @_; |
875 |
|
876 |
my $from_api_mapping = $self->from_api_mapping; |
877 |
|
878 |
my $params; |
879 |
my $columns_info = $self->_result->result_source->columns_info; |
880 |
|
881 |
while ( my ( $key, $value ) = each %{$from_api_params} ) { |
882 |
my $koha_field_name = |
883 |
exists $from_api_mapping->{$key} |
884 |
? $from_api_mapping->{$key} |
885 |
: $key; |
872 |
|
886 |
|
873 |
$params->{$koha_field_name} = $value; |
887 |
$params->{$koha_field_name} = $self->_recursive_fixup( $key, $value, $columns_info->{$koha_field_name} ); |
874 |
} |
888 |
} |
875 |
|
889 |
|
876 |
return $params; |
890 |
return $params; |