X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FCustomVariables.pm;h=3869f161302f2ec3d06cc0d19ae1bb53d8f34ce4;hb=4b65183fea159f1912c806b159cee0fbbcbe7f46;hp=921e7072329dfa6a54c139bfea3924941d7eacee;hpb=def4a030c8823e1cf3ebbe9291b532223a20e9d2;p=kivitendo-erp.git diff --git a/SL/DB/Helper/CustomVariables.pm b/SL/DB/Helper/CustomVariables.pm index 921e70723..3869f1613 100644 --- a/SL/DB/Helper/CustomVariables.pm +++ b/SL/DB/Helper/CustomVariables.pm @@ -4,6 +4,7 @@ use strict; use Carp; use Data::Dumper; use List::Util qw(first); +use List::UtilsBy qw(sort_by); use constant META_CVARS => 'cvars_config'; @@ -12,7 +13,7 @@ sub import { my $caller_package = caller; # TODO: if module is empty, module overloading needs to take effect - # certain stuff may have more than one overload, odr even more than one type + # certain stuff may have more than one overload, or even more than one type defined $caller_package or croak 'need to be included from a caller reference'; $params{module} ||= _calc_modules_from_overloads(%params) if $params{overloads}; @@ -28,6 +29,7 @@ sub import { make_cvar_by_name($caller_package, %params); make_cvar_as_hashref($caller_package, %params); make_cvar_value_parser($caller_package, %params); + make_cvar_custom_filter($caller_package, %params); } sub save_meta_info { @@ -97,6 +99,8 @@ sub make_cvar_by_configs { @$configs ); + @return = sort_by { $_->config->sortkey } @return; + return \@return; } } @@ -196,7 +200,7 @@ sub _new_cvar { # value needs config $inherited_value ? $cvar->value($inherited_value) - : $cvar->value($params{config}->type_dependant_default_value); + : $cvar->value($params{config}->type_dependent_default_value); return $cvar; } @@ -223,6 +227,132 @@ sub _get_primary_key_column { return $column_name; } +sub make_cvar_custom_filter { + my ($caller_package, %params) = @_; + + my $manager = $caller_package->meta->convention_manager->auto_manager_class_name; + + return unless $manager->can('filter'); + + $manager->add_filter_specs( + cvar => sub { + my ($key, $value, $prefix, $config_id) = @_; + my $config = SL::DB::Manager::CustomVariableConfig->find_by(id => $config_id); + + if (!$config) { + die "invalid config_id in $caller_package\::cvar custom filter: $config_id"; + } + + if ($config->module != $params{module}) { + die "invalid config_id in $caller_package\::cvar custom filter: expected module $params{module} - got @{[ $config->module ]}"; + } + + my @filter; + if ($config->type eq 'bool') { + @filter = $value ? ($config->value_col => 1) : (or => [ $config->value_col => undef, $config->value_col => 0 ]); + } else { + @filter = ($config->value_col => $value); + } + + my (%query, %bind_vals); + ($query{customized}, $bind_vals{customized}) = Rose::DB::Object::QueryBuilder::build_select( + dbh => $config->dbh, + select => 'trans_id', + tables => [ 'custom_variables' ], + columns => { custom_variables => [ qw(trans_id config_id text_value number_value bool_value timestamp_value sub_module) ] }, + query => [ + config_id => $config_id, + sub_module => $params{sub_module}, + @filter, + ], + query_is_sql => 1, + ); + + if ($config->type eq 'bool') { + if ($value) { + @filter = ( + '!default_value' => undef, + '!default_value' => '', + default_value => '1', + ); + + } else { + @filter = ( + or => [ + default_value => '0', + default_value => '', + default_value => undef, + ], + ); + } + + } else { + @filter = ( + '!default_value' => undef, + '!default_value' => '', + default_value => $value, + ); + } + + + my $conversion = $config->type =~ m{^(?:date|timestamp)$} ? $config->type + : $config->type =~ m{^(?:customer|vendor|part)$} ? 'integer' + : $config->type eq 'number' ? 'numeric' + : ''; + + ($query{config}, $bind_vals{config}) = Rose::DB::Object::QueryBuilder::build_select( + dbh => $config->dbh, + select => 'id', + tables => [ 'custom_variable_configs' ], + columns => { custom_variable_configs => [ qw(id default_value) ] }, + query => [ + id => $config->id, + @filter, + ], + query_is_sql => 1, + ); + + $query{config} =~ s{ (? $config->dbh, + select => 'trans_id', + tables => [ 'custom_variables' ], + columns => { custom_variables => [ qw(trans_id config_id sub_module) ] }, + query => [ + config_id => $config_id, + sub_module => $params{sub_module}, + ], + query_is_sql => 1, + ); + + foreach my $key (keys %query) { + # remove rose aliases. query builder sadly is not reentrant, and will reuse the same aliases. :( + $query{$key} =~ s{\bt\d+(?:\.)?\b}{}g; + + # manually inline the values. again, rose doesn't know how to handle bind params in subqueries :( + $query{$key} =~ s{\?}{ $config->dbh->quote(shift @{ $bind_vals{$key} }) }xeg; + + $query{$key} =~ s{\n}{ }g; + } + + my $qry_config = "EXISTS (" . $query{config} . ")"; + + my @result = ( + 'or' => [ + $prefix . 'id' => [ \$query{customized} ], + and => [ + "!${prefix}id" => [ \$query{not_customized} ], + \$qry_config, + ] + ], + ); + + return @result; + } + ); +} + 1; __END__ @@ -231,7 +361,7 @@ __END__ =head1 NAME -SL::DB::Helper::CustomVariables - Mixin to provide custom variables relations +SL::DB::Helper::CustomVariables - Mixin to provide custom variable relations =head1 SYNOPSIS @@ -371,6 +501,18 @@ some way then you have to call this function manually. For example: =back +=head1 INSTALLED MANAGER METHODS + +=over 4 + +=item Custom filter for GetModels + +If the Manager for the calling C has included the helper L, a custom filter for cvars will be added to the specs, with the following syntax: + + filter.cvar.$config_id + +=back + =head1 AUTHOR Sven Schöling Es.schoeling@linet-services.deE,