X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FCVar.pm;h=779bfc3254431d369cc0b06fe898c39787e76345;hb=89c9ff022d3f13e27ba6bda085df15707fcfb0eb;hp=27febc5b654f31876f6ff5c931448736e7af827a;hpb=b2f44e3d98cbbd622b1497c23602eb79e71a3c64;p=kivitendo-erp.git diff --git a/SL/CVar.pm b/SL/CVar.pm index 27febc5b6..779bfc325 100644 --- a/SL/CVar.pm +++ b/SL/CVar.pm @@ -1,8 +1,12 @@ package CVar; +use strict; + use List::Util qw(first); +use Data::Dumper; use SL::DBUtils; +use SL::MoreCommon qw(listify); sub get_configs { $main::lxdebug->enter_sub(); @@ -21,23 +25,32 @@ sub get_configs { push @values, $params{module}; } - my $query = qq|SELECT * FROM custom_variable_configs $where ORDER BY sortkey|; + my $query = <{CVAR_CONFIGS} = {} unless 'HASH' eq ref $::form->{CVAR_CONFIGS}; + 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); } + $::form->{CVAR_CONFIGS}->{$params{module}} = $configs; } $main::lxdebug->leave_sub(); - return $configs; + return $::form->{CVAR_CONFIGS}->{$params{module}}; } sub get_config { @@ -57,11 +70,30 @@ sub get_config { my $config = selectfirst_hashref_query($form, $dbh, $query, conv_i($params{id})) || { }; + $self->_unpack_flags($config); + $main::lxdebug->leave_sub(); return $config; } +sub _unpack_flags { + $main::lxdebug->enter_sub(); + + my $self = shift; + my $config = shift; + + foreach my $flag (split m/:/, $config->{flags}) { + if ($flag =~ m/(.*?)=(.*)/) { + $config->{"flag_${1}"} = $2; + } else { + $config->{"flag_${flag}"} = 1; + } + } + + $main::lxdebug->leave_sub(); +} + sub save_config { $main::lxdebug->enter_sub(); @@ -166,20 +198,14 @@ 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 WHERE (config_id = ?) AND (trans_id = ?)|; + $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') { @@ -199,10 +225,15 @@ sub get_custom_variables { $cvar->{OPTIONS} = [ map { { 'value' => $_ } } split(m/\#\#/, $cvar->{options}) ]; } - my $act_var; + my ($act_var, $valid); if ($params{trans_id}) { - do_statement($form, $h_var, $q_var, conv_i($cvar->{id}), conv_i($params{trans_id})); + my @values = (conv_i($cvar->{id}), conv_i($params{trans_id})); + push @values, $params{sub_module} if $params{sub_module}; + + do_statement($form, $h_var, $q_var, @values); $act_var = $h_var->fetchrow_hashref(); + + $valid = $self->get_custom_variables_validity(config_id => $cvar->{id}, trans_id => $params{trans_id}); } if ($act_var) { @@ -211,8 +242,10 @@ sub get_custom_variables { : $cvar->{type} eq 'number' ? $act_var->{number_value} : $cvar->{type} eq 'bool' ? $act_var->{bool_value} : $act_var->{text_value}; - + $cvar->{valid} = $valid; } else { + $cvar->{valid} = 1; + if ($cvar->{type} eq 'date') { if ($cvar->{default_value} eq 'NOW') { $cvar->{value} = $cvar->{current_date}; @@ -263,7 +296,7 @@ sub save_custom_variables { my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); - my @configs = grep { $_->{module} eq $params{module} } @{ CVar->get_configs() }; + my @configs = $params{configs} ? @{ $params{configs} } : grep { $_->{module} eq $params{module} } @{ CVar->get_configs() }; my $query = qq|DELETE FROM custom_variables @@ -271,17 +304,24 @@ sub save_custom_variables { AND (config_id IN (SELECT DISTINCT id FROM custom_variable_configs WHERE module = ?))|; - do_query($form, $dbh, $query, conv_i($params{trans_id}), $params{module}); + my @values = (conv_i($params{trans_id}), $params{module}); + + if ($params{sub_module}) { + $query .= qq| AND (sub_module = ?)|; + push @values, $params{sub_module}; + } + + do_query($form, $dbh, $query, @values); $query = - qq|INSERT INTO custom_variables (config_id, trans_id, bool_value, timestamp_value, text_value, number_value) - VALUES (?, ?, ?, ?, ?, ?)|; + qq|INSERT INTO custom_variables (config_id, sub_module, trans_id, bool_value, timestamp_value, text_value, number_value) + VALUES (?, ?, ?, ?, ?, ?, ?)|; my $sth = prepare_query($form, $dbh, $query); foreach my $config (@configs) { - my @values = (conv_i($config->{id}), conv_i($params{trans_id})); + my @values = (conv_i($config->{id}), "$params{sub_module}", conv_i($params{trans_id})); - my $value = $params{variables}->{"cvar_$config->{name}"}; + 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')) { push @values, undef, undef, $value, undef; @@ -297,6 +337,14 @@ 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(); @@ -307,7 +355,7 @@ sub save_custom_variables { } sub render_inputs { - $main::lxdebug->enter_sub(); + $main::lxdebug->enter_sub(2); my $self = shift; my %params = @_; @@ -317,11 +365,18 @@ sub render_inputs { my $myconfig = \%main::myconfig; my $form = $main::form; + my %options = ( name_prefix => "$params{name_prefix}", + name_postfix => "$params{name_postfix}", + hide_non_editable => $params{hide_non_editable}, + show_disabled_message => $params{show_disabled_message}, + ); + foreach my $var (@{ $params{variables} }) { - $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { 'var' => $var }); + $var->{HTML_CODE} = $form->parse_html_template('amcvar/render_inputs', { var => $var, %options }); + $var->{VALID_BOX} = $form->parse_html_template('amcvar/render_checkboxes', { var => $var, %options }); } - $main::lxdebug->leave_sub(); + $main::lxdebug->leave_sub(2); } sub render_search_options { @@ -335,6 +390,8 @@ sub render_search_options { my $myconfig = \%main::myconfig; my $form = $main::form; + $params{hidden_cvar_filters} = $myconfig->{hide_cvar_search_options}; + $params{include_prefix} = 'l_' unless defined($params{include_prefix}); $params{include_value} ||= '1'; @@ -401,6 +458,7 @@ sub build_filter_query { my $f_op = $params{filter}->{"${name}_qtyop"}; + my $op; if ($f_op eq '==') { $op = '='; @@ -434,6 +492,8 @@ sub build_filter_query { } if (@sub_where) { + add_token(\@sub_where, \@sub_values, col => 'cvar.sub_module', val => $params{sub_module} || ''); + push @where, qq|$not EXISTS( SELECT cvar.id @@ -475,17 +535,25 @@ sub add_custom_variables_to_report { return; } + # allow sub_module to be a coderef or a fixed value + if (ref $params{sub_module} ne 'CODE') { + my $sub_module = "$params{sub_module}"; + $params{sub_module} = sub { $sub_module }; + } + my %cfg_map = map { $_->{id} => $_ } @{ $configs }; my @cfg_ids = keys %cfg_map; my $query = qq|SELECT text_value, timestamp_value, timestamp_value::date AS date_value, number_value, bool_value, config_id FROM custom_variables - WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|)) AND (trans_id = ?)|; + WHERE (config_id IN (| . join(', ', ('?') x scalar(@cfg_ids)) . qq|)) + AND (trans_id = ?) + AND (sub_module = ?)|; my $sth = prepare_query($form, $dbh, $query); foreach my $row (@{ $params{data} }) { - do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}})); + do_statement($form, $sth, $query, @cfg_ids, conv_i($row->{$params{trans_id_field}}), $params{sub_module}->($row)); while (my $ref = $sth->fetchrow_hashref()) { my $cfg = $cfg_map{$ref->{config_id}}; @@ -493,7 +561,7 @@ sub add_custom_variables_to_report { $row->{"cvar_$cfg->{name}"} = $cfg->{type} eq 'date' ? $ref->{date_value} : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value} - : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $config->{precision}) + : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision}) : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No')) : $ref->{text_value}; } @@ -539,5 +607,123 @@ sub get_field_format_list { return ($date_fields, $number_fields); } +sub save_custom_variables_validity { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, qw(config_id trans_id validity)); + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my (@where, @values); + add_token(\@where, \@values, col => "config_id", val => $params{config_id}, esc => \&conv_i); + add_token(\@where, \@values, col => "trans_id", val => $params{trans_id}, esc => \&conv_i); + + my $where = scalar @where ? "WHERE " . join ' AND ', @where : ''; + my $query = qq|DELETE FROM custom_variables_validity $where|; + + do_query($form, $dbh, $query, @values); + + $query = + qq|INSERT INTO custom_variables_validity (config_id, trans_id) + VALUES (?, ? )|; + my $sth = prepare_query($form, $dbh, $query); + + unless ($params{validity}) { + foreach my $config_id (listify $params{config_id}) { + foreach my $trans_id (listify $params{trans_id}) { + do_statement($form, $sth, $query, conv_i($config_id), conv_i($trans_id)); + } + } + } + + $sth->finish(); + + $dbh->commit(); + + $main::lxdebug->leave_sub(); +} + +sub get_custom_variables_validity { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, qw(config_id trans_id)); + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my $query = qq|SELECT COUNT(*) FROM custom_variables_validity WHERE config_id = ? AND trans_id = ?|; + + my ($invalid) = selectfirst_array_query($form, $dbh, $query, conv_i($params{config_id}), conv_i($params{trans_id})); + + $main::lxdebug->leave_sub(2); + + 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 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