2
###################################################################################
4
# Embperl - Copyright (c) 1997-2005 Gerald Richter / ecos gmbh www.ecos.de
6
# You may distribute under the terms of either the GNU General Public
7
# License or the Artistic License, as specified in the Perl README file.
9
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15
###################################################################################
18
package Embperl::Form ;
25
use Embperl::Form::Control ;
26
use Embperl::Form::Validate ;
27
use Embperl::Form::Control::blank ;
34
our %CLEANUP = ('forms' => 0) ;
36
# ---------------------------------------------------------------------------
38
# new - create a new form
45
my ($class, $controls, $options, $id, $validate_rules, $parentid) = @_ ;
47
my $toplevel = $validate_rules?0:1 ;
51
my $self = ref $class?$class:{} ;
53
$self -> {controls} = $controls ;
55
$self -> {parentid} = $parentid ;
56
$self -> {formname} = $options -> {formname} || 'topform' ;
57
$self -> {bottom_code} = [] ;
58
$self -> {validate_rules} = [] ;
59
$self -> {toplevel} = $toplevel ;
61
bless $self, $class if (!ref $class);
66
$validate_rules = $self -> {validate_rules} = [] ;
69
$self -> new_controls ($controls, $options, undef, $id, $validate_rules, $options -> {masks}, $options -> {defaults}) ;
71
$self -> {noframe} = 1 if ($controls && @$controls > 0 &&
72
$controls -> [0] -> noframe) ;
77
my $epf = $self -> {validate} = Embperl::Form::Validate -> new ($validate_rules, $self -> {formname}) if ($self -> {validate_rules}) ;
78
$self -> add_code_at_bottom ($epf -> get_script_code) ;
84
# ---------------------------------------------------------------------------
93
delete $forms{$self->{id}} ;
96
# ---------------------------------------------------------------------------
98
# get_control_packages
100
# returns an array ref with packges where to search for controls
103
sub get_control_packages
107
return $self -> {control_packages} || ['Embperl::Form::Control'] ;
110
# ---------------------------------------------------------------------------
112
# new_controls - transform elements to control objects
119
my ($self, $controls, $options, $id, $formid, $validate_rules, $masks, $defaults) = @_ ;
122
my $packages = $self -> get_control_packages ;
124
foreach my $control (@$controls)
126
my $name = $control -> {name} ;
127
$control -> {type} =~ s/sf_select.+/select/ ;
128
$control -> {parentid} = $id if ($id) ;
129
$control -> {id} ||= "$control->{name}-$n" ;
130
$control -> {formid} = $formid ;
132
my $type = $control -> {type} ;
133
my $default = $defaults -> {$name} || $defaults -> {"*$type"} || $defaults -> {'*'};
134
my $mask = $masks -> {$name} || $masks -> {"*$type"} || $masks -> {'*'};
137
foreach (keys %$mask)
139
$control -> {$_} = $mask -> {$_} ;
144
foreach (keys %$default)
146
$control -> {$_} = $default -> {$_} if (!exists $control -> {$_}) ;
151
if (ref $control eq 'HASH')
154
my $type = $control -> {type} || ($control -> {name}?'input':'blank') ;
157
if (!defined (&{"$type\:\:new"}))
159
eval "require $type" ;
162
$type -> new ($control) ;
167
foreach my $package (@$packages)
169
my $mod = "$package\:\:$type" ;
170
if ($mod -> can('new'))
172
$mod -> new ($control) ;
179
foreach my $package (@$packages)
181
my $mod = "$package\:\:$type" ;
182
eval "require $mod" ;
184
if ($mod -> can('new'))
186
$mod -> new ($control) ;
193
die "No Module found for type = $type, searched: @$packages" if (!$ctlmod) ;
196
next if ($control -> is_disabled) ;
197
push @{$validate_rules}, $control -> get_validate_rules ;
198
if ($control -> {sublines})
201
my $name = $control -> {name} ;
202
foreach my $subcontrols (@{$control -> {sublines}})
204
next if (!$subcontrols) ;
205
$self -> new_controls ($subcontrols, $options, "$name-$i", $formid, $validate_rules, $masks, $defaults) ;
209
if ($control -> {subforms})
215
foreach my $subcontrols (@{$control -> {subforms}})
217
next if (!$subcontrols) ;
218
my $id = "$control->{name}-$i" ;
219
my $class = ref $self ;
220
my $subform = $class -> new ($subcontrols, $options, $id, $validate_rules, $self -> {id}) ;
222
push @obj, $subform ;
225
$control -> {subobjects} = \@obj ;
226
$control -> {subids} = \@ids ;
232
# ---------------------------------------------------------------------------
234
# parent_form - return parent form object if any
241
return $Embperl::Form::forms{$self -> {parentid}} ;
245
# ---------------------------------------------------------------------------
247
# add_code_at_bottom - add js code at the bottom of the page
250
sub add_code_at_bottom
253
my ($self, $code) = @_ ;
255
push @{$self->{bottom_code}}, $code ;
259
# ---------------------------------------------------------------------------
261
# layout - build the layout of the form
267
my ($self, $controls) = @_ ;
269
$controls ||= $self -> {controls} ;
277
foreach my $control (@$controls)
279
next if ($control -> is_disabled) ;
280
my $width = $control -> {width_percent} || int($max_x / ($control -> {width} || 2)) ;
281
if ($x + $width > $max_x || $control -> {newline} > 0 || (($control -> {sublines} || $control -> {subobjects}) && @$line))
285
push @$line, Embperl::Form::Control::blank -> new (
286
{width_percent => $max_x - $x }) ;
293
push @$line, $control ;
294
$control -> {width_percent} = $width ;
295
$control -> {x_percent} = $x ;
298
$max_num = $num if ($num > $max_num) ;
300
if ($control -> {subobjects} || $control -> {sublines} || $control -> {newline} < 0)
304
push @$line, Embperl::Form::Control::blank -> new (
305
{width_percent => $max_x - $x }) ;
313
if ($control -> {sublines})
315
foreach my $subcontrols (@{$control -> {sublines}})
317
next if (!$subcontrols) ;
318
my $sublines = $self -> layout ($subcontrols) ;
319
push @lines, @$sublines ;
322
if ($control -> {subobjects})
325
foreach my $subobj (@{$control -> {subobjects}})
333
push @lines, $line if (@$line);
334
$self -> {max_num} = $max_num ;
335
return $self -> {layout} = \@lines ;
339
# ---------------------------------------------------------------------------
341
# show_controls - output the form control area
347
my ($self, $data, $activeid) = @_ ;
349
my $lines = $self -> {layout} ;
353
$self -> show_controls_begin ($activeid) ;
355
foreach my $line (@$lines)
357
my $lineid = @$line && $line->[0]{parentid}?"$line->[0]{parentid}":'id' ;
360
$self -> show_line_begin ($lineno, "$lineid-$n{$lineid}", $activesubid);
361
foreach my $control (@$line)
363
my $newactivesubid = $control -> get_active_id ;
364
$control -> show ($data);
365
$activesubid = $newactivesubid if ($newactivesubid) ;
366
if ($control -> {subobjects})
369
$control -> show_sub_begin ;
370
foreach my $subobj (@{$control -> {subobjects}})
372
next if (!$subobj || !$subobj -> {controls} || !@{$subobj -> {controls}}) ;
373
$subobj -> show ($data, $activesubid) ;
375
$control -> show_sub_end ;
378
$self -> show_line_end ($lineno);
382
$self -> show_controls_end ;
388
# ---------------------------------------------------------------------------
390
# show - output the form
396
my ($self, $data, $activeid) = @_ ;
398
$self -> show_form_begin if ($self -> {toplevel});
399
$self -> show_controls ($data, $activeid) ;
400
$self -> show_form_end if ($self -> {toplevel});
404
# ---------------------------------------------------------------------------
406
# validate - validate the form input
416
#------------------------------------------------------------------------------------------
420
# f�gt ein tab elsement mit subforms zu einem Formular hinzu
422
# in $subform array mit hashs
423
# text => <anzeige text>
425
# fields => Felddefinitionen (alternativ zu fn)
431
my ($self, $subforms, $args) = @_ ;
438
foreach my $file (@$subforms)
440
my $fn = $file -> {fn} ;
441
my $subfields = $file -> {fields} ;
443
push @options, $file -> {text};
446
my $obj = Execute ({object => "./$fn"} ) ;
447
#$subfields = eval {$obj -> fields ($r, {%$file, %$args}) || undef};
449
push @forms, $subfields;
450
push @grids, $file -> {grid};
451
push @values, $file -> {value} ||= scalar(@forms);
455
section => 'cSectionText',
460
options => \@options,
466
#------------------------------------------------------------------------------------------
470
# adds the given controls into one line
477
my ($self, $controls, $cnt) = @_ ;
479
$cnt ||= @$controls ;
480
foreach my $control (@$controls)
482
$control -> {width} = $cnt ;
488
#------------------------------------------------------------------------------------------
492
# f�gt ein tab elsement mit subforms zu einem Formular hinzu
494
# in $subform array mit hashs
495
# text => <anzeige text>
497
# fields => Felddefinitionen (alternativ zu fn)
503
my ($self, $object_data, $subforms, $type) = @_;
505
my $name = $object_data->{name};
506
my $text = $object_data->{text};
507
my $width = $object_data->{width};
508
my $section = $object_data->{section};
516
foreach my $file (@$subforms)
518
my $fn = $file -> {fn} ;
519
my $subfields = $file -> {fields} ;
522
my $obj = Execute ({object => "./$fn"} ) ;
523
#$subfields = eval {$obj -> fields ($r,$file) || undef};
525
push @forms, $subfields || [];
526
push @values, $file->{value} || $file->{name};
527
push @options, $file -> {text} || $file->{value} || $file->{name};
530
return { section => $section , width => $width, name => $name , text => $text, type => $type || 'select',
531
values => \@values, options => \@options, sublines => \@forms,
532
class => $object_data->{class}, controlclass => $object_data->{controlclass}};
536
#------------------------------------------------------------------------------------------
538
# fields_add_checkbox_subform
540
# f�gt ein checkbox Element mit Subforms hinzu
542
# in $subform array mit hashs
543
# text => <anzeige text>
544
# name => <name des Attributes>
545
# value => <Wert der checkbox>
547
# fields => Felddefinitionen (alternativ zu fn)
550
sub add_checkbox_subform
552
my ($self, $subform, $args) = @_ ;
555
my $name = $subform->{name};
556
my $text = $subform->{text};
557
my $value = $subform->{value} || 1 ;
559
my $width = $subform->{width};
562
if(! $subform->{nosection})
564
$section = $subform->{section};
568
$name ||= "__$value";
573
if($subfield = $subform->{fields})
577
elsif($fn = $subform->{fn})
579
my $obj = Execute ({object => "./$fn"} ) ;
580
#$subfield = [eval {$obj -> fields ($r, { %$file, %$args} ) || undef}];
584
return {type => 'checkbox' , section => $section, width => $width, name => $name, text => $text, value => $value, sublines => $subfield}
594
[$syntax EmbperlBlocks $]
596
[# ---------------------------------------------------------------------------
598
# show_form_begin - output begin of form
601
[$ sub show_form_begin ($self) $]
602
<script language="javascript">var doValidate = 1 ;</script>
603
<script src="/js/EmbperlForm.js"></script>
604
<script src="/js/TableCtrl.js"></script>
606
<form id="[+ $self->{formname} +]" name="[+ $self->{formname} +]" method="post" action="[+ $self->{actionurl}+]"
607
[$ if ($self -> {on_submit_function}) $]
608
onSubmit="s=[+ $self->{on_submit_function} +];if (s) { v=doValidate; doValidate=1; return ((!v) || epform_validate_[+ $self->{formname} +]()); } else { return false; }"
610
onSubmit="v=doValidate; doValidate=1; return ( (!v) || epform_validate_[+ $self->{formname}+]());"
615
[# ---------------------------------------------------------------------------
617
# show_form_end - output end of form
620
[$ sub show_form_end $]
624
[ ---------------------------------------------------------------------------
626
# show_controls_begin - output begin of form controls area
629
[$ sub show_controls_begin ($self, $activeid)
631
my $parent = $self -> parent_form ;
632
my $class = $parent -> {noframe}?'cTableDivU':'cTableDiv' ;
634
<div id="[+ $self->{id} +]"
635
[$if ($activeid && $self->{id} ne $activeid) $] style="display: none" [$endif$]
637
[$if (!$self -> {noframe}) $]<table class="[+ $class +]"><tr><td class="cTabTD"> [$endif$]
638
<table class="cBase cTable">
641
[# ---------------------------------------------------------------------------
643
# show_controls_end - output end of form controls area
646
[$sub show_controls_end ($self) $]
648
[$ if (!$self -> {noframe}) $]</td></tr></table> [$endif$]
651
[$ if (@{$self->{bottom_code}}) $]
652
<script language="javascript">
653
[+ do { local $escmode = 0; join ("\n", @{$self->{bottom_code}}) } +]
658
[# ---------------------------------------------------------------------------
660
# show_line_begin - output begin of line
663
[$ sub show_line_begin ($self, $lineno, $id, $activeid)
665
$id =~ /^(.+)-(\d+?)-(\d+?)$/ ;
668
$activeid =~ /^(.+)-(\d+?)$/ ;
672
my $class = $lineno == 0?'cTableRow1':'cTableRow' ;
674
<tr class="[+ $class +]"
675
[$if $id $] id="[+ $id +]"[$endif$]
676
[$if ($baseid eq $baseaid && $baseidn != $baseaidn) $] style="display: none"[$endif$]
680
[# ---------------------------------------------------------------------------
682
# show_line_end - output end of line
685
[$ sub show_line_end $]
696
Embperl::Form - Embperl Form class
704
=head2 new ($controls, $options)
710
Array ref with controls which should be displayed
711
inside the form. Each control needs either to be a
712
hashref with all parameters for the control or
715
If hash refs are given it's necessary to specify
716
the C<type> parameter, to let Embperl::Form
717
know which control to create.
719
See Embperl::Form::Control and Embperl::Form::Control::*
720
for a list of available parameters.
724
Hash ref which can take the following parameters:
730
Will be used as name and id attribute of the form. If you have more
731
then one form on a page it's necessary to have different form names
732
to make form validation work correctly.
736
Contains a hash ref which can specify a set of masks
737
for the controls. A mask is a set of parameter which
738
overwrite the setting of a control. You can specify
739
a mask for a control name (key is name), for a control
740
type (key is *type) or for all controls (key is *).
745
'info' => { readonly => 1},
746
'*textarea' => { cols => 80 },
747
'*' => { labelclass => 'myclass', labelnowrap => 1}
750
This will force the control with the name C<info> to be readonly, it
751
will force all C<textarea> controls to have 80 columns and
752
it will force the label of all controls to have a class of myclass
753
and not to wrap the text.
757
Contains a hash ref which can specify a set of defaults
758
for the controls. You can specify
759
a default for a control name (key is name), for a control
760
type (key is *type) or for all controls (key is *).
765
'info' => { readonly => 1},
766
'*textarea' => { cols => 80 },
767
'*' => { labelclass => 'myclass', labelnowrap => 1}
770
This will make the control with the name C<info> to default to be readonly, it
771
will deafult all C<textarea> controls to have 80 columns and
772
it will set the default class for the labels of all controls to
773
myclass and not to wrap the text.
786
G. Richter (richter@dev.ecos.de)
790
perl(1), Embperl, Embperl::Form::Control
2
###################################################################################
4
# Embperl - Copyright (c) 1997-2005 Gerald Richter / ecos gmbh www.ecos.de
6
# You may distribute under the terms of either the GNU General Public
7
# License or the Artistic License, as specified in the Perl README file.
9
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15
###################################################################################
18
package Embperl::Form ;
25
use Embperl::Form::Control ;
26
use Embperl::Form::Validate ;
27
use Embperl::Form::Control::blank ;
34
our %CLEANUP = ('forms' => 0) ;
36
# ---------------------------------------------------------------------------
38
# new - create a new form
45
my ($class, $controls, $options, $id, $validate_rules, $parentptr) = @_ ;
47
my $toplevel = $validate_rules?0:1 ;
51
my $self = ref $class?$class:{} ;
53
$self -> {controls} = $controls ;
54
$self -> {options} = $options ;
56
$self -> {parentptr} = $parentptr ;
57
$self -> {formname} = $options -> {formname} || 'topform' ;
58
$self -> {bottom_code} = [] ;
59
$self -> {validate_rules} = [] ;
60
$self -> {toplevel} = $toplevel ;
61
$self -> {valign} = $options -> {valign} || 'top' ;
62
$self -> {jsnamespace} = $options -> {jsnamespace} || '' ;
63
$self -> {jsnamespace} .= '.' if ($self -> {jsnamespace}) ;
65
bless $self, $class if (!ref $class);
67
$Embperl::FormData::forms{"$self"} = $self ;
70
$validate_rules = $self -> {validate_rules} = [] ;
75
$self -> {fields2empty} = [] ;
76
$self -> {init_data} = [] ;
77
$self -> {prepare_fdat} = [] ;
81
$self -> {fields2empty} = $self -> parent_form -> {fields2empty} ;
82
$self -> {init_data} = $self -> parent_form -> {init_data} ;
83
$self -> {prepare_fdat} = $self -> parent_form -> {prepare_fdat} ;
86
$self -> new_controls ($controls, $options, undef, $id, $validate_rules, $options -> {masks}, $options -> {defaults}) ;
88
$self -> {noframe} = 1 if ($controls && @$controls > 0 &&
89
$controls -> [0] -> noframe) ;
94
my $epf = $self -> {validate} = Embperl::Form::Validate -> new ($validate_rules, $self -> {formname}) if ($self -> {validate_rules}) ;
95
$self -> add_code_at_bottom ($epf -> get_script_code) ;
101
# ---------------------------------------------------------------------------
110
delete $Embperl::FormData::forms{"$self"} ;
113
# ---------------------------------------------------------------------------
115
# get_control_packages
117
# returns an array ref with packges where to search for control classes
120
sub get_control_packages
124
return $self -> {control_packages} || ['Embperl::Form::Control'] ;
127
# ---------------------------------------------------------------------------
129
# get_datasrc_packages
131
# returns an array ref with packges where to search for data source classes
134
sub get_datasrc_packages
138
return $self -> {datasrc_packages} || ['Embperl::Form::DataSource'] ;
141
# ---------------------------------------------------------------------------
143
# new_object - load a control or datasrc class and create a new object of
146
# in $packages arrayref of packages to search the class
147
# $name name of the class. Either a full package name or
148
# only the last part of the package. In the later
149
# @$packages are searched for this class
150
# ret reference to the object
156
my ($self, $packages, $name, $args) = @_ ;
165
if (!defined (&{"$name\:\:new"}))
167
eval "require $name" ;
168
warn $@ if ($@ && ($@ !~ /Can\'t locate/)) ;
170
$obj = $name -> new ($args) ;
175
foreach my $package (@$packages)
177
my $mod = "$package\:\:$name" ;
178
if ($mod -> can('new'))
180
$obj = $mod -> new ($args) ;
187
foreach my $package (@$packages)
189
my $mod = "$package\:\:$name" ;
190
eval "require $mod" ;
191
warn $@ if ($@ && ($@ !~ /Can\'t locate/)) ;
192
if ($mod -> can('new'))
194
$obj = $mod -> new ($args) ;
201
die "No Module found for type = $name, searched: @$packages" if (!$ctlmod || !$obj) ;
207
# ---------------------------------------------------------------------------
209
# new_controls - transform elements to control objects
216
my ($self, $controls, $options, $id, $formid, $validate_rules, $masks, $defaults) = @_ ;
219
my $packages = $self -> get_control_packages ;
221
foreach my $control (@$controls)
223
die "control definition must be a hashref or an object, is '$control' " if (!ref $control || ref $control eq 'ARRAY');
225
my $name = $control -> {name} ;
226
$control -> {type} =~ s/sf_select.+/select/ ;
227
$control -> {parentid} = $id if ($id) ;
228
$control -> {id} ||= "$control->{name}-$n" ;
229
$control -> {formid} = $formid ;
230
$control -> {formptr} = "$self" ;
232
my $type = $control -> {type} ;
233
my $default = $defaults -> {$name} || $defaults -> {"*$type"} || $defaults -> {'*'};
234
my $mask = $masks -> {$name} || $masks -> {"*$type"} || $masks -> {'*'};
237
foreach (keys %$mask)
239
$control -> {$_} = $mask -> {$_} ;
244
foreach (keys %$default)
246
$control -> {$_} = $default -> {$_} if (!exists $control -> {$_}) ;
251
if (ref $control eq 'HASH')
253
my $type = $control -> {type} || ($control -> {name}?'input':'blank') ;
254
$control = $self -> new_object ($packages, $type, $control) ;
255
push @{$self -> {init_data}}, $control if ($control -> can ('init_data')) ;
256
push @{$self -> {prepare_fdat}}, $control if ($control -> can ('prepare_fdat')) ;
260
next if ($control -> is_disabled) ;
261
push @{$validate_rules}, $control -> get_validate_rules ;
262
if ($control -> {sublines})
265
my $name = $control -> {name} ;
266
foreach my $subcontrols (@{$control -> {sublines}})
268
next if (!$subcontrols) ;
269
$self -> new_controls ($subcontrols, $options, "$name-$i", $formid, $validate_rules, $masks, $defaults) ;
273
if ($control -> {subforms})
279
foreach my $subcontrols (@{$control -> {subforms}})
281
next if (!$subcontrols) ;
282
my $id = "$control->{name}-$i" ;
283
my $class = ref $self ;
284
my $subform = $class -> new ($subcontrols, $options, $id, $validate_rules, "$self") ;
286
push @obj, $subform ;
289
$control -> {subobjects} = \@obj ;
290
$control -> {subids} = \@ids ;
296
# ---------------------------------------------------------------------------
298
# parent_form - return parent form object if any
305
return $Embperl::FormData::forms{$self -> {parentptr}} ;
309
# ---------------------------------------------------------------------------
311
# add_code_at_bottom - add js code at the bottom of the page
314
sub add_code_at_bottom
317
my ($self, $code) = @_ ;
319
push @{$self->{bottom_code}}, $code ;
323
# ---------------------------------------------------------------------------
325
# layout - build the layout of the form
331
my ($self, $controls, $level) = @_ ;
333
$controls ||= $self -> {controls} ;
342
foreach my $control (@$controls)
344
next if ($control -> is_disabled) ;
345
my $width = $control -> {width_percent} || int($max_x / ($control -> {width} || 2)) ;
346
if ($x + $width > $max_x || $control -> {newline} > 0 || (($control -> {sublines} || $control -> {subobjects}) && @$line))
350
push @$line, Embperl::Form::Control::blank -> new (
351
{width_percent => $max_x - $x, level => $level }) ;
358
push @$line, $control ;
359
$control -> {width_percent} = $width ;
360
$control -> {x_percent} = $x ;
361
$control -> {level} = $level ;
364
$max_num = $num if ($num > $max_num) ;
366
if ($control -> {subobjects} || $control -> {sublines} || $control -> {newline} < 0)
370
push @$line, Embperl::Form::Control::blank -> new (
371
{width_percent => $max_x - $x }) ;
379
if ($control -> {sublines})
381
foreach my $subcontrols (@{$control -> {sublines}})
383
next if (!$subcontrols) ;
384
my $sublines = $self -> layout ($subcontrols, $level + 1) ;
385
push @lines, @$sublines ;
388
if ($control -> {subobjects})
391
foreach my $subobj (@{$control -> {subobjects}})
399
push @lines, $line if (@$line);
400
$self -> {max_num} = $max_num ;
401
return $self -> {layout} = \@lines ;
405
# ---------------------------------------------------------------------------
407
# show_controls - output the form control area
413
my ($self, $req, $activeid) = @_ ;
415
my $lines = $self -> {layout} ;
420
$self -> show_controls_begin ($req, $activeid) ;
422
foreach my $line (@$lines)
424
my $linelevel = @$line?$line->[0]{level}:0 ;
425
my $lineid = @$line && $line->[0]{parentid}?"$line->[0]{parentid}":'id' ;
428
my $visible = $self -> show_line_begin ($req, $lineno, "$lineid-$n{$lineid}", $activesubid[$linelevel-1] || $activeid);
429
foreach my $control (@$line)
431
my $newactivesubid = $visible?$control -> get_active_id ($req):'-' ;
432
$control -> show ($req);
433
$activesubid[$control -> {level}] = $newactivesubid if ($newactivesubid) ;
434
if ($control -> {subobjects})
437
$control -> show_sub_begin ($req) ;
438
foreach my $subobj (@{$control -> {subobjects}})
440
next if (!$subobj || !$subobj -> {controls} || !@{$subobj -> {controls}}) ;
441
$subobj -> show ($req, $activesubid[$control -> {level}]) ;
443
$control -> show_sub_end ($req) ;
446
$self -> show_line_end ($req, $lineno);
450
$self -> show_controls_end ($req) ;
456
# ---------------------------------------------------------------------------
458
# show - output the form
464
my ($self, $req, $activeid) = @_ ;
466
$self -> init_data ($req) if ($self -> {toplevel});
467
#$self -> validate ($req) if ($self -> {toplevel});
468
$self -> show_form_begin ($req) if ($self -> {toplevel});
469
$self -> show_controls ($req, $activeid) ;
470
$self -> show_form_end ($req) if ($self -> {toplevel});
474
# ---------------------------------------------------------------------------
476
# init_data - init fdat before showing
482
my ($self, $req) = @_ ;
484
foreach my $control (@{$self -> {init_data}})
486
$control -> init_data ($req) ;
490
# ---------------------------------------------------------------------------
492
# prepare_fdat - change fdat after submit
498
my ($self, $req) = @_ ;
499
warn "embperl::form::prepare_fdat c=@{$self->{prepare_fdat}}" ;
500
foreach my $control (@{$self -> {prepare_fdat}})
502
$control -> prepare_fdat ($req) ;
506
# ---------------------------------------------------------------------------
508
# validate - validate the form input
518
#------------------------------------------------------------------------------------------
522
# f�gt ein tab elsement mit subforms zu einem Formular hinzu
524
# in $subform array mit hashs
525
# text => <anzeige text>
527
# fields => Felddefinitionen (alternativ zu fn)
533
my ($self, $subforms, $args) = @_ ;
540
foreach my $file (@$subforms)
542
my $fn = $file -> {fn} ;
543
my $subfields = $file -> {fields} ;
545
push @options, $file -> {text};
548
my $obj = Execute ({object => "./$fn"} ) ;
549
#$subfields = eval {$obj -> fields ($r, {%$file, %$args}) || undef};
551
push @forms, $subfields;
552
push @grids, $file -> {grid};
553
push @values, $file -> {value} ||= scalar(@forms);
557
section => 'cSectionText',
562
options => \@options,
568
#------------------------------------------------------------------------------------------
572
# adds the given controls into one line
579
my ($self, $controls, $cnt) = @_ ;
581
$cnt ||= @$controls ;
582
foreach my $control (@$controls)
584
$control -> {width} = $cnt ;
590
#------------------------------------------------------------------------------------------
594
# f�gt ein tab elsement mit subforms zu einem Formular hinzu
596
# in $subform array mit hashs
597
# text => <anzeige text>
599
# fields => Felddefinitionen (alternativ zu fn)
605
my ($self, $object_data, $subforms, $type) = @_;
607
my $name = $object_data->{name};
608
my $text = $object_data->{text};
609
my $width = $object_data->{width};
610
my $section = $object_data->{section};
618
foreach my $file (@$subforms)
620
my $fn = $file -> {fn} ;
621
my $subfields = $file -> {fields} ;
624
my $obj = Execute ({object => "./$fn"} ) ;
625
#$subfields = eval {$obj -> fields ($r,$file) || undef};
627
push @forms, $subfields || [];
628
push @values, $file->{value} || $file->{name};
629
push @options, $file -> {text} || $file->{value} || $file->{name};
632
return { section => $section , width => $width, name => $name , text => $text, type => $type || 'select',
633
values => \@values, options => \@options, sublines => \@forms,
634
class => $object_data->{class}, controlclass => $object_data->{controlclass}};
638
#------------------------------------------------------------------------------------------
640
# fields_add_checkbox_subform
642
# f�gt ein checkbox Element mit Subforms hinzu
644
# in $subform array mit hashs
645
# text => <anzeige text>
646
# name => <name des Attributes>
647
# value => <Wert der checkbox>
649
# fields => Felddefinitionen (alternativ zu fn)
652
sub add_checkbox_subform
654
my ($self, $subform, $args) = @_ ;
657
my $name = $subform->{name};
658
my $text = $subform->{text};
659
my $value = $subform->{value} || 1 ;
661
my $width = $subform->{width};
664
if(! $subform->{nosection})
666
$section = $subform->{section};
670
$name ||= "__$value";
675
if($subfield = $subform->{fields})
679
elsif($fn = $subform->{fn})
681
my $obj = Execute ({object => "./$fn"} ) ;
682
#$subfield = [eval {$obj -> fields ($r, { %$file, %$args} ) || undef}];
686
return {type => 'checkbox' , section => $section, width => $width, name => $name, text => $text, value => $value, sublines => $subfield}
696
[$syntax EmbperlBlocks $]
698
[# ---------------------------------------------------------------------------
700
# show_form_begin - output begin of form
703
[$ sub show_form_begin ($self, $req) $]
704
<script language="javascript">var doValidate = 1 ;</script>
705
<script src="/js/EmbperlForm.js"></script>
706
<script src="/js/TableCtrl.js"></script>
708
<form id="[+ $self->{formname} +]" name="[+ $self->{formname} +]" method="post" action="[+ $self->{actionurl}+]"
709
[$ if ($self -> {on_submit_function}) $]
710
onSubmit="s=[+ $self->{on_submit_function} +];if (s) { v=doValidate; doValidate=1; return ((!v) || epform_validate_[+ $self->{formname} +]()); } else { return false; }"
712
onSubmit="v=doValidate; doValidate=1; return ( (!v) || epform_validate_[+ $self->{formname}+]());"
717
[# ---------------------------------------------------------------------------
719
# show_form_end - output end of form
722
[$ sub show_form_end ($req) $]
726
[ ---------------------------------------------------------------------------
728
# show_controls_begin - output begin of form controls area
731
[$ sub show_controls_begin ($self, $req, $activeid)
733
my $parent = $self -> parent_form ;
734
my $class = $parent -> {noframe}?'cTableDivU':'cTableDiv' ;
736
<div id="[+ $self->{id} +]"
737
[$if ($activeid && $self->{id} ne $activeid) $] style="display: none" [$endif$]
739
[$if (!$self -> {noframe}) $]<table class="[+ $class +]"><tr><td class="cTabTD"> [$endif$]
740
<table class="cBase cTable">
743
[# ---------------------------------------------------------------------------
745
# show_controls_end - output end of form controls area
748
[$sub show_controls_end ($self, $req) $]
750
[$ if (!$self -> {noframe}) $]</td></tr></table> [$endif$]
753
[$ if (@{$self->{bottom_code}}) $]
754
<script language="javascript">
755
[+ do { local $escmode = 0; join ("\n", @{$self->{bottom_code}}) } +]
758
[$ if ($self -> {toplevel} && @{$self -> {fields2empty}}) $]
759
<input type="hidden" name="-fields2empty" value="[+ join (' ', @{$self -> {fields2empty}}) +]">
764
[# ---------------------------------------------------------------------------
766
# show_line_begin - output begin of line
769
[$ sub show_line_begin ($self, $req, $lineno, $id, $activeid)
771
$id =~ /^(.+)-(\d+?)-(\d+?)$/ ;
774
$activeid =~ /^(.+)-(\d+?)$/ ;
778
my $class = $lineno == 0?'cTableRow1':'cTableRow' ;
780
<tr class="[+ $class +]" valign="[+ $self->{valign} +]"
781
[$if $id $] id="[+ $id +]" [$endif$]
782
[$if ($activeid eq '-' || ($baseid eq $baseaid && $baseidn != $baseaidn)) $] style="display: none" [$endif$]
784
[* return !($activeid eq '-' || ($baseid eq $baseaid && $baseidn != $baseaidn)) *]
787
[# ---------------------------------------------------------------------------
789
# show_line_end - output end of line
792
[$ sub show_line_end ($req) $]
803
Embperl::Form - Embperl Form class
811
=head2 new ($controls, $options)
817
Array ref with controls which should be displayed
818
inside the form. Each control needs either to be a
819
hashref with all parameters for the control or
822
If hash refs are given it's necessary to specify
823
the C<type> parameter, to let Embperl::Form
824
know which control to create.
826
See Embperl::Form::Control and Embperl::Form::Control::*
827
for a list of available parameters.
831
Hash ref which can take the following parameters:
837
Will be used as name and id attribute of the form. If you have more
838
then one form on a page it's necessary to have different form names
839
to make form validation work correctly.
843
Contains a hash ref which can specify a set of masks
844
for the controls. A mask is a set of parameter which
845
overwrite the setting of a control. You can specify
846
a mask for a control name (key is name), for a control
847
type (key is *type) or for all controls (key is *).
852
'info' => { readonly => 1},
853
'*textarea' => { cols => 80 },
854
'*' => { labelclass => 'myclass', labelnowrap => 1}
857
This will force the control with the name C<info> to be readonly, it
858
will force all C<textarea> controls to have 80 columns and
859
it will force the label of all controls to have a class of myclass
860
and not to wrap the text.
864
Contains a hash ref which can specify a set of defaults
865
for the controls. You can specify
866
a default for a control name (key is name), for a control
867
type (key is *type) or for all controls (key is *).
872
'info' => { readonly => 1},
873
'*textarea' => { cols => 80 },
874
'*' => { labelclass => 'myclass', labelnowrap => 1}
877
This will make the control with the name C<info> to default to be readonly, it
878
will deafult all C<textarea> controls to have 80 columns and
879
it will set the default class for the labels of all controls to
880
myclass and not to wrap the text.
884
valign for control cells. Defaults to 'top' .
888
Give the JavaScript Namespace. This allows to load js Files in
889
a top frame or different frame, which will speed up page loading,
890
because the browser does not need to reload the js code on every load.
907
G. Richter (richter@dev.ecos.de)
911
perl(1), Embperl, Embperl::Form::Control