| 1 | package Catalyst::Controller::FormBuilder::Action::HTML::Template; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Tie::IxHash; | ||
| 5 | use base qw/Catalyst::Controller::FormBuilder::Action/; | ||
| 6 | use CGI::FormBuilder::Util qw/toname ismember optval/; | ||
| 7 | |||
| 8 | our %FORM_VARS; | ||
| 9 | # ensuring order to avoid FB's script before start warning msg | ||
| 10 | tie( | ||
| 11 | %FORM_VARS, 'Tie::IxHash', | ||
| 12 | 'form-jshead' => 'script', | ||
| 13 | 'js-head' => 'script', | ||
| 14 | 'form-title' => 'title', | ||
| 15 | 'form-start' => 'start', | ||
| 16 | 'form-submit' => 'submit', | ||
| 17 | 'form-reset' => 'reset', | ||
| 18 | 'form-end' => 'end', | ||
| 19 | 'form-invalid' => 'invalid', | ||
| 20 | 'form-required' => 'required', | ||
| 21 | 'form-statetags' => 'statetags', | ||
| 22 | 'form-keepextras' => 'keepextras', | ||
| 23 | ); | ||
| 24 | |||
| 25 | our %FIELD_VARS = ( | ||
| 26 | 'cleanopts' => 'cleanopts-%s', | ||
| 27 | 'value' => 'value-%s', | ||
| 28 | 'missing' => 'missing-%s', | ||
| 29 | 'nameopts' => 'nameopts-%s', | ||
| 30 | 'comment' => 'comment-%s', | ||
| 31 | 'required' => 'required-%s', | ||
| 32 | 'error' => 'error-%s', | ||
| 33 | 'label' => 'label-%s', | ||
| 34 | 'type' => 'type-%s', | ||
| 35 | 'tag' => 'field-%s', | ||
| 36 | 'invalid' => 'invalid-%s' | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub setup_template_vars { | ||
| 40 | my ( $self, $controller, $c ) = @_; | ||
| 41 | |||
| 42 | my $tvar = {}; | ||
| 43 | my $form = $controller->_formbuilder; | ||
| 44 | |||
| 45 | while ( my ( $to, $from ) = each %FORM_VARS ) { | ||
| 46 | $tvar->{$to} = $form->$from; | ||
| 47 | } | ||
| 48 | |||
| 49 | # | ||
| 50 | # For HTML::Template, each data struct is manually assigned | ||
| 51 | # to a separate <tmpl_var> and <tmpl_loop> tag | ||
| 52 | # | ||
| 53 | my @fieldlist; | ||
| 54 | for my $field ( $form->fields ) { | ||
| 55 | |||
| 56 | # Field name is usually a good idea | ||
| 57 | my $name = $field->name; | ||
| 58 | |||
| 59 | # Get all values | ||
| 60 | my @value = $field->values; | ||
| 61 | my @options = $field->options; | ||
| 62 | |||
| 63 | # | ||
| 64 | # Auto-expand all of our field tags, such as field, label, value | ||
| 65 | # comment, error, etc, etc | ||
| 66 | # | ||
| 67 | |||
| 68 | while ( my ( $key, $str ) = each %FIELD_VARS ) { | ||
| 69 | my $var = sprintf $str, $name; | ||
| 70 | $tvar->{$var} = $field->$key; | ||
| 71 | } | ||
| 72 | |||
| 73 | # | ||
| 74 | # Create a <tmpl_loop> for multi-values/multi-opts | ||
| 75 | # we can't include the field, really, since this would involve | ||
| 76 | # too much effort knowing what type | ||
| 77 | # | ||
| 78 | my @tmpl_loop = (); | ||
| 79 | for my $opt (@options) { | ||
| 80 | |||
| 81 | # Since our data structure is a series of ['',''] things, | ||
| 82 | # we get the name from that. If not, then it's a list | ||
| 83 | # of regular old data that we _toname if nameopts => 1 | ||
| 84 | my ( $o, $n ) = optval $opt; | ||
| 85 | |||
| 86 | $n ||= | ||
| 87 | $form->{"nameopts-$name"} | ||
| 88 | ? toname($o) | ||
| 89 | : $o; | ||
| 90 | |||
| 91 | my ( $slct, $chk ) = | ||
| 92 | ismember( $o, @value ) | ||
| 93 | ? ( 'selected', 'checked' ) | ||
| 94 | : ( '', '' ); | ||
| 95 | |||
| 96 | push @tmpl_loop, | ||
| 97 | { | ||
| 98 | label => $n, | ||
| 99 | value => $o, | ||
| 100 | checked => $chk, | ||
| 101 | selected => $slct, | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Now assign our loop-field | ||
| 106 | $form->{"loop-$name"} = \@tmpl_loop; | ||
| 107 | |||
| 108 | # Finally, push onto a top-level loop named "fields" | ||
| 109 | push @fieldlist, | ||
| 110 | { | ||
| 111 | field => $tvar->{"field-$name"}, | ||
| 112 | value => $tvar->{"value-$name"}, | ||
| 113 | values => \@value, | ||
| 114 | options => \@options, | ||
| 115 | label => $tvar->{"label-$name"}, | ||
| 116 | comment => $tvar->{"comment-$name"}, | ||
| 117 | error => $tvar->{"error-$name"}, | ||
| 118 | required => $tvar->{"required-$name"}, | ||
| 119 | missing => $tvar->{"missing-$name"}, | ||
| 120 | loop => \@tmpl_loop | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | |||
| 124 | # use Data::Dumper; | ||
| 125 | # print STDERR Dumper( $fieldlist[0] ); | ||
| 126 | |||
| 127 | # kill our previous fields list | ||
| 128 | $tvar->{fields} = \@fieldlist; | ||
| 129 | |||
| 130 | # loop thru each field we have and set the tmpl_param | ||
| 131 | while ( my ( $param, $tag ) = each %$tvar ) { | ||
| 132 | $c->stash->{$param} = $tag; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |