From 37ff0a6b1b9bab9d12a6e92373764d0ff6b4de03 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 14 May 2014 15:53:12 +0200 Subject: [PATCH] CustomVariables: Verwendung mit RDBO als Writer implementiert --- SL/DB/CustomVariable.pm | 45 ++++++++++++-- SL/DB/Helper/CustomVariables.pm | 59 +++++++++++++++++++ .../webpages/common/render_cvar_input.html | 2 +- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/SL/DB/CustomVariable.pm b/SL/DB/CustomVariable.pm index dd32ef9cb..3adbab2a9 100644 --- a/SL/DB/CustomVariable.pm +++ b/SL/DB/CustomVariable.pm @@ -11,18 +11,53 @@ __PACKAGE__->meta->initialize; # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. __PACKAGE__->meta->make_manager_class; +sub unparsed_value { + my ($self, $new) = @_; + + $self->{__unparsed_value} = $new; +} + +sub _ensure_config { + my ($self) = @_; + + return $self->config if $self->config; + return undef if !defined $self->config_id; + $self->config( SL::DB::CustomVariableConfig->new(id => $self->config_id)->load ); +} + +sub parse_value { + my ($self) = @_; + my $type = $self->_ensure_config->type; + + return unless exists $self->{__unparsed_value}; + + my $unparsed = delete $self->{__unparsed_value}; + + if ($type =~ m{^(?:customer|vendor|part|bool|number)}) { + return $self->number_value(defined($unparsed) ? $unparsed * 1 : undef); + } + + if ($type =~ m{^(?:date|timestamp)}) { + return $self->timestamp_value(defined($unparsed) ? DateTime->from_kivi($unparsed) : undef); + } + + # text, textfield, select + $self->text_value($unparsed); +} + sub value { my $self = $_[0]; - my $type = $self->config->type; + my $type = $self->_ensure_config->type; + + if (scalar(@_) > 1) { + $self->unparsed_value($_[1]); + $self->parse_value; + } goto &bool_value if $type eq 'bool'; goto ×tamp_value if $type eq 'timestamp'; goto &number_value if $type eq 'number'; - if ( $_[1] && ($type eq 'customer' || $type eq 'vendor' || $type eq 'part') ) { - $self->number_value($_[1]); - } - if ( $type eq 'customer' ) { require SL::DB::Customer; diff --git a/SL/DB/Helper/CustomVariables.pm b/SL/DB/Helper/CustomVariables.pm index 6f470357d..737c163a9 100644 --- a/SL/DB/Helper/CustomVariables.pm +++ b/SL/DB/Helper/CustomVariables.pm @@ -27,6 +27,7 @@ sub import { make_cvar_by_configs($caller_package, %params); make_cvar_by_name($caller_package, %params); make_cvar_as_hashref($caller_package, %params); + make_cvar_value_parser($caller_package, %params); } sub save_meta_info { @@ -144,6 +145,20 @@ sub make_cvar_as_hashref { } } +sub make_cvar_value_parser { + my ($caller_package) = @_; + no strict 'refs'; + *{ $caller_package . '::parse_custom_variable_values' } = sub { + my ($self) = @_; + + $_->parse_value for @{ $self->custom_variables || [] }; + + return $self; + }; + + $caller_package->before_save('parse_custom_variable_values'); +} + sub _all_configs { my (%params) = @_; @@ -310,6 +325,50 @@ Returns the CVar object for this object which matches the given internal name. Useful for print templates. If the requested cvar is not present, it will be vivified with the same rules as in C. +=item C + +When you want to edit custom variables in a form then you have +unparsed values from the user. These should be written to the +variable's C field. + +This function then processes all variables and parses their +C field into the proper field. It returns C<$self> for +easy chaining. + +This is automatically called in a C hook so you don't +have to do it manually if you save directly after assigning the +values. + +In an HTML form you could e.g. use something like the following: + + [%- FOREACH var = SELF.project.cvars_by_config.as_list %] + [% HTML.escape(var.config.description) %]: + [% L.hidden_tag('project.custom_variables[+].config_id', var.config.id) %] + [% PROCESS 'common/render_cvar_input.html' var_name='project.custom_variables[].unparsed_value' %] + [%- END %] + +Later in the controller when you want to save this project you don't +have to do anything special: + + my $project = SL::DB::Project->new; + my $params = $::form->{project} || {}; + + $project->assign_attributes(%{ $params }); + + $project->parse_custom_variable_values->save; + +However, if you need access to a variable's value before saving in +some way then you have to call this function manually. For example: + + my $project = SL::DB::Project->new; + my $params = $::form->{project} || {}; + + $project->assign_attributes(%{ $params }); + + $project->parse_custom_variable_values; + + print STDERR "CVar[0] value: " . $project->custom_variables->[0]->value . "\n"; + =back =head1 AUTHOR diff --git a/templates/webpages/common/render_cvar_input.html b/templates/webpages/common/render_cvar_input.html index c1b77203d..836db1df7 100644 --- a/templates/webpages/common/render_cvar_input.html +++ b/templates/webpages/common/render_cvar_input.html @@ -3,7 +3,7 @@ [%- USE L %] [%- USE LxERP %] -[%- SET var_name = HTML.escape(cvar_name_prefix) _ HTML.escape(var.config.name) _ HTML.escape(cvar_name_postfix) %] +[%- DEFAULT var_name = HTML.escape(cvar_name_prefix) _ HTML.escape(var.config.name) _ HTML.escape(cvar_name_postfix) %] [%- IF ( hide_non_editable && !var.config.is_flag('editable') ) %] [% L.hidden_tag(var_name, var.value) %] -- 2.20.1