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

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 247 SCAR 74.5
CC
41
Cov
0%
prepare
1640.0
new
6.0
render
2.0
CRAP 1722.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::Div;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Template::Div - Div 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 # Begin form
77 my $txt = $form->text;
78 push @html, $txt if $txt;
79 push @html, $form->start;
80  
81 # Put id's around state tags if they exist
82 if (my $st = $form->statetags) {
83 push @html,
84 $form->div(id => $form->idname($form->statename),
85 class => $form->class($form->statename)) .
86 $st . htmltag('/div');
87 }
88 if (my $ke = $form->keepextras) {
89 push @html,
90 $form->div(id => $form->idname($form->extraname),
91 class => $form->class($form->extraname)) .
92 $ke . htmltag('/div');
93 }
94  
95 # Render hidden fields first
96 my @unhidden;
97 for my $field ($form->fieldlist) {
98 push(@unhidden, $field), next if $field->type ne 'hidden';
99 push @html, $field->tag; # no label/etc for hidden fields
100 }
101  
102 my $div = $form->div(id => $form->idname($form->bodyname), class => $form->class);
103 my $tabn = 1;
104 push @html, $div if $div;
105  
106 # Support fieldset => 'name' to organize by fieldset on the fly
107 my $legend = $form->fieldsets;
108  
109 # Render regular fields in <div> for CSS happiness
110 my $lastset;
111 for my $field (@unhidden) {
112 if (my $set = $field->fieldset) {
113 # hooks (hack?) for fieldsets
114 if ($set ne $lastset) {
115 # close any open divs/fieldsets
116 if ($lastset) {
117 push @html, htmltag('/fieldset');
118 push @html, htmltag('/div');
119 } else {
120 # Catch in case we have an empty div - ie the previous
121 # element is just <div>. This workaround is needed
122 # in case the user wants to mix fields with/without
123 # fieldsets in the same form
124 if ($html[-1] =~ /^<div\b/) {
125 pop @html;
126 } else {
127 # close non-fieldset div
128 push @html, htmltag('/div');
129 }
130 }
131  
132 # wrap fieldset in a <div> to allow jquery #tabs
133 push @html, $form->div(id => $form->idname($form->tabname.$tabn++),
134 class => $form->class($form->tabname));
135  
136 (my $sn = lc $set) =~ s/\W+/_/g;
137 push @html, htmltag('fieldset', id => $form->idname("_$sn"),
138 class => $form->class('_set'));
139 push @html, htmltag('legend') . ($legend->{$set}||$set) . htmltag('/legend')
140 if defined $legend->{$set};
141  
142 $lastset = $set;
143 }
144 } elsif ($lastset) {
145 # ended <fieldset> defs before form has ended
146 # remaining fields are not in a fieldset
147 push @html, htmltag('/div') if $div;
148 push @html, htmltag('/fieldset');
149 push @html, $div;
150 undef $lastset; # avoid dup </fieldset> below
151 }
152  
153 debug 2, "render: attacking normal field '$field'";
154 next if $field->static > 1 && ! $field->tag_value; # skip missing static vals
155  
156 #
157 # Since this was cut-and-pasted from ::Div, the variables
158 # are all named for <table> elements. But, the purpose is
159 # the same: rows, labels, etc.
160 #
161 push @html, $form->div(id => $form->idname("_$field", $form->rowname));
162  
163 my $cl = $form->class($form->{labelname});
164 my $row = ' ' . $form->div(id => $form->idname("_$field", $form->labelname),
165 class => $cl) . $font;
166 if ($field->invalid) {
167 $row .= $form->invalid_tag($field->label);
168 } elsif ($field->required && ! $field->static) {
169 $row .= $form->required_tag($field->label);
170 } else {
171 $row .= $field->label;
172 }
173 $row .= $fcls . htmltag('/div');
174 push @html, $row;
175  
176 # tag plus optional errors and/or comments
177 $row = '';
178 if ($field->invalid) {
179 $row .= ' ' . $field->message;
180 }
181 if ($field->comment) {
182 $row .= ' ' . $field->comment unless $field->static;
183 }
184 $row = $field->tag . $row;
185 $cl = $form->class($form->{fieldname});
186 push @html, (' ' . $form->div(id => $form->idname("_$field", $form->fieldname),
187 class => $cl) . $font
188 . $row . $fcls . htmltag('/div'));
189 push @html, htmltag('/div');
190 }
191  
192 # Close fieldset before [Submit] if using fieldsets
193 if ($lastset) {
194 push @html, htmltag('/div');
195 push @html, htmltag('/fieldset');
196 undef $div; # avoid dup </div> below
197 }
198 push @html, htmltag('/div') if $div; # fields
199  
200 # Throw buttons in a row
201 my $reset = $form->reset;
202 my $slist = $form->submits; # arrayref
203 push @html, $form->div(id => $form->idname('_controls'), class => $form->class('_controls'))
204 if $reset || $slist;
205 if ($reset) {
206 my $row = '';
207 my $c = $form->class($form->resetname);
208 my %a = $c ? () : (align => 'center');
209 $row .= $form->div(id => $form->idname($form->resetname, $form->rowname)) . "\n "
210 . $form->div(class => $c, %a) . $font;
211 $row .= $reset;
212 $row .= htmltag('/font') if $font;
213 $row .= htmltag('/div') . "\n" . htmltag('/div');
214 push @html, $row;
215 }
216 if (@$slist) {
217 my $row = '';
218 my $c = $form->class($form->submitname);
219 my %a = $c ? () : (align => 'center');
220 $row .= $form->div(id => $form->idname($form->submitname, $form->rowname)) . "\n";
221 for my $button (@$slist) {
222 $row .= ' '
223 . $form->div(class => $c, %a) . $font . $button;
224 $row .= htmltag('/font') if $font;
225 $row .= htmltag('/div') . "\n";
226 }
227 $row .= htmltag('/div');
228 push @html, $row;
229 }
230 push @html, htmltag('/div') if $reset || $slist;
231  
232 # Properly nest closing tags
233 push @html, htmltag('/form'); # $form->end
234 push @html, htmltag('/font') if $font && defined $hd;
235 push @html, htmltag('/body'),htmltag('/html') if defined $hd;
236  
237 # Always return scalar since print() is a list function
238 return $self->{output} = join("\n", @html) . "\n"
239 }
240  
241 sub render {
242 my $self = shift;
243 return $self->{output};
244 }
245  
246 1;
247 __END__