| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 2 |  |  | 2 |  | 31540 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 79 |  | 
| 2 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 113 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package XHTML::Instrumented::Form; | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.09'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 1167 | use XHTML::Instrumented::Form::Control; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 48 |  | 
| 9 | 2 |  |  | 2 |  | 1169 | use XHTML::Instrumented::Form::Select; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 69 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 2 |  |  | 2 |  | 11 | use Params::Validate qw( validate SCALAR BOOLEAN HASHREF OBJECT ARRAYREF ); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 147 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 2 |  |  | 2 |  | 10 | use Carp qw (croak carp); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 3664 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | XHTML::Instramented::Form - XHTML::Instramented Form Object | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | my $template = XHTML::Instrumented->new(name => 'bob'); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | my $form = $template->get_form(name => 'myform'); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 API | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =head2 Constructor | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =over | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =item new | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =back | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =cut | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub new | 
| 38 |  |  |  |  |  |  | { | 
| 39 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 0 |  |  |  |  |  | my %p = validate( | 
| 42 |  |  |  |  |  |  | @_, { | 
| 43 |  |  |  |  |  |  | action => 0, | 
| 44 |  |  |  |  |  |  | id => 0, | 
| 45 |  |  |  |  |  |  | required => 0, | 
| 46 |  |  |  |  |  |  | name => 0, | 
| 47 |  |  |  |  |  |  | method => 0, | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | ); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 |  |  |  |  |  | bless { %p }, $class; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub add_params | 
| 55 |  |  |  |  |  |  | { | 
| 56 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | $self->{params} = { @_ }; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | for my $name (keys %{$self->{params}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  |  | my $element = $self->get_element($name); | 
| 62 | 0 | 0 |  |  |  |  | next unless $element; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | $element->set_value($self->{params}{$name}); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub add_defaults | 
| 69 |  |  |  |  |  |  | { | 
| 70 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | $self->{defaults} = { @_ }; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  |  | for my $name (keys %{$self->{defaults}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | my $element = $self->get_element($name); | 
| 76 | 0 | 0 |  |  |  |  | if ($element) { | 
| 77 | 0 |  |  |  |  |  | $element->set_default($self->{defaults}{$name}); | 
| 78 |  |  |  |  |  |  | } else { | 
| 79 | 0 |  |  |  |  |  | warn 'add defaults ' . $name; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub element_values | 
| 85 |  |  |  |  |  |  | { | 
| 86 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 87 | 0 | 0 |  |  |  |  | my $key = shift or die; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | my @ret; | 
| 90 | 0 | 0 |  |  |  |  | @ret = @{ $self->{defaults}{$key} || [] }; | 
|  | 0 |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 0 | 0 |  |  |  |  | if ($self->{params}{$key}) { | 
| 93 | 0 | 0 |  |  |  |  | @ret = @{$self->{params}{$key} || []}; | 
|  | 0 |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  |  | @ret; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub element_value | 
| 100 |  |  |  |  |  |  | { | 
| 101 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 102 | 0 | 0 |  |  |  |  | my $key = shift or die; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | my @ret; | 
| 105 | 0 |  |  |  |  |  | @ret = ($self->{defaults}{$key}); | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 | 0 |  |  |  |  | if ($self->{params}{$key}) { | 
| 108 | 0 | 0 |  |  |  |  | @ret = @{$self->{params}{$key} || []}; | 
|  | 0 |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | $ret[0]; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub _control | 
| 115 |  |  |  |  |  |  | { | 
| 116 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | bless { self => $self, }, 'XHTML::Instrumented::Form::Control'; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub set_select_data | 
| 122 |  |  |  |  |  |  | { | 
| 123 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 124 | 0 |  |  |  |  |  | my %p = validate(@_, { | 
| 125 |  |  |  |  |  |  | name => 1, | 
| 126 |  |  |  |  |  |  | data => ARRAYREF, | 
| 127 |  |  |  |  |  |  | }); | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  |  |  |  | my $select = $self->get_element($p{name}); | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 | 0 |  |  |  |  | croak('No select element found: ' . $p{name}) unless $select; | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  |  |  |  | $select->set_select_data(@{$p{data}}); | 
|  | 0 |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub delete_element | 
| 137 |  |  |  |  |  |  | { | 
| 138 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 139 | 0 |  |  |  |  |  | my %p = validate(@_, | 
| 140 |  |  |  |  |  |  | { | 
| 141 |  |  |  |  |  |  | name => 1, | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | ); | 
| 144 | 0 | 0 |  |  |  |  | my $name = $p{name} or die 'No name for element'; | 
| 145 | 0 |  |  |  |  |  | my $ret = $self->{elements}->{$name}; | 
| 146 | 0 |  |  |  |  |  | delete $self->{elements}->{$name}; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 0 |  |  |  |  |  | return $ret; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub add_element | 
| 152 |  |  |  |  |  |  | { | 
| 153 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 154 | 0 |  |  |  |  |  | my %p = validate(@_, | 
| 155 |  |  |  |  |  |  | { | 
| 156 |  |  |  |  |  |  | name => 1, | 
| 157 |  |  |  |  |  |  | type => 1, | 
| 158 |  |  |  |  |  |  | required => 0, | 
| 159 |  |  |  |  |  |  | optional => 0, | 
| 160 |  |  |  |  |  |  | default => 0, | 
| 161 |  |  |  |  |  |  | value => 0, | 
| 162 |  |  |  |  |  |  | data => 0, | 
| 163 |  |  |  |  |  |  | values => 0, | 
| 164 |  |  |  |  |  |  | multiple => 0, | 
| 165 |  |  |  |  |  |  | remove => 0, | 
| 166 |  |  |  |  |  |  | onclick => 0, | 
| 167 |  |  |  |  |  |  | onchange => 0, | 
| 168 |  |  |  |  |  |  | class => 0, | 
| 169 |  |  |  |  |  |  | package => { | 
| 170 |  |  |  |  |  |  | optional => 1, | 
| 171 |  |  |  |  |  |  | isa => 'XHTML::Instrumented::Form::ElementControl', | 
| 172 |  |  |  |  |  |  | }, | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  | ); | 
| 175 | 0 | 0 |  |  |  |  | my $name = $p{name} or die 'No name for element'; | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 0 | 0 |  |  |  |  | if ($self->{elements}->{$name}) { | 
| 178 | 0 |  |  |  |  |  | die "element $name already defined"; | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 0 |  | 0 |  |  |  | $p{type} ||= 'text'; | 
| 182 |  |  |  |  |  |  |  | 
| 183 | 0 | 0 | 0 |  |  |  | if (my $package = $p{package}) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 184 | 0 |  |  |  |  |  | delete $p{package}; | 
| 185 | 0 |  |  |  |  |  | $self->{elements}->{$name} = $package->new(%p); | 
| 186 |  |  |  |  |  |  | } elsif ($p{type} eq 'multiselect' or $p{type} eq 'checkbox') { | 
| 187 | 0 |  |  |  |  |  | require XHTML::Instrumented::Form::Checkbox; | 
| 188 | 0 |  |  |  |  |  | $self->{elements}->{$name} = XHTML::Instrumented::Form::Checkbox->new(%p); | 
| 189 |  |  |  |  |  |  | } elsif ($p{type} eq 'select') { | 
| 190 | 0 |  |  |  |  |  | require XHTML::Instrumented::Form::Select; | 
| 191 | 0 |  |  |  |  |  | $self->{elements}->{$name} = XHTML::Instrumented::Form::Select->new(%p); | 
| 192 |  |  |  |  |  |  | } elsif ($p{type} eq 'hidden') { | 
| 193 | 0 |  |  |  |  |  | require XHTML::Instrumented::Form::Hidden; | 
| 194 | 0 |  |  |  |  |  | $self->{elements}->{$name} = XHTML::Instrumented::Form::Hidden->new(%p); | 
| 195 |  |  |  |  |  |  | } else { | 
| 196 | 0 |  |  |  |  |  | require XHTML::Instrumented::Form::Element; | 
| 197 | 0 |  |  |  |  |  | $self->{elements}->{$name} = XHTML::Instrumented::Form::Element->new(%p); | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 0 |  |  |  |  |  | $self->{elements}->{$name}; | 
| 201 |  |  |  |  |  |  | } | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | sub set_element | 
| 204 |  |  |  |  |  |  | { | 
| 205 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 206 | 0 |  |  |  |  |  | my $args = { @_ }; | 
| 207 |  |  |  |  |  |  |  | 
| 208 | 0 | 0 |  |  |  |  | my $name = $args->{name} or die 'No name for element'; | 
| 209 |  |  |  |  |  |  |  | 
| 210 | 0 |  |  |  |  |  | my $element = $self->{elements}->{$name}; | 
| 211 |  |  |  |  |  |  |  | 
| 212 | 0 |  |  |  |  |  | for my $key (keys %$args) { | 
| 213 | 0 |  |  |  |  |  | $element->{$key} = $args->{$key}; | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  |  | 
| 216 | 0 |  |  |  |  |  | return $element; | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | sub args | 
| 220 |  |  |  |  |  |  | { | 
| 221 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 222 |  |  |  |  |  |  |  | 
| 223 | 0 | 0 |  |  |  |  | %{$self->{elements} || {}}; | 
|  | 0 |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | } | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | sub params | 
| 227 |  |  |  |  |  |  | { | 
| 228 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 229 | 0 |  |  |  |  |  | my $hash = {}; | 
| 230 | 0 |  |  |  |  |  | die; | 
| 231 | 0 | 0 |  |  |  |  | $hash->{action} = $self->{action} if $self->{action}; | 
| 232 | 0 | 0 |  |  |  |  | $hash->{method} = $self->{method} if $self->{method}; | 
| 233 |  |  |  |  |  |  |  | 
| 234 | 0 |  |  |  |  |  | $hash; | 
| 235 |  |  |  |  |  |  | } | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | sub context | 
| 238 |  |  |  |  |  |  | { | 
| 239 |  |  |  |  |  |  | { | 
| 240 | 0 |  |  | 0 | 1 |  | action => 'bob', | 
| 241 |  |  |  |  |  |  | }; | 
| 242 |  |  |  |  |  |  | } | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | sub is_select | 
| 245 |  |  |  |  |  |  | { | 
| 246 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 247 | 0 |  | 0 |  |  |  | my $id = shift || die 'no id.'; | 
| 248 | 0 |  |  |  |  |  | my $ret = 0; | 
| 249 | 0 | 0 |  |  |  |  | if (my $element = $self->{elements}{$id}) { | 
| 250 | 0 | 0 |  |  |  |  | if ($element->{type} eq 'select') { | 
| 251 | 0 |  |  |  |  |  | for my $data (@{$element->{data}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 252 | 0 | 0 | 0 |  |  |  | if (defined($data->{value}) && defined($self->element_value($id)) | 
|  |  |  | 0 |  |  |  |  | 
| 253 |  |  |  |  |  |  | && $self->element_value($id) eq $data->{value}) { | 
| 254 | 0 |  |  |  |  |  | $data->{selected} = 'selected'; | 
| 255 |  |  |  |  |  |  | } | 
| 256 |  |  |  |  |  |  | } | 
| 257 |  |  |  |  |  |  | } | 
| 258 | 0 |  |  |  |  |  | [ @{$element->{data}} ]; | 
|  | 0 |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | } else { | 
| 260 | 0 |  |  |  |  |  | []; | 
| 261 |  |  |  |  |  |  | } | 
| 262 |  |  |  |  |  |  | } | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | sub get_element | 
| 265 |  |  |  |  |  |  | { | 
| 266 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 267 | 0 |  |  |  |  |  | my $name = shift; | 
| 268 |  |  |  |  |  |  |  | 
| 269 | 0 |  |  |  |  |  | my $ret = $self->{elements}{$name}; | 
| 270 |  |  |  |  |  |  |  | 
| 271 | 0 | 0 |  |  |  |  | if ($ret) { | 
| 272 | 0 |  |  |  |  |  | $ret->{_default} = $self->element_value($name); | 
| 273 | 0 |  | 0 |  |  |  | $ret->{default} ||= $self->element_value($name); | 
| 274 |  |  |  |  |  |  | } | 
| 275 |  |  |  |  |  |  |  | 
| 276 | 0 |  |  |  |  |  | return $ret; | 
| 277 |  |  |  |  |  |  | } | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | sub is_element | 
| 280 |  |  |  |  |  |  | { | 
| 281 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 282 | 0 |  |  |  |  |  | my $args = { @_ }; | 
| 283 | 0 |  |  |  |  |  | my $ret = 0; | 
| 284 |  |  |  |  |  |  |  | 
| 285 | 0 | 0 |  |  |  |  | if (my $name = $args->{name}) { | 
| 286 | 0 | 0 |  |  |  |  | if ($self->{elements}{$name}) { | 
| 287 | 0 |  |  |  |  |  | $ret = 1; | 
| 288 |  |  |  |  |  |  | } | 
| 289 |  |  |  |  |  |  | } | 
| 290 | 0 |  |  |  |  |  | $ret; | 
| 291 |  |  |  |  |  |  | } | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | sub auto | 
| 294 |  |  |  |  |  |  | { | 
| 295 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 296 | 0 |  |  |  |  |  | my @ret; | 
| 297 |  |  |  |  |  |  |  | 
| 298 | 0 |  |  |  |  |  | for my $element (keys %{$self->{elements}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 299 | 0 | 0 |  |  |  |  | next unless $self->{elements}{$element}{type} eq 'hidden'; | 
| 300 | 0 |  |  |  |  |  | my $x = $self->get_element($element); | 
| 301 |  |  |  |  |  |  |  | 
| 302 | 0 | 0 |  |  |  |  | if (ref($x->{value}) eq 'ARRAY') { | 
| 303 | 0 |  |  |  |  |  | push(@ret, map({bless({ type => 'hidden', name => $x->{name}, value => $_ }, ref($x) );} @{$x->{value}} )); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 304 |  |  |  |  |  |  | } else { | 
| 305 | 0 | 0 |  |  |  |  | unless (defined $x->{value}) { | 
| 306 | 0 |  |  |  |  |  | $x->{value} = $self->element_value($element); | 
| 307 |  |  |  |  |  |  | } | 
| 308 | 0 |  |  |  |  |  | push(@ret, $x); | 
| 309 |  |  |  |  |  |  | } | 
| 310 |  |  |  |  |  |  | } | 
| 311 |  |  |  |  |  |  |  | 
| 312 | 0 |  |  |  |  |  | @ret; | 
| 313 |  |  |  |  |  |  | } | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | sub update_argumants | 
| 316 |  |  |  |  |  |  | { | 
| 317 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 318 | 0 |  |  |  |  |  | my $args = { @_ }; | 
| 319 |  |  |  |  |  |  |  | 
| 320 | 0 |  |  |  |  |  | $args->{args}; | 
| 321 |  |  |  |  |  |  | } | 
| 322 |  |  |  |  |  |  |  | 
| 323 |  |  |  |  |  |  | sub name | 
| 324 |  |  |  |  |  |  | { | 
| 325 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 326 |  |  |  |  |  |  |  | 
| 327 | 0 |  |  |  |  |  | $self->{name}; | 
| 328 |  |  |  |  |  |  | } | 
| 329 |  |  |  |  |  |  |  | 
| 330 |  |  |  |  |  |  |  | 
| 331 |  |  |  |  |  |  | 1; | 
| 332 |  |  |  |  |  |  | __END__ |