line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
########################################################################### |
3
|
|
|
|
|
|
|
# Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. |
4
|
|
|
|
|
|
|
# Please visit http://formbuilder.org for tutorials, support, and examples. |
5
|
|
|
|
|
|
|
########################################################################### |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package CGI::FormBuilder::Template::Builtin; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
CGI::FormBuilder::Template::Builtin - Builtin HTML rendering |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $form = CGI::FormBuilder->new; |
16
|
|
|
|
|
|
|
$form->render; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
4
|
|
|
4
|
|
14
|
use Carp; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
206
|
|
21
|
4
|
|
|
4
|
|
14
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
61
|
|
22
|
4
|
|
|
4
|
|
10
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
82
|
|
23
|
4
|
|
|
4
|
|
9
|
no warnings 'uninitialized'; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
102
|
|
24
|
|
|
|
|
|
|
|
25
|
4
|
|
|
4
|
|
14
|
use CGI::FormBuilder::Util; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
4752
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = '3.10'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
58
|
|
|
58
|
0
|
92
|
my $self = shift; |
32
|
58
|
|
33
|
|
|
208
|
my $class = ref($self) || $self; |
33
|
58
|
50
|
|
|
|
118
|
my %opt = @_ if @_ > 1; |
34
|
58
|
|
|
|
|
144
|
return bless \%opt, $class; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub prepare { |
38
|
58
|
|
|
58
|
0
|
67
|
my $self = shift; |
39
|
58
|
|
|
|
|
59
|
my $form = shift; |
40
|
|
|
|
|
|
|
|
41
|
58
|
|
|
|
|
80
|
my @html = (); # joined with newline |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Opening CGI/title gunk |
44
|
58
|
|
|
|
|
136
|
my $hd = $form->header; |
45
|
58
|
100
|
|
|
|
109
|
if (defined $hd) { |
46
|
14
|
|
|
|
|
42
|
push @html, $form->dtd, htmltag('head'); |
47
|
14
|
50
|
|
|
|
44
|
push @html, htmltag('title') . $form->title . htmltag('/title') |
48
|
|
|
|
|
|
|
if $form->title; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# stylesheet path if specified |
51
|
14
|
100
|
100
|
|
|
71
|
if ($form->{stylesheet} && $form->{stylesheet} ne 1) { |
52
|
|
|
|
|
|
|
# user-specified path |
53
|
|
|
|
|
|
|
push @html, htmltag('link', { rel => 'stylesheet', |
54
|
|
|
|
|
|
|
type => 'text/css', |
55
|
4
|
|
|
|
|
19
|
href => $form->{stylesheet} }); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# JavaScript validate/head functions |
60
|
58
|
|
|
|
|
134
|
my $js = $form->script; |
61
|
58
|
100
|
|
|
|
110
|
push @html, $js if $js; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Opening HTML if so requested |
64
|
58
|
|
|
|
|
161
|
my $font = $form->font; |
65
|
58
|
50
|
|
|
|
93
|
my $fcls = $font ? htmltag('/font') : ''; |
66
|
58
|
100
|
|
|
|
110
|
if (defined $hd) { |
67
|
14
|
|
|
|
|
35
|
push @html, htmltag('/head'), $form->body; |
68
|
14
|
50
|
|
|
|
31
|
push @html, $font if $font; |
69
|
14
|
50
|
|
|
|
33
|
push @html, htmltag('h3') . $form->title . htmltag('/h3') |
70
|
|
|
|
|
|
|
if $form->title; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Include warning if noscript |
74
|
58
|
100
|
|
|
|
122
|
push @html, $form->noscript if $js; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Put id's around state tags if so required |
77
|
58
|
|
|
|
|
63
|
my($stid, $keid); |
78
|
58
|
100
|
|
|
|
159
|
if (my $fn = $form->name) { |
79
|
8
|
|
|
|
|
26
|
$stid = tovar("${fn}$form->{statename}"); |
80
|
8
|
|
|
|
|
23
|
$keid = tovar("${fn}$form->{extraname}"); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Begin form |
84
|
58
|
|
|
|
|
171
|
my $txt = $form->text; |
85
|
58
|
100
|
|
|
|
128
|
push @html, $txt if $txt; |
86
|
58
|
|
|
|
|
134
|
push @html, $form->start; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Put id's around state tags if they exist |
89
|
58
|
50
|
|
|
|
168
|
if (my $st = $form->statetags) { |
90
|
58
|
|
|
|
|
265
|
push @html, |
91
|
|
|
|
|
|
|
$form->div(id => $form->idname($form->statename), |
92
|
|
|
|
|
|
|
class => $form->class($form->statename)) . |
93
|
|
|
|
|
|
|
$st . htmltag('/div'); |
94
|
|
|
|
|
|
|
} |
95
|
58
|
100
|
|
|
|
172
|
if (my $ke = $form->keepextras) { |
96
|
4
|
|
|
|
|
27
|
push @html, |
97
|
|
|
|
|
|
|
$form->div(id => $form->idname($form->extraname), |
98
|
|
|
|
|
|
|
class => $form->class($form->extraname)) . |
99
|
|
|
|
|
|
|
$ke . htmltag('/div'); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Render hidden fields first |
103
|
58
|
|
|
|
|
59
|
my @unhidden; |
104
|
58
|
|
|
|
|
154
|
for my $field ($form->fieldlist) { |
105
|
169
|
100
|
|
|
|
284
|
push(@unhidden, $field), next if $field->type ne 'hidden'; |
106
|
2
|
|
|
|
|
6
|
push @html, $field->tag; # no label/etc for hidden fields |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Support fieldset => 'name' to organize by fieldset on the fly |
110
|
58
|
|
|
|
|
128
|
my $legend = $form->fieldsets; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Get table stuff and reused calls |
113
|
58
|
|
|
|
|
230
|
my $table = $form->table(id => $form->idname($form->bodyname), class => $form->class); |
114
|
58
|
|
|
|
|
77
|
my $tabn = 1; |
115
|
58
|
100
|
|
|
|
126
|
push @html, $table if $table; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Render regular fields in table |
118
|
58
|
|
|
|
|
49
|
my $lastset; |
119
|
58
|
|
|
|
|
89
|
for my $field (@unhidden) { |
120
|
167
|
100
|
|
|
|
619
|
if (my $set = $field->fieldset) { |
|
|
100
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# hooks (hack?) for fieldsets |
122
|
9
|
100
|
|
|
|
18
|
if ($set ne $lastset) { |
123
|
|
|
|
|
|
|
# close any open tables/fieldsets |
124
|
4
|
100
|
|
|
|
8
|
if ($lastset) { |
|
|
50
|
|
|
|
|
|
125
|
3
|
50
|
|
|
|
9
|
push @html, htmltag('/table') if $table; |
126
|
3
|
|
|
|
|
7
|
push @html, htmltag('/fieldset'); |
127
|
3
|
|
|
|
|
5
|
push @html, htmltag('/div'); |
128
|
|
|
|
|
|
|
} elsif ($table) { |
129
|
|
|
|
|
|
|
# Catch in case we have an empty table - ie the previous |
130
|
|
|
|
|
|
|
# element is just . This workaround is needed
131
|
|
|
|
|
|
|
# in case the user wants to mix fields with/without |
132
|
|
|
|
|
|
|
# fieldsets in the same form |
133
|
1
|
50
|
|
|
|
4
|
if ($html[-1] =~ /^
134
|
1
|
|
|
|
|
2
|
pop @html; |
135
|
|
|
|
|
|
|
} else { |
136
|
|
|
|
|
|
|
# close non-fieldset table |
137
|
0
|
|
|
|
|
0
|
push @html, htmltag('/table'); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Wrap fieldset in a to allow jquery #tabs |
142
|
4
|
|
|
|
|
19
|
push @html, $form->div(id => $form->idname($form->tabname.$tabn++), |
143
|
|
|
|
|
|
|
class => $form->class($form->tabname)); |
144
|
|
|
|
|
|
|
|
145
|
4
|
|
|
|
|
12
|
(my $sn = lc $set) =~ s/\W+/_/g; |
146
|
4
|
|
|
|
|
11
|
push @html, htmltag('fieldset', id => $form->idname("_$sn"), |
147
|
|
|
|
|
|
|
class => $form->class('_set')); |
148
|
|
|
|
|
|
|
push @html, htmltag('legend') . ($legend->{$set}||$set) . htmltag('/legend') |
149
|
4
|
50
|
33
|
|
|
14
|
if defined $legend->{$set}; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# Wrap fields in a table |
152
|
4
|
50
|
|
|
|
15
|
push @html, $form->table if $table; |
153
|
|
|
|
|
|
|
|
154
|
4
|
|
|
|
|
7
|
$lastset = $set; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} elsif ($lastset) { |
157
|
|
|
|
|
|
|
# ended |
158
|
|
|
|
|
|
|
# remaining fields are not in a fieldset |
159
|
1
|
50
|
|
|
|
5
|
push @html, htmltag('/table') if $table; |
160
|
1
|
|
|
|
|
3
|
push @html, htmltag('/fieldset'); |
161
|
1
|
|
|
|
|
3
|
push @html, htmltag('/div'); |
162
|
1
|
50
|
|
|
|
4
|
push @html, $table if $table; |
163
|
1
|
|
|
|
|
2
|
undef $lastset; # avoid dup below |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
167
|
|
|
|
|
401
|
debug 2, "render: attacking normal field '$field'"; |
167
|
167
|
50
|
33
|
|
|
331
|
next if $field->static > 1 && ! $field->tag_value; # skip missing static vals |
168
|
|
|
|
|
|
|
|
169
|
167
|
100
|
|
|
|
242
|
if ($table) { |
170
|
165
|
|
|
|
|
292
|
push @html, $form->tr(id => $form->idname("_$field", $form->rowname)); |
171
|
|
|
|
|
|
|
|
172
|
165
|
|
|
|
|
641
|
my $cl = $form->class($form->labelname); |
173
|
165
|
|
|
|
|
419
|
my $row = ' ' . $form->td(id => $form->idname("_$field", $form->labelname), |
174
|
|
|
|
|
|
|
class => $cl) . $font; |
175
|
165
|
100
|
66
|
|
|
680
|
if ($field->invalid) { |
|
|
100
|
|
|
|
|
|
176
|
2
|
|
|
|
|
7
|
$row .= $form->invalid_tag($field->label); |
177
|
|
|
|
|
|
|
} elsif ($field->required && ! $field->static) { |
178
|
34
|
|
|
|
|
72
|
$row .= $form->required_tag($field->label); |
179
|
|
|
|
|
|
|
} else { |
180
|
129
|
|
|
|
|
251
|
$row .= $field->label; |
181
|
|
|
|
|
|
|
} |
182
|
165
|
|
|
|
|
379
|
$row .= $fcls . htmltag('/td'); |
183
|
165
|
|
|
|
|
235
|
push @html, $row; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# tag plus optional errors and/or comments |
186
|
165
|
|
|
|
|
125
|
$row = ''; |
187
|
165
|
100
|
|
|
|
517
|
if ($field->invalid) { |
188
|
2
|
|
|
|
|
6
|
$row .= ' ' . $field->message; |
189
|
|
|
|
|
|
|
} |
190
|
165
|
100
|
|
|
|
345
|
if ($field->comment) { |
191
|
11
|
50
|
|
|
|
28
|
$row .= ' ' . $field->comment unless $field->static; |
192
|
|
|
|
|
|
|
} |
193
|
165
|
|
|
|
|
382
|
$row = $field->tag . $row; |
194
|
165
|
|
|
|
|
439
|
$cl = $form->class($form->{fieldname}); |
195
|
165
|
|
|
|
|
473
|
push @html, (' ' . $form->td(id => $form->idname("_$field", $form->fieldname), |
196
|
|
|
|
|
|
|
class => $cl) . $font |
197
|
|
|
|
|
|
|
. $row . $fcls . htmltag('/td')); |
198
|
165
|
|
|
|
|
371
|
push @html, htmltag('/tr'); |
199
|
|
|
|
|
|
|
} else { |
200
|
|
|
|
|
|
|
# no table |
201
|
2
|
|
|
|
|
4
|
my $row = $font; |
202
|
2
|
50
|
33
|
|
|
8
|
if ($field->invalid) { |
|
|
50
|
|
|
|
|
|
203
|
0
|
|
|
|
|
0
|
$row .= $form->invalid_tag($field->label); |
204
|
|
|
|
|
|
|
} elsif ($field->required && ! $field->static) { |
205
|
0
|
|
|
|
|
0
|
$row .= $form->required_tag($field->label); |
206
|
|
|
|
|
|
|
} else { |
207
|
2
|
|
|
|
|
7
|
$row .= $field->label; |
208
|
|
|
|
|
|
|
} |
209
|
2
|
|
|
|
|
3
|
$row .= $fcls; |
210
|
2
|
|
|
|
|
5
|
push @html, $row; |
211
|
2
|
|
|
|
|
6
|
push @html, $field->tag; |
212
|
2
|
50
|
|
|
|
10
|
push @html, $field->message if $field->invalid; |
213
|
2
|
50
|
|
|
|
8
|
push @html, $field->comment if $field->comment; |
214
|
2
|
50
|
|
|
|
16
|
push @html, ' ' if $form->linebreaks; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Close fieldset before [Submit] if using fieldsets |
219
|
58
|
50
|
|
|
|
116
|
if ($lastset) { |
220
|
0
|
0
|
|
|
|
0
|
push @html, htmltag('/table') if $table; |
221
|
0
|
|
|
|
|
0
|
push @html, htmltag('/fieldset'); |
222
|
0
|
|
|
|
|
0
|
push @html, htmltag('/div'); |
223
|
0
|
|
|
|
|
0
|
undef $table; # avoid dup | below |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Throw buttons in a colspan |
227
|
58
|
|
|
|
|
149
|
my $buttons = $form->reset . $form->submit; |
228
|
58
|
100
|
|
|
|
168
|
if ($buttons) { |
229
|
55
|
|
|
|
|
68
|
my $row = ''; |
230
|
55
|
100
|
|
|
|
97
|
if ($table) { |
231
|
53
|
|
|
|
|
149
|
my $c = $form->class($form->{submitname}); |
232
|
53
|
100
|
|
|
|
153
|
my %a = $c ? () : (align => 'center'); |
233
|
53
|
|
|
|
|
300
|
$row .= $form->tr(id => $form->idname($form->submitname, $form->rowname)) . "\n " |
234
|
|
|
|
|
|
|
. $form->td(id => $form->idname($form->submitname, $form->fieldname), |
235
|
|
|
|
|
|
|
class => $c, colspan => 2, %a) . $font; |
236
|
|
|
|
|
|
|
} else { |
237
|
|
|
|
|
|
|
# wrap in a for fieldsets |
238
|
2
|
|
|
|
|
10
|
$row .= $form->div(id => $form->idname('_controls'), |
239
|
|
|
|
|
|
|
class => $form->class('_controls')); |
240
|
|
|
|
|
|
|
} |
241
|
55
|
|
|
|
|
107
|
$row .= $buttons; |
242
|
55
|
100
|
|
|
|
91
|
if ($table) { |
243
|
53
|
50
|
|
|
|
100
|
$row .= htmltag('/font') if $font; |
244
|
53
|
50
|
|
|
|
165
|
$row .= htmltag('/td') . "\n" . htmltag('/tr') if $table; |
245
|
|
|
|
|
|
|
} else { |
246
|
2
|
|
|
|
|
7
|
$row .= htmltag('/div'); |
247
|
|
|
|
|
|
|
} |
248
|
55
|
|
|
|
|
99
|
push @html, $row; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
# Properly nest closing tags |
252
|
58
|
100
|
|
|
|
171
|
push @html, htmltag('/table') if $table; |
253
|
58
|
|
|
|
|
118
|
push @html, htmltag('/form'); # $form->end |
254
|
58
|
50
|
33
|
|
|
132
|
push @html, htmltag('/font') if $font && defined $hd; |
255
|
58
|
100
|
|
|
|
121
|
push @html, htmltag('/body'),htmltag('/html') if defined $hd; |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
# Always return scalar since print() is a list function |
258
|
58
|
|
|
|
|
667
|
return $self->{output} = join("\n", @html) . "\n" |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub render { |
262
|
58
|
|
|
58
|
0
|
56
|
my $self = shift; |
263
|
58
|
|
|
|
|
300
|
return $self->{output}; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
1; |
267
|
|
|
|
|
|
|
__END__ |
|