require SL::DB::CustomVariableValidity;
- my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
- return (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0;
+ # only base level custom variables can be invalid. ovverloaded ones could potentially clash on trans_id, so disallow them
+ return 1 if $_->sub_module;
+
+ $self->{is_valid} //= do {
+ my $query = [config_id => $self->config_id, trans_id => $self->trans_id];
+ (SL::DB::Manager::CustomVariableValidity->get_all_count(query => $query) == 0) ? 1 : 0;
+ }
}
1;
use Carp;
use Data::Dumper;
use List::Util qw(first);
-use List::UtilsBy qw(sort_by);
+use List::UtilsBy qw(sort_by partition_by);
use constant META_CVARS => 'cvars_config';
my $configs = _all_configs(%params);
my $cvars = $self->custom_variables;
my %cvars_by_config = map { $_->config_id => $_ } @$cvars;
+ my $invalids = _all_invalids($self->${\ $self->meta->primary_key_columns->[0]->name }, $configs, %params);
+ my %invalids_by_config = map { $_->config_id => 1 } @$invalids;
my @return = map(
{
+ my $cvar;
if ( $cvars_by_config{$_->id} ) {
- $cvars_by_config{$_->id};
+ $cvar = $cvars_by_config{$_->id};
+ $cvar->{is_valid} = !$invalids_by_config{$_->id};
}
else {
- my $cvar = _new_cvar($self, %params, config => $_);
+ $cvar = _new_cvar($self, %params, config => $_);
$self->add_custom_variables($cvar);
- $cvar;
+ $cvar->{is_valid} = 1;
}
+ $cvar->{config} = $_;
+ $cvar;
}
@$configs
);
require SL::DB::CustomVariableConfig;
- SL::DB::Manager::CustomVariableConfig->get_all_sorted($params{module} ? (query => [ module => $params{module} ]) : ());
+ my $cache = $::request->cache("::SL::DB::Helper::CustomVariables::object_cache");
+
+ if (!$cache->{all}) {
+ my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted;
+ $cache->{all} = $configs;
+ $cache->{module} = { partition_by { $_->module } @$configs };
+ }
+
+ $params{module}
+ ? $cache->{modules}{$params{module}}
+ : $cache->{all};
}
sub _overload_by_module {
);
}
+
+sub _all_invalids {
+ my ($trans_id, $configs, %params) = @_;
+
+ require SL::DB::CustomVariableValidity;
+
+ # easy 1: no trans_id, all valid by default.
+ return [] unless $trans_id;
+
+ # easy 2: no module in params? no validity
+ return [] unless $params{module};
+
+ my @module_configs = grep { $_->module eq $params{module} } @$configs;
+
+ return [] unless @module_configs;
+
+ # nor find all entries for that and return
+ SL::DB::Manager::CustomVariableValidity->get_all(
+ query => [
+ config_id => [ map { $_->id } @module_configs ],
+ trans_id => $trans_id,
+ ]
+ );
+}
+
1;
__END__