1 package SL::DB::Helper::CustomVariables;
 
   6 use List::Util qw(first);
 
   8 use constant META_CVARS => 'cvars_config';
 
  11   my ($class, %params) = @_;
 
  12   my $caller_package = caller;
 
  14   # TODO: if module is empty, module overloading needs to take effect
 
  15   # certain stuff may have more than one overload, odr even more than one type
 
  16   defined $caller_package     or croak 'need to be included from a caller reference';
 
  18   $params{module}     ||= _calc_modules_from_overloads(%params) if $params{overloads};
 
  19   $params{sub_module} ||= '';
 
  20   $params{id}         ||= _get_primary_key_column($caller_package);
 
  22   $params{module} || $params{sub_module}  or croak 'need param module or sub_module';
 
  24   return unless save_meta_info($caller_package, %params);
 
  25   make_cvar_accessor($caller_package, %params);
 
  26   make_cvar_alias($caller_package, %params)      if $params{cvars_alias};
 
  27   make_cvar_by_configs($caller_package, %params);
 
  28   make_cvar_by_name($caller_package, %params);
 
  32   my ($caller_package, %params) = @_;
 
  34   my $meta = $caller_package->meta;
 
  35   return 0 if $meta->{META_CVARS()};
 
  37   $meta->{META_CVARS()} = \%params;
 
  42 sub make_cvar_accessor {
 
  43   my ($caller_package, %params) = @_;
 
  45   my $modules = ('ARRAY' eq ref $params{module}) ?
 
  46       join ',', @{ $params{module} } :
 
  48   my @module_filter = $modules ?
 
  49     ("config_id" => [ \"(SELECT custom_variable_configs.id FROM custom_variable_configs WHERE custom_variable_configs.module IN ( '$modules' ))" ]) : # " make emacs happy
 
  52   $caller_package->meta->add_relationships(
 
  54       type         => 'one to many',
 
  55       class        => 'SL::DB::CustomVariable',
 
  56       column_map   => { $params{id} => 'trans_id' },
 
  57       query_args   => [ sub_module => $params{sub_module}, @module_filter ],
 
  63   my ($caller_package) = @_;
 
  65   *{ $caller_package . '::cvars' } =  sub {
 
  66     goto &{ $caller_package . '::custom_variables' };
 
  70 # this is used for templates where you need to list every applicable config
 
  71 # auto vivifies non existent cvar objects as necessary.
 
  72 sub make_cvar_by_configs {
 
  73   my ($caller_package, %params) = @_;
 
  76   *{ $caller_package . '::cvars_by_config' } = sub {
 
  78     @_ > 1 and croak "not an accessor";
 
  80     my $configs     = _all_configs(%params);
 
  81     my $cvars       = $self->custom_variables;
 
  82     my %cvars_by_config = map { $_->config_id => $_ } @$cvars;
 
  86         if ( $cvars_by_config{$_->id} ) {
 
  87           $cvars_by_config{$_->id};
 
  90           my $cvar = _new_cvar($self, %params, config => $_);
 
  91           $self->add_custom_variables($cvar);
 
 102 # this is used for print templates where you need to refer to a variable by name
 
 103 # TODO typically these were referred as prefix_'cvar'_name
 
 104 sub make_cvar_by_name {
 
 105   my ($caller_package, %params) = @_;
 
 108   *{ $caller_package . '::cvar_by_name' } = sub {
 
 109     my ($self, $name) = @_;
 
 111     my $configs = _all_configs(%params);
 
 112     my $cvars   = $self->custom_variables;
 
 113     my $config  = first { $_->name eq $name } @$configs;
 
 115     croak "unknown cvar name $name" unless $config;
 
 117     my $cvar    = first { $_->config_id eq $config->id } @$cvars;
 
 120       $cvar = _new_cvar($self, %params, config => $config);
 
 121       $self->add_custom_variables($cvar);
 
 131   require SL::DB::CustomVariableConfig;
 
 134     ? SL::DB::Manager::CustomVariableConfig->get_all(query => [ module => $params{module} ])
 
 135     : SL::DB::Manager::CustomVariableConfig->get_all;
 
 138 sub _overload_by_module {
 
 139   my ($module, %params) = @_;
 
 141   keys %{ $params{overloads} }; # reset each iterator
 
 142   while (my ($fk, $def) = each %{ $params{overloads} }) {
 
 143     return ($fk, $def->{class}) if $def->{module} eq $module;
 
 146   croak "unknown overload, cannot resolve module $module";
 
 150   my ($self, %params) = @_;
 
 152   # check overloading first
 
 153   if ($params{sub_module}) {
 
 154     my ($fk, $class) = _overload_by_module($params{config}->module, %params);
 
 155     my $base_cvar = $class->new(id => $self->$fk)->load->cvar_by_name($params{config}->name);
 
 156     $inherited_value = $base_cvar->value;
 
 159   my $cvar = SL::DB::CustomVariable->new(
 
 160     config     => $params{config},
 
 161     trans_id   => $self->${ \ $params{id} },
 
 162     sub_module => $params{sub_module},
 
 166    ? $cvar->value($inherited_value)
 
 167    : $cvar->value($params{config}->default_value);
 
 171 sub _calc_modules_from_overloads {
 
 175   for my $def (values %{ $params{overloads} || {} }) {
 
 176     $modules{$def->{module}} = 1;
 
 179   return [ keys %modules ];
 
 182 sub _get_primary_key_column {
 
 183   my ($caller_package) = @_;
 
 184   my $meta             = $caller_package->meta;
 
 187   $column_name = $meta->{primary_key}->{columns}->[0] if $meta->{primary_key} && (ref($meta->{primary_key}->{columns}) eq 'ARRAY') && (1 == scalar(@{ $meta->{primary_key}->{columns} }));
 
 189   croak "Unable to retrieve primary key column name: meta information for package $caller_package not set up correctly" unless $column_name;
 
 202 SL::DB::Helper::CustomVariables - Mixin to provide custom variables relations
 
 206   # use in a primary class
 
 207   use SL::DB::Helper::CustomVariables (
 
 212   # use overloading in a secondary class
 
 213   use SL::DB::Helper::CustomVariables (
 
 214     sub_module  => 'orderitems',
 
 218         class => 'SL::DB::Part',
 
 226 This module provides methods to deal with named custom variables. Two concepts are understood.
 
 228 =head2 Primary CVar Classes
 
 230 Primary classes are those that feature cvars for themselves. Currently those
 
 231 are Part, Contact, Customer and Vendor. cvars for these will get saved directly
 
 234 =head2 Secondary CVar Classes
 
 236 Secondary classes inherit their cvars from member relationships. This is built
 
 237 so that orders can save a copy of the cvars of their parts, customers and the
 
 238 like to be immutable later on.
 
 240 Secondary classes may currently not have cvars of their own.
 
 242 =head1 INSTALLED METHODS
 
 246 =item C<custom_variables [ CUSTOM_VARIABLES ]>
 
 248 This is a Rose::DB::Object::Relationship accessor, generated for cvars. Use it
 
 249 like any other OneToMany relationship.
 
 251 =item C<cvars [ CUSTOM_VARIABLES ]>
 
 253 Alias to C<custom_variables>. Will only be installed if C<cvars_alias> was
 
 256 =item C<cvars_by_config>
 
 258 Thi will return a list of CVars with the following changes over the standard accessor:
 
 264 The list will be returned in the sorted order of the configs.
 
 268 For every config exactly one CVar will be returned.
 
 272 If no cvar was found for a config, a new one will be vivified, set to the
 
 273 correct config, module etc, and registered into the object.
 
 277 Vivified cvars for secondary classes will first try to find their base object
 
 278 and use that value. If no such value or cvar is found the default value from
 
 283 This is useful if you need to list every possible CVar, like in CRUD masks.
 
 285 =item C<cvar_by_name NAME [ VALUE ]>
 
 287 Returns the CVar object for this object which matches the given internal name.
 
 288 Useful for print templates. If the requested cvar is not present, it will be
 
 289 vivified with the same rules as in C<cvars_by_config>.
 
 295 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>