blib/lib/CGI/FormBuilder/Field/select.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 180 SCAR 64.0
CC
24
Cov
0%
tag
506.0
script
12.0
BEGIN
2.0
CRAP 600.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 # 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 use strict;
16 use warnings;
17 no warnings 'uninitialized';
18  
19 use CGI::FormBuilder::Util;
20 use CGI::FormBuilder::Field;
21 use base 'CGI::FormBuilder::Field';
22  
23  
24 our $VERSION = '3.10';
25  
26 sub script {
27 my $self = shift;
28 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 my $jsfunc = '';
37 my $jsfield = tovar($name);
38 my $close_brace = '';
39 my $in = indent(my $idt = 1); # indent
40  
41 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 $alertstr .= '\n';
43  
44 # Get value for field from select list
45 # Always assume it's multiple to guarantee we get all values
46 $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 if ($self->other) {
58 my $oth = $self->othername;
59 $jsfunc .= <<EOJS;
60 if ($jsfield == '$oth') $jsfield = form.elements['$oth'].value;
61 EOJS
62 }
63  
64 $close_brace = <<EOJS;
65  
66 } // if
67 } // for $name
68 EOJS
69  
70 $close_brace .= <<EOJS if $self->required;
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 $in = indent($idt += 2);
79  
80 return $self->jsfield($jsfunc, $close_brace, $in);
81 }
82  
83 *render = \&tag;
84 sub tag {
85 local $^W = 0; # -w sucks
86 my $self = shift;
87 my $attr = $self->attr;
88  
89 my $jspre = $self->{_form}->jsprefix;
90  
91 my $tag = '';
92 my @value = $self->tag_value; # sticky is different in <tag>
93 my @opt = $self->options;
94 debug 2, "my(@opt) = \$field->options";
95  
96 # Add in our "Other:" option if applicable
97 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
98 if $self->other;
99  
100 debug 2, "$self->{name}: generating $attr->{type} input type";
101  
102 # First the top-level select
103 delete $attr->{type}; # type="select" invalid
104 $self->multiple ? $attr->{multiple} = 'multiple'
105 : delete $attr->{multiple};
106  
107 belch "$self->{name}: No options specified for 'select' field" unless @opt;
108  
109 # Prefix options with "-select-", unless selectname => 0
110 if ($self->{_form}->smartness && ! $attr->{multiple} # set above
111 && $self->selectname ne 0)
112 {
113 # Use selectname if => "choose" or messages otherwise
114 my $name = $self->selectname =~ /\D+/
115 ? $self->selectname
116 : $self->{_form}{messages}->form_select_default;
117 unshift @opt, ['', $name]
118 }
119  
120 # Special event handling for our _other field
121 if ($self->other && $self->javascript) {
122 my $b = $self->othername; # box
123 # w/o newlines
124 $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 $tag .= htmltag('select', $attr) . "\n";
130  
131 # Stuff for optgroups
132 my $optgroups = $self->optgroups;
133 my $lastgroup = '';
134 my $didgroup = 0;
135 my $foundit = 0; # found option in list? (for "Other:")
136 debug 2, "$self->{name}: rendering options: (@opt)";
137 while (defined(my $opt = shift @opt)) {
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 my($o,$n,$g) = optval($opt);
142 debug 2, "optval($opt) = ($o,$n,$g)";
143  
144 # Must use defined() or else labels of "0" are lost
145 unless (defined($n)) {
146 $n = $attr->{labels}{$o};
147 unless (defined($n)) {
148 $n = $self->nameopts ? toname($o) : $o;
149 }
150 }
151  
152 # If we asked for optgroups => 1, then we add an our
153 # <optgroup> each time our $lastgroup changes
154 if ($optgroups) {
155 if ($g && $g ne $lastgroup) {
156 # close previous optgroup and start a new one
157 $tag .= " </optgroup>\n" if $didgroup;
158 $lastgroup = $g;
159 if (UNIVERSAL::isa($optgroups, 'HASH')) {
160 # lookup by name
161 $g = exists $optgroups->{$g} ? $optgroups->{$g} : $g;
162 } elsif ($self->nameopts) {
163 $g = toname($g);
164 }
165 $tag .= ' ' . htmltag('optgroup', label => $g) . "\n";
166 $didgroup++;
167 } elsif (!$g && $lastgroup) {
168 # finished an optgroup but next option is not in one
169 $tag .= " </optgroup>\n" if $didgroup;
170 $didgroup = 0; # reset counter
171 }
172 }
173  
174 my %slct;
175 if (ismember($o, @value) ||
176 (! $foundit && $self->other && @value && ! @opt))
177 {
178 debug 2, "$self->{name}: found $o as member of (@value), setting 'selected'";
179 %slct = (selected => 'selected');
180 $foundit++;
181 }
182 $slct{value} = $o;
183  
184 debug 2, "$self->{name}: tag .= option $n";
185 $tag .= ' '
186 . htmltag('option', %slct)
187 . ($self->cleanopts ? escapehtml($n) : $n)
188 . "</option>\n";
189  
190 }
191 $tag .= " </optgroup>\n" if $didgroup;
192 $tag .= ' </select>';
193  
194 # add an additional tag for our _other field
195 $tag .= ' ' . $self->othertag if $self->other;
196  
197 debug 2, "$self->{name}: generated tag = $tag";
198 return $tag; # always return scalar tag
199 }
200  
201 1;
202  
203 __END__