blib/lib/Embperl/Form.pm untested

filter: Statementstmt 0.0% 0 / 519 Branchbran 0.0% 0 / 174 Conditioncond 0.0% 0 / 211 MC/DCmcdc 0.0% 0 / 121 Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 1099 CC 88 SCAR 49.7
CC
88
Cov
0%
new_controls
420.0
layout
342.0
new_object
132.0
CRAP 143.5
1  
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2015 Gerald Richter
6 # Embperl - Copyright (c) 2015-2023 actevy.io
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 ###################################################################################
16  
17  
18 package Embperl::Form ;
19  
20 use strict ;
21  
22 use lib qw{..} ;
23  
24 use Embperl ;
25 use Embperl::Form::Control ;
26 use Embperl::Form::Validate ;
27 use Embperl::Form::Control::blank ;
28  
29 use Embperl::Inline ;
30  
31 use Data::Dumper ;
32 use Storable ;
33 use MIME::Base64 ;
34 use Scalar::Util qw{weaken} ;
35  
36 our %forms ;
37 our $form_cnt = 1 ;
38 our %CLEANUP = ('forms' => 0) ;
39  
40 use vars qw{$epreq} ;
41  
42 # ---------------------------------------------------------------------------
43 #
44 # sub_new - create a new sub form
45 #
46  
47  
48 sub sub_new
49  
50 {
51 my ($class, $controls, $options, $id, $toplevel, $parentptr) = @_ ;
52  
53 $id ||= 'topdiv' ;
54 $options ||= {} ;
55 $toplevel = 1 if (!defined ($toplevel)) ;
56  
57 my $self = ref $class?$class:{} ;
58  
59 $self -> {controls} = $controls ;
60 $self -> {options} = $options ;
61 $self -> {id} = $id ;
62 $self -> {parentptr} = $parentptr ;
63 $self -> {formname} = $options -> {formname} || 'topform' ;
64 $self -> {bottom_code} = [] ;
65 $self -> {validate_rules} = [] ;
66 $self -> {toplevel} = $toplevel ;
67 $self -> {checkitems} = $options -> {checkitems} ;
68 $self -> {valign} = $options -> {valign} || 'top' ;
69 $self -> {jsnamespace} = $options -> {jsnamespace} || '' ;
70 $self -> {jsnamespace} .= '.' if ($self -> {jsnamespace}) ;
71 $self -> {disable} = $options -> {disable} ;
72 $self -> {control_packages} = $options -> {control_packages} ;
73 $self -> {datasrc_packages} = $options -> {datasrc_packages} ;
74 $self -> {formptr} = ($options -> {formptr} || "$self") . '/' . $id ;
75 bless $self, $class if (!ref $class);
76  
77 # The following lines needs to there twice!
78 # some weired bug in Perl?
79 $Embperl::FormData::forms{$self -> {formptr}} = $self ;
80 weaken($Embperl::FormData::forms{$self -> {formptr}});
81 #$Embperl::FormData::forms{$self -> {formptr}} = $self ;
82  
83 if ($toplevel)
84 {
85 $self -> {fields2empty} = [] ;
86 $self -> {init_data} = [] ;
87 $self -> {init_markup} = [] ;
88 $self -> {prepare_fdat} = [] ;
89 $self -> {code_refs} = [] ;
90 $self -> {constrain_attrs} = [] ;
91 $self -> {do_validate} = [] ;
92 $self -> {all_controls} = {} ;
93 }
94 else
95 {
96 $self -> {fields2empty} = $self -> parent_form -> {fields2empty} ;
97 $self -> {init_data} = $self -> parent_form -> {init_data} ;
98 $self -> {init_markup} = $self -> parent_form -> {init_markup} ;
99 $self -> {prepare_fdat} = $self -> parent_form -> {prepare_fdat} ;
100 $self -> {constrain_attrs} = $self -> parent_form -> {constrain_attrs} ;
101 $self -> {code_refs} = $self -> parent_form -> {code_refs} ;
102 $self -> {do_validate} = $self -> parent_form -> {do_validate} ;
103 $self -> {all_controls} = $self -> parent_form -> {all_controls} ;
104 }
105 if ($self -> has_code_refs)
106 {
107 push @{$self -> {code_refs}}, $self ;
108 weaken ($self -> {code_refs}[-1]) ;
109 }
110 $self -> new_controls ($controls, $options, undef, $id, $options -> {masks}, $options -> {defaults}) ;
111  
112 $self -> {noframe} = 1 if ($controls && @$controls > 0 &&
113 $controls -> [0] -> noframe) ;
114  
115  
116 return $self ;
117 }
118  
119 # ---------------------------------------------------------------------------
120 #
121 # new - create a new form
122 #
123  
124 sub new
125 {
126 my $class = shift ;
127 return $class -> sub_new (@_) ;
128 }
129  
130 # ---------------------------------------------------------------------------
131 #
132 # clone - clone an existing form. trivial new here, maybe more complex for kids
133 # This will always return a Embperl::Form, no matter what $self is
134  
135 sub cloned_form
136 {
137 my $self = shift ;
138 return Embperl::Form -> sub_new (@_) ;
139 }
140  
141 # ---------------------------------------------------------------------------
142 #
143 # DESTROY
144 #
145  
146 sub DESTROY
147 {
148 my ($self) = @_ ;
149  
150 delete $Embperl::FormData::forms{$self -> {formptr}} ;
151 }
152  
153 # ---------------------------------------------------------------------------
154 #
155 # get_control_packages
156 #
157 # returns an array ref with packges where to search for control classes
158 #
159  
160 sub get_control_packages
161 {
162 my ($self) = @_ ;
163  
164 return $self -> {control_packages} || ['Embperl::Form::Control'] ;
165 }
166  
167 # ---------------------------------------------------------------------------
168 #
169 # get_datasrc_packages
170 #
171 # returns an array ref with packges where to search for data source classes
172 #
173  
174 sub get_datasrc_packages
175 {
176 my ($self) = @_ ;
177  
178 return $self -> {datasrc_packages} || ['Embperl::Form::DataSource'] ;
179 }
180  
181 # ---------------------------------------------------------------------------
182 #
183 # new_object - load a control or datasrc class and create a new object of
184 # this class
185 #
186 # in $packages arrayref of packages to search the class
187 # $name name of the class. Either a full package name or
188 # only the last part of the package. In the later
189 # @$packages are searched for this class
190 # ret reference to the object
191 #
192  
193 sub new_object
194  
195 {
196 my ($self, $packages, $name, $args) = @_ ;
197  
198 my $ctlmod ;
199 my $obj ;
200  
201 $args ||= {} ;
202  
203 if ($name =~ /::/)
204 {
205 if (!defined (&{"$name\:\:new"}))
206 {
207 {
208 local $SIG{__DIE__} ;
209 eval "require $name" ;
210 }
211 if ($@)
212 {
213 my $modfile = $name . '.pm' ;
214 $modfile =~ s/::/\//g ;
215 if ($@ !~ /Can\'t locate $modfile/)
216 {
217 die "require $name: $@" ;
218 }
219 }
220 }
221  
222 $obj = $name -> new ($args) ;
223 $ctlmod = $name ;
224 }
225 else
226 {
227 foreach my $package (@$packages)
228 {
229 my $mod = "$package\:\:$name" ;
230 if ($mod -> can('new'))
231 {
232 $obj = $mod -> new ($args) ;
233 $ctlmod = $mod ;
234 last ;
235 }
236 }
237 if (!$ctlmod)
238 {
239 foreach my $package (@$packages)
240 {
241 my $mod = "$package\:\:$name" ;
242 {
243 local $SIG{__DIE__} ;
244 eval "require $mod" ;
245 }
246 if ($@)
247 {
248 my $modfile = $mod . '.pm' ;
249 $modfile =~ s/::/\//g ;
250 if ($@ !~ /Can\'t locate $modfile/)
251 {
252 die "require $mod: $@" ;
253 }
254 }
255 if ($mod -> can('new'))
256 {
257 $obj = $mod -> new ($args) ;
258 $ctlmod = $mod ;
259 last ;
260 }
261 }
262 }
263 }
264 die "No Module found for type = $name, searched: @$packages" if (!$ctlmod || !$obj) ;
265  
266 return $obj ;
267 }
268  
269  
270 # ---------------------------------------------------------------------------
271 #
272 # new_controls - transform elements to control objects
273 #
274  
275  
276 sub new_controls
277  
278 {
279 my ($self, $controls, $options, $id, $formid, $masks, $defaults, $no_init) = @_ ;
280  
281 my $n = 0 ;
282 my $packages = $self -> get_control_packages ;
283  
284 foreach my $control (@$controls)
285 {
286 die "control definition must be a hashref or an object, is '$control' " if (!ref $control || ref $control eq 'ARRAY');
287  
288 my $ctlid = $control->{name} ;
289 my $q = 2 ;
290 while (exists $self -> {controlids}{$ctlid})
291 {
292 $ctlid = $control->{name} . '_' . $q ;
293 $q++ ;
294 }
295  
296 my $name = $control -> {name} ;
297 $control -> {type} =~ s/sf_select.+/select/ ;
298 $control -> {type} ||= ($control -> {name}?'input':'blank') ;
299 $control -> {parentid} = $id if ($id) ;
300 $control -> {id} ||= $ctlid ;
301 $control -> {basename}||= $control->{name} ;
302 $control -> {formid} = $formid ;
303 $control -> {formptr} = $self -> {formptr} ;
304  
305 my $type = $control -> {type} ;
306 my $default = $defaults -> {$name} || $defaults -> {"*$type"} || $defaults -> {'*'};
307 my $mask = $masks -> {$name} || $masks -> {"*$type"} || $masks -> {'*'};
308  
309 if ($mask)
310 {
311 foreach (keys %$mask)
312 {
313 $control -> {$_} = $mask -> {$_} ;
314 }
315 }
316 if ($default)
317 {
318 foreach (keys %$default)
319 {
320 $control -> {$_} = $default -> {$_} if (!exists $control -> {$_}) ;
321 }
322 }
323  
324 if (ref $control eq 'HASH')
325 {
326 my $type = $control -> {type} ;
327 $control = $self -> new_object ($packages, $type, $control) ;
328 if (!$no_init)
329 {
330 if ($control -> can ('init_data'))
331 {
332 push @{$self -> {init_data}}, $control ;
333 weaken ($self -> {init_data}[-1]) ;
334 }
335 if ($control -> can ('init_markup'))
336 {
337 push @{$self -> {init_markup}}, $control ;
338 weaken ($self -> {init_markup}[-1]) ;
339 }
340 if ($control -> can ('prepare_fdat'))
341 {
342 push @{$self -> {prepare_fdat}}, $control ;
343 weaken ($self -> {prepare_fdat}[-1]) ;
344 }
345 if ($control -> has_code_refs)
346 {
347 push @{$self -> {code_refs}}, $control ;
348 weaken ($self -> {code_refs}[-1]) ;
349 }
350 if ($control -> has_validate_rules)
351 {
352 push @{$self -> {do_validate}}, $control ;
353 weaken ($self -> {do_validate}[-1]) ;
354 }
355 push @{$self -> {constrain_attrs}}, $control -> constrain_attrs ;
356 $self -> {all_controls}{$name} = $control ;
357 weaken ($self -> {all_controls}{$name}) ;
358 }
359 }
360 $self -> {controlids}{$control->{id}} = $control ;
361  
362 next if ($control -> is_disabled ()) ;
363 if ($control -> {sublines})
364 {
365 my $i = 0 ;
366 my $name = $control -> {name} ;
367 foreach my $subcontrols (@{$control -> {sublines}})
368 {
369 next if (!$subcontrols) ;
370 $self -> new_controls ($subcontrols, $options, "$name-$i", $formid, $masks, $defaults, $no_init) ;
371 $i++ ;
372 }
373 }
374 if ($control -> {subforms})
375 {
376 my @obj ;
377 my @ids ;
378 my $i = 0 ;
379  
380 foreach my $subcontrols (@{$control -> {subforms}})
381 {
382 next if (!$subcontrols) ;
383 my $ctlid = $control -> {values}[$i] || $control->{name} ;
384 my $q = 2 ;
385 while (exists $self -> {controlids}{$ctlid})
386 {
387 $ctlid = $control->{name} . '_' . $q ;
388 $q++ ;
389 }
390 my $class = ref $self ;
391 local $options -> {disable} = $control -> {disables}[$i] ;
392 my $subform = $class -> sub_new ($subcontrols, $options, $ctlid, 0, $self -> {formptr}) ;
393 $subform -> {text} ||= $control -> {options}[$i] if (exists ($control -> {options}) && $control -> {options}[$i]) ;
394 $subform -> {parent_control} = $control ;
395 weaken ($subform -> {parent_control}) ;
396 push @ids, $ctlid ;
397 push @obj, $subform ;
398 $i++ ;
399 }
400 $control -> {subobjects} = \@obj ;
401 $control -> {subids} = \@ids ;
402 }
403 $n++ ;
404 }
405 }
406  
407 # ---------------------------------------------------------------------------
408 #
409 # parent_form - return parent form object if any
410 #
411  
412 sub parent_form
413 {
414 my ($self) = @_ ;
415  
416 return $Embperl::FormData::forms{$self -> {parentptr}} ;
417 }
418  
419  
420  
421 # ---------------------------------------------------------------------------
422 #
423 # add_code_at_bottom - add js code at the bottom of the page
424 #
425  
426 sub add_code_at_bottom
427  
428 {
429 my ($self, $code) = @_ ;
430  
431 push @{$self->{bottom_code}}, $code ;
432 }
433  
434  
435 # ---------------------------------------------------------------------------
436 #
437 # layout - build the layout of the form
438 #
439  
440 sub layout
441  
442 {
443 my ($self, $controls, $level) = @_ ;
444  
445 $controls ||= $self -> {controls} ;
446 $level ||= 1 ;
447  
448 my $hidden = $self -> {hidden} ||= [] ;
449  
450 my $x = 0 ;
451 my $max_x = 100 ;
452 my $line = [] ;
453 my @lines ;
454 my $max_num = 0 ;
455 my $num = 0 ;
456 my $last_state ;
457 foreach my $control (@$controls)
458 {
459 next if ($control -> is_disabled ()) ;
460 if ($control -> is_hidden)
461 {
462 $control -> {width_percent} = 0 ;
463 push @$hidden, $control ;
464 next ;
465 }
466 my $width = ($control -> {width} eq 'expand')?100:$control -> {width_percent} || int($max_x / ($control -> {width} || 2)) ;
467 #$width = 21 if ($x == 0 && $width < 21) ;
468 if ($x + $width > $max_x || $control -> {newline} > 0 || (($control -> {sublines} || $control -> {subobjects}) && @$line))
469 { # new line
470 if ($x < $max_x)
471 {
472 push @$line, Embperl::Form::Control::blank -> new (
473 {width_percent => int($max_x - $x), level => $level, x_percent => int($x), state => $last_state }) ;
474 }
475 push @lines, $line ;
476 $line = [] ;
477 $x = 0 ;
478 $num = 0 ;
479 }
480 push @$line, $control ;
481 $last_state = $control -> {state} ;
482 $control -> {width_percent} = $control -> {width} eq 'expand'?'expand':int($width) ;
483 $control -> {x_percent} = int($x) ;
484 $control -> {level} = $level ;
485 $x += $width ;
486 $num++ ;
487 $max_num = $num if ($num > $max_num) ;
488 if ($control -> {subobjects} || $control -> {sublines} || $control -> {newline} < 0)
489 { # new line
490 if ($x < $max_x)
491 {
492 push @$line, Embperl::Form::Control::blank -> new (
493 {width_percent => int($max_x - $x), level => $level, x_percent => int($x), state => $last_state }) ;
494 $num++ ;
495 $max_num = $num if ($num > $max_num) ;
496 }
497 push @lines, $line ;
498 $line = [] ;
499 $x = 0 ;
500 $num = 0 ;
501 }
502  
503 if ($control -> {sublines})
504 {
505 foreach my $subcontrols (@{$control -> {sublines}})
506 {
507 next if (!$subcontrols) ;
508 my $sublines = $self -> layout ($subcontrols, $level + 1) ;
509 push @lines, @$sublines ;
510 }
511 }
512 if ($control -> {subobjects})
513 {
514 my @obj ;
515 foreach my $subobj (@{$control -> {subobjects}})
516 {
517 next if (!$subobj) ;
518 $subobj -> layout ;
519 push @$hidden, @{$subobj -> {hidden}} ;
520 delete $subobj -> {hidden} ;
521 }
522 }
523 }
524  
525 if ($x > 0 && $x < $max_x)
526 {
527 push @$line, Embperl::Form::Control::blank -> new (
528 {width_percent => int($max_x - $x), level => $level, x_percent => int($x), state => $last_state }) ;
529 $num++ ;
530 $max_num = $num if ($num > $max_num) ;
531 }
532 push @lines, $line if (@$line);
533 $self -> {max_num} = $max_num ;
534 return $self -> {layout} = \@lines ;
535 }
536  
537  
538 # ---------------------------------------------------------------------------
539 #
540 # show_controls - output the form control area
541 #
542  
543 sub show_controls
544  
545 {
546 my ($self, $req, $activeid, $options) = @_ ;
547  
548 if ($self -> {toplevel})
549 {
550 $req -> {form_options_masks} = ($options && $options -> {masks}) || {} ;
551 $req -> {uuid} ||= $form_cnt++ ;
552 @{$self -> {fields2empty}} = () ;
553 }
554 my $lines = $self -> {layout} ;
555 my %n ;
556 my $activesubid ;
557 my @activesubid ;
558  
559 $self -> show_controls_begin ($req, $activeid) ;
560 my $lineno = 0 ;
561 foreach my $line (@$lines)
562 {
563 my $linelevel = @$line?$line->[0]{level}:0 ;
564 my $lineid = @$line && $line->[0]{parentid}?"$line->[0]{parentid}":'id' ;
565 $n{$lineid} ||= 10 ;
566 my $visible = $self -> show_line_begin ($req, $lineno, "$lineid-$n{$lineid}", $activesubid[$linelevel-1] || $activeid);
567 foreach my $control (@$line)
568 {
569 # my $newactivesubid = $control -> {subobjects} && $visible?$control -> get_active_id ($req):'-' ;
570 my $newactivesubid = ($control -> {subobjects} || $control -> {sublines}) && $visible?$control -> get_active_id ($req):'' ;
571 $control -> show ($req) if (!$control -> is_disabled ($req)) ;
572 $activesubid[$control -> {level}] = $newactivesubid if ($newactivesubid) ;
573 if ($control -> {subobjects})
574 {
575 my @obj ;
576 $control -> show_sub_begin ($req) ;
577 foreach my $subobj (@{$control -> {subobjects}})
578 {
579  
580 next if (!$subobj || !$subobj -> {controls} || !@{$subobj -> {controls}} || $subobj -> is_disabled ($req)) ;
581  
582 $subobj -> show ($req, $activesubid[$control -> {level}]) ;
583 }
584 $control -> show_sub_end ($req) ;
585 }
586 }
587 $self -> show_line_end ($req, $lineno);
588 $lineno++ ;
589 $n{$lineid}++ ;
590 }
591 $self -> show_controls_end ($req) ;
592 $self -> show_controls_hidden ($req) if ($self -> {hidden}) ;
593 $self -> show_checkitems ($req) if ($self -> {checkitems} && $self -> {toplevel}) ;
594  
595 return ;
596 }
597  
598 # ---------------------------------------------------------------------------
599 #
600 # init_validate - init validate functions
601 #
602  
603 sub init_validate
604  
605 {
606 my ($self, $req, $options) = @_ ;
607  
608 if ($self -> {toplevel})
609 {
610 my $epf = $self -> {validate} ;
611 if (!defined ($epf))
612 {
613 my @validate_rules ;
614 foreach my $control (@{$self -> {do_validate}})
615 {
616 push @validate_rules, $control -> get_validate_rules ($req) ;
617 }
618 if (@validate_rules)
619 {
620 $epf = $self -> {validate} = Embperl::Form::Validate -> new (\@validate_rules, $self -> {formname}, $options -> {language}, $options -> {charset}) ;
621 $self -> add_code_at_bottom ($epf -> get_script_code) ;
622 }
623 else
624 {
625 $self -> add_code_at_bottom (" function epform_validate_$self->{formname} () { return false } ") ;
626 $self -> {validate} = 0 ;
627 }
628 }
629 }
630  
631 return $self -> {validate}?1:0 ;
632 }
633  
634 # ---------------------------------------------------------------------------
635 #
636 # show - output the form
637 #
638  
639 sub show
640  
641 {
642 my ($self, $req, $activeid, $options) = @_ ;
643  
644 if ($self -> {toplevel})
645 {
646 $self -> init_validate ($req, $options) ;
647 $self -> init_data ($req) ;
648 $self -> show_form_begin ($req) ;
649 }
650  
651 #$self -> validate ($req) if ($self -> {toplevel});
652 $self -> show_controls ($req, $activeid, $options) ;
653 $self -> show_form_end ($req) if ($self -> {toplevel});
654 }
655  
656  
657 # ---------------------------------------------------------------------------
658 #
659 # init_data - init fdat before showing
660 #
661  
662 sub init_data
663  
664 {
665 my ($self, $req, $options) = @_ ;
666  
667 if ($self -> {toplevel} && $options)
668 {
669 $req -> {form_options_masks} = ($options && $options -> {masks}) || {} ;
670 }
671 foreach my $control (@{$self -> {init_data}})
672 {
673 next if (!$control) ;
674 $control -> init_data ($req) if ($control -> should_init_data ($req)) ;
675 }
676 }
677  
678 # ---------------------------------------------------------------------------
679 #
680 # init_markup - add any dynamic markup to the form data
681 #
682  
683 sub init_markup
684  
685 {
686 my ($self, $req, $parentctl, $method, $options) = @_ ;
687  
688 if ($self -> {toplevel} && $options)
689 {
690 $req -> {form_options_masks} = ($options && $options -> {masks}) || {} ;
691 }
692 foreach my $control (@{$self -> {init_markup}})
693 {
694 $control -> init_markup ($req, $parentctl, $method) if (!$control -> is_disabled ($req)) ;
695 }
696 }
697  
698 # ---------------------------------------------------------------------------
699 #
700 # prepare_fdat - change fdat after submit
701 #
702  
703 sub prepare_fdat
704  
705 {
706 my ($self, $req, $options) = @_ ;
707  
708 if ($self -> {toplevel} && $options)
709 {
710 $req -> {form_options_masks} = ($options && $options -> {masks}) || {} ;
711 }
712 foreach my $control (@{$self -> {prepare_fdat}})
713 {
714 $control -> prepare_fdat ($req) if (!$control -> is_disabled ($req)) ;
715 }
716 }
717  
718 # ---------------------------------------------------------------------------
719 #
720 # is_disabled - do not display this control at all
721 #
722  
723 sub is_disabled
724  
725 {
726 my ($self, $req) = @_ ;
727  
728 my $disable = $self -> {disable} ;
729  
730 $disable = &{$disable}($self, $req) if (ref ($disable) eq 'CODE') ;
731  
732 return $disable ;
733 }
734  
735  
736 # ---------------------------------------------------------------------------
737 #
738 # has_code_refs - returns true if is_readonly or is_disabled are coderefs
739 #
740  
741 sub has_code_refs
742  
743 {
744 my ($self, $req) = @_ ;
745  
746 return ref ($self -> {disable}) eq 'CODE' ;
747 }
748  
749  
750 # ---------------------------------------------------------------------------
751 #
752 # code_ref_fingerprint - returns fingerprint of is_disabled
753 #
754  
755 sub code_ref_fingerprint
756  
757 {
758 my ($self, $req) = @_ ;
759  
760 return ($self -> is_disabled($req)?'D':'E') ;
761 }
762  
763  
764 # ---------------------------------------------------------------------------
765 #
766 # all_code_ref_fingerprints - returns a fingerprint of the result of all code refs
767 # can be used to check if is_readonly or is_disabled
768 # has dynamicly changed
769 #
770  
771 sub all_code_ref_fingerprints
772  
773 {
774 my ($self, $req) = @_ ;
775  
776 my $fp ;
777 foreach my $control (@{$self -> {code_refs}})
778 {
779 $fp .= $control -> code_ref_fingerprint ($req) ;
780 }
781 return $fp ;
782 }
783  
784 # ---------------------------------------------------------------------------
785 #
786 # constrain_attrs - returns attrs that might change the form layout
787 # if there value changes
788 #
789  
790 sub constrain_attrs
791  
792 {
793 my ($self, $req) = @_ ;
794  
795 return $self -> {constrain_attrs} ;
796 }
797  
798  
799 # ---------------------------------------------------------------------------
800 #
801 # validate - validate the form input
802 #
803  
804 sub validate
805  
806 {
807 my ($self, $fdat, $pref, $epreq) = @_ ;
808  
809 my $validate = $self -> {validate} ;
810 my $result = $validate -> validate ($fdat, $pref, $epreq) ;
811 my @msgs ;
812 foreach my $err (@$result)
813 {
814 my $msg = $validate -> error_message ($err, $pref, $epreq) ;
815 push @msgs, $msg ;
816 }
817  
818 return ($result, \@msgs) ;
819 }
820  
821  
822 #------------------------------------------------------------------------------------------
823 #
824 # add_tabs
825 #
826 # fügt ein tab element mit subforms zu einem Formular hinzu
827 # wird nur eine Subform übergeben, werden nur diese Felder zurückgeliefert
828 # ohne tabs
829 #
830 # in $subform array mit hashs
831 # text => <anzeige text>
832 # fn => Dateiname
833 # fields => Felddefinitionen (alternativ zu fn)
834 # $args wird an fields funktionen durchgereicht
835 # $tabs_per_line anzahl tabs pro Zeile
836 #
837  
838 sub add_tabs
839  
840 {
841 my ($self, $subforms, $args, $tabs_per_line) = @_ ;
842 my @forms ;
843 my @values ;
844 my @options ;
845 my @grids;
846 $args ||= {} ;
847  
848 foreach my $file (@$subforms)
849 {
850 my $fn = $file -> {fn} ;
851 my $subfields = $file -> {fields} ;
852  
853 push @options, $file -> {text};
854 if ($fn)
855 {
856 my $obj = Execute ({object => $fn} ) ;
857 $subfields = $obj -> fields ($epreq, {%$file, %$args}) ;
858 }
859 push @forms, $subfields;
860 push @grids, $file -> {grid};
861 push @values, $file -> {value} ||= scalar(@forms);
862 }
863  
864 if (@forms == 1)
865 {
866 return @{$forms[0]} ;
867 }
868  
869 return {
870 section => 'cSectionText',
871 name => '__auswahl',
872 type => 'tabs',
873 values => \@values,
874 grids => \@grids,
875 options => \@options,
876 subforms=> \@forms,
877 width => 1,
878 'tabs_per_line' => $tabs_per_line,
879 },
880 }
881  
882 #------------------------------------------------------------------------------------------
883 #
884 # add_line
885 #
886 # adds the given controls into one line
887 #
888 #
889  
890 sub add_line
891  
892 {
893 my ($self, $controls, $cnt) = @_ ;
894  
895 $cnt ||= @$controls ;
896 foreach my $control (@$controls)
897 {
898 $control -> {width} = $cnt ;
899 }
900  
901 return @$controls ;
902 }
903  
904 #------------------------------------------------------------------------------------------
905 #
906 # add_sublines
907 #
908 # fügt ein tab elsement mit subforms zu einem Formular hinzu
909 #
910 # in $subform array mit hashs
911 # text => <anzeige text>
912 # fn => Dateiname
913 # fields => Felddefinitionen (alternativ zu fn)
914 #
915  
916  
917 sub add_sublines
918 {
919 my ($self, $object_data, $subforms, $type) = @_;
920  
921 $object_data ||= {} ;
922 $object_data -> {text} ||= $object_data -> {name} ;
923  
924 my @forms ;
925 my @values ;
926 my @options ;
927  
928 foreach my $file (@$subforms)
929 {
930 my $fn = $file -> {fn} ;
931 my $subfields = $file -> {fields} ;
932 if ($fn)
933 {
934 my $obj = Execute ({object => "$fn"} ) ;
935 $subfields = $obj -> fields ($epreq, $file) ;
936 }
937 $subfields ||= [] ;
938 foreach (@$subfields)
939 {
940 $_ -> {state} = $object_data -> {name} . '-show-' . ($file->{value} || $file->{name}) ;
941 }
942 push @forms, $subfields ;
943 push @values, $file->{value} || $file->{name};
944 push @options, $file -> {text} || $file->{value} || $file->{name};
945 }
946 $object_data -> {trigger} = 1 ;
947 return
948 {
949 %$object_data, type => $type || 'select',
950 values => \@values, options => \@options, sublines => \@forms,
951 };
952 }
953  
954 #------------------------------------------------------------------------------------------
955 #
956 # fields_add_checkbox_subform
957 #
958 # fügt ein checkbox Element mit Subforms hinzu
959 #
960 # in $subform array mit hashs
961 # text => <anzeige text>
962 # name => <name des Attributes>
963 # value => <Wert der checkbox>
964 # fn => Dateiname
965 # fields => Felddefinitionen (alternativ zu fn)
966 #
967  
968 sub add_checkbox_subform
969 {
970 my ($self, $subform, $args) = @_ ;
971 $args ||= {} ;
972  
973 my $name = $subform->{name};
974 my $text = $subform->{text};
975 my $value = $subform->{value} || 1 ;
976  
977 my $width = $subform->{width};
978 my $section;
979  
980 if(! $subform->{nosection})
981 {
982 $section = $subform->{section};
983 $section ||= 1;
984 }
985  
986 $name ||= "__$value";
987 $width ||= 1;
988  
989 my $subfield;
990 my $fn;
991 if($subfield = $subform->{fields})
992 {
993 # .... ok
994 }
995 elsif($fn = $subform->{fn})
996 {
997 my $obj = Execute ({object => "./$fn"} ) ;
998 #$subfield = [eval {$obj -> fields ($r, { %$file, %$args} ) || undef}];
999 }
1000  
1001 my $subfields = $subfield -> [0] ;
1002 foreach (@$subfields)
1003 {
1004 $_ -> {state} = $subform -> {name} . '-show' ;
1005 }
1006 $subfields = $subfield -> [1] ;
1007 foreach (@$subfields)
1008 {
1009 $_ -> {state} = $subform -> {name} . '-hide';
1010 }
1011  
1012 return {type => 'checkbox' , trigger => 1, section => $section, width => $width, name => $name, text => $text, value => $value, sublines => $subfield}
1013  
1014 }
1015  
1016 #------------------------------------------------------------------------------------------
1017 #
1018 # convert_label
1019 #
1020 # converts the label of a control to the text that should be outputed.
1021 # By default does return the text or name parameter of the control.
1022 # Can be overwritten to allow for example internationalization.
1023 #
1024 # in $ctrl Embperl::Form::Control object
1025 # $name optional: name to translate, if not given take $ctrl -> {text}
1026 #
1027  
1028 sub convert_label
1029 {
1030 my ($self, $ctrl, $name, $text, $req) = @_ ;
1031  
1032 return $text || $ctrl->{text} || $name || $ctrl->{name} ;
1033 }
1034  
1035 #------------------------------------------------------------------------------------------
1036 #
1037 # convert_options
1038 #
1039 # converts the values/options of a control to the text that should be outputed.
1040 # By default does nothing.
1041 # Can be overwritten to allow for example internationalization.
1042 #
1043 # in $ctrl Embperl::Form::Control object
1044 # $values values of the control i.e. values that are submitted
1045 # $options options of the control i.e. text that should be displayed
1046 #
1047  
1048 sub convert_options
1049 {
1050 my ($self, $ctrl, $values, $options, $req) = @_ ;
1051  
1052 return $options ;
1053 }
1054  
1055 #------------------------------------------------------------------------------------------
1056 #
1057 # convert_text
1058 #
1059 # converts the text of a controls like transparent to the text that should be outputed.
1060 # By default does nothing.
1061 # Can be overwritten to allow for example internationalization.
1062 #
1063 # in $ctrl Embperl::Form::Control object
1064 # $value value that is shown
1065 #
1066  
1067 sub convert_text
1068 {
1069 my ($self, $ctrl, $value, $text, $req) = @_ ;
1070  
1071 return $value || $ctrl->{text} || $ctrl->{name} ;
1072 }
1073  
1074  
1075 #------------------------------------------------------------------------------------------
1076 #
1077 # diff_checkitems
1078 #
1079 # Takes the posted form data and the checkitems, compares them and return the
1080 # fields that have changed
1081 #
1082 # in $check optional: arrayref with fieldnames that should be checked
1083 # ret \%diff fields that have changed
1084 #
1085  
1086 sub diff_checkitems
1087 {
1088 my ($self, $check) = @_ ;
1089  
1090 my %diff ;
1091 my $checkitems = eval { Storable::thaw(MIME::Base64::decode ($Embperl::fdat{-checkitems})) } ;
1092  
1093 foreach ($check?@$check:keys %Embperl::fdat)
1094 {
1095 next if ($_ eq '-checkitems') ;
1096 $diff{$_} = 1 if ($checkitems -> {$_} ne $Embperl::fdat{$_}) ;
1097 }
1098  
1099 return \%diff ;
1100 }
1101  
1102  
1103 1;
1104  
1105  
1106 __EMBPERL__
1107  
1108 [$syntax EmbperlBlocks $]
1109  
1110 [# ---------------------------------------------------------------------------
1111 #
1112 # show_form_begin - output begin of form
1113 #]
1114  
1115 [$ sub show_form_begin ($self, $req) $]
1116 <script language="javascript">var doValidate = 1 ;</script>
1117 <script src="/js/EmbperlForm.js"></script>
1118 <script src="/js/TableCtrl.js"></script>
1119  
1120 <form id="[+ $self->{formname} +]" name="[+ $self->{formname} +]" method="post" action="[+ $self->{actionurl}+]"
1121 [$ if ($self -> {on_submit_function}) $]
1122 onSubmit="s=[+ $self->{on_submit_function} +];if (s) { v=doValidate; doValidate=1; return ((!v) || epform_validate_[+ $self->{formname} +]()); } else { return false; }"
1123 [$else$]
1124 onSubmit="v=doValidate; doValidate=1; return ( (!v) || epform_validate_[+ $self->{formname}+]());"
1125 [$endif$]
1126 >
1127 [$endsub$]
1128  
1129 [# ---------------------------------------------------------------------------
1130 #
1131 # show_form_end - output end of form
1132 #]
1133  
1134 [$ sub show_form_end ($req) $]
1135 </form>
1136 [$endsub$]
1137  
1138 [ ---------------------------------------------------------------------------
1139 #
1140 # show_controls_begin - output begin of form controls area
1141 #]
1142  
1143 [$ sub show_controls_begin ($self, $req, $activeid)
1144  
1145 my $parent = $self -> parent_form ;
1146 my $class = $self -> {options}{classdiv} || ($parent -> {noframe}?'ef-tabs-border-u':'ef-tabs-border') ;
1147 my $parent_control = $self -> {parent_control} ;
1148 $]
1149  
1150 [$if $parent_control && $parent_control -> can('show_subform_controls_begin') $]
1151 [- $parent_control -> show_subform_controls_begin ($self, $req, $activeid) -]
1152 [$else$]
1153 <div id="[+ $self -> {unique_id} +]_[+ $self->{id} +]" class="ef-tabs-content"
1154 [$if ($activeid && $self->{id} ne $activeid) $] style="display: none" [$endif$]
1155 >
1156 [$if (!$self -> {noframe}) $]<table class="[+ $class +]"><tr><td class="ef-tabs-content-cell"> [$endif$]
1157 [$endif$]
1158 [$endsub$]
1159  
1160 [# ---------------------------------------------------------------------------
1161 #
1162 # show_controls_end - output end of form controls area
1163 #]
1164  
1165 [$sub show_controls_end ($self, $req)
1166 my $parent_control = $self -> {parent_control} ;
1167 $]
1168 [$if $parent_control && $parent_control -> can('show_subform_controls_end') $]
1169 [- $parent_control -> show_subform_controls_end ($self, $req) -]
1170 [$else$]
1171 [$ if (!$self -> {noframe}) $]</td></tr></table> [$endif$]
1172 </div>
1173 [$endif$]
1174  
1175 [$ if (@{$self->{bottom_code}}) $]
1176 <script language="javascript">
1177 [+ do { local $escmode = 0; join ("\n", @{$self->{bottom_code}}) } +]
1178 </script>
1179 [$endif$]
1180 [$ if ($self -> {toplevel} && @{$self -> {fields2empty}}) $]
1181 <input type="hidden" name="-fields2empty" value="[+ join (' ', @{$self -> {fields2empty}}) +]">
1182 [$endif$]
1183 [$endsub$]
1184  
1185 [# ---------------------------------------------------------------------------
1186 #
1187 # show_controls_hidden - output hidden controls and the end of form
1188 #]
1189  
1190 [$sub show_controls_hidden ($self, $req) $]
1191  
1192 [$ foreach my $ctl (@{$self->{hidden}}) $]
1193 [- $ctl -> show ($req) ; -]
1194 [$ endforeach $]
1195  
1196 [$endsub$]
1197  
1198 [# ---------------------------------------------------------------------------
1199 #
1200 # show_checkitems - output data to allow verifying if any data has changed
1201 #]
1202  
1203 [$sub show_checkitems ($self, $req)
1204  
1205 my $checkitems = MIME::Base64::encode (Storable::freeze (\%idat)) ;
1206 $]
1207 <input type="hidden" name="-checkitems" value="[+ $checkitems +]">
1208  
1209 [$endsub$]
1210  
1211  
1212 [# ---------------------------------------------------------------------------
1213 #
1214 # show_line_begin - output begin of line
1215 #]
1216  
1217 [$ sub show_line_begin ($self, $req, $lineno, $id, $activeid)
1218  
1219 my $baseid ;
1220 my $baseidn ;
1221 my $baseaid ;
1222 my $baseaidn ;
1223 if ($id =~ /^(.+)-(\d+?)-(\d+?)$/)
1224 {
1225 $baseid = $1 ;
1226 $baseidn = $2 ;
1227 }
1228 if ($activeid =~ /^(.+)-(\d+?)$/)
1229 {
1230 $baseaid = $1 ;
1231 $baseaidn = $2 ;
1232 }
1233  
1234 my $class = $lineno == 0?'cTableRow1':'cTableRow' ;
1235 $]<!-- line begin -->
1236 [# <tr class="[+ $class +]" valign="[+ $self->{valign} +]"
1237 [$if $id $] id="[+ $id +]" [$endif$]
1238 [$if ($activeid eq '-' || ($baseid eq $baseaid && $baseidn != $baseaidn)) $] style="display: none" [$endif$]
1239 >
1240 #][* return !($activeid eq '-' || ($baseid eq $baseaid && $baseidn != $baseaidn))
1241 *][$endsub$]
1242  
1243 [# ---------------------------------------------------------------------------
1244 #
1245 # show_line_end - output end of line
1246 #]
1247  
1248 [$ sub show_line_end ($req) $]<!-- line end -->[$endsub$]
1249  
1250  
1251 __END__