| 1 | package Template::Flute::Form; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Scalar::Util qw/blessed/; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Template::Flute::Form - Form object for Template::Flute templates. | ||
| 10 | |||
| 11 | =head1 CONSTRUCTOR | ||
| 12 | |||
| 13 | =head2 new | ||
| 14 | |||
| 15 | Creates Template::Flute::Form object. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | # Constructor | ||
| 20 | sub new { | ||
| 21 | my ($class, $sob, $static) = @_; | ||
| 22 | my ($self); | ||
| 23 | |||
| 24 | $self = {sob => $sob, static => $static, valid_input => undef}; | ||
| 25 | |||
| 26 | # retrieve values for action and method attributes | ||
| 27 | my $action = $self->{sob}->{elts}->[0]->att('action'); | ||
| 28 | |||
| 29 | if (defined $action) { | ||
| 30 | $self->{action} = $action; | ||
| 31 | } | ||
| 32 | else { | ||
| 33 | $self->{action} = ''; | ||
| 34 | } | ||
| 35 | |||
| 36 | my $method = $self->{sob}->{elts}->[0]->att('method'); | ||
| 37 | |||
| 38 | if (defined $method && $method =~ /\S/) { | ||
| 39 | $self->{method} = uc($method); | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | $self->{method} = 'GET'; | ||
| 43 | } | ||
| 44 | |||
| 45 | bless $self, $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 params_add PARAMS | ||
| 51 | |||
| 52 | Add parameters from PARAMS to form. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub params_add { | ||
| 57 | my ($self, $params) = @_; | ||
| 58 | |||
| 59 | $self->{params} = $params || []; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head2 fields_add FIELDS | ||
| 63 | |||
| 64 | Add fields from FIELDS to form. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub fields_add { | ||
| 69 | my ($self, $fields) = @_; | ||
| 70 | my (%field_iters); | ||
| 71 | |||
| 72 | for my $field (@$fields) { | ||
| 73 | if ($field->{iterator}) { | ||
| 74 | $field_iters{$field->{iterator}} = $field->{name}; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | $self->{iterators} = \%field_iters; | ||
| 79 | $self->{fields} = $fields || []; | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 inputs_add INPUTS | ||
| 83 | |||
| 84 | Add inputs from INPUTS to form. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub inputs_add { | ||
| 89 | my ($self, $inputs) = @_; | ||
| 90 | |||
| 91 | if (ref($inputs) eq 'HASH') { | ||
| 92 | $self->{inputs} = $inputs; | ||
| 93 | $self->{valid_input} = 0; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 name | ||
| 98 | |||
| 99 | Returns name of the form. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub name { | ||
| 104 | my ($self) = @_; | ||
| 105 | |||
| 106 | return $self->{sob}->{name}; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 elt | ||
| 110 | |||
| 111 | Returns corresponding HTML template element of the form. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub elt { | ||
| 116 | my ($self) = @_; | ||
| 117 | |||
| 118 | return $self->{sob}->{elts}->[0]; | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 fields | ||
| 122 | |||
| 123 | Returns form fields. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub fields { | ||
| 128 | my ($self) = @_; | ||
| 129 | |||
| 130 | return $self->{fields}; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 params | ||
| 134 | |||
| 135 | Returns form parameters. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub params { | ||
| 140 | my ($self) = @_; | ||
| 141 | |||
| 142 | return $self->{params}; | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 inputs | ||
| 146 | |||
| 147 | Returns form inputs. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub inputs { | ||
| 152 | my ($self) = @_; | ||
| 153 | |||
| 154 | return $self->{inputs}; | ||
| 155 | } | ||
| 156 | |||
| 157 | =head2 input PARAMS | ||
| 158 | |||
| 159 | Verifies that input parameters are sufficient. | ||
| 160 | Returns 1 in case of success, 0 otherwise. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub input { | ||
| 165 | my ($self, $params) = @_; | ||
| 166 | my ($error_count); | ||
| 167 | |||
| 168 | if (! $params && $self->{valid_input} == 1) { | ||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | $error_count = 0; | ||
| 173 | $params ||= {}; | ||
| 174 | |||
| 175 | for my $input (values %{$self->{inputs}}) { | ||
| 176 | if ($input->{required} && ! $params->{$input->{name}}) { | ||
| 177 | warn "Missing input for $input->{name}.\n"; | ||
| 178 | $error_count++; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | $input->{value} = $params->{$input->{name}}; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | if ($error_count) { | ||
| 186 | return 0; | ||
| 187 | } | ||
| 188 | |||
| 189 | $self->{valid_input} = 1; | ||
| 190 | return 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | =head2 iterators | ||
| 194 | |||
| 195 | Returns names of all iterators used by the fields for this form. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub iterators { | ||
| 200 | my ($self) = @_; | ||
| 201 | |||
| 202 | return keys(%{$self->{iterators}}); | ||
| 203 | } | ||
| 204 | |||
| 205 | =head2 action | ||
| 206 | |||
| 207 | Returns current form action. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub action { | ||
| 212 | my ($self) = @_; | ||
| 213 | |||
| 214 | return $self->{action}; | ||
| 215 | } | ||
| 216 | |||
| 217 | =head2 set_action ACTION | ||
| 218 | |||
| 219 | Sets from action to ACTION. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub set_action { | ||
| 224 | my ($self, $action) = @_; | ||
| 225 | |||
| 226 | $self->{sob}->{elts}->[0]->set_att('action', $action); | ||
| 227 | $self->{action} = $action; | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 method | ||
| 231 | |||
| 232 | Returns current form method, e.g. GET or POST. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | sub method { | ||
| 237 | my ($self) = @_; | ||
| 238 | |||
| 239 | return $self->{method}; | ||
| 240 | }; | ||
| 241 | |||
| 242 | =head2 set_method METHOD | ||
| 243 | |||
| 244 | Sets form method to METHOD, e.g. GET or POST. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub set_method { | ||
| 249 | my ($self, $method) = @_; | ||
| 250 | |||
| 251 | $self->{sob}->{elts}->[0]->set_att('method', $method); | ||
| 252 | $self->{method} = $method; | ||
| 253 | } | ||
| 254 | |||
| 255 | =head2 fill PARAMS | ||
| 256 | |||
| 257 | Fills form with parameters from hash reference PARAMS. | ||
| 258 | |||
| 259 | =head2 is_filled | ||
| 260 | |||
| 261 | Return true if you called fill on the form. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | |||
| 266 | # fill - fills form fields | ||
| 267 | |||
| 268 | sub _set_filled { | ||
| 269 | my $self = shift; | ||
| 270 | $self->{_form_is_filled} = 1; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub is_filled { | ||
| 274 | my $self = shift; | ||
| 275 | return $self->{_form_is_filled}; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub fill { | ||
| 279 | my ($self, $href) = @_; | ||
| 280 | my ($f, @elts, $value, $zref, $type); | ||
| 281 | $self->_set_filled; | ||
| 282 | if (blessed($href) and $href->isa('Hash::MultiValue')) { | ||
| 283 | $href = $href->mixed; | ||
| 284 | } | ||
| 285 | for my $f (@{$self->fields()}) { | ||
| 286 | @elts = @{$f->{elts}}; | ||
| 287 | |||
| 288 | if (exists $href->{$f->{name}} | ||
| 289 | && defined $href->{$f->{name}}) { | ||
| 290 | $value = $href->{$f->{name}}; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | $value = ''; | ||
| 294 | } | ||
| 295 | |||
| 296 | if (@elts == 1) { | ||
| 297 | $zref = $elts[0]->{"flute_$f->{name}"}; | ||
| 298 | $type = $elts[0]->att('type') || ''; | ||
| 299 | |||
| 300 | if ($zref->{rep_sub}) { | ||
| 301 | # call subroutine to handle this element | ||
| 302 | $zref->{rep_sub}->($elts[0], $value); | ||
| 303 | } | ||
| 304 | elsif ($elts[0]->gi() eq 'textarea') { | ||
| 305 | $elts[0]->set_text($value); | ||
| 306 | } | ||
| 307 | elsif ($elts[0]->gi() eq 'input') { | ||
| 308 | if ($type eq 'submit') { | ||
| 309 | # don't override button text | ||
| 310 | } | ||
| 311 | elsif ($type eq 'checkbox') { | ||
| 312 | my $att_value = $elts[0]->att('value'); | ||
| 313 | |||
| 314 | if (defined $att_value && $value eq $att_value) { | ||
| 315 | $elts[0]->set_att('checked', 'checked'); | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | $elts[0]->del_att('checked'); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | else { | ||
| 322 | $elts[0]->set_att('value', $value); | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | elsif (@elts > 1) { | ||
| 327 | # handle radio buttons | ||
| 328 | for my $elt (@elts) { | ||
| 329 | if ($elt->gi() eq 'input') { | ||
| 330 | if ($elt->att('type') eq 'radio') { | ||
| 331 | if ($value eq $elt->att('value')) { | ||
| 332 | $elt->set_att('checked', 'checked'); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | elsif ($elt->att('type') eq 'checkbox') { | ||
| 336 | if (ref($value) eq 'ARRAY') { | ||
| 337 | if (grep {$_ eq $elt->att('value')} | ||
| 338 | @$value) { | ||
| 339 | $elt->set_att('checked', 'checked'); | ||
| 340 | } | ||
| 341 | else { | ||
| 342 | $elt->del_att('checked'); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | elsif ($value eq $elt->att('value')) { | ||
| 346 | $elt->set_att('checked', 'checked'); | ||
| 347 | } | ||
| 348 | else { | ||
| 349 | $elt->del_att('checked'); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | $elt->del_att('checked'); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | =head1 AUTHOR | ||
| 362 | |||
| 363 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 364 | |||
| 365 | =head1 LICENSE AND COPYRIGHT | ||
| 366 | |||
| 367 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 368 | |||
| 369 | This program is free software; you can redistribute it and/or modify it | ||
| 370 | under the terms of either: the GNU General Public License as published | ||
| 371 | by the Free Software Foundation; or the Artistic License. | ||
| 372 | |||
| 373 | See http://dev.perl.org/licenses/ for more information. | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | 1; |