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

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 157 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::radio;
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 #
45 # Get field from radio buttons or checkboxes.
46 # Must cycle through all again to see which is checked. Yeesh.
47 #
48  
49 $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 if ($self->other) {
61 my $oth = $self->othername;
62 $jsfunc .= <<EOJS;
63 if ($jsfield == '$oth') $jsfield = form.elements['$oth'].value;
64 EOJS
65 }
66  
67 $close_brace = <<EOJS;
68  
69 } // if
70 } // for $name
71 EOJS
72  
73 # required?
74 $close_brace .= <<EOJS if $self->required;
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 $in = indent($idt += 2);
84  
85 return $self->jsfield($jsfunc, $close_brace, $in);
86 }
87  
88 *render = \&tag;
89 sub tag {
90 local $^W = 0; # -w sucks
91 my $self = shift;
92 my $attr = $self->attr;
93  
94 my $jspre = $self->{_form}->jsprefix;
95  
96 my $tag = '';
97 my @value = $self->tag_value; # sticky is different in <tag>
98 my @opt = $self->options;
99 debug 2, "my(@opt) = \$field->options";
100  
101 # Add in our "Other:" option if applicable
102 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
103 if $self->other;
104  
105 debug 2, "$self->{name}: generating $attr->{type} input type";
106  
107 my $checkbox_table = 0; # toggle
108 my $checkbox_col = 0;
109 if ($self->columns > 0) {
110 $checkbox_table = 1;
111 my $c = $self->{_form}->class('_columns');
112 $tag .= $self->{_form}->table(class => $c) . "\n";
113 }
114  
115 belch "$self->{name}: No options specified for 'radio' field" unless @opt;
116 for my $opt (@opt) {
117 # Divide up checkboxes in a user-controlled manner
118 if ($checkbox_table) {
119 $tag .= " ".htmltag('tr')."\n" if $checkbox_col % $self->columns == 0;
120 $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 my($o,$n) = optval($opt);
126  
127 # Must use defined() or else labels of "0" are lost
128 unless (defined($n)) {
129 $n = $attr->{labels}{$o};
130 unless (defined($n)) {
131 $n = $self->nameopts ? toname($o) : $o;
132 }
133 }
134  
135 ismember($o, @value) ? $attr->{checked} = 'checked'
136 : delete $attr->{checked};
137  
138 # reset some attrs
139 $attr->{value} = $o;
140 if (@opt == 1) {
141 # single option checkboxes do not modify id
142 $attr->{id} ||= tovar($attr->{name});
143 } else {
144 # all others add the current option name
145 $attr->{id} = tovar($o eq $self->othername
146 ? "_$attr->{name}" : "$attr->{name}_$o");
147 }
148  
149 # Special event handling for our _other field
150 if ($self->other && $self->javascript) {
151 my $b = $self->othername; # box
152 if ($n eq $self->{_form}{messages}->form_other_default) {
153 # turn on when they click the "_other" field
154 $attr->{onclick} = "${jspre}other_on('$b')";
155 } else {
156 # turn off when they select any, well, others
157 $attr->{onclick} = "${jspre}other_off('$b')";
158 }
159 }
160  
161 # Each radio/checkbox gets a human thingy with <label> around it
162 $tag .= $self->add_before_option;
163 $tag .= htmltag('input', $attr);
164 $tag .= $checkbox_table
165 ? (htmltag('/td')."\n ".htmltag('td').$self->{_form}->font) : ' ';
166 my $c = $self->{_form}->class('_option');
167 $tag .= htmltag('label', for => $attr->{id}, class => $c)
168 . ($self->cleanopts ? escapehtml($n) : $n)
169 . htmltag('/label');
170 $tag .= $self->add_after_option;
171  
172 $tag .= '<br />' if $self->linebreaks;
173  
174 if ($checkbox_table) {
175 $checkbox_col++;
176 $tag .= htmltag('/td');
177 $tag .= "\n ".htmltag('/tr') if $checkbox_col % $self->columns == 0;
178 }
179 $tag .= "\n";
180 }
181 $tag .= ' '.htmltag('/tr') if $checkbox_table && ($checkbox_col % $self->columns > 0);
182 $tag .= ' '.htmltag('/table') if $checkbox_table;
183  
184 # add an additional tag for our _other field
185 $tag .= ' ' . $self->othertag if $self->other;
186  
187 debug 2, "$self->{name}: generated tag = $tag";
188 return $tag; # always return scalar tag
189 }
190  
191 1;
192  
193 __END__