Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 37ff0a6b

Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt

  • ID 37ff0a6b1b9bab9d12a6e92373764d0ff6b4de03
  • Vorgänger 8cc5dbe1
  • Nachfolger 1e6fdf48

CustomVariables: Verwendung mit RDBO als Writer implementiert

Unterschiede anzeigen:

SL/DB/CustomVariable.pm
11 11
# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
12 12
__PACKAGE__->meta->make_manager_class;
13 13

  
14
sub unparsed_value {
15
  my ($self, $new) = @_;
16

  
17
  $self->{__unparsed_value} = $new;
18
}
19

  
20
sub _ensure_config {
21
  my ($self) = @_;
22

  
23
  return $self->config if  $self->config;
24
  return undef         if !defined $self->config_id;
25
  $self->config( SL::DB::CustomVariableConfig->new(id => $self->config_id)->load );
26
}
27

  
28
sub parse_value {
29
  my ($self) = @_;
30
  my $type   = $self->_ensure_config->type;
31

  
32
  return unless exists $self->{__unparsed_value};
33

  
34
  my $unparsed = delete $self->{__unparsed_value};
35

  
36
  if ($type =~ m{^(?:customer|vendor|part|bool|number)}) {
37
    return $self->number_value(defined($unparsed) ? $unparsed * 1 : undef);
38
  }
39

  
40
  if ($type =~ m{^(?:date|timestamp)}) {
41
    return $self->timestamp_value(defined($unparsed) ? DateTime->from_kivi($unparsed) : undef);
42
  }
43

  
44
  # text, textfield, select
45
  $self->text_value($unparsed);
46
}
47

  
14 48
sub value {
15 49
  my $self = $_[0];
16
  my $type = $self->config->type;
50
  my $type = $self->_ensure_config->type;
51

  
52
  if (scalar(@_) > 1) {
53
    $self->unparsed_value($_[1]);
54
    $self->parse_value;
55
  }
17 56

  
18 57
  goto &bool_value      if $type eq 'bool';
19 58
  goto &timestamp_value if $type eq 'timestamp';
20 59
  goto &number_value    if $type eq 'number';
21 60

  
22
  if ( $_[1] && ($type eq 'customer' || $type eq 'vendor' || $type eq 'part') ) {
23
    $self->number_value($_[1]);
24
  }
25

  
26 61
  if ( $type eq 'customer' ) {
27 62
    require SL::DB::Customer;
28 63

  
SL/DB/Helper/CustomVariables.pm
27 27
  make_cvar_by_configs($caller_package, %params);
28 28
  make_cvar_by_name($caller_package, %params);
29 29
  make_cvar_as_hashref($caller_package, %params);
30
  make_cvar_value_parser($caller_package, %params);
30 31
}
31 32

  
32 33
sub save_meta_info {
......
144 145
  }
145 146
}
146 147

  
148
sub make_cvar_value_parser {
149
  my ($caller_package) = @_;
150
  no strict 'refs';
151
  *{ $caller_package . '::parse_custom_variable_values' } =  sub {
152
    my ($self) = @_;
153

  
154
    $_->parse_value for @{ $self->custom_variables || [] };
155

  
156
    return $self;
157
  };
158

  
159
  $caller_package->before_save('parse_custom_variable_values');
160
}
161

  
147 162
sub _all_configs {
148 163
  my (%params) = @_;
149 164

  
......
310 325
Useful for print templates. If the requested cvar is not present, it will be
311 326
vivified with the same rules as in C<cvars_by_config>.
312 327

  
328
=item C<parse_custom_variable_values>
329

  
330
When you want to edit custom variables in a form then you have
331
unparsed values from the user. These should be written to the
332
variable's C<unparsed_value> field.
333

  
334
This function then processes all variables and parses their
335
C<unparsed_value> field into the proper field. It returns C<$self> for
336
easy chaining.
337

  
338
This is automatically called in a C<before_save> hook so you don't
339
have to do it manually if you save directly after assigning the
340
values.
341

  
342
In an HTML form you could e.g. use something like the following:
343

  
344
  [%- FOREACH var = SELF.project.cvars_by_config.as_list %]
345
    [% HTML.escape(var.config.description) %]:
346
    [% L.hidden_tag('project.custom_variables[+].config_id', var.config.id) %]
347
    [% PROCESS 'common/render_cvar_input.html' var_name='project.custom_variables[].unparsed_value' %]
348
  [%- END %]
349

  
350
Later in the controller when you want to save this project you don't
351
have to do anything special:
352

  
353
  my $project = SL::DB::Project->new;
354
  my $params  = $::form->{project} || {};
355

  
356
  $project->assign_attributes(%{ $params });
357

  
358
  $project->parse_custom_variable_values->save;
359

  
360
However, if you need access to a variable's value before saving in
361
some way then you have to call this function manually. For example:
362

  
363
  my $project = SL::DB::Project->new;
364
  my $params  = $::form->{project} || {};
365

  
366
  $project->assign_attributes(%{ $params });
367

  
368
  $project->parse_custom_variable_values;
369

  
370
  print STDERR "CVar[0] value: " . $project->custom_variables->[0]->value . "\n";
371

  
313 372
=back
314 373

  
315 374
=head1 AUTHOR
templates/webpages/common/render_cvar_input.html
3 3
[%- USE L %]
4 4
[%- USE LxERP %]
5 5

  
6
[%- SET var_name = HTML.escape(cvar_name_prefix) _ HTML.escape(var.config.name) _ HTML.escape(cvar_name_postfix) %]
6
[%- DEFAULT var_name = HTML.escape(cvar_name_prefix) _ HTML.escape(var.config.name) _ HTML.escape(cvar_name_postfix) %]
7 7

  
8 8
[%- IF ( hide_non_editable && !var.config.is_flag('editable') ) %]
9 9
  [% L.hidden_tag(var_name, var.value) %]

Auch abrufbar als: Unified diff