File Coverage

lib/CGI/FormBuilder/Template/Div.pm
Criterion Covered Total %
statement 103 126 81.7
branch 37 80 46.2
condition 8 24 33.3
subroutine 8 8 100.0
pod 0 3 0.0
total 156 241 64.7


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