Revision 37ff0a6b
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
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 |
Auch abrufbar als: Unified diff
CustomVariables: Verwendung mit RDBO als Writer implementiert