| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | 48 | for my $opt (@opt) { | |||||||||||||||||||||||||||
| 117 | # Divide up checkboxes in a user-controlled manner | ||||||||||||||||||||||||||||
| 118 | 148 | ▶ | if ($checkbox_table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 104 | ▶ | $tag .= " ".htmltag('tr')."\n" if $checkbox_col % $self->columns == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 129 | 114 | $n = $attr->{labels}{$o}; | |||||||||||||||||||||||||||
| 130 | 114 | ▶ | unless (defined($n)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 114 | ▶ | $n = $self->nameopts ? toname($o) : $o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ismember($o, @value) ? $attr->{checked} = 'checked' | ||||||||||||||||||||||||||||
| 136 | 148 | ▶ | : delete $attr->{checked}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # reset some attrs | ||||||||||||||||||||||||||||
| 139 | 148 | $attr->{value} = $o; | |||||||||||||||||||||||||||
| 140 | 148 | ▶ | if (@opt == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | # single option checkboxes do not modify id | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $attr->{id} ||= tovar($attr->{name}); | ||||||||||||||||||||||||||
|
Condition0%0/3
$attr->{"id"} ||= &tovar($attr->{"name"})
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"})
| |||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||
| 144 | # all others add the current option name | ||||||||||||||||||||||||||||
| 145 | 148 | ▶ | $attr->{id} = tovar($o eq $self->othername | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->other and $self->javascript
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
| |||||||||||||||||||||||||||||
| 151 | 10 | my $b = $self->othername; # box | |||||||||||||||||||||||||||
| 152 | 10 | ▶ | if ($n eq $self->{_form}{messages}->form_other_default) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 166 | 148 | my $c = $self->{_form}->class('_option'); | |||||||||||||||||||||||||||
| 167 | 148 | ▶ | $tag .= htmltag('label', for => $attr->{id}, class => $c) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 148 | ▶ | if ($checkbox_table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 104 | $checkbox_col++; | |||||||||||||||||||||||||||
| 176 | 104 | $tag .= htmltag('/td'); | |||||||||||||||||||||||||||
| 177 | 104 | ▶ | $tag .= "\n ".htmltag('/tr') if $checkbox_col % $self->columns == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 148 | $tag .= "\n"; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 48 | ▶ | $tag .= ' '.htmltag('/tr') if $checkbox_table && ($checkbox_col % $self->columns > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$checkbox_table and $checkbox_col % $self->columns > 0
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
| |||||||||||||||||||||||||||||
| 182 | 48 | ▶ | $tag .= ' '.htmltag('/table') if $checkbox_table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # add an additional tag for our _other field | ||||||||||||||||||||||||||||
| 185 | 48 | ▶ | $tag .= ' ' . $self->othertag if $self->other; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||