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 { defined $_ } |
41 |
map { |
42 |
eval { |
43 |
$_->can('name') && !defined( $self->params->{ $_->name } ) |
44 |
}; |
45 |
} |
46 |
grep { |
47 |
my $required = eval { |
48 |
my $attr = $self->class->meta->get_attribute($_); |
49 |
$attr->is_required; |
50 |
}; |
51 |
warn "# attribute $_ error: $@" if $@; |
52 |
$required; |
53 |
} $self->class->meta->get_attribute_list; |
54 |
|
55 |
warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug; |
56 |
return @required if wantarray; |
57 |
return \@required; |
58 |
} |
59 |
|
60 |
=head2 attributes |
61 |
|
62 |
Generated from attributes specified in code (extracted using L<Frey::PPI>) |
63 |
and required atributes |
64 |
|
65 |
my @class_attributes = $self->attributes; |
66 |
my @class_attributes = $self->attributes; |
67 |
|
68 |
=cut |
69 |
|
70 |
sub attributes { |
71 |
my ( $self ) = @_; |
72 |
my $a; |
73 |
my @attrs = @{ $self->attribute_order }; |
74 |
@attrs = map { $a->{$_}++; $_ } @attrs; |
75 |
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required }; |
76 |
warn "# attributes = ",dump( @attrs ) if $self->debug; |
77 |
return @attrs if wantarray; |
78 |
return \@attrs; |
79 |
} |
80 |
|
81 |
=head2 params_form |
82 |
|
83 |
my $html = $self->params_form; |
84 |
my ($html,$default_params) = $self->params_form; |
85 |
|
86 |
=cut |
87 |
|
88 |
sub params_form { |
89 |
my ( $self ) = @_; |
90 |
my @required = $self->required; |
91 |
if ( ! @required ) { |
92 |
warn "all params available ", dump( $self->params ), " not creating form" if $self->debug; |
93 |
return (undef,$self->params) if wantarray; |
94 |
return; |
95 |
} else { |
96 |
warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug; |
97 |
} |
98 |
|
99 |
my $class = $self->class; |
100 |
|
101 |
$self->load_class( $class ); |
102 |
|
103 |
my $default = clone $self->params; # XXX we really don't want to modify params! |
104 |
|
105 |
my $config_params = {}; |
106 |
$config_params = $self->config($class); |
107 |
warn "# $class config = ",dump( $config_params ) if $self->debug; |
108 |
|
109 |
my $form; |
110 |
|
111 |
foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { |
112 |
my $attr_type = ''; |
113 |
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
114 |
my $label = $name; |
115 |
my $value = ''; |
116 |
my $label_title = ''; |
117 |
my $value_html = ''; |
118 |
if ( ref($config_params) eq 'HASH' ) { |
119 |
$value = $config_params->{$name}; |
120 |
} elsif ( ref($config_params) eq 'ARRAY' ) { |
121 |
$value_html = qq|<select name="$name">| . join("\n", |
122 |
map { |
123 |
my $v = $_->{$name}; |
124 |
qq|<option value="$v">$v</option>| |
125 |
} @$config_params |
126 |
) . qq|</select>|; |
127 |
$default->{$name} = $config_params->[0]->{$name}; |
128 |
} elsif ( my $attr = $class->meta->get_attribute( $name ) ) { |
129 |
if ( $attr->has_type_constraint ) { |
130 |
$attr_type = $attr->type_constraint->name; |
131 |
if ( $attr->type_constraint->can('values') ) { |
132 |
$value_html = qq|<select name="$name">| . join("\n", |
133 |
map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values } |
134 |
) . qq|</select>|; |
135 |
} elsif ( $attr_type !~ m{^(Str|Int)$} ) { |
136 |
$value_html = qq|<textarea name="$name" title="$attr_type">$value</textarea>|; |
137 |
} |
138 |
} |
139 |
$value = $attr->default( $name ) if ! $value && $attr->has_default; |
140 |
$label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; |
141 |
} else { |
142 |
warn "wired attribute $name"; |
143 |
} |
144 |
|
145 |
$value_html = qq|<input type="$type" name="$name" title="$attr_type" value="$value">| unless $value_html; |
146 |
|
147 |
$default->{$name} = $value unless defined $default->{$name}; |
148 |
|
149 |
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
150 |
$form .= qq|<label for="$name"$label_title>$label</label>| . $value_html; |
151 |
} |
152 |
my $html = qq|<h1>$class params</h1><form method="post">$form<input type="submit" value="Run $class"></form>|; |
153 |
push @{ $self->status }, { 'Params' => |
154 |
{ |
155 |
'Config' => $config_params, |
156 |
'Default' => $default |
157 |
}, |
158 |
}; |
159 |
|
160 |
return ($html,$default) if wantarray; |
161 |
return $html; |
162 |
} |
163 |
|
164 |
1; |