From cbd75fe6ce607eae2b12f5a90151d2330e95987c Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 12 Oct 2021 11:44:12 +0200
Subject: [PATCH] Bug 28692: Get from storage before log actions
Content-Type: text/plain; charset=utf-8

To make sure we have logging the values from DB.
It will reduce DB action_log table size when we log the full object and
it contains DateTime object.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 C4/Log.pm      |  9 +++++++++
 C4/Reserves.pm | 11 +++++------
 Koha/Hold.pm   |  9 ++++-----
 Koha/Item.pm   |  3 +--
 4 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/C4/Log.pm b/C4/Log.pm
index 5a9077de0d..f40b7c09f9 100644
--- a/C4/Log.pm
+++ b/C4/Log.pm
@@ -76,6 +76,15 @@ sub logaction {
     $usernumber ||= 0;
     $interface //= C4::Context->interface;
 
+    if ( ref($infos) && ref($infos) !~ /HASH|ARRAY/ && $infos->isa('Koha::Object') ) {
+        $infos = $infos->get_from_storage if $infos->in_storage;
+        $infos = Dumper( $infos->unblessed );
+
+        if ( $infos->isa('Koha::Item') && $modulename eq 'CATALOGUING' && $actionname eq 'MODIFY' ) {
+            $infos = "item " . $infos;
+        }
+    }
+
     my $dbh = C4::Context->dbh;
     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)");
     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface);
diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index f2ee463979..a42168bc72 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -46,7 +46,6 @@ use Koha::Old::Hold;
 use Koha::Patrons;
 use Koha::Plugins;
 
-use Data::Dumper qw( Dumper );
 use List::MoreUtils qw( any );
 
 =head1 NAME
@@ -261,7 +260,7 @@ sub AddReserve {
     )->store();
     $hold->set_waiting() if $found && $found eq 'W';
 
-    logaction( 'HOLDS', 'CREATE', $hold->id, Dumper($hold->unblessed) )
+    logaction( 'HOLDS', 'CREATE', $hold->id, $hold )
         if C4::Context->preference('HoldsLog');
 
     my $reserve_id = $hold->id();
@@ -1053,14 +1052,14 @@ sub ModReserve {
         $hold->cancel({ cancellation_reason => $cancellation_reason });
     }
     elsif ($hold->found && $hold->priority eq '0' && $date) {
-        logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
+        logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, $hold )
             if C4::Context->preference('HoldsLog');
 
         # The only column that can be updated for a found hold is the expiration date
         $hold->expirationdate(dt_from_string($date))->store();
     }
     elsif ($rank =~ /^\d+/ and $rank > 0) {
-        logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
+        logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, $hold )
             if C4::Context->preference('HoldsLog');
 
         my $properties = {
@@ -1122,7 +1121,7 @@ sub ModReserveFill {
         }
     );
 
-    logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->unblessed) )
+    logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, $hold )
         if C4::Context->preference('HoldsLog');
 
     # FIXME Must call Koha::Hold->cancel ? => No, should call ->filled and add the correct log
@@ -1250,7 +1249,7 @@ sub ModReserveAffect {
     });
     $std->execute($hold->reserve_id);
 
-    logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, Dumper($hold->get_from_storage->unblessed) )
+    logaction( 'HOLDS', 'MODIFY', $hold->reserve_id, $hold )
         if C4::Context->preference('HoldsLog');
 
     return;
diff --git a/Koha/Hold.pm b/Koha/Hold.pm
index 26c41d7924..9736dc527f 100644
--- a/Koha/Hold.pm
+++ b/Koha/Hold.pm
@@ -20,7 +20,6 @@ package Koha::Hold;
 
 use Modern::Perl;
 
-use Data::Dumper qw( Dumper );
 use List::MoreUtils qw( any );
 
 use C4::Context;
@@ -113,7 +112,7 @@ sub suspend_hold {
     $self->suspend_until($date);
     $self->store();
 
-    logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, Dumper( $self->unblessed ) )
+    logaction( 'HOLDS', 'SUSPEND', $self->reserve_id, $self )
         if C4::Context->preference('HoldsLog');
 
     return $self;
@@ -133,7 +132,7 @@ sub resume {
 
     $self->store();
 
-    logaction( 'HOLDS', 'RESUME', $self->reserve_id, Dumper($self->unblessed) )
+    logaction( 'HOLDS', 'RESUME', $self->reserve_id, $self )
         if C4::Context->preference('HoldsLog');
 
     return $self;
@@ -150,7 +149,7 @@ sub delete {
 
     my $deleted = $self->SUPER::delete($self);
 
-    logaction( 'HOLDS', 'DELETE', $self->reserve_id, Dumper($self->unblessed) )
+    logaction( 'HOLDS', 'DELETE', $self->reserve_id, $self )
         if C4::Context->preference('HoldsLog');
 
     return $deleted;
@@ -568,7 +567,7 @@ sub cancel {
                 );
             }
 
-            C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, Dumper($self->unblessed) )
+            C4::Log::logaction( 'HOLDS', 'CANCEL', $self->reserve_id, $self )
                 if C4::Context->preference('HoldsLog');
         }
     );
diff --git a/Koha/Item.pm b/Koha/Item.pm
index 5fada53af8..d70fbc3f8f 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -20,7 +20,6 @@ package Koha::Item;
 use Modern::Perl;
 
 use List::MoreUtils qw( any );
-use Data::Dumper qw( Dumper );
 
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
@@ -205,7 +204,7 @@ sub store {
     if ( $log_action && C4::Context->preference("CataloguingLog") ) {
         $action eq 'create'
           ? logaction( "CATALOGUING", "ADD", $self->itemnumber, "item" )
-          : logaction( "CATALOGUING", "MODIFY", $self->itemnumber, "item " . Dumper( $self->unblessed ) );
+          : logaction( "CATALOGUING", "MODIFY", $self->itemnumber, $self );
     }
     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
     $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" )
-- 
2.20.1