blib/lib/CGI/FormBuilder/Template/Builtin.pm untested

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