blib/lib/Embperl/Form/Control.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 413 CC 33 SCAR 29.0
CC
33
Cov
0%
get_display_text
72.0
is_readonly
20.0
code_ref_fingerprint
20.0
CRAP 18.2
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 package Embperl::Form::Control ;
18  
19 use strict ;
20 use vars qw{%fdat} ;
21  
22 use Embperl::Inline ;
23  
24 # ---------------------------------------------------------------------------
25 #
26 # new - create a new control
27 #
28  
29  
30 sub new
31  
32 {
33 my ($class, $args) = @_ ;
34  
35 my $self = { %$args } ;
36 bless $self, $class ;
37  
38 $self -> init ;
39  
40 return $self ;
41 }
42  
43 # ---------------------------------------------------------------------------
44 #
45 # init - init the new control
46 #
47  
48 sub init
49  
50 {
51 my ($self) = @_ ;
52  
53 my $eventattrs = '' ;
54 if (my $e = $self -> {event})
55 {
56 for (my $i = 0; $i < @$e; $i += 2)
57 {
58 $eventattrs .= $e -> [$i] . '="' . $e -> [$i+1] . '" ' ;
59 }
60 }
61 $self -> {eventattrs} = $eventattrs ;
62 $self -> {imagedir} ||= '/images' ;
63
64 return $self ;
65 }
66  
67  
68 # ---------------------------------------------------------------------------
69 #
70 # noframe - do not draw frame border if this is the only control
71 #
72  
73 sub noframe
74  
75 {
76 return ;
77 }
78  
79 # ---------------------------------------------------------------------------
80 #
81 # is_disabled - do not display this control at all
82 #
83  
84 sub is_disabled
85  
86 {
87 my ($self, $req) = @_ ;
88  
89 my $ret ;
90 foreach my $disable ($self -> {disable}, $req -> {form_options_masks}{$self->{name}}{disable}, $req -> {form_options_masks}{'*'}{disable})
91 {
92 return 1 if (ref ($disable) eq 'CODE'?&{$disable}($self, $req):$disable) ;
93 }
94
95 return ;
96 }
97  
98 # ---------------------------------------------------------------------------
99 #
100 # is_blanked - display this control as blank field
101 #
102  
103 sub is_blanked
104  
105 {
106 my ($self, $req) = @_ ;
107  
108 foreach my $blank ($self -> {blank}, $req -> {form_options_masks}{$self->{name}}{blank}, $req -> {form_options_masks}{'*'}{blank})
109 {
110 return 1 if (ref ($blank) eq 'CODE'?&{$blank}($self, $req):$blank) ;
111 }
112 return ;
113 }
114  
115 # ---------------------------------------------------------------------------
116 #
117 # is_readonly - could value of this control be changed ?
118 #
119  
120 sub is_readonly
121  
122 {
123 my ($self, $req) = @_ ;
124  
125 return 0 if ($self -> {readonly} eq '0') ;
126
127 foreach my $readonly ($self -> {readonly}, $req -> {form_options_masks}{$self->{name}}{readonly}, $req -> {form_options_masks}{'*'}{readonly})
128 {
129 return 1 if (ref ($readonly) eq 'CODE'?&{$readonly}($self, $req):$readonly) ;
130 }
131 return ;
132 }
133  
134 # ---------------------------------------------------------------------------
135 #
136 # is_with_id - returns true if the control shows something that has an internal id
137 #
138  
139 sub is_with_id
140  
141 {
142 my ($self, $req) = @_ ;
143  
144 return 0 ;
145 }
146  
147 # ---------------------------------------------------------------------------
148 #
149 # is_hidden - returns true if this is a hidden control
150 #
151  
152 sub is_hidden
153  
154 {
155 my ($self, $req) = @_ ;
156  
157 return ;
158 }
159  
160 # ---------------------------------------------------------------------------
161 #
162 # should_init_data - returns true if init_data should be called for this control
163 #
164  
165 sub should_init_data
166  
167 {
168 my ($self, $req) = @_ ;
169  
170 return !$self -> is_disabled ($req) && !$self -> is_readonly ($req) ;
171 }
172  
173  
174  
175 # ---------------------------------------------------------------------------
176 #
177 # has_code_refs - returns true if is_readonly or is_disabled are coderefs
178 #
179  
180 sub has_code_refs
181  
182 {
183 my ($self, $req) = @_ ;
184  
185 return ref ($self -> {readonly}) eq 'CODE' || ref ($self -> {disable}) eq 'CODE' || ref ($self -> {blank}) eq 'CODE' ;
186 }
187  
188  
189 # ---------------------------------------------------------------------------
190 #
191 # code_ref_fingerprint - returns fingerprint of is_readonly and is_disabled
192 #
193  
194 sub code_ref_fingerprint
195  
196 {
197 my ($self, $req) = @_ ;
198  
199 return ($self -> is_readonly($req)?'R':'W') . ($self -> is_disabled($req)?'D':'E') . ($self -> is_blanked($req)?'B':'S') ;
200 }
201  
202 # ---------------------------------------------------------------------------
203 #
204 # constrain_attrs - returns attrs that might change the form layout
205 # if there value changes
206 #
207  
208 sub constrain_attrs
209  
210 {
211 my ($self, $req) = @_ ;
212  
213 return () ;
214 }
215  
216 # ---------------------------------------------------------------------------
217 #
218 # get_on_show_code
219 #
220 # retuns js code that should be excuted when form becomes visible
221 #
222  
223 sub get_on_show_code
224 {
225 return ;
226 }
227  
228 # ---------------------------------------------------------------------------
229 #
230 # get_active_id - get the id of the value which is currently active
231 #
232  
233 sub get_active_id
234  
235 {
236 return ;
237 }
238  
239 # ---------------------------------------------------------------------------
240 #
241 # form - return form object
242 #
243  
244 sub form
245 {
246 my ($self) = @_ ;
247  
248 return $Embperl::FormData::forms{$self -> {formptr}} ;
249 }
250  
251 # ---------------------------------------------------------------------------
252 #
253 # load_form - load a form to a given formptr.
254 #
255 # This class method should be overwritten, to load a form to a given
256 # formptr, in case it is not already loaded
257 # The formptr maybe passed in the options hash during form creation
258 #
259 # in $formptr
260 #
261  
262  
263 sub load_form
264 {
265 my ($class, $formptr) = @_ ;
266
267 }
268
269 # ---------------------------------------------------------------------------
270 #
271 # get_control_from_id
272 #
273  
274 sub get_control_from_id
275 {
276 my ($class, $id) = @_ ;
277
278 my ($formptr, $ctlid) = split /#/, $id ;
279 my $form = $Embperl::FormData::forms{$formptr} ;
280
281 if (!$form)
282 {
283 $class -> load_form ($formptr) ;
284 $form = $Embperl::FormData::forms{$formptr} ;
285 die "Form for '$formptr' is not available" if (!$form) ;
286 }
287
288 my $ctl = $form -> {controlids}{$ctlid} ;
289 die "Control '$ctlid' in Form '$formptr' is not available" if (!$ctl) ;
290
291 return $ctl ;
292 }
293
294 # ---------------------------------------------------------------------------
295 #
296 # get_id_for_control
297 #
298  
299 sub get_id_for_control
300 {
301 my ($self, $reqdata) = @_ ;
302
303 return "$self->{formptr}#$self->{id}" ;
304 }
305
306 # ---------------------------------------------------------------------------
307 #
308 # label_text - return text of label
309 #
310  
311 sub label_text
312 {
313 my ($self, $req) = @_ ;
314  
315 my $key = 'label_text' . ($req -> {form_options}{language_fingerprint} || $req -> {form_options}{language}) ;
316  
317 return $self -> {$key} if ($self -> {$key}) ;
318  
319 return $self -> {$key} = $self -> {showtext}?($self->{text} ||
320 $self->{name}):$self -> form -> convert_label ($self, undef, undef, $req) ;
321 }
322  
323  
324 # ---------------------------------------------------------------------------
325 #
326 # get_validate_auto_rules - get rules for validation, in case user did
327 # not specify any
328 # should be overwritten by control
329 #
330  
331 sub get_validate_auto_rules
332 {
333 my ($self, $req) = @_ ;
334
335 return if (!$self -> {required}) ;
336 return [ required => 1 ] ;
337 }
338
339 # ---------------------------------------------------------------------------
340 #
341 # get_validate_rules - get rules for validation
342 #
343  
344 sub get_validate_rules
345 {
346 my ($self, $req) = @_ ;
347  
348 my @local_rules ;
349 if ($self -> {validate} && @{$self -> {validate}} > 0)
350 {
351 @local_rules = ( -key => $self->{name} );
352 push @local_rules, -name => $self -> label_text ($req);
353 push @local_rules, @{$self -> {validate}};
354 }
355 else
356 {
357 my $auto = $self -> get_validate_auto_rules ($req) ;
358 if ($auto)
359 {
360 @local_rules = ( -key => $self->{name} );
361 push @local_rules, -name => $self -> label_text ($req) ;
362 push @local_rules, @{$auto};
363 }
364 }
365 return \@local_rules ;
366 }
367  
368 # ---------------------------------------------------------------------------
369 #
370 # has_validate_rules - check if there is anything to validate and
371 # create auto rules
372 #
373  
374 sub has_validate_rules
375 {
376 my ($self, $req) = @_ ;
377  
378 if ($self -> {validate})
379 {
380 return scalar(@{$self -> {validate}}) ;
381 }
382 my $auto = $self -> get_validate_auto_rules ($req) ;
383 if ($auto)
384 {
385 #$self -> {validate} = $auto ;
386 return scalar(@$auto) ;
387 }
388
389 $self -> {validate} = [] ;
390 return 0 ;
391 }
392  
393 # ---------------------------------------------------------------------------
394 #
395 # has_auto_label_size - returns true if label should be auto sized for this control
396 #
397  
398 sub has_auto_label_size
399 {
400 return 1 ;
401 }
402  
403
404 # ---------------------------------------------------------------------------
405 #
406 # get_value - return the current value for the control
407 # if dataprefix is set, every hash key within dataprefix is tried
408 #
409  
410 sub get_value
411 {
412 my ($self, $req) = @_ ;
413
414 my $fdat = $req -> {docdata} || \%Embperl::fdat ;
415 my $name = $self -> {srcname} || $self -> {force_name} || $self -> {name} ;
416 return $fdat -> {$name} ;
417 my $dataprefix = $self -> {dataprefix} ;
418  
419 return $fdat -> {$name} if (!$dataprefix) ;
420
421 foreach my $prefix (@$dataprefix)
422 {
423 my $item = $prefix?$fdat -> {$prefix}{$name}:$fdat -> {$name} ;
424 return $item if (defined ($item)) ;
425 }
426
427 return ;
428 }
429  
430  
431 # ---------------------------------------------------------------------------
432 #
433 # get_std_control_attr - return the default attributes for the control
434 #
435 # ret string with all standard attribute, already html escaped
436 #
437  
438 sub get_std_control_attr
439 {
440 my ($self, $req, $id, $type, $addclass) = @_ ;
441  
442 my $name = $self -> {force_name} || $self -> {name} ;
443 my $ctrlid = $id || ($req -> {uuid} . '_' . $name) ;
444 my $class = $self -> {class} ;
445 my $width = $self -> {width_percent} ;
446 my $events = $self -> {eventattrs} ;
447 $type ||= $self -> {type} ;
448 my $state = $self -> {state} ;
449 $state =~ s/[^-a-zA-Z0-9_ ]/_/g ;
450
451 my $attrs = qq{class="ef-control ef-control-width-$width ef-control-$type ef-control-$type-width-$width $addclass $class $state" id="$ctrlid" $events} ;
452 return wantarray?($attrs, $ctrlid, $name):$attrs ;
453 }
454  
455 # ------------------------------------------------------------------------------------------
456 #
457 # get_display_text - returns the text that should be displayed
458 #
459  
460 sub get_display_text
461 {
462 my ($self, $req, $value) = @_ ;
463
464 $value = $self -> get_value ($req) if (!defined ($value)) ;
465 if (ref $value)
466 {
467 if (ref ($value) eq 'ARRAY')
468 {
469 return join (' ', map { $_ ne ''?$self -> get_display_text ($req, $_):() } @$value) ;
470 }
471 elsif (ref ($value) eq 'HASH')
472 {
473 return join (' ', map { $_ ne ''?$self -> get_display_text ($req, $_):() } %$value) ;
474 }
475 elsif (ref ($value) eq 'SCALAR')
476 {
477 return $$value ;
478 }
479 }
480  
481 return $value ;
482 }
483
484 # ------------------------------------------------------------------------------------------
485 #
486 # get_sort_value - returns the value that should be used to sort
487 #
488  
489 sub get_sort_value
490 {
491 my ($self, $req, $value) = @_ ;
492
493 return lc($self -> get_display_text ($req, $value)) ;
494 }
495
496 # ---------------------------------------------------------------------------
497 #
498 # get_id_from_value - returns id for a given value
499 #
500  
501 sub get_id_from_value
502  
503 {
504 #my ($self, $value, $req) = @_ ;
505  
506 return ;
507 }
508  
509
510 1 ;
511  
512 # ===========================================================================
513  
514 __EMBPERL__
515  
516 [$syntax EmbperlBlocks $]
517  
518  
519  
520 [# ---------------------------------------------------------------------------
521 #
522 # show - output the whole control including the label
523 #]
524  
525 [$sub show ($self, $req)
526  
527 $fdat{$self -> {name}} = $self -> {default} if ($fdat{$self -> {name}} eq '' && exists ($self -> {default})) ;
528 my $span = 0 ;
529 my $state = $self -> {state} ;
530 $state =~ s/[^-a-zA-Z0-9_ ]/_/g ;
531  
532 $]<table class="ef-element ef-element-width-[+ $self -> {width_percent} +] ef-element-[+ $self -> {type} +] [+ $state +]">
533 <tr>
534 [$ if ($self -> is_blanked ($req)) $]
535 <td class="ef-label-box ef-label-box-width-100"> </td>
536 [$else$][-
537 $span += $self -> show_label_cell ($req, $span);
538 $self -> show_control_cell ($req, $span) ;
539 -][$endif$]
540 </tr>
541 </table>[$
542 endsub $]
543  
544 [# ---------------------------------------------------------------------------
545 #
546 # show_sub_begin - output begin of sub form
547 #]
548  
549 [$sub show_sub_begin ($self, $req)
550  
551 my $span = $self->{width_percent} ;
552 $]
553 <!-- sub begin --></tr><tr><td class="cBase cTabTD" colspan="[+ $span +]">
554 [$endsub$]
555  
556 [# ---------------------------------------------------------------------------
557 #
558 # show_sub_end - output end of sub form
559 #]
560  
561 [$sub show_sub_end ($self, $req) $]
562 </td><!-- sub end -->
563 [$endsub$]
564  
565 [# ---------------------------------------------------------------------------
566 #
567 # show - output the label
568 #]
569  
570 [$ sub show_label ($self, $req) $][-
571  
572 if ($self -> {showoptionslabel})
573 {
574 my $opts = $self -> form -> convert_options ($self, [$self -> {value}], undef, $req) ;
575 $self -> {text} = $opts -> [0] ;
576 $self -> {showtext} = 1 ;
577 }
578 -][+ $self -> label_text ($req) +][$endsub$]
579  
580 [# ---------------------------------------------------------------------------
581 #
582 # show_label_icon - output the icon before the label
583 #]
584  
585 [$sub show_label_icon ($self, $req) $]
586 [$if $self -> {xxsublines} $]&nbsp;<img src="/images/plus.png" style="vertical-align: middle;">[$endif$]
587 [$if $self -> {xxparentid} $]&nbsp;<img src="/images/vline.png" style="vertical-align: middle;">[$endif$]
588 [$endsub$]
589  
590 [# ---------------------------------------------------------------------------
591 #
592 # show - output the control
593 #]
594  
595 [$ sub show_label_cell ($self, $req)
596  
597 my $style = '';
598 $style = 'white-space:nowrap; ' if ($self->{labelnowrap}) ;
599 $addclass = 'ef-label-box-width-' . ($self->{width_percent}) ;
600 $addclass2 = 'ef-label-width-' . ($self->{width_percent}) ;
601 $]
602 <td class="ef-label-box [+ $addclass +] [$ if $self->{labelclass} $][+ " $self->{labelclass}" +][$ endif $]" [$ if $style $]style="[+ $style +]"[$ endif $]>
603 <div class="ef-label [+ $addclass2 +]" _ef_attr="[+ $self -> {name} +]">
604 [-
605 $self -> show_label ($req);
606 $self -> show_label_icon ($req) ;
607 -]
608 </div>
609 </td>
610 [- return $span ; -]
611 [$endsub$]
612  
613 [# ---------------------------------------------------------------------------
614 #
615 # show_control - output the control itself
616 #]
617  
618 [$ sub show_control ($self, $req) $]<div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req) } +]>[+ $self->{value} +]</div>[$endsub$]
619  
620 [# ---------------------------------------------------------------------------
621 #
622 # show_control_readonly - output the control as readonly
623 #]
624  
625 [$ sub show_control_readonly ($self, $req, $value, $suffix)
626  
627 my $text = $self -> get_display_text ($req, $value) ;
628 my $name = $self -> {force_name} || $self -> {name} ;
629 $]
630 <div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req, '', 'readonly') } +] _ef_divname="[+ $name +]">[+ $text +][+ $suffix +]</div>
631 [$ if $self->{hidden} $]
632 <input type="hidden" name="[+ $name +]" value="[+ $value +]">
633 [$endif$]
634 [$endsub$]
635  
636 [# ---------------------------------------------------------------------------
637 #
638 # show_control_readonly_array - output the control as readonly, multiple
639 # times in case of array
640 #]
641  
642 [!
643
644 sub show_control_readonly_array
645 {
646 my ($self, $req, $value) = @_ ;
647  
648 $value = $self -> get_value ($req) if (!defined ($value)) ;
649  
650 if (ref ($value) eq 'ARRAY')
651 {
652 foreach my $subval (@$value)
653 {
654
655 $self -> show_control_readonly ($req, defined ($subval)?$subval:'') ;
656 }
657 }
658 else
659 {
660 $self -> show_control_readonly ($req, $value) ;
661 }
662 }
663 !]
664  
665 [# ---------------------------------------------------------------------------
666 #
667 # show_control_addons - output additional things after the control
668 #]
669  
670 [$ sub show_control_addons ($self, $req) $][$endsub$]
671  
672  
673 [# ---------------------------------------------------------------------------
674 #
675 # show_controll_cell - output the table cell for the control
676 #]
677  
678 [$ sub show_control_cell ($self, $req, $x)
679  
680 my $ro = $self -> is_readonly ($req) ;
681 my $addclass = 'ef-control-box-width-' . ($self->{width_percent} || 50 ) ;
682 $addclass .= ' ef-control-box-readonly' if ($ro) ;
683 $addclass .= ' ' . $self->{controlclass} if ($self->{controlclass}) ;
684 $]
685 <td class="ef-control-box [+ $addclass +]">
686 [*
687 my @ret = $ro?$self -> show_control_readonly_array($req):$self -> show_control ($req);
688 $self -> show_control_addons ($req) ;
689 *]
690 </td>
691 [* return @ret ; *]
692 [$endsub$]
693  
694 __END__