--- trunk/lib/Frey/Action.pm 2008/11/17 14:37:48 369 +++ trunk/lib/Frey/Action.pm 2008/11/30 16:22:45 645 @@ -4,6 +4,7 @@ with 'Frey::Web'; with 'Frey::Config'; +use Clone qw/clone/; use Data::Dump qw/dump/; =head1 DESCRIPTION @@ -37,14 +38,16 @@ $self->load_class( $self->class ); my @required = grep { - defined $_ && $_->can('name') && !defined( $self->params->{ $_->name } ) + defined $_ && $_->can('name') && + ! defined( $self->params->{ $_->name } ) && + ! $_->is_lazy } map { my $attr = $self->class->meta->get_attribute($_); - $attr->is_required && $attr; + blessed $attr && $attr->is_required && $attr; } $self->class->meta->get_attribute_list; - warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class; + warn "## required = ",dump( map { $_->name } @required ), " for ", $self->class if @required && $self->debug; return @required if wantarray; return \@required; } @@ -65,14 +68,15 @@ my @attrs = @{ $self->attribute_order }; @attrs = map { $a->{$_}++; $_ } @attrs; push @attrs, $_ foreach grep { ! $a->{$_} } map { $_->name } @{ $self->required }; - warn "# attributes = ",dump( @attrs ); + warn "# attributes = ",dump( @attrs ) if $self->debug; return @attrs if wantarray; return \@attrs; } =head2 params_form - my $html = $self->params_form; + my $html = $self->params_form; + my ($html,$default_params) = $self->params_form; =cut @@ -80,52 +84,112 @@ my ( $self ) = @_; my @required = $self->required; if ( ! @required ) { - warn "all params available ", dump( $self->params ), " not creating form"; + warn "all params available ", dump( $self->params ), " not creating form" if $self->debug; + return (undef,$self->params) if wantarray; return; } else { - warn $self->class, " required params ", dump( @required ); + warn $self->class, " required params ", map { $_->dump(2) } @required if $self->debug; } my $class = $self->class; $self->load_class( $class ); - my $params = {}; - $params = $self->config($class); - warn "# $class config = ",dump( $params ) if $self->debug; + my $default = clone $self->params; # XXX we really don't want to modify params! - my $html = qq|

$class params

|; + my $config_params = {}; + $config_params = $self->config($class); + warn "# $class config = ",dump( $config_params ) if $self->debug; + + my $form; + + sub select_values { + my ( $name, $attr_type, $values ) = @_; + my $options = join("\n", + map { + my $v = ref($_) eq 'HASH' ? $_->{$name} : $_; + qq|| if $v; + } @$values + ); + qq|| if $options; + } + + foreach my $checkbox ( split(/\s+/, $default->{'frey-checkboxes'} ) ) { + next if defined $default->{ $checkbox }; + + $default->{ $checkbox } = 0; + $self->params->{ $checkbox } = 0; + warn "# checkbox $checkbox not ticked"; + } - foreach my $name ( grep { ! $class->meta->get_attribute($_)->is_lazy } $self->attributes ) { + my @checkboxes; + + foreach my $name ( + grep { + ! $class->meta->get_attribute($_)->is_lazy + && ! defined $default->{$_} + } $self->attributes + ) { + my $attr_type = ''; my $type = $name =~ m/^pass/ ? 'password' : 'text'; - my $value = ''; + my $label = $name; + my $label_title = ''; my $value_html = ''; - if ( ref($params) eq 'HASH' ) { - $value = $params->{$name}; - } elsif ( ref($params) eq 'ARRAY' ) { - $value_html = qq||; - } elsif ( my $attr = $class->meta->get_attribute( $name ) ) { - if ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { - $value_html = qq||; - } elsif ( $attr->has_default ) { - $value = $attr->default( $name ); - } - } else { - warn "wired attribute $name"; + + my $attr = $class->meta->get_attribute( $name ); + $attr_type = $attr->type_constraint->name if $attr->has_type_constraint; + + my $value = + defined $default->{$name} ? $default->{$name} : + $attr->has_default ? $attr->default( $name ) : + ''; + + if ( ref($config_params) eq 'HASH' ) { + $value = $config_params->{$name}; + } elsif ( ref($config_params) eq 'ARRAY' ) { + $value_html = select_values( $name, $attr_type, $config_params ); + $default->{$name} = $config_params->[0]->{$name}; + } elsif ( $attr->has_type_constraint && $attr->type_constraint->can('values') ) { + $value_html = select_values( $name, $attr_type, $attr->type_constraint->values ); + } elsif ( $attr_type =~ m{^Bool} ) { + my $suffix = ''; + $suffix = ' checked' if $value; + $value_html = qq||; + push @checkboxes, $name; + } elsif ( $attr_type !~ m{^(Str|Int)$} ) { + $value_html = qq||; } - $value_html = qq|| unless $value_html; + + $label_title = qq| title="| . $attr->documentation . qq|"| if $attr->has_documentation; + + $default->{$name} = $value unless defined $default->{$name}; + + $value_html = qq|| unless $value_html; + # warn "# required $name ", $class->meta->get_attribute( $name )->dump( 2 ); - $html .= qq|| . $value_html; + $form .= qq|| . $value_html; } - $html .= qq|
|; + $form .= qq|| if @checkboxes; + + my $html; + + $html = qq| +

$class params

+
+ $form + +
+ | if $form; + + $self->add_status({ + $self->class => { + params => $self->params, + config_params => $config_params, + default => $default + }, + }); + return ($html,$default) if wantarray; return $html; }