1 |
dpavlin |
369 |
package Frey::Action; |
2 |
|
|
use Moose; |
3 |
|
|
extends 'Frey::PPI'; |
4 |
|
|
with 'Frey::Web'; |
5 |
|
|
with 'Frey::Config'; |
6 |
|
|
|
7 |
|
|
use Data::Dump qw/dump/; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
Invoke any L<Frey> object creating html for with various default parameters |
12 |
|
|
if not supplied at invocation. |
13 |
|
|
|
14 |
|
|
=cut |
15 |
|
|
|
16 |
|
|
has 'class' => ( |
17 |
|
|
is => 'rw', |
18 |
|
|
isa => 'Str', |
19 |
|
|
required => 1, |
20 |
|
|
); |
21 |
|
|
|
22 |
|
|
has 'params' => ( |
23 |
|
|
is => 'rw', |
24 |
|
|
isa => 'HashRef', |
25 |
|
|
default => sub { {} }, |
26 |
|
|
); |
27 |
|
|
|
28 |
|
|
=head2 required |
29 |
|
|
|
30 |
|
|
my @required_attributes = $self->required; |
31 |
|
|
my $required_attributes = $self->required; |
32 |
|
|
|
33 |
|
|
=cut |
34 |
|
|
|
35 |
|
|
sub required { |
36 |
|
|
my ( $self ) = @_; |
37 |
|
|
$self->load_class( $self->class ); |
38 |
|
|
my @required = |
39 |
|
|
grep { |
40 |
|
|
defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) |
41 |
|
|
} |
42 |
|
|
map { |
43 |
|
|
my $attr = $self->class->meta->get_attribute($_); |
44 |
|
|
$attr->is_required && $attr; |
45 |
|
|
} $self->class->meta->get_attribute_list; |
46 |
|
|
|
47 |
|
|
warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class; |
48 |
|
|
return @required if wantarray; |
49 |
|
|
return \@required; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
=head2 attributes |
53 |
|
|
|
54 |
|
|
Generated from attributes specified in code (extracted using L<Frey::PPI>) |
55 |
|
|
and required atributes |
56 |
|
|
|
57 |
|
|
my @class_attributes = $self->attributes; |
58 |
|
|
my @class_attributes = $self->attributes; |
59 |
|
|
|
60 |
|
|
=cut |
61 |
|
|
|
62 |
|
|
sub attributes { |
63 |
|
|
my ( $self ) = @_; |
64 |
|
|
my $a; |
65 |
|
|
my @attrs = @{ $self->attribute_order }; |
66 |
|
|
@attrs = map { $a->{$_}++; $_ } @attrs; |
67 |
|
|
push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required }; |
68 |
|
|
warn "# attributes = ",dump( @attrs ); |
69 |
|
|
return @attrs if wantarray; |
70 |
|
|
return \@attrs; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
=head2 params_form |
74 |
|
|
|
75 |
|
|
my $html = $self->params_form; |
76 |
|
|
|
77 |
|
|
=cut |
78 |
|
|
|
79 |
|
|
sub params_form { |
80 |
|
|
my ( $self ) = @_; |
81 |
|
|
my @required = $self->required; |
82 |
|
|
if ( ! @required ) { |
83 |
|
|
warn "all params available ", dump( $self->params ), " not creating form"; |
84 |
|
|
return; |
85 |
|
|
} else { |
86 |
|
|
warn $self->class, " required params ", dump( @required ); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
my $class = $self->class; |
90 |
|
|
|
91 |
|
|
$self->load_class( $class ); |
92 |
|
|
|
93 |
|
|
my $params = {}; |
94 |
|
|
$params = $self->config($class); |
95 |
|
|
warn "# $class config = ",dump( $params ) if $self->debug; |
96 |
|
|
|
97 |
|
|
my $html = qq|<h1>$class params</h1><form method="post">|; |
98 |
|
|
|
99 |
|
|
foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { |
100 |
|
|
my $type = $name =~ m/^pass/ ? 'password' : 'text'; |
101 |
|
|
my $value = ''; |
102 |
|
|
my $value_html = ''; |
103 |
|
|
if ( ref($params) eq 'HASH' ) { |
104 |
|
|
$value = $params->{$name}; |
105 |
|
|
} elsif ( ref($params) eq 'ARRAY' ) { |
106 |
|
|
$value_html = qq|<select name="$name">| . join("\n", |
107 |
|
|
map { |
108 |
|
|
my $v = $_->{$name}; |
109 |
|
|
qq|<option value="$v">$v</option>| |
110 |
|
|
} @$params |
111 |
|
|
) . qq|</select>|; |
112 |
|
|
} elsif ( my $attr = $class->meta->get_attribute( $name ) ) { |
113 |
|
|
if ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { |
114 |
|
|
$value_html = qq|<select name="$name">| . join("\n", |
115 |
|
|
map { qq|<option value="$_">$_</option>| } @{ $attr->type_constraint->values } |
116 |
|
|
) . qq|</select>|; |
117 |
|
|
} elsif ( $attr->has_default ) { |
118 |
|
|
$value = $attr->default( $name ); |
119 |
|
|
} |
120 |
|
|
} else { |
121 |
|
|
warn "wired attribute $name"; |
122 |
|
|
} |
123 |
|
|
$value_html = qq|<input type="$type" name="$name" value="$value">| unless $value_html; |
124 |
|
|
# warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); |
125 |
|
|
$html .= qq|<label for="$name">$name</label>| . $value_html; |
126 |
|
|
} |
127 |
|
|
$html .= qq|<input type="submit" value="Run $class"></form>|; |
128 |
|
|
|
129 |
|
|
return $html; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
1; |