36 |
sub required { |
sub required { |
37 |
my ( $self ) = @_; |
my ( $self ) = @_; |
38 |
$self->load_class( $self->class ); |
$self->load_class( $self->class ); |
39 |
|
|
40 |
my @required = |
my @required = |
41 |
grep { |
grep { |
42 |
defined $_ && $_->can('name') && |
defined $_ && $_->can('name') && |
98 |
|
|
99 |
my $default = clone $self->params; # XXX we really don't want to modify params! |
my $default = clone $self->params; # XXX we really don't want to modify params! |
100 |
|
|
101 |
my $config_params = {}; |
my $params_config = {}; |
102 |
$config_params = $self->config($class); |
$params_config = $self->config($class); |
103 |
warn "# $class config = ",dump( $config_params ) if $self->debug; |
warn "# $class config = ",dump( $params_config ) if $self->debug; |
104 |
|
|
105 |
my $form; |
my $form; |
106 |
|
|
115 |
qq|<select title="$attr_type" name="$name">$options</select>| if $options; |
qq|<select title="$attr_type" name="$name">$options</select>| if $options; |
116 |
} |
} |
117 |
|
|
118 |
foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { |
foreach my $checkbox ( split(/\s+/, $default->{'frey-checkboxes'} ) ) { |
119 |
|
next if defined $default->{ $checkbox }; |
120 |
|
|
121 |
|
$default->{ $checkbox } = 0; |
122 |
|
$self->params->{ $checkbox } = 0; |
123 |
|
warn "# checkbox $checkbox not ticked"; |
124 |
|
} |
125 |
|
|
126 |
|
my @checkboxes; |
127 |
|
|
128 |
|
my $skip_prefix; |
129 |
|
if ( $self->class->can('action') ) { |
130 |
|
my $action = eval $self->class . '->action'; |
131 |
|
$skip_prefix->{$_}++ foreach eval $self->class .'->action_order'; |
132 |
|
warn "# skip_prefix = ", $self->dump( $skip_prefix ); |
133 |
|
} |
134 |
|
|
135 |
|
my $label_width = 1; # minimum |
136 |
|
|
137 |
|
foreach my $name ( |
138 |
|
grep { |
139 |
|
! $class->meta->get_attribute($_)->is_lazy |
140 |
|
&& ! defined $default->{$_} |
141 |
|
&& ! m{^_} # skip _private |
142 |
|
} $self->attributes |
143 |
|
) { |
144 |
my $attr_type = ''; |
my $attr_type = ''; |
145 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
146 |
my $label = $name; |
my $label = $name; |
|
my $value = ''; |
|
147 |
my $label_title = ''; |
my $label_title = ''; |
148 |
my $value_html = ''; |
my $value_html = ''; |
149 |
|
|
150 |
my $attr = $class->meta->get_attribute( $name ); |
my $attr = $class->meta->get_attribute( $name ); |
151 |
$attr_type = $attr->type_constraint->name if $attr->has_type_constraint; |
$attr_type = $attr->type_constraint->name if $attr->has_type_constraint; |
152 |
|
|
153 |
$value = $attr->default( $name ) if ! $value && $attr->has_default; |
my $value = |
154 |
|
defined $default->{$name} ? $default->{$name} : |
155 |
if ( ref($config_params) eq 'HASH' ) { |
$attr->has_default ? $attr->default( $name ) : |
156 |
$value = $config_params->{$name}; |
''; |
157 |
} elsif ( ref($config_params) eq 'ARRAY' ) { |
|
158 |
$value_html = select_values( $name, $attr_type, $config_params ); |
if ( ref($params_config) eq 'HASH' ) { |
159 |
$default->{$name} = $config_params->[0]->{$name}; |
$value = $params_config->{$name}; |
160 |
|
} elsif ( ref($params_config) eq 'ARRAY' ) { |
161 |
|
$value_html = select_values( $name, $attr_type, $params_config ); |
162 |
|
$default->{$name} = $params_config->[0]->{$name}; |
163 |
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
164 |
$value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); |
$value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); |
165 |
} elsif ( $attr_type !~ m{^(Str|Int)$} ) { |
} elsif ( $attr_type =~ m{^Bool} ) { |
166 |
$value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|; |
my $suffix = ''; |
167 |
|
$suffix = ' checked' if $value; |
168 |
|
$value_html = qq|<input type="checkbox" name="$name" title="$attr_type" value="$value"$suffix>|; |
169 |
|
push @checkboxes, $name; |
170 |
|
} elsif ( ! defined $value ) { |
171 |
|
$value_html = qq|<tt id="$name">undef</tt><!-- $name = undef -->|; # FIXME if $self->debug |
172 |
|
} elsif ( $attr_type !~ m{^(Str|Int)$} || $value =~ $Frey::Web::re_html ) { |
173 |
|
$value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|; |
174 |
} |
} |
175 |
|
|
176 |
$label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; |
$label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; |
177 |
|
|
178 |
$default->{$name} = $value unless defined $default->{$name}; |
$default->{$name} = $value unless defined $default->{$name}; |
179 |
|
|
180 |
if ( ! $value_html ) { |
$value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value">| unless $value_html; |
|
my $suffix = ''; |
|
|
if ( $attr_type =~ m{^Bool$} ) { |
|
|
$type = 'checkbox'; |
|
|
$suffix = ' checked' if $value; |
|
|
} |
|
|
$value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value"$suffix>|; |
|
|
} |
|
181 |
|
|
182 |
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
183 |
$form .= qq|<label for="$name"$label_title>$label</label>| . $value_html; |
$form .= qq|<label for="$name"$label_title>$label</label>$value_html<br>|; |
184 |
|
my $ll = length($label); |
185 |
|
$label_width = $ll if $ll > $label_width; |
186 |
} |
} |
187 |
my $html = qq|<h1>$class params</h1><form method="post">$form<input type="submit" value="Run $class"></form>|; |
$form .= qq|<input type="hidden" name="frey-checkboxes" value="| . join(' ', @checkboxes) . qq|">| if @checkboxes; |
188 |
|
|
189 |
|
$self->add_css(qq| |
190 |
|
label,input { |
191 |
|
display: block; |
192 |
|
float: left; |
193 |
|
margin-bottom: 10px; |
194 |
|
} |
195 |
|
|
196 |
|
label { |
197 |
|
text-align: right; |
198 |
|
width: ${label_width}ex; |
199 |
|
padding-right: 20px; |
200 |
|
} |
201 |
|
|
202 |
|
br { |
203 |
|
clear: left; |
204 |
|
} |
205 |
|
|); |
206 |
|
|
207 |
|
my $html; |
208 |
|
|
209 |
|
$form =~ s{<([^>]+)(name=")([^"]+)(")([^>]*)>} |
210 |
|
{<$1$2$3$4 id="$3" $5}gs; # http://www.quirksmode.org/oddsandends/forms.html |
211 |
|
|
212 |
|
$html = qq| |
213 |
|
<h1>$class params</h1> |
214 |
|
<form method="post"> |
215 |
|
$form |
216 |
|
<input type="submit" value="Run $class"> |
217 |
|
</form> |
218 |
|
| if $form; |
219 |
|
|
220 |
$self->add_status({ |
$self->add_status({ |
221 |
$self->class => { |
$self->class => { |
222 |
params => $self->params, |
params => $self->params, |
223 |
config_params => $config_params, |
params_config => $params_config, |
224 |
default => $default |
default => $default, |
225 |
}, |
}, |
226 |
}); |
}); |
227 |
|
|
229 |
return $html; |
return $html; |
230 |
} |
} |
231 |
|
|
232 |
|
=head1 SEE ALSO |
233 |
|
|
234 |
|
L<http://www.quirksmode.org/css/forms.html> for info on CSS2 forms |
235 |
|
|
236 |
|
=cut |
237 |
|
|
238 |
1; |
1; |