SL::ShopConnector::WooCommerce Kategorien per page
[kivitendo-erp.git] / SL / CVar.pm
index a2668c2..a905ede 100644 (file)
@@ -11,6 +11,7 @@ use Data::Dumper;
 use SL::DBUtils;
 use SL::MoreCommon qw(listify);
 use SL::Util qw(trim);
+use SL::DB;
 
 sub get_configs {
   $main::lxdebug->enter_sub();
@@ -200,8 +201,16 @@ sub get_custom_variables {
 }
 
 sub save_custom_variables {
+  my ($self, %params) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_save_custom_variables, $self, %params);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _save_custom_variables {
   my $self     = shift;
   my %params   = @_;
 
@@ -210,7 +219,7 @@ sub save_custom_variables {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+  my $dbh      = $params{dbh} || SL::DB->client->dbh;
 
   my @configs  = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() };
 
@@ -235,6 +244,29 @@ sub save_custom_variables {
   my $sth = prepare_query($form, $dbh, $query);
 
   foreach my $config (@configs) {
+    if ($params{save_validity}) {
+      my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
+      my $new_valid   = $params{variables}{$valid_index} || $params{always_valid} ? 1 : 0;
+      my $old_valid   = $self->get_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id});
+
+      $self->save_custom_variables_validity(trans_id  => $params{trans_id},
+                                            config_id => $config->{id},
+                                            validity  => $new_valid,
+                                           );
+
+      if (!$new_valid || !$old_valid) {
+        # When activating a cvar (old_valid == 0 && new_valid == 1)
+        # the input to hold the variable's value wasn't actually
+        # rendered, meaning saving the value now would only save an
+        # empty value/the value 0. This means that the next time the
+        # form is rendered, an existing value is found and used
+        # instead of the variable's default value from the
+        # configuration. Therefore don't save the values in such
+        # cases.
+        next;
+      }
+    }
+
     my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id}));
 
     my $value  = $params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}"};
@@ -255,21 +287,11 @@ sub save_custom_variables {
     }
 
     do_statement($form, $sth, $query, @values);
-
-    if ($params{save_validity}) {
-      my $valid_index = "$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid";
-      $self->save_custom_variables_validity(trans_id  => $params{trans_id},
-                                            config_id => $config->{id},
-                                            validity  => ($params{variables}{$valid_index} || $params{always_valid} ? 1 : 0)
-                                           );
-    }
   }
 
   $sth->finish();
 
-  $dbh->commit() unless $params{dbh};
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
 sub render_inputs {
@@ -548,8 +570,16 @@ sub get_field_format_list {
 }
 
 sub save_custom_variables_validity {
+  my ($self, %params) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_save_custom_variables_validity, $self, %params);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _save_custom_variables_validity {
   my $self     = shift;
   my %params   = @_;
 
@@ -558,7 +588,7 @@ sub save_custom_variables_validity {
   my $myconfig = \%main::myconfig;
   my $form     = $main::form;
 
-  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+  my $dbh      = $params{dbh} || SL::DB->client->dbh;
 
   my (@where, @values);
   add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i);
@@ -584,9 +614,7 @@ sub save_custom_variables_validity {
 
   $sth->finish();
 
-  $dbh->commit() unless $params{dbh};
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }
 
 my %_validity_sub_module_mapping = (