lib/CGI/FormBuilder/Field/radio.pm

filter: Statementstmt 98.8% 89 / 90 Branchbran 89.1% 41 / 46 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 90.6% 146 / 161 SCAR 33.4
CC
28
Cov
93%
tag
26.6
script
3.0
BEGIN
1.0
CRAP 28.3
« lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary blib/lib/CGI/FormBuilder/Template/CGI_SSI.pm »
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 # The majority of this module's methods (including new) are
8 # inherited directly from ::base, since they involve things
9 # which are common, such as parameter parsing. The only methods
10 # that are individual to different fields are those that affect
11 # the rendering, such as script() and tag()
12  
13 package CGI::FormBuilder::Field::radio;
14  
15 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use CGI::FormBuilder::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use base 'CGI::FormBuilder::Field';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 6 our $VERSION = '3.10';
25  
26 sub script {
27 46 ▶ my $self = shift;
Subroutine script: called CC=3 SCAR=11.0
Pod undocumented
28 46 my $name = $self->name;
29  
30 # The way script() works is slightly backwards: First the
31 # type-specific JS DOM code is generated, then this is
32 # passed as a string to Field->jsfield, which wraps this
33 # in the generic handling.
34  
35 # Holders for different parts of JS code
36 46 my $jsfunc = '';
37 46 my $jsfield = tovar($name);
38 46 my $close_brace = '';
39 46 my $in = indent(my $idt = 1); # indent
40  
41 46 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 46 $alertstr .= '\n';
43  
44 #
45 # Get field from radio buttons or checkboxes.
46 # Must cycle through all again to see which is checked. Yeesh.
47 #
48  
49 46 $jsfunc .= <<EOJS;
50 // $name: radio group or multiple checkboxes
51 var $jsfield = null;
52 var selected_$jsfield = 0;
53 for (var loop = 0; loop < form.elements['$name'].length; loop++) {
54 if (form.elements['$name']\[loop].checked) {
55 $jsfield = form.elements['$name']\[loop].value;
56 selected_$jsfield++;
57 EOJS
58  
59 # Add catch for "other" if applicable
60 46 ▶ if ($self->other) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other) 2 44 46
61 2 my $oth = $self->othername;
62 2 $jsfunc .= <<EOJS;
63 if ($jsfield == '$oth') $jsfield = form.elements['$oth'].value;
64 EOJS
65 }
66  
67 46 $close_brace = <<EOJS;
68  
69 } // if
70 } // for $name
71 EOJS
72  
73 # required?
74 46 ▶ $close_brace .= <<EOJS if $self->required;
Branch: 1 branch
BranchTrueFalseTotal
if $self->required 8 38 46
75 if (! selected_$jsfield) {
76 alertstr += '$alertstr';
77 invalid++;
78 invalid_fields.push('$jsfield');
79 }
80 EOJS
81  
82 # indent the very last if/else tests so they're in the for loop
83 46 $in = indent($idt += 2);
84  
85 46 return $self->jsfield($jsfunc, $close_brace, $in);
86 }
87  
88 6 *render = \&tag;
89 sub tag {
90 48 ▶ local $^W = 0; # -w sucks
Subroutine tag: called CC=26 SCAR=32.8
Pod documented
91 48 my $self = shift;
92 48 my $attr = $self->attr;
93  
94 48 my $jspre = $self->{_form}->jsprefix;
95  
96 48 my $tag = '';
97 48 my @value = $self->tag_value; # sticky is different in <tag>
98 48 my @opt = $self->options;
99 48 debug 2, "my(@opt) = \$field->options";
100  
101 # Add in our "Other:" option if applicable
102 48 ▶ push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 4 44 48
103 if $self->other;
104  
105 48 debug 2, "$self->{name}: generating $attr->{type} input type";
106  
107 48 my $checkbox_table = 0; # toggle
108 48 my $checkbox_col = 0;
109 48 ▶ if ($self->columns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->columns > 0) 32 16 48
110 32 $checkbox_table = 1;
111 32 my $c = $self->{_form}->class('_columns');
112 32 $tag .= $self->{_form}->table(class => $c) . "\n";
113 }
114  
115 48 ▶ belch "$self->{name}: No options specified for 'radio' field" unless @opt;
Branch: 1 branch
BranchTrueFalseTotal
unless @opt 0 48 48
116 48 for my $opt (@opt) {
117 # Divide up checkboxes in a user-controlled manner
118 148 ▶ if ($checkbox_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkbox_table) 104 44 148
119 104 ▶ $tag .= " ".htmltag('tr')."\n" if $checkbox_col % $self->columns == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $checkbox_col % $self->columns == 0 88 16 104
120 104 $tag .= ' '.htmltag('td') . $self->{_form}->font;
121 }
122 # Since our data structure is a series of ['',''] things,
123 # we get the name from that. If not, then it's a list
124 # of regular old data that we toname() if nameopts => 1
125 148 my($o,$n) = optval($opt);
126  
127 # Must use defined() or else labels of "0" are lost
128 148 ▶ unless (defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 114 34 148
129 114 $n = $attr->{labels}{$o};
130 114 ▶ unless (defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 114 0 114
131 114 ▶ $n = $self->nameopts ? toname($o) : $o;
Branch: 1 branch
BranchTrueFalseTotal
$self->nameopts ? : 4 110 114
132 }
133 }
134  
135 ismember($o, @value) ? $attr->{checked} = 'checked'
136 148 ▶ : delete $attr->{checked};
Branch: 1 branch
BranchTrueFalseTotal
&ismember($o, @value) ? : 14 134 148
137  
138 # reset some attrs
139 148 $attr->{value} = $o;
140 148 ▶ if (@opt == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opt == 1) { } 0 148 148
141 # single option checkboxes do not modify id
142 0 ▶ $attr->{id} ||= tovar($attr->{name});
Condition0%0/3
$attr->{"id"} ||= &tovar($attr->{"name"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{"id"} ||= &tovar($attr->{"name"})
$attr->{"id"} &tovar($attr->{"name"})
Truth table0%0/3
A ||= B
A= $attr->{"id"}B= &tovar($attr->{"name"})
ABresult
1X1
011
000
143 } else {
144 # all others add the current option name
145 148 ▶ $attr->{id} = tovar($o eq $self->othername
Branch: 1 branch
BranchTrueFalseTotal
$o eq $self->othername ? : 4 144 148
146 ? "_$attr->{name}" : "$attr->{name}_$o");
147 }
148  
149 # Special event handling for our _other field
150 148 ▶ if ($self->other && $self->javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other and $self->javascript) 10 138 148
Condition100%3/3
$self->other and $self->javascript
!ll&&!rl&&r
1281010
MC/DC100%2/2
$self->other and $self->javascript
$self->other $self->javascript
Truth table100%3/3
A and B
A= $self->otherB= $self->javascript
ABresult
0X0
100
111
151 10 my $b = $self->othername; # box
152 10 ▶ if ($n eq $self->{_form}{messages}->form_other_default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq $self->{'_form'}{'messages'}->form_other_default) { } 2 8 10
153 # turn on when they click the "_other" field
154 2 $attr->{onclick} = "${jspre}other_on('$b')";
155 } else {
156 # turn off when they select any, well, others
157 8 $attr->{onclick} = "${jspre}other_off('$b')";
158 }
159 }
160  
161 # Each radio/checkbox gets a human thingy with <label> around it
162 148 $tag .= $self->add_before_option;
163 148 $tag .= htmltag('input', $attr);
164 $tag .= $checkbox_table
165 148 ▶ ? (htmltag('/td')."\n ".htmltag('td').$self->{_form}->font) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$checkbox_table ? : 104 44 148
166 148 my $c = $self->{_form}->class('_option');
167 148 ▶ $tag .= htmltag('label', for => $attr->{id}, class => $c)
Branch: 1 branch
BranchTrueFalseTotal
$self->cleanopts ? : 144 4 148
168 . ($self->cleanopts ? escapehtml($n) : $n)
169 . htmltag('/label');
170 148 $tag .= $self->add_after_option;
171  
172 148 ▶ $tag .= '<br />' if $self->linebreaks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->linebreaks 0 148 148
173  
174 148 ▶ if ($checkbox_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checkbox_table) 104 44 148
175 104 $checkbox_col++;
176 104 $tag .= htmltag('/td');
177 104 ▶ $tag .= "\n ".htmltag('/tr') if $checkbox_col % $self->columns == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $checkbox_col % $self->columns == 0 88 16 104
178 }
179 148 $tag .= "\n";
180 }
181 48 ▶ $tag .= ' '.htmltag('/tr') if $checkbox_table && ($checkbox_col % $self->columns > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $checkbox_table and $checkbox_col % $self->columns > 0 0 48 48
Condition66%2/3
$checkbox_table and $checkbox_col % $self->columns > 0
!ll&&!rl&&r
16320
MC/DC0%0/2
$checkbox_table and $checkbox_col % $self->columns > 0
$checkbox_table $checkbox_col % $self->columns > 0
Truth table66%2/3
A and B
A= $checkbox_tableB= $checkbox_col % $self->columns > 0
ABresult
0X0
100
111
182 48 ▶ $tag .= ' '.htmltag('/table') if $checkbox_table;
Branch: 1 branch
BranchTrueFalseTotal
if $checkbox_table 32 16 48
183  
184 # add an additional tag for our _other field
185 48 ▶ $tag .= ' ' . $self->othertag if $self->other;
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 4 44 48
186  
187 48 debug 2, "$self->{name}: generated tag = $tag";
188 48 return $tag; # always return scalar tag
189 }
190  
191 6 1;
192  
193 __END__
« lib/CGI/FormBuilder/Field/checkbox.pm ↑ Summary blib/lib/CGI/FormBuilder/Template/CGI_SSI.pm »