TableLayout/Form.pm untested

filter: Statementstmt 0.0% 0 / 237 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 408 CC 27 SCAR 21.7
CC
27
Cov
0%
tl_setDefaultValue
30.0
tl_setDefaultValue
30.0
passCGI
20.0
CRAP 8.8
1 # ====================================================================
2 # Copyright (C) 1997,1998 Stephen Farrell <stephen@farrell.org>
3 #
4 # All rights reserved. This program is free software; you can
5 # redistribute it and/or modify it under the same terms as Perl
6 # itself.
7 #
8 # ====================================================================
9 # File: Form.pm
10 # Author: Stephen Farrell
11 # Created: August 1997
12 # Locations: http://www.palefire.org/~sfarrell/TableLayout/
13 # CVS $Id: Form.pm,v 1.21 1998/09/20 21:06:01 sfarrell Exp $
14 # ====================================================================
15  
16 ## ===================================================================
17 ## This is the Form class itself
18 ## ===================================================================
19 ##
20 ## NB: only cells know how to print forms. If you have your own
21 ## spiffy componentcontainer that you want to contain a form, you have
22 ## to be very careful (it's best to just stick it into a cell or
23 ## table--which puts it in a cell for you). If you decide you know
24 ## what you're doing and want to not heed this advice, then know the
25 ## following: You probably do NOT want to make the form be one of
26 ## tl_components, and you must call _print_end(). And maybe some
27 ## other stuff. Heads up.
28 ##
29 package HTML::TableLayout::Form;
30 use HTML::TableLayout::Symbols;
31 @HTML::TableLayout::Form::ISA=qw(HTML::TableLayout::ComponentContainer);
32 use Carp;
33 use strict;
34  
35 sub setMethod { shift->{TL_PARAMS}->{method} = pop }
36 sub getMethod { return shift->{TL_PARAMS}->{method} }
37 sub setAction { shift->{TL_PARAMS}->{action} = pop }
38 sub getAction { return shift->{TL_PARAMS}->{action} }
39  
40  
41 ##
42 ## I think this is the ideal for passCGI... if just a hashref is
43 ## given, then it passes all those values as hidden. If that is given
44 ## plus a list afterwards, then it fills in the values just for the
45 ## members of the list. Additionally, if you put an "=" sign in those
46 ## elements of the list, it replaces the value with the one you
47 ## provided (this can also be accomplished by adding "hidden" fields
48 ## to a cell that contains a form.
49 ##
50  
51 sub passCGI {
52 my ($this, $hashref, @pass) = @_;
53  
54
55 ##
56 ## We want a *copy* of this hashref because we don't want it
57 ## changing under our feet, and we want to be able to muck with it.
58 ##
59  
60 if (ref $hashref) {
61 my %copy = %$hashref;
62 $this->{passcgi} = \ %copy;
63 }
64  
65 if (ref $pass[0] eq "ARRAY" and
66 scalar(@{ $pass[0] }) > 0) {
67 my @copy = @{ $pass[0] };
68 $this->{passlist} = \ @copy;
69 }
70 elsif (scalar(@pass) > 0) {
71 $this->{passlist} = \ @pass;
72 }
73  
74 return $this;
75 }
76  
77 ##
78 ## NB: We just get a reference, not a copy. No guarantees that your
79 ## original will not get trampled! In my code, I take care to use
80 ## exists to check if keys exist in the hash...
81 ##
82 sub useData {
83 my ($this, @data) = @_;
84 if (ref $data[0] eq "HASH") {
85 $this->{default_data} = $data[0];
86 }
87 else {
88 $this->{default_data} = { @data };
89 }
90 return $this;
91 }
92  
93  
94 ##
95 ## NB: One must be careful when using this insert. Normally it is
96 ## AUTOMAGICALLY called when you insert an object into a cell or
97 ## table. HOWEVER, you CAN choose to insert hidden items directly
98 ## into the form. The deal is that form won't call tl_print() on the
99 ## object, so if it doesn't need tl_print() to be called (such as
100 ## hidden) then it's ok to insert only into the form. I think you'll
101 ## agree this actually makes a kind of sense....
102 ##
103 sub insert {
104 my ($this, $c) = @_;
105  
106 if ($c->isa("HTML::TableLayout::FormComponent::Hidden")) {
107 if ($this->{passlist}) {
108
109 ##
110 ## Note we don't check if it is already on the passlist
111 ## this doesn't matter too much because it'll just show up
112 ## twice and both times it'll have the same value--but it'll
113 ## still cause problems!
114 ##
115
116 push @{ $this->{passlist} }, $c->tl_getName();
117 }
118 $this->{passcgi}->{$c->tl_getName()} =
119 $c->tl_getValue() || $this->{default_data}->{$c->tl_getName()};
120 }
121 else {
122
123 ##
124 ## Note it is not deleted off the passlist--this is your own dang
125 ## problem if you insert things AND add them to the passlist
126 ##
127
128 delete $this->{passcgi}->{$c->tl_getName()};
129 }
130  
131 $this->SUPER::insert($c);
132 }
133  
134  
135 ##
136 ## this function can only be called during the tl_setup() phase
137 ##
138 sub getName {
139 my ($this, $force_numeric) = @_;
140 if ($force_numeric or
141 $this->{TL_PARAMS}->{name} eq undef) {
142 return $this->{TL_WINDOW}->_getNumForms() - 1;
143 }
144 else {
145 return $this->{TL_PARAMS}->{name};
146 }
147 }
148  
149  
150  
151  
152 sub tl_print {
153 my ($this) = @_;
154 my $w = $this->{TL_WINDOW};
155 $w->i_print("><FORM".params($this->tl_getParameters())."");
156 }
157  
158 sub _print_end {
159 my ($this) = @_;
160 my $w = $this->{TL_WINDOW};
161 my @pass;
162 if ($this->{passlist}) {
163 @pass = @{ $this->{passlist} };
164 }
165 else {
166 @pass = keys %{ $this->{passcgi} };
167 }
168 my $k;
169 foreach $k (@pass) {
170 my $v;
171 if ($k =~ s/=(.*)//) {
172 $v = $1;
173 }
174 else {
175 $v = $this->{passcgi}->{$k};
176 }
177 $w->i_print("><INPUT TYPE=HIDDEN NAME=\"$k\" VALUE=\"$v\"");
178 }
179 $w->i_print("></FORM");
180 }
181  
182  
183 sub _getDefaultData { return shift->{default_data} }
184  
185  
186 ##
187 ## FIXME: this is NOT a full implementation of clone for this class!!!
188 ##
189 # sub clone {
190 # my ($this) = @_;
191 # my $class;
192 # my $clone = HTML::TableLayout::Form->new();
193 # my %passcgi_copy = %{ $this->{passcgi} };
194 # my %params_copy = %{ $this->{TL_PARAMS} };
195 # $clone->{passcgi} = \%passcgi_copy;
196 # $clone->{TL_PARAMS} = \%params_copy;
197 # return $clone;
198 # }
199  
200  
201 ##
202 ## ====================================================================
203 ## These are the form components
204 ## ====================================================================
205 ##
206 package HTML::TableLayout::FormComponent;
207 @HTML::TableLayout::FormComponent::ISA=qw(HTML::TableLayout::Component);
208 use HTML::TableLayout::Symbols;
209  
210  
211 sub tl_getName { return shift->{TL_PARAMS}->{name} }
212 sub tl_setName { shift->{TL_PARAMS}->{name} = pop }
213 sub tl_getValue { return shift->{TL_PARAMS}->{value} }
214 sub tl_setValue { shift->{TL_PARAMS}->{value} = pop }
215  
216 sub tl_setDefaultValue {
217 my ($this) = @_;
218 return if $this->{TL_PARAMS}->{value};
219 die("No form (BUG!) [$this]") unless $this->{TL_FORM};
220 my $data_hash = $this->{TL_FORM}->_getDefaultData();
221 return unless exists $data_hash->{$this->{TL_PARAMS}->{name}};
222 my $v;
223 if ($v = $data_hash->{$this->{TL_PARAMS}->{name}}) {
224 $this->{TL_PARAMS}->{value} = $v;
225 }
226 }
227  
228  
229 sub tl_setup {
230 my ($this) = @_;
231 $this->SUPER::tl_setup();
232 $this->tl_setDefaultValue();
233 }
234  
235  
236 # ---------------------------------------------------------------------
237 package HTML::TableLayout::FormComponent::Hidden;
238 use HTML::TableLayout::Symbols;
239 @HTML::TableLayout::FormComponent::Hidden::ISA=
240 qw(HTML::TableLayout::FormComponent);
241  
242 sub tl_init {
243 my $this = shift;
244 my $name = shift;
245 my $value = shift;
246 $this->{visible} = shift;
247 $this->SUPER::tl_init(@_);
248 $this->{TL_PARAMS}->{name} = $name;
249 $this->{TL_PARAMS}->{value} = $value;
250 }
251  
252 ##
253 ## Everything about hidden is handled by the Form itself, using the
254 ## "passcgi" mechanism.
255 ##
256  
257 sub tl_print {
258 my ($this) = @_;
259 if ($this->{visible}) {
260 ##
261 ## This is kind of funky, but a "visible" hidden entry displays the
262 ## value as plain text.
263 ##
264 $this->{TL_WINDOW}->i_print($this->{TL_PARAMS}->{value});
265 }
266 }
267  
268 # ---------------------------------------------------------------------
269 package HTML::TableLayout::FormComponent::Faux;
270 @HTML::TableLayout::FormComponent::Faux::ISA=
271 qw(HTML::TableLayout::FormComponent);
272  
273 ##
274 ## This behaves like a form component, but it just prints the text
275 ## value; it does not do any other form-like things such as *passing
276 ## it's value*. Use a "visible" Hidden if you want to do this.
277 ##
278 sub tl_init {
279 my $this = shift;
280 my $name = shift;
281 my $value = shift;
282 $this->SUPER::tl_init(@_);
283 $this->{TL_PARAMS}->{name} = $name;
284 $this->{TL_PARAMS}->{value} = shift;
285 }
286  
287 sub tl_print {
288 my ($this) = @_;
289 $this->{TL_WINDOW}->i_print($this->{TL_PARAMS}->{value});
290 }
291  
292  
293 # ---------------------------------------------------------------------
294 package HTML::TableLayout::FormComponent::InputText;
295 use HTML::TableLayout::Symbols;
296 @HTML::TableLayout::FormComponent::InputText::ISA=
297 qw(HTML::TableLayout::FormComponent);
298  
299 sub tl_print {
300 my ($this) = @_;
301 $this->{TL_WINDOW}
302 ->i_print("><INPUT TYPE=TEXT".params(%{ $this->{TL_PARAMS} })."");
303 }
304  
305 # ---------------------------------------------------------------------
306 package HTML::TableLayout::FormComponent::Button;
307 use HTML::TableLayout::Symbols;
308 @HTML::TableLayout::FormComponent::Button::ISA=
309 qw(HTML::TableLayout::FormComponent);
310  
311 sub tl_print {
312 my ($this) = @_;
313 $this->{TL_WINDOW}
314 ->i_print("><INPUT TYPE=BUTTON".params(%{ $this->{TL_PARAMS} })."");
315 }
316  
317 # ---------------------------------------------------------------------
318 package HTML::TableLayout::FormComponent::Checkbox;
319 use HTML::TableLayout::Symbols;
320 @HTML::TableLayout::FormComponent::Checkbox::ISA=
321 qw(HTML::TableLayout::FormComponent);
322  
323  
324 sub tl_print {
325 my ($this) = @_;
326 $this->{TL_WINDOW}
327 ->i_print("><INPUT TYPE=CHECKBOX".params(%{ $this->{TL_PARAMS} })."");
328 }
329  
330  
331 sub tl_setDefaultValue {
332 my ($this) = @_;
333 return if exists $this->{TL_PARAMS}->{checked};
334 die("No form (BUG!) [$this]") unless $this->{TL_FORM};
335 my $data_hash = $this->{TL_FORM}->_getDefaultData();
336 return unless exists $data_hash->{$this->{TL_PARAMS}->{name}};
337 if (exists $data_hash->{$this->{TL_PARAMS}->{name}} and
338 $data_hash->{$this->{TL_PARAMS}->{name}}) {
339 $this->{TL_PARAMS}->{checked} = undef;
340 }
341 }
342  
343  
344 # ---------------------------------------------------------------------
345 package HTML::TableLayout::FormComponent::Textarea;
346 use HTML::TableLayout::Symbols;
347 @HTML::TableLayout::FormComponent::Textarea::ISA=
348 qw(HTML::TableLayout::FormComponent);
349  
350 sub tl_init {
351 my $this = shift;
352 my $value = shift;
353 $this->SUPER::tl_init(@_);
354 $this->{text} = $value;
355 }
356  
357 sub tl_print {
358 my ($this) = @_;
359 my $w = $this->{TL_WINDOW};
360 $w->i_print("><TEXTAREA".params(%{ $this->{TL_PARAMS} }).">");
361 $w->f_print($this->{text});
362 $w->i_print("</TEXTAREA");
363 }
364  
365  
366 # ---------------------------------------------------------------------
367 package HTML::TableLayout::FormComponent::Password;
368 use HTML::TableLayout::Symbols;
369 @HTML::TableLayout::FormComponent::Password::ISA=
370 qw(HTML::TableLayout::FormComponent);
371  
372 sub tl_print {
373 my ($this) = @_;
374 $this->{TL_WINDOW}
375 ->i_print("><INPUT TYPE=PASSWORD".params(%{ $this->{TL_PARAMS} })."");
376 }
377  
378  
379 # ---------------------------------------------------------------------
380 package HTML::TableLayout::FormComponent::Submit;
381 use HTML::TableLayout::Symbols;
382 @HTML::TableLayout::FormComponent::Submit::ISA=
383 qw(HTML::TableLayout::FormComponent);
384  
385  
386 ## 99/100 times, you'll just be passing in the value here
387 sub tl_init {
388 my $this = shift;
389 my $value = shift;
390 $this->SUPER::tl_init(@_);
391 (defined $value) and $this->{TL_PARAMS}->{value} = $value;
392 }
393  
394 ##
395 ## I override this b/c chances are, if the value changes, it'll not be
396 ## what was expected.
397 ##
398 sub tl_setDefaultValue { }
399 sub tl_print {
400 my ($this) = @_;
401 die($this->{TL_PARAMS}->{value}) unless $this->{TL_WINDOW};
402 $this->{TL_WINDOW}
403 ->i_print("><INPUT TYPE=SUBMIT".params(%{ $this->{TL_PARAMS} })."");
404 }
405  
406 # ---------------------------------------------------------------------
407 package HTML::TableLayout::FormComponentMulti;
408 use HTML::TableLayout::Symbols;
409 use Carp;
410 @HTML::TableLayout::FormComponentMulti::ISA =
411 qw(HTML::TableLayout::ComponentContainer HTML::TableLayout::FormComponent);
412 ##
413 ## FIXME: why is this a ComponentContainer??
414 ##
415  
416 sub tl_init {
417 my $this = shift;
418 $this->{TL_OPS} = shift;
419 $this->SUPER::tl_init(@_);
420 }
421  
422  
423 sub tl_setup {
424 my ($this) = @_;
425 my ($x, $o, @new_tl_ops);
426  
427 foreach $o (@{ $this->{TL_OPS} }) {
428 $x = (ref $o->[1]) ? $o->[1] :
429 HTML::TableLayout::Component::Text->new($o->[1]);
430 $this->insert($x);
431 push @new_tl_ops, [ $o->[0], $x ];
432 }
433 $this->{TL_OPS} = \ @new_tl_ops;
434  
435 $this->SUPER::tl_setup();
436  
437 ##
438 ## if "Default" was passed in as a parameter, then we use that as the
439 ## default, otherwise we fall back on the form's default values.
440 ##
441 if (exists $this->{TL_PARAMS}->{Default}) {
442 $this->{TL_DEFAULT_VALUE} = $this->{TL_PARAMS}->{Default};
443 delete $this->{TL_PARAMS}->{Default};
444 }
445 else {
446 ##
447 ## Because of the order of the multiple inheretence, it doesn't find
448 ## the super that does the tl_setDefaults() call on tl_setup(), so
449 ## need to do so here.
450 ##
451 $this->tl_setDefaultValue();
452 }
453 }
454  
455 sub tl_setDefaultValue {
456 my ($this) = @_;
457 my $data_hash = $this->{TL_FORM}->_getDefaultData();
458 return unless exists $data_hash->{$this->{TL_PARAMS}->{name}};
459 $this->{TL_DEFAULT_VALUE} = $data_hash->{$this->{TL_PARAMS}->{name}};
460 }
461  
462 # ---------------------------------------------------------------------
463 package HTML::TableLayout::FormComponent::Radio;
464 use HTML::TableLayout::Symbols;
465 @HTML::TableLayout::FormComponent::Radio::ISA=
466 qw(HTML::TableLayout::FormComponentMulti);
467  
468 ##
469 ## Need to add an arbitrary flag to have it use breakafters
470 ## call it "Breakafter"
471 ##
472  
473 sub tl_setup {
474 my ($this) = @_;
475 if (exists $this->{TL_PARAMS}->{Breakafter}) {
476 $this->{breakafter} = 1;
477 delete $this->{TL_PARAMS}->{Breakafter};
478 }
479  
480 $this->SUPER::tl_setup();
481 }
482  
483 sub tl_print {
484 my ($this) = @_;
485 my $br = $this->{breakafter};
486 my $r;
487 my $w = $this->{TL_WINDOW};
488 foreach $r (@{ $this->{TL_OPS} }) {
489 my %params = %{ $this->{TL_PARAMS} };
490 $params{value} = $r->[0];
491 $w->i_print("><INPUT TYPE=RADIO".params(%params));
492 if ($r->[0] eq $this->{TL_DEFAULT_VALUE}) {
493 $w->f_print(" CHECKED");
494 }
495 $r->[1]->tl_print();
496 $br and $w->i_print("><BR");
497 }
498 }
499  
500 # ---------------------------------------------------------------------
501 package HTML::TableLayout::FormComponent::Choice;
502 use HTML::TableLayout::Symbols;
503 @HTML::TableLayout::FormComponent::Choice::ISA=
504 qw(HTML::TableLayout::FormComponentMulti);
505  
506 sub tl_print {
507 my ($this) = @_;
508 my $w = $this->{TL_WINDOW};
509 $w->i_print("><SELECT".params(%{ $this->{TL_PARAMS} }). "");
510 $w->_indentIncrement();
511 my $o;
512 foreach $o (@{ $this->{TL_OPS} }) {
513 if (! (ref $o eq "ARRAY")) { die("malformed options") }
514 $w->i_print("><OPTION VALUE=\"$o->[0]\"");
515 if ($o->[0] eq $this->{TL_DEFAULT_VALUE}) {
516 $w->f_print(" SELECTED");
517 }
518 $o->[1]->tl_print();
519 }
520 $w->_indentDecrement();
521 $w->i_print("></SELECT");
522 }
523  
524  
525 1;