1 package Reaction::UI::ViewPort::ActionForm;
5 use aliased 'Reaction::UI::ViewPort::Field::Text';
6 use aliased 'Reaction::UI::ViewPort::Field::Number';
7 use aliased 'Reaction::UI::ViewPort::Field::Boolean';
8 use aliased 'Reaction::UI::ViewPort::Field::File';
9 use aliased 'Reaction::UI::ViewPort::Field::String';
10 use aliased 'Reaction::UI::ViewPort::Field::Password';
11 use aliased 'Reaction::UI::ViewPort::Field::DateTime';
12 use aliased 'Reaction::UI::ViewPort::Field::ChooseOne';
13 use aliased 'Reaction::UI::ViewPort::Field::ChooseMany';
14 use aliased 'Reaction::UI::ViewPort::Field::HiddenArray';
15 use aliased 'Reaction::UI::ViewPort::Field::TimeRange';
17 class ActionForm is 'Reaction::UI::ViewPort', which {
19 isa => 'Reaction::InterfaceModel::Action', is => 'ro', required => 1
22 has ordered_fields => (is => 'rw', isa => 'ArrayRef', lazy_build => 1);
25 isa => 'HashRef', is => 'rw', init_arg => 'fields', lazy_build => 1,
29 isa => 'Int', is => 'rw', reader => 'is_changed', default => sub { 0 }
33 isa => 'ArrayRef', is => 'rw', required => 0, predicate => 'has_next_action'
36 has on_apply_callback => (
37 isa => 'CodeRef', is => 'rw', required => 0,
38 predicate => 'has_on_apply_callback'
42 isa => 'Str', is => 'rw', required => 1, default => sub { 'ok' }
46 isa => 'Str', is => 'rw', required => 1, default => sub { 'apply' }
49 has close_label => (isa => 'Str', is => 'rw', lazy_fail => 1);
51 has close_label_close => (
52 isa => 'Str', is => 'rw', required => 1, default => sub { 'close' }
55 has close_label_cancel => (
56 isa => 'Str', is => 'rw', required => 1, default => sub { 'cancel' }
59 sub fields { shift->_field_map }
61 implements BUILD => as {
62 my ($self, $args) = @_;
63 unless ($self->_has_field_map) {
65 my $action = $self->action;
66 foreach my $attr ($action->parameter_attributes) {
67 push(@field_map, $self->_build_fields_for($attr => $args));
69 $self->_field_map({ @field_map });
71 $self->close_label($self->close_label_close);
74 implements _build_fields_for => as {
75 my ($self, $attr, $args) = @_;
76 my $attr_name = $attr->name;
77 #TODO: DOCUMENT ME!!!!!!!!!!!!!!!!!
78 my $builder = "_build_fields_for_name_${attr_name}";
80 if ($self->can($builder)) {
81 @fields = $self->$builder($attr, $args); # re-use coderef from can()
82 } elsif ($attr->has_type_constraint) {
83 my $constraint = $attr->type_constraint;
84 my $base_name = $constraint->name;
86 CONSTRAINT: while (defined($constraint)) {
87 my $name = $constraint->name;
88 $name = $attr->_isa_metadata if($name eq '__ANON__');
89 if (eval { $name->can('meta') } && !$tried_isa++) {
90 foreach my $class ($name->meta->class_precedence_list) {
91 my $mangled_name = $class;
92 $mangled_name =~ s/:+/_/g;
93 my $builder = "_build_fields_for_type_${mangled_name}";
94 if ($self->can($builder)) {
95 @fields = $self->$builder($attr, $args);
100 if (defined($name)) {
101 unless (defined($base_name)) {
102 $base_name = "(anon subtype of ${name})";
104 my $mangled_name = $name;
105 $mangled_name =~ s/:+/_/g;
106 my $builder = "_build_fields_for_type_${mangled_name}";
107 if ($self->can($builder)) {
108 @fields = $self->$builder($attr, $args);
112 $constraint = $constraint->parent;
114 if (!defined($constraint)) {
115 confess "Can't build field ${attr_name} of type ${base_name} without $builder method or _build_fields_for_type_<type> method for type or any supertype";
118 confess "Can't build field ${attr} without $builder method or type constraint";
123 implements _build_field_map => as {
124 confess "Lazy field map building not supported by default";
127 implements _build_ordered_fields => as {
129 my $ordered = $self->sort_by_spec($self->column_order, [keys %{$self->_field_map}]);
130 return [@{$self->_field_map}{@$ordered}];
133 implements can_apply => as {
135 foreach my $field ( @{ $self->ordered_fields } ) {
136 return 0 if $field->needs_sync;
137 # if e.g. a datetime field has an invalid value that can't be re-assembled
138 # into a datetime object, the action may be in a consistent state but
139 # not synchronized from the fields; in this case, we must not apply
141 return $self->action->can_apply;
144 implements do_apply => as {
146 return $self->action->do_apply;
149 implements ok => as {
151 if ($self->apply(@_)) {
156 implements apply => as {
158 if ($self->can_apply && (my $result = $self->do_apply)) {
160 $self->close_label($self->close_label_close);
161 $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
165 $self->close_label($self->close_label_cancel);
170 implements close => as {
172 my ($controller, $name, @args) = @{$self->next_action};
173 $controller->pop_viewport;
174 $controller->$name($self->action->ctx, @args);
179 override accept_events => sub {
180 (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
181 }; # can't do a close-type operation if there's nowhere to go afterwards
183 override child_event_sinks => sub {
185 return ((grep { ref($_) =~ 'Hidden' } values %{$self->_field_map}),
186 (grep { ref($_) !~ 'Hidden' } values %{$self->_field_map}),
190 after apply_child_events => sub {
191 # interrupt here because fields will have been updated
193 $self->sync_action_from_fields;
196 implements sync_action_from_fields => as {
198 my $field_map = $self->_field_map;
199 my @fields = values %{$field_map};
200 foreach my $field (@fields) {
201 $field->sync_to_action; # get the field to populate the $action if possible
203 $self->action->sync_all;
204 foreach my $field (@fields) {
205 $field->sync_from_action; # get errors from $action if applicable
209 implements _build_simple_field => as {
210 my ($self, $class, $attr, $args) = @_;
211 my $attr_name = $attr->name;
213 if (my $config = $args->{Field}{$attr_name}) {
216 my $field = $class->new(
217 action => $self->action,
220 location => join('-', $self->location, 'field', $attr->name),
224 return ($attr_name => $field);
227 implements _build_fields_for_type_Num => as {
228 my ($self, $attr, $args) = @_;
229 return $self->_build_simple_field(Number, $attr, $args);
232 implements _build_fields_for_type_Int => as {
233 my ($self, $attr, $args) = @_;
234 return $self->_build_simple_field(Number, $attr, $args);
237 implements _build_fields_for_type_Bool => as {
238 my ($self, $attr, $args) = @_;
239 return $self->_build_simple_field(Boolean, $attr, $args);
242 implements _build_fields_for_type_File => as {
243 my ($self, $attr, $args) = @_;
244 return $self->_build_simple_field(File, $attr, $args);
247 implements _build_fields_for_type_Str => as {
248 my ($self, $attr, $args) = @_;
249 if ($attr->has_valid_values) { # There's probably a better way to do this
250 return $self->_build_simple_field(ChooseOne, $attr, $args);
252 return $self->_build_simple_field(Text, $attr, $args);
255 implements _build_fields_for_type_SimpleStr => as {
256 my ($self, $attr, $args) = @_;
257 return $self->_build_simple_field(String, $attr, $args);
260 implements _build_fields_for_type_Password => as {
261 my ($self, $attr, $args) = @_;
262 return $self->_build_simple_field(Password, $attr, $args);
265 implements _build_fields_for_type_DateTime => as {
266 my ($self, $attr, $args) = @_;
267 return $self->_build_simple_field(DateTime, $attr, $args);
270 implements _build_fields_for_type_Enum => as {
271 my ($self, $attr, $args) = @_;
272 return $self->_build_simple_field(ChooseOne, $attr, $args);
275 #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
276 implements _build_fields_for_type_DBIx_Class_Row => as {
277 my ($self, $attr, $args) = @_;
278 return $self->_build_simple_field(ChooseOne, $attr, $args);
281 implements _build_fields_for_type_ArrayRef => as {
282 my ($self, $attr, $args) = @_;
283 if ($attr->has_valid_values) {
284 return $self->_build_simple_field(ChooseMany, $attr, $args)
286 return $self->_build_simple_field(HiddenArray, $attr, $args)
290 implements _build_fields_for_type_DateTime_Spanset => as {
291 my ($self, $attr, $args) = @_;
292 return $self->_build_simple_field(TimeRange, $attr, $args);
298 delete ${__PACKAGE__ . '::'}{inner};
306 Reaction::UI::ViewPort::ActionForm
310 use aliased 'Reaction::UI::ViewPort::ActionForm';
312 $self->push_viewport(ActionForm,
313 layout => 'register',
315 next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
318 qw / contact_title company_name email address1 address2 address3
319 city country post_code telephone mobile fax/ ],
324 This subclass of viewport is used for rendering a collection of
325 L<Reaction::UI::ViewPort::Field> objects for user editing.
331 L<Reaction::InterfaceModel::Action>
341 =head2 close_label_close
345 =head2 close_label_cancel
347 This label is only shown when C<changed> is true.
359 Returns true if a field has been edited.
363 =head2 on_apply_callback
371 Calls C<apply>, and then C<close> if successful.
375 Pop viewport and proceed to C<next_action>.
379 Attempt to save changes and update C<changed> attribute if required.
383 L<Reaction::UI::ViewPort>
385 L<Reaction::InterfaceModel::Action>
389 See L<Reaction::Class> for authors.
393 See L<Reaction::Class> for the license.