Merge branch 'master' of ssh://git-sdonath@lx-office.linet-services.de/~/lx-office-erp
[kivitendo-erp.git] / SL / CVar.pm
index 0099b7c..7f82a2d 100644 (file)
@@ -25,25 +25,31 @@ sub get_configs {
     push @values, $params{module};
   }
 
-  my $query    = qq|SELECT * FROM custom_variable_configs $where ORDER BY sortkey|;
+  my $query    = <<SQL;
+    SELECT *, date_trunc('seconds', localtimestamp) AS current_timestamp
+    FROM custom_variable_configs $where ORDER BY sortkey
+SQL
 
-  my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
+  if (!$::form->{CVAR_CONFIGS}->{$params{module}}) {
+    my $configs  = selectall_hashref_query($form, $dbh, $query, @values);
 
-  foreach my $config (@{ $configs }) {
-    if ($config->{type} eq 'select') {
-      $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
+    foreach my $config (@{ $configs }) {
+      if ($config->{type} eq 'select') {
+        $config->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $config->{options}) ];
 
-    } elsif ($config->{type} eq 'number') {
-      $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
+      } elsif ($config->{type} eq 'number') {
+        $config->{precision} = $1 if ($config->{options} =~ m/precision=(\d+)/i);
 
-    }
+      }
 
-    $self->_unpack_flags($config);
+      $self->_unpack_flags($config);
+    }
+    $::form->{CVAR_CONFIGS}->{$params{module}} = $configs;
   }
 
   $main::lxdebug->leave_sub();
 
-  return $configs;
+  return $::form->{CVAR_CONFIGS}->{$params{module}};
 }
 
 sub get_config {
@@ -191,13 +197,6 @@ sub get_custom_variables {
 
   my $trans_id = $params{trans_id} ? 'OR (v.trans_id = ?) ' : '';
 
-  my $q_cfg    =
-    qq|SELECT id, name, description, type, default_value, options,
-         date_trunc('seconds', localtimestamp) AS current_timestamp, current_date AS current_date
-       FROM custom_variable_configs
-       WHERE module = ?
-       ORDER BY sortkey|;
-
   my $q_var    =
     qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value
        FROM custom_variables
@@ -205,7 +204,7 @@ sub get_custom_variables {
   $q_var      .= qq| AND (sub_module = ?)| if $params{sub_module};
   my $h_var    = prepare_query($form, $dbh, $q_var);
 
-  my $custom_variables = selectall_hashref_query($form, $dbh, $q_cfg, $params{module});
+  my $custom_variables = $self->get_configs(module => $params{module});
 
   foreach my $cvar (@{ $custom_variables }) {
     if ($cvar->{type} eq 'textfield') {
@@ -338,11 +337,10 @@ sub save_custom_variables {
 
     do_statement($form, $sth, $query, @values);
 
-    unless ($params{always_valid}) {
-      $self->save_custom_variables_validity(trans_id => $params{trans_id}, config_id => $config->{id},
-        validity => ($params{variables}->{"$params{name_prefix}cvar_$config->{name}$params{name_postfix}_valid"} ? 1 : 0)
-      );
-    };
+    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();
@@ -490,8 +488,7 @@ sub build_filter_query {
     }
 
     if (@sub_where) {
-      push @sub_where,  qq|cvar.sub_module = ?|;
-      push @sub_values, "$params{sub_module}";
+      add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || '');
 
       push @where,
         qq|$not EXISTS(
@@ -606,18 +603,6 @@ sub get_field_format_list {
   return ($date_fields, $number_fields);
 }
 
-=head2 VALIDITY
-
-Suppose the following scenario:
-
-You have a lot of parts in your database, and a set of properties cofigured. Now not every part has every of these properties, some combinations will just make no sense. In order to clean up your inputs a bit, you want to mark certain combinations as invalid, blocking them from modification and possibly display.
-
-Validity is assumed. If you modify validity, you actually save B<invalidity>.
-iNvalidity is saved as a function of config_id, and the trans_id
-
-In the naive way, disable an attribute for a specific id (simple)
-
-=cut
 sub save_custom_variables_validity {
   $main::lxdebug->enter_sub();
 
@@ -682,4 +667,59 @@ sub get_custom_variables_validity {
   return !$invalid;
 }
 
+sub custom_variables_validity_by_trans_id {
+  $main::lxdebug->enter_sub(2);
+
+  my $self     = shift;
+  my %params   = @_;
+
+  return sub { 0 } unless $params{trans_id};
+
+  my $myconfig = \%main::myconfig;
+  my $form     = $main::form;
+
+  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+  my $query    = qq|SELECT config_id, COUNT(*) FROM custom_variables_validity WHERE trans_id = ? GROUP BY config_id|;
+
+  my %invalids = selectall_as_map($form, $dbh, $query, 'config_id', 'count', $params{trans_id});
+
+  $main::lxdebug->leave_sub(2);
+
+  return sub { !$invalids{+shift} };
+}
+
 1;
+
+__END__
+
+=head1 NAME
+
+SL::CVar.pm - Custom Variables module
+
+=head1 SYNOPSIS
+
+  # dealing with configs
+
+  my $all_configs = CVar->get_configs()
+  my $config      = CVar->get_config(id => '1234')
+
+  CVar->save_config($config);
+  CVar->delete->config($config)
+
+  # dealing with custom vars
+
+  CVar->get_custom_variables(module => 'ic')
+
+=head2 VALIDITY
+
+Suppose the following scenario:
+
+You have a lot of parts in your database, and a set of properties cofigured. Now not every part has every of these properties, some combinations will just make no sense. In order to clean up your inputs a bit, you want to mark certain combinations as invalid, blocking them from modification and possibly display.
+
+Validity is assumed. If you modify validity, you actually save B<invalidity>.
+Invalidity is saved as a function of config_id, and the trans_id
+
+In the naive way, disable an attribute for a specific id (simple)
+
+=cut