FormBuilder/FieldSet.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 85.0% 17 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod 100% 3 / 3 totaltotal 89.0% 122 / 137 SCAR 27.8
CC
16
Cov
91%
build
10.5
_build_fieldset_foreword
4.0
__ANON__
2.1
CRAP 16.2
« FormBuilder/Field.pm ↑ Summary FormBuilder/Base.pm »
1 package HTML::FormBuilder::FieldSet;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 our $VERSION = '0.11';
7  
8 7 ▶use HTML::FormBuilder::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 extends qw(HTML::FormBuilder::Base);
15  
16 has data => (
17 is => 'ro',
18 isa => sub {
19 26 ▶ my $data = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
20 26 ▶ croak('data should be a hashref') unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 26 26
21 },
22 default => sub {
23 0 ▶ {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 },
25 7 );
26  
27 has fields => (
28 is => 'rw',
29 isa => sub {
30 26 ▶ my $fields = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
31 26 ▶ croak('fields should be an arrayref') unless ref($fields) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "ARRAY" 0 26 26
32 },
33 default => sub {
34 26 ▶ [];
Subroutine __ANON__: called CC=1 SCAR=0.0
35 },
36 7 );
37  
38 sub add_field {
39 97 ▶ my $self = shift;
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
40 97 my $_args = shift;
41  
42 97 my $field = HTML::FormBuilder::Field->new(
43 data => $_args,
44 classes => $self->classes,
45 localize => $self->localize
46 );
47 97 push @{$self->{'fields'}}, $field;
48  
49 97 return $field;
50 }
51  
52 sub add_fields {
53 1 ▶ my ($self, @field_args) = @_;
Subroutine add_fields: called CC=2 SCAR=6.9
Pod documented
54  
55 1 for my $field_arg (@field_args) {
56 2 $self->add_field($field_arg);
57 }
58 1 return scalar @field_args;
59 }
60  
61 #####################################################################
62 # Usage : generate the form content for a fieldset
63 # Purpose : check and parse the parameters and generate the form
64 # properly
65 # Returns : a piece of form HTML for a fieldset
66 # Parameters : fieldset
67 # Comments :
68 # See Also :
69 #####################################################################
70 sub build {
71 18 ▶ my $self = shift;
Subroutine build: called CC=10 SCAR=23.5
Pod documented
72  
73 18 my $data = $self->{data};
74  
75 #FIXME this attribute should be deleted, or it will emit to the html code
76 18 my $fieldset_group = $data->{'group'};
77 18 ▶ my $stacked = defined $data->{'stacked'} ? $data->{'stacked'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'stacked'} ? : 1 17 18
78  
79 18 ▶ if (not $fieldset_group) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fieldset_group) 17 1 18
80 17 $fieldset_group = 'no-group';
81 }
82  
83 18 my $fieldset_html = $self->_build_fieldset_foreword();
84  
85 18 my $input_fields_html = '';
86  
87 18 foreach my $input_field (@{$self->{'fields'}}) {
88 71 $input_fields_html .= $input_field->build({stacked => $stacked});
89 }
90  
91 18 ▶ if ($stacked == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stacked == 0) 1 17 18
92 1 $input_fields_html = $self->_build_element_and_attributes('div', {class => $self->{classes}{'no_stack_field_parent'}}, $input_fields_html);
93 }
94  
95 18 $fieldset_html .= $input_fields_html;
96  
97 # message at the bottom of the fieldset
98 18 ▶ if (defined $data->{'footer'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"footer"}) 1 17 18
99 1 my $footer = delete $data->{'footer'};
100 1 $fieldset_html .= qq{<div class="$self->{classes}{fieldset_footer}">$footer</div>};
101 }
102  
103 18 $fieldset_html = $self->_build_element_and_attributes('fieldset', $data, $fieldset_html);
104  
105 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (!$data->{"id"} || $data->{"id"} ne "formlayout" and not $data->{"class"} && $data->{"class"} =~ /no-wrap|invisible/) 18 0 18
Condition33%3/9
!$data->{"id"} || $data->{"id"} ne "formlayout" and not $data->{"class"} && $data->{"class"} =~ /no-wrap|invisible/
!ll&&!rl&&r
0018
not $data->{"id"} or $data->{"id"} ne "formlayout"
l!l&&r!l&&!r
1800
$data->{'class'} && $data->{'class'} =~ /no-wrap|invisible/
!ll&&!rl&&r
1800
MC/DC0%0/4
!$data->{"id"} || $data->{"id"} ne "formlayout" and not $data->{"class"} && $data->{"class"} =~ /no-wrap|invisible/
not $data->{"id"} $data->{"id"} ne "formlayout" $data->{'class'} $data->{'class'} =~ /no-wrap|invisible/
Truth table14%1/7
not(A) or B and not(C && D)
A= not $data->{"id"}B= $data->{"id"} ne "formlayout"C= $data->{'class'}D= $data->{'class'} =~ /no-wrap|invisible/
ABCDresult
00XX0
1X110
01110
1X0X1
1X101
010X1
01101
106 (not $data->{'id'} or $data->{'id'} ne 'formlayout')
107 and (not $data->{'class'}
108 or $data->{'class'} !~ /no-wrap|invisible/))
109 {
110 18 $fieldset_html = $self->_wrap_fieldset($fieldset_html);
111  
112 }
113 18 return ($fieldset_group, $fieldset_html);
114 }
115  
116 #####################################################################
117 # Usage : generate the form content for a fieldset foreword thing
118 # Purpose : check and parse the parameters and generate the form
119 # properly
120 # Returns : a piece of form HTML code for a fieldset foreword
121 # Parameters : input_field, stacked
122 # Comments :
123 # See Also :
124 #####################################################################
125 sub _build_fieldset_foreword {
126 18 ▶ my $self = shift;
Subroutine _build_fieldset_foreword: called CC=4 SCAR=13.9
127 18 my $data = $self->{data};
128  
129 # fieldset legend
130 18 my $legend = '';
131 18 ▶ if (defined $data->{'legend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"legend"}) 1 17 18
132 1 $legend = qq{<legend>$data->{legend}</legend>};
133 1 undef $data->{'legend'};
134 }
135  
136 # header at the top of the fieldset
137 18 my $header = '';
138 18 ▶ if (defined $data->{'header'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"header"}) 1 17 18
139 1 $header = qq{<h2>$data->{header}</h2>};
140 1 undef $data->{'header'};
141 }
142  
143 # message at the top of the fieldset
144 18 my $comment = '';
145 18 ▶ if (defined $data->{'comment'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"comment"}) 1 17 18
146 1 $comment = qq{<div class="$self->{classes}{comment}"><p>$data->{comment}</p></div>};
147 1 undef $data->{'comment'};
148 }
149  
150 18 return $legend . $header . $comment;
151 }
152  
153 #####################################################################
154 # Usage : $self->_wrap_fieldset($fieldset_html)
155 # Purpose : wrap fieldset html by template
156 # Returns : HTML
157 # Comments :
158 # See Also :
159 #####################################################################
160 sub _wrap_fieldset {
161 18 ▶ my ($self, $fieldset_html) = @_;
Subroutine _wrap_fieldset: called CC=1 SCAR=0.0
162 18 my $output = '';
163 18 my $fieldset_template = <<EOF;
164 <div class="rbox form">
165 <div class="rbox-wrap">
166 $fieldset_html
167 <span class="tl">&nbsp;</span><span class="tr">&nbsp;</span><span class="bl">&nbsp;</span><span class="br">&nbsp;</span>
168 </div>
169 </div>
170 EOF
171  
172 18 return $fieldset_template;
173 }
174  
175 7 1;
176  
177 =head1 NAME
178  
179 HTML::FormBuilder::FieldSet - FieldSet container used by HTML::FormBuilder
180  
181 =head1 SYNOPSIS
182  
183 my $form = HTML::FormBuilder->new(data => {id => 'testform});
184  
185 my $fieldset = $form->add_fieldset({id => 'fieldset1'});
186  
187 $fieldset->add_field({input => {type => 'text', value => 'Join'}});
188  
189 $form->add_field($fieldset_index, {input => {type => 'text', value => 'Join'}});
190  
191 =head1 Attributes
192  
193 =head2 fields
194  
195 The fields included by this fieldset.
196  
197 =head1 Methods
198  
199 =head2 build
200  
201 my ($fieldset_group, $fieldset_html) = $fieldset->build();
202  
203 =head2 add_field
204  
205 $fieldset->add_field({input => {type => 'text', value => 'name'}});
206  
207 append the field into fieldset and return that field
208  
209 =head2 add_fields
210  
211 $fieldset->add_fields({input => {type => 'text', value => 'name'}},{input => {type => 'text', value => 'address'}});
212  
213 append fields into fieldset and return the number of fields added.
214  
215 =head1 AUTHOR
216  
217 Chylli L<chylli@binary.com>
218  
219 =head1 CONTRIBUTOR
220  
221 Fayland Lam L<fayland@binary.com>
222  
223 Tee Shuwn Yuan L<shuwnyuan@binary.com>
224  
225 =head1 COPYRIGHT AND LICENSE
226  
227 =cut
« FormBuilder/Field.pm ↑ Summary FormBuilder/Base.pm »