1 |
package Frey::Action; |
2 |
use Moose; |
3 |
extends 'Frey::PPI'; |
4 |
with 'Frey::Web'; |
5 |
with 'Frey::Config'; |
6 |
|
7 |
use Clone qw/clone/; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
=head1 DESCRIPTION |
11 |
|
12 |
Invoke any L<Frey> object creating html for with various default parameters |
13 |
if not supplied at invocation. |
14 |
|
15 |
=cut |
16 |
|
17 |
has 'class' => ( |
18 |
is => 'rw', |
19 |
isa => 'Str', |
20 |
required => 1, |
21 |
); |
22 |
|
23 |
has 'params' => ( |
24 |
is => 'rw', |
25 |
isa => 'HashRef', |
26 |
default => sub { {} }, |
27 |
); |
28 |
|
29 |
=head2 required |
30 |
|
31 |
my @required_attributes = $self->required; |
32 |
my $required_attributes = $self->required; |
33 |
|
34 |
=cut |
35 |
|
36 |
sub required { |
37 |
my ( $self ) = @_; |
38 |
$self->load_class( $self->class ); |
39 |
my @required = |
40 |
grep { |
41 |
defined $_ && $_->can('name') && |
42 |
! defined( $self->params->{ $_->name } ) && |
43 |
! $_->is_lazy |
44 |
} |
45 |
map { |
46 |
my $attr = $self->class->meta->get_attribute($_); |
47 |
blessed $attr && $attr->is_required && $attr; |
48 |
} $self->class->meta->get_attribute_list; |
49 |
|
50 |
warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug; |
51 |
return @required if wantarray; |
52 |
return \@required; |
53 |
} |
54 |
|
55 |
=head2 attributes |
56 |
|
57 |
Generated from attributes specified in code (extracted using L<Frey::PPI>) |
58 |
and required atributes |
59 |
|
60 |
my @class_attributes = $self->attributes; |
61 |
my @class_attributes = $self->attributes; |
62 |
|
63 |
=cut |
64 |
|
65 |
sub attributes { |
66 |
my ( $self ) = @_; |
67 |
my $a; |
68 |
my @attrs = @{ $self->attribute_order }; |
69 |
@attrs = map { $a->{$_}++; $_ } @attrs; |
70 |
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required }; |
71 |
warn "# attributes = ",dump( @attrs ) if $self->debug; |
72 |
return @attrs if wantarray; |
73 |
return \@attrs; |
74 |
} |
75 |
|
76 |
=head2 params_form |
77 |
|
78 |
my $html = $self->params_form; |
79 |
my ($html,$default_params) = $self->params_form; |
80 |
|
81 |
=cut |
82 |
|
83 |
sub params_form { |
84 |
my ( $self ) = @_; |
85 |
my @required = $self->required; |
86 |
if ( ! @required ) { |
87 |
warn "all params available ", dump( $self->params ), " not creating form" if $self->debug; |
88 |
return (undef,$self->params) if wantarray; |
89 |
return; |
90 |
} else { |
91 |
warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug; |
92 |
} |
93 |
|
94 |
my $class = $self->class; |
95 |
|
96 |
$self->load_class( $class ); |
97 |
|
98 |
my $default = clone $self->params; # XXX we really don't want to modify params! |
99 |
|
100 |
my $config_params = {}; |
101 |
$config_params = $self->config($class); |
102 |
warn "# $class config = ",dump( $config_params ) if $self->debug; |
103 |
|
104 |
my $form; |
105 |
|
106 |
sub select_values { |
107 |
my ( $name, $attr_type, $values ) = @_; |
108 |
my $options = join("\n", |
109 |
map { |
110 |
my $v = ref($_) eq 'HASH' ? $_->{$name} : $_; |
111 |
qq|<option value="$v">$v</option>| if $v; |
112 |
} @$values |
113 |
); |
114 |
qq|<select title="$attr_type" name="$name">$options</select>| if $options; |
115 |
} |
116 |
|
117 |
foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { |
118 |
my $attr_type = ''; |
119 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
120 |
my $label = $name; |
121 |
my $value = ''; |
122 |
my $label_title = ''; |
123 |
my $value_html = ''; |
124 |
|
125 |
my $attr = $class->meta->get_attribute( $name ); |
126 |
$attr_type = $attr->type_constraint->name if $attr->has_type_constraint; |
127 |
|
128 |
$value = $attr->default( $name ) if ! $value && $attr->has_default; |
129 |
|
130 |
if ( ref($config_params) eq 'HASH' ) { |
131 |
$value = $config_params->{$name}; |
132 |
} elsif ( ref($config_params) eq 'ARRAY' ) { |
133 |
$value_html = select_values( $name, $attr_type, $config_params ); |
134 |
$default->{$name} = $config_params->[0]->{$name}; |
135 |
} elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
136 |
$value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); |
137 |
} elsif ( $attr_type =~ m{^Bool} ) { |
138 |
my $suffix = ''; |
139 |
$suffix = ' checked' if $value; |
140 |
$value_html = qq|<input type="checkbox" name="$name" title="$attr_type" value="$value"$suffix>|; |
141 |
} elsif ( $attr_type !~ m{^(Str|Int)$} ) { |
142 |
$value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|; |
143 |
} |
144 |
|
145 |
$label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; |
146 |
|
147 |
$default->{$name} = $value unless defined $default->{$name}; |
148 |
|
149 |
$value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value">| unless $value_html; |
150 |
|
151 |
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
152 |
$form .= qq|<label for="$name"$label_title>$label</label>| . $value_html; |
153 |
} |
154 |
my $html = qq|<h1>$class params</h1><form method="post">$form<input type="submit" value="Run $class"></form>|; |
155 |
$self->add_status({ |
156 |
$self->class => { |
157 |
params => $self->params, |
158 |
config_params => $config_params, |
159 |
default => $default |
160 |
}, |
161 |
}); |
162 |
|
163 |
return ($html,$default) if wantarray; |
164 |
return $html; |
165 |
} |
166 |
|
167 |
1; |