Bug 33161: (follow-up) Consistent use of db fields throughout
[koha-ffzg.git] / Koha / Object.pm
index 5d0f3f7..102d715 100644 (file)
@@ -335,15 +335,15 @@ sub get_from_storage {
     return $object_class->_new_from_dbic($stored_object);
 }
 
-=head3 $object->messages
+=head3 $object->object_messages
 
-    my @messages = @{ $object->messages };
+    my @messages = @{ $object->object_messages };
 
 Returns the (probably non-fatal) messages that were recorded on the object.
 
 =cut
 
-sub messages {
+sub object_messages {
     my ( $self ) = @_;
 
     $self->{_messages} = []
@@ -553,43 +553,70 @@ sub to_api {
     my ( $self, $params ) = @_;
     my $json_object = $self->TO_JSON;
 
-    # Remove forbidden attributes if required
+    # Make sure we duplicate the $params variable to avoid
+    # breaking calls in a loop (Koha::Objects->to_api)
+    $params = defined $params ? {%$params} : {};
+
+    # children should be able to handle without
+    my $embeds  = delete $params->{embed};
+    my $strings = delete $params->{strings};
+
+    # coded values handling
+    my $avs = {};
+    if ( $strings and $self->can('api_strings_mapping') ) {
+        $avs = $self->api_strings_mapping($params);
+    }
+
+    # 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} ) {
+                delete $avs->{$field}
+                  unless any { $_ eq $field } @{ $self->public_read_list };
+            }
         }
     }
 
     my $to_api_mapping = $self->to_api_mapping;
 
-    # Rename attributes if there's a mapping
+    # Rename attributes and coded values if there's a mapping
     if ( $self->can('to_api_mapping') ) {
         foreach my $column ( keys %{ $self->to_api_mapping } ) {
             my $mapped_column = $self->to_api_mapping->{$column};
             if ( exists $json_object->{$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 )
             {
+
                 # key == undef
                 delete $json_object->{$column};
+                delete $avs->{$column};
             }
         }
     }
 
-    # Make sure we duplicate the $params variable to avoid
-    # breaking calls in a loop (Koha::Objects->to_api)
-    $params    = {%$params};
-    my $embeds = delete $params->{embed};
+    $json_object->{_strings} = $avs
+      if $strings;
 
     if ($embeds) {
         foreach my $embed ( keys %{$embeds} ) {
-            if ( $embed =~ m/^(?<relation>.*)_count$/
-                and $embeds->{$embed}->{is_count} ) {
+            if (    $embed =~ m/^(?<relation>.*)_count$/
+                and $embeds->{$embed}->{is_count} )
+            {
 
                 my $relation = $+{relation};
                 $json_object->{$embed} = $self->$relation->count;
@@ -598,6 +625,9 @@ sub to_api {
                 my $curr = $embed;
                 my $next = $embeds->{$curr}->{children};
 
+                $params->{strings} = 1
+                  if $embeds->{$embed}->{strings};
+
                 my $children = $self->$curr;
 
                 if ( defined $children and ref($children) eq 'ARRAY' ) {
@@ -646,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<hashref>. Each class should have its
+own mapping returned.
+
+=cut
+
+sub api_strings_mapping {
+    return {};
+}
+
 =head3 public_read_list
 
 
@@ -751,7 +802,7 @@ sub attributes_from_api {
         elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
             try {
                 if ( $columns_info->{$koha_field_name}->{data_type} eq 'date' ) {
-                    $value = $dtf->format_date(dt_from_string($value, 'rfc3339'))
+                    $value = $dtf->format_date(dt_from_string($value, 'iso'))
                         if defined $value;
                 }
                 else {
@@ -907,7 +958,7 @@ sub _handle_to_api_child {
 
     if ( defined $child ) {
 
-        Koha::Exceptions::Exception->throw( "Asked to embed $curr but its return value doesn't implement to_api" )
+        Koha::Exception->throw( "Asked to embed $curr but its return value doesn't implement to_api" )
             if defined $next and blessed $child and !$child->can('to_api');
 
         if ( blessed $child ) {