Bug 28585: Revert of the 3 commits
[koha-ffzg.git] / Koha / Object.pm
index c6ae91e..08602da 100644 (file)
@@ -20,14 +20,15 @@ package Koha::Object;
 
 use Modern::Perl;
 
-use Carp;
+use Carp qw( croak );
 use Mojo::JSON;
 use Scalar::Util qw( blessed looks_like_number );
-use Try::Tiny;
+use Try::Tiny qw( catch try );
 
 use Koha::Database;
 use Koha::Exceptions::Object;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Object::Message;
 
 =head1 NAME
 
@@ -77,6 +78,8 @@ sub new {
           $schema->resultset( $class->_type() )->new($attributes);
     }
 
+    $self->{_messages} = [];
+
     croak("No _type found! Koha::Object must be subclassed!")
       unless $class->_type();
 
@@ -219,9 +222,8 @@ sub update {
 Removes the object from storage.
 
 Returns:
-    1  if the deletion was a success
-    0  if the deletion failed
-    -1 if the object was never in storage
+    The item object if deletion was a success
+    The DBIX::Class error if deletion failed
 
 =cut
 
@@ -332,6 +334,50 @@ sub get_from_storage {
     return $object_class->_new_from_dbic($stored_object);
 }
 
+=head3 $object->messages
+
+    my @messages = @{ $object->messages };
+
+Returns the (probably non-fatal) messages that were recorded on the object.
+
+=cut
+
+sub messages {
+    my ( $self ) = @_;
+
+    $self->{_messages} = []
+        unless defined $self->{_messages};
+
+    return $self->{_messages};
+}
+
+=head3 $object->add_message
+
+    try {
+        <some action that might fail>
+    }
+    catch {
+        if ( <fatal condition> ) {
+            Koha::Exception->throw...
+        }
+
+        # This is a non fatal error, notify the caller
+        $self->add_message({ message => $error, type => 'error' });
+    }
+    return $self;
+
+Adds a message.
+
+=cut
+
+sub add_message {
+    my ( $self, $params ) = @_;
+
+    push @{ $self->{_messages} }, Koha::Object::Message->new($params);
+
+    return $self;
+}
+
 =head3 $object->TO_JSON
 
 Returns an unblessed representation of the object, suitable for JSON output.
@@ -360,8 +406,9 @@ sub TO_JSON {
         ) {
 
             # TODO: Remove once the solution for
-            # https://rt.cpan.org/Ticket/Display.html?id=119904
+            # https://github.com/perl5-dbi/DBD-mysql/issues/212
             # is ported to whatever distro we support by that time
+            # or we move to DBD::MariaDB
             $unblessed->{$col} += 0;
         }
         elsif ( _decimal_column_type( $columns_info->{$col}->{data_type} )
@@ -369,8 +416,9 @@ sub TO_JSON {
         ) {
 
             # TODO: Remove once the solution for
-            # https://rt.cpan.org/Ticket/Display.html?id=119904
+            # https://github.com/perl5-dbi/DBD-mysql/issues/212
             # is ported to whatever distro we support by that time
+            # or we move to DBD::MariaDB
             $unblessed->{$col} += 0.00;
         }
         elsif ( _datetime_column_type( $columns_info->{$col}->{data_type} ) ) {
@@ -410,8 +458,9 @@ sub _datetime_column_type {
 
 sub _numeric_column_type {
     # TODO: Remove once the solution for
-    # https://rt.cpan.org/Ticket/Display.html?id=119904
+    # https://github.com/perl5-dbi/DBD-mysql/issues/212
     # is ported to whatever distro we support by that time
+    # or we move to DBD::MariaDB
     my ($column_type) = @_;
 
     my @numeric_types = (
@@ -428,8 +477,9 @@ sub _numeric_column_type {
 
 sub _decimal_column_type {
     # TODO: Remove once the solution for
-    # https://rt.cpan.org/Ticket/Display.html?id=119904
+    # https://github.com/perl5-dbi/DBD-mysql/issues/212
     # is ported to whatever distro we support by that time
+    # or we move to DBD::MariaDB
     my ($column_type) = @_;
 
     my @decimal_types = (