lib/CGI/FormBuilder/Field/select.pm

filter: Statementstmt 98.9% 92 / 93 Branchbran 85.4% 41 / 48 Conditioncond 92.5% 25 / 27 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 91.6% 176 / 192 SCAR 35.3
CC
34
Cov
94%
tag
32.5
script
3.0
BEGIN
1.0
CRAP 34.2
« lib/CGI/FormBuilder/Field/text.pm ↑ Summary lib/CGI/FormBuilder/Field/checkbox.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::select;
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use CGI::FormBuilder::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use base 'CGI::FormBuilder::Field';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 3 our $VERSION = '3.10';
25  
26 sub script {
27 43 ▶ my $self = shift;
Subroutine script: called CC=3 SCAR=11.0
Pod undocumented
28 43 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 43 my $jsfunc = '';
37 43 my $jsfield = tovar($name);
38 43 my $close_brace = '';
39 43 my $in = indent(my $idt = 1); # indent
40  
41 43 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 43 $alertstr .= '\n';
43  
44 # Get value for field from select list
45 # Always assume it's multiple to guarantee we get all values
46 43 $jsfunc .= <<EOJS;
47 // $name: select list, always assume it's multiple to get all values
48 var $jsfield = null;
49 var selected_$jsfield = 0;
50 for (var loop = 0; loop < form.elements['$name'].options.length; loop++) {
51 if (form.elements['$name'].options[loop].selected) {
52 $jsfield = form.elements['$name'].options[loop].value;
53 selected_$jsfield++;
54 EOJS
55  
56 # Add catch for "other" if applicable
57 43 ▶ if ($self->other) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other) 3 40 43
58 3 my $oth = $self->othername;
59 3 $jsfunc .= <<EOJS;
60 if ($jsfield == '$oth') $jsfield = form.elements['$oth'].value;
61 EOJS
62 }
63  
64 43 $close_brace = <<EOJS;
65  
66 } // if
67 } // for $name
68 EOJS
69  
70 43 ▶ $close_brace .= <<EOJS if $self->required;
Branch: 1 branch
BranchTrueFalseTotal
if $self->required 4 39 43
71 if (! selected_$jsfield) {
72 alertstr += '$alertstr';
73 invalid++;
74 }
75 EOJS
76  
77 # indent the very last if/else tests so they're in the for loop
78 43 $in = indent($idt += 2);
79  
80 43 return $self->jsfield($jsfunc, $close_brace, $in);
81 }
82  
83 3 *render = \&tag;
84 sub tag {
85 47 ▶ local $^W = 0; # -w sucks
Subroutine tag: called CC=32 SCAR=34.8
Pod documented
86 47 my $self = shift;
87 47 my $attr = $self->attr;
88  
89 47 my $jspre = $self->{_form}->jsprefix;
90  
91 47 my $tag = '';
92 47 my @value = $self->tag_value; # sticky is different in <tag>
93 47 my @opt = $self->options;
94 47 debug 2, "my(@opt) = \$field->options";
95  
96 # Add in our "Other:" option if applicable
97 47 ▶ push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 3 44 47
98 if $self->other;
99  
100 47 debug 2, "$self->{name}: generating $attr->{type} input type";
101  
102 # First the top-level select
103 47 delete $attr->{type}; # type="select" invalid
104 $self->multiple ? $attr->{multiple} = 'multiple'
105 47 ▶ : delete $attr->{multiple};
Branch: 1 branch
BranchTrueFalseTotal
$self->multiple ? : 16 31 47
106  
107 47 ▶ belch "$self->{name}: No options specified for 'select' field" unless @opt;
Branch: 1 branch
BranchTrueFalseTotal
unless @opt 0 47 47
108  
109 # Prefix options with "-select-", unless selectname => 0
110 47 ▶ if ($self->{_form}->smartness && ! $attr->{multiple} # set above
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_form"}->smartness and not $attr->{"multiple"} and $self->selectname ne 0) 29 18 47
Condition83%5/6
$self->{"_form"}->smartness and not $attr->{"multiple"} and $self->selectname ne 0
!ll&&!rl&&r
16229
$self->{"_form"}->smartness and not $attr->{"multiple"}
!ll&&!rl&&r
01631
MC/DC66%2/3
$self->{"_form"}->smartness and not $attr->{"multiple"} and $self->selectname ne 0
$self->{"_form"}->smartness not $attr->{"multiple"} $self->selectname ne 0
Truth table75%3/4
A and not(B) and C
A= $self->{"_form"}->smartnessB= not $attr->{"multiple"}C= $self->selectname ne 0
ABCresult
0XX0
10X0
1100
1111
111 && $self->selectname ne 0)
112 {
113 # Use selectname if => "choose" or messages otherwise
114 my $name = $self->selectname =~ /\D+/
115 ? $self->selectname
116 29 ▶ : $self->{_form}{messages}->form_select_default;
Branch: 1 branch
BranchTrueFalseTotal
$self->selectname =~ /\D+/ ? : 2 27 29
117 29 unshift @opt, ['', $name]
118 }
119  
120 # Special event handling for our _other field
121 47 ▶ if ($self->other && $self->javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->other and $self->javascript) 3 44 47
Condition66%2/3
$self->other and $self->javascript
!ll&&!rl&&r
4403
MC/DC50%1/2
$self->other and $self->javascript
$self->other $self->javascript
Truth table66%2/3
A and B
A= $self->otherB= $self->javascript
ABresult
0X0
100
111
122 3 my $b = $self->othername; # box
123 # w/o newlines
124 3 $attr->{onchange} .= "if (this.selectedIndex + 1 == this.options.length) { "
125 . "${jspre}other_on('$b') } else { ${jspre}other_off('$b') }";
126 }
127  
128 # render <select> tag
129 47 $tag .= htmltag('select', $attr) . "\n";
130  
131 # Stuff for optgroups
132 47 my $optgroups = $self->optgroups;
133 47 my $lastgroup = '';
134 47 my $didgroup = 0;
135 47 my $foundit = 0; # found option in list? (for "Other:")
136 47 debug 2, "$self->{name}: rendering options: (@opt)";
137 47 ▶ while (defined(my $opt = shift @opt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $opt = shift @opt)) 449 47 496
138 # Since our data structure is a series of ['',''] things,
139 # we get the name from that. If not, then it's a list
140 # of regular old data that we toname() if nameopts => 1
141 449 my($o,$n,$g) = optval($opt);
142 449 debug 2, "optval($opt) = ($o,$n,$g)";
143  
144 # Must use defined() or else labels of "0" are lost
145 449 ▶ unless (defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 307 142 449
146 307 $n = $attr->{labels}{$o};
147 307 ▶ unless (defined($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 307 0 307
148 307 ▶ $n = $self->nameopts ? toname($o) : $o;
Branch: 1 branch
BranchTrueFalseTotal
$self->nameopts ? : 12 295 307
149 }
150 }
151  
152 # If we asked for optgroups => 1, then we add an our
153 # <optgroup> each time our $lastgroup changes
154 449 ▶ if ($optgroups) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optgroups) 48 401 449
155 48 ▶ if ($g && $g ne $lastgroup) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g and $g ne $lastgroup) { } 10 38 48
elsif (not $g and $lastgroup) { } 10 46 56
Condition100%3/3
$g and $g ne $lastgroup
!ll&&!rl&&r
201810
MC/DC100%2/2
$g and $g ne $lastgroup
$g $g ne $lastgroup
Truth table100%3/3
A and B
A= $gB= $g ne $lastgroup
ABresult
0X0
100
111
156 # close previous optgroup and start a new one
157 10 ▶ $tag .= " </optgroup>\n" if $didgroup;
Branch: 1 branch
BranchTrueFalseTotal
if $didgroup 4 6 10
158 10 $lastgroup = $g;
159 10 ▶ if (UNIVERSAL::isa($optgroups, 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($optgroups, 'HASH')) { } 10 0 10
elsif ($self->nameopts) { } 0 0 0
160 # lookup by name
161 10 ▶ $g = exists $optgroups->{$g} ? $optgroups->{$g} : $g;
Branch: 1 branch
BranchTrueFalseTotal
exists $optgroups->{$g} ? : 4 6 10
162 } elsif ($self->nameopts) {
163 0 $g = toname($g);
164 }
165 10 $tag .= ' ' . htmltag('optgroup', label => $g) . "\n";
166 10 $didgroup++;
167 ▶ } elsif (!$g && $lastgroup) {
Condition100%3/3
not $g and $lastgroup
!ll&&!rl&&r
181010
MC/DC100%2/2
not $g and $lastgroup
not $g $lastgroup
Truth table100%3/3
not(A) and B
A= not $gB= $lastgroup
ABresult
0X0
100
111
168 # finished an optgroup but next option is not in one
169 10 ▶ $tag .= " </optgroup>\n" if $didgroup;
Branch: 1 branch
BranchTrueFalseTotal
if $didgroup 6 4 10
170 10 $didgroup = 0; # reset counter
171 }
172 }
173  
174 449 my %slct;
175 449 ▶ if (ismember($o, @value) ||
Branch: 1 branch
BranchTrueFalseTotal
if (&ismember($o, @value) or not $foundit and $self->other and @value and not @opt) 65 384 449
Condition100%12/12
&ismember($o, @value) or not $foundit and $self->other and @value and not @opt
l!l&&r!l&&!r
641384
not $foundit and $self->other and @value and not @opt
!ll&&!rl&&r
37861
not $foundit and $self->other and @value
!ll&&!rl&&r
364147
not $foundit and $self->other
!ll&&!rl&&r
5630821
MC/DC40%2/5
&ismember($o, @value) or not $foundit and $self->other and @value and not @opt
&ismember($o, @value) not $foundit $self->other @value not @opt
Truth table50%3/6
A or not(B) and C and D and not(E)
A= &ismember($o, @value)B= not $founditC= $self->otherD= @valueE= not @opt
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
176 (! $foundit && $self->other && @value && ! @opt))
177 {
178 65 debug 2, "$self->{name}: found $o as member of (@value), setting 'selected'";
179 65 %slct = (selected => 'selected');
180 65 $foundit++;
181 }
182 449 $slct{value} = $o;
183  
184 449 debug 2, "$self->{name}: tag .= option $n";
185 449 ▶ $tag .= ' '
Branch: 1 branch
BranchTrueFalseTotal
$self->cleanopts ? : 449 0 449
186 . htmltag('option', %slct)
187 . ($self->cleanopts ? escapehtml($n) : $n)
188 . "</option>\n";
189  
190 }
191 47 ▶ $tag .= " </optgroup>\n" if $didgroup;
Branch: 1 branch
BranchTrueFalseTotal
if $didgroup 0 47 47
192 47 $tag .= ' </select>';
193  
194 # add an additional tag for our _other field
195 47 ▶ $tag .= ' ' . $self->othertag if $self->other;
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 3 44 47
196  
197 47 debug 2, "$self->{name}: generated tag = $tag";
198 47 return $tag; # always return scalar tag
199 }
200  
201 3 1;
202  
203 __END__
« lib/CGI/FormBuilder/Field/text.pm ↑ Summary lib/CGI/FormBuilder/Field/checkbox.pm »