epic-s6ts
[kivitendo-erp.git] / SL / CVar.pm
index 37d865a..637f129 100644 (file)
@@ -10,6 +10,7 @@ use Data::Dumper;
 
 use SL::DBUtils;
 use SL::MoreCommon qw(listify);
+use SL::Presenter::Text;
 use SL::Util qw(trim);
 use SL::DB;
 
@@ -46,7 +47,7 @@ SQL
       } elsif ($config->{type} eq 'number') {
         $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
 
-      } elsif ($config->{type} eq 'textfield') {
+      } elsif ($config->{type} =~ m{^(?:html|text)field$}) {
         $config->{width}  = 30;
         $config->{height} =  5;
         $config->{width}  = $1 if ($config->{options} =~ m/width=(\d+)/i);
@@ -112,7 +113,7 @@ sub get_custom_variables {
   my $custom_variables = $self->get_configs(module => $params{module});
 
   foreach my $cvar (@{ $custom_variables }) {
-    if ($cvar->{type} eq 'textfield') {
+    if ($cvar->{type} =~ m{^(?:html|text)field}) {
       $cvar->{width}  = 30;
       $cvar->{height} =  5;
 
@@ -244,11 +245,34 @@ 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}"};
 
-    if (($config->{type} eq 'text') || ($config->{type} eq 'textfield') || ($config->{type} eq 'select')) {
+    if (any { $config->{type} eq $_ } qw(text textfield htmlfield select)) {
       push @values, undef, undef, $value, undef;
 
     } elsif (($config->{type} eq 'date') || ($config->{type} eq 'timestamp')) {
@@ -264,14 +288,6 @@ 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();
@@ -360,7 +376,7 @@ sub build_filter_query {
 
     my (@sub_values, @sub_where, $not);
 
-    if (($config->{type} eq 'text') || ($config->{type} eq 'textfield')) {
+    if (any { $config->{type} eq $_ } qw(text textfield htmlfield)) {
       next unless ($params{filter}->{$name});
 
       push @sub_where,  qq|cvar.text_value ILIKE ?|;
@@ -510,6 +526,7 @@ sub add_custom_variables_to_report {
         : $cfg->{type} eq 'vendor'    ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value})   || SL::DB::Vendor->new)->name
         : $cfg->{type} eq 'part'      ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value})     || SL::DB::Part->new)->partnumber
         : $cfg->{type} eq 'bool'      ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No'))
+        : $cfg->{type} eq 'htmlfield' ? SL::Presenter::Text::stripped_html($ref->{text_value})
         :                               $ref->{text_value};
     }
   }