fba28cf29a05fb1f5d9d547a40de1c6c55a2f478
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
1 package Reaction::UI::ViewPort::Action;
2
3 use Reaction::Class;
4
5 use aliased 'Reaction::UI::ViewPort::Object';
6
7 BEGIN { *DEBUG_EVENTS = \&Reaction::UI::ViewPort::DEBUG_EVENTS; }
8
9 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Text';
10 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Array';
11 use aliased 'Reaction::UI::ViewPort::Field::Mutable::String';
12 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Number';
13 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Integer';
14 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Boolean';
15 use aliased 'Reaction::UI::ViewPort::Field::Mutable::Password';
16 use aliased 'Reaction::UI::ViewPort::Field::Mutable::DateTime';
17 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseOne';
18 use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseMany';
19
20 use aliased 'Reaction::UI::ViewPort::Field::Mutable::File';
21 #use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange';
22
23 use Reaction::Types::Core qw/NonEmptySimpleStr/;
24
25 class Action is Object, which {
26   has model  => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
27   #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
28   has method => ( isa => NonEmptySimpleStr, is => 'rw', default => sub { 'post' } );
29
30   has next_action       => (is => 'rw', isa => 'ArrayRef');
31   has on_apply_callback => (is => 'rw', isa => 'CodeRef');
32
33   has ok_label           => (is => 'rw', isa => 'Str', lazy_build => 1);
34   has apply_label        => (is => 'rw', isa => 'Str', lazy_build => 1);
35   has close_label        => (is => 'rw', isa => 'Str', lazy_fail  => 1);
36   has close_label_close  => (is => 'rw', isa => 'Str', lazy_build => 1);
37   has close_label_cancel => (is => 'rw', isa => 'Str', lazy_build => 1);
38
39   has changed => (is => 'rw', isa => 'Int', reader => 'is_changed', default => sub{0});
40
41   implements BUILD => as{
42     my $self = shift;
43     $self->close_label($self->close_label_close);
44   };
45
46   implements _build_ok_label           => as{ 'ok'     };
47   implements _build_apply_label        => as{ 'apply'  };
48   implements _build_close_label_close  => as{ 'close'  };
49   implements _build_close_label_cancel => as{ 'cancel' };
50
51   implements can_apply => as {
52     my ($self) = @_;
53     foreach my $field ( @{ $self->fields } ) {
54       if ($field->needs_sync) {
55         if (DEBUG_EVENTS) {
56           $self->ctx->log->debug(
57             "Failing out of can_apply on ${\ref($self)} at ${\$self->location}"
58             ." because field for ${\$field->attribute->name} needs sync"
59           );
60         }
61         return 0;
62       }
63       # if e.g. a datetime field has an invalid value that can't be re-assembled
64       # into a datetime object, the action may be in a consistent state but
65       # not synchronized from the fields; in this case, we must not apply
66     }
67     if (DEBUG_EVENTS) {
68       my $ret = $self->model->can_apply;
69       $self->ctx->log->debug(
70         "model can_apply returned ${ret}"
71         ." on ${\ref($self)} at ${\$self->location}"
72       );
73       return $ret;
74     }
75     return $self->model->can_apply;
76   };
77
78   implements do_apply => as {
79     shift->model->do_apply;
80   };
81
82   implements ok => as {
83     my $self = shift;
84     $self->close(@_) if $self->apply(@_);
85   };
86
87   implements apply => as {
88     my $self = shift;
89     if ($self->can_apply && (my $result = $self->do_apply)) {
90       $self->changed(0);
91       $self->close_label($self->close_label_close);
92       $self->on_apply_callback->($self => $result) if $self->has_on_apply_callback;
93       return 1;
94     } else {
95       $self->changed(1);
96       $self->close_label($self->close_label_cancel);
97       return 0;
98     }
99   };
100
101   implements close => as {
102     my $self = shift;
103     my ($controller, $name, @args) = @{$self->next_action};
104     $controller->pop_viewport;
105     $controller->$name($self->ctx, @args);
106   };
107
108   implements can_close => as { 1 };
109
110   override accept_events => sub {
111     (($_[0]->has_next_action ? ('ok', 'close') : ()), 'apply', super());
112   }; # can't do a close-type operation if there's nowhere to go afterwards
113
114   after apply_child_events => sub {
115     # interrupt here because fields will have been updated
116     my ($self) = @_;
117     $self->sync_action_from_fields;
118   };
119
120   implements sync_action_from_fields => as {
121     my ($self) = @_;
122     foreach my $field (@{$self->fields}) {
123       $field->sync_to_action; # get the field to populate the $action if possible
124     }
125     $self->model->sync_all;
126     foreach my $field (@{$self->fields}) {
127       $field->sync_from_action; # get errors from $action if applicable
128     }
129   };
130
131
132   implements _build_fields_for_type_Num => as {
133     my ($self, $attr, $args) = @_;
134     $self->_build_simple_field(attribute => $attr, class => Number, %$args);
135   };
136
137   implements _build_fields_for_type_Int => as {
138     my ($self, $attr, $args) = @_;
139     $self->_build_simple_field(attribute => $attr, class => Integer, %$args);
140   };
141
142   implements _build_fields_for_type_Bool => as {
143     my ($self,  $attr, $args) = @_;
144     $self->_build_simple_field(attribute => $attr, class => Boolean, %$args);
145   };
146
147   implements _build_fields_for_type_Reaction_Types_Core_SimpleStr => as {
148     my ($self, $attr, $args) = @_;
149     $self->_build_simple_field(attribute => $attr, class => String, %$args);
150   };
151
152   implements _build_fields_for_type_Reaction_Types_File_File => as {
153     my ($self, $attr, $args) = @_;
154     $self->_build_simple_field(attribute => $attr, class => File, %$args);
155   };
156
157   implements _build_fields_for_type_Str => as {
158     my ($self, $attr, $args) = @_;
159     if ($attr->has_valid_values) { # There's probably a better way to do this
160       $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
161     } else {
162       $self->_build_simple_field(attribute => $attr, class => Text, %$args);
163     }
164   };
165
166   implements _build_fields_for_type_Reaction_Types_Core_Password => as {
167     my ($self, $attr, $args) = @_;
168     $self->_build_simple_field(attribute => $attr, class => Password, %$args);
169   };
170
171   implements _build_fields_for_type_Reaction_Types_DateTime_DateTime => as {
172     my ($self, $attr, $args) = @_;
173     $self->_build_simple_field(attribute => $attr, class => DateTime, %$args);
174   };
175
176   implements _build_fields_for_type_Enum => as {
177     my ($self, $attr, $args) = @_;
178       $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
179   };
180
181   #this needs to be fixed. somehow. beats the shit our of me. really.
182   #implements build_fields_for_type_Reaction_InterfaceModel_Object => as {
183   implements _build_fields_for_type_DBIx_Class_Row => as {
184     my ($self, $attr, $args) = @_;
185     $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args);
186   };
187
188   implements _build_fields_for_type_ArrayRef => as {
189     my ($self, $attr, $args) = @_;
190     if ($attr->has_valid_values) {
191       $self->_build_simple_field(attribute => $attr, class => ChooseMany,  %$args);
192     } else {
193       $self->_build_simple_field
194         (
195          attribute => $attr,
196          class     => Array,
197          layout    => 'field/mutable/hidden_array',
198          %$args);
199     }
200   };
201
202   #implements _build_fields_for_type_DateTime_Spanset => as {
203   #  my ($self, $attr, $args) = @_;
204   #    $self->_build_simple_field(attribute => $attr, class => TimeRange,  %$args);
205   #};
206
207 };
208
209   1;
210
211 =head1 NAME
212
213 Reaction::UI::ViewPort::Action
214
215 =head1 SYNOPSIS
216
217   use aliased 'Reaction::UI::ViewPort::Action';
218
219   $self->push_viewport(Action,
220     layout => 'register',
221     model => $action,
222     next_action => [ $self, 'redirect_to', 'accounts', $c->req->captures ],
223     ctx => $c,
224     field_order => [
225       qw / contact_title company_name email address1 address2 address3
226            city country post_code telephone mobile fax/ ],
227   );
228
229 =head1 DESCRIPTION
230
231 This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
232 collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
233
234 =head1 ATTRIBUTES
235
236 =head2 model
237
238 L<Reaction::InterfaceModel::Action>
239
240 =head2 ok_label
241
242 Default: 'ok'
243
244 =head2 apply_label
245
246 Default: 'apply'
247
248 =head2 close_label_close
249
250 Default: 'close'
251
252 =head2 close_label_cancel
253
254 This label is only shown when C<changed> is true.
255
256 Default: 'cancel'
257
258 =head2 fields
259
260 =head2 can_apply
261
262 =head2 can_close
263
264 =head2 changed
265
266 Returns true if a field has been edited.
267
268 =head2 next_action
269
270 =head2 on_apply_callback
271
272 CodeRef.
273
274 =head1 METHODS
275
276 =head2 ok
277
278 Calls C<apply>, and then C<close> if successful.
279
280 =head2 close
281
282 Pop viewport and proceed to C<next_action>.
283
284 =head2 apply
285
286 Attempt to save changes and update C<changed> attribute if required.
287
288 =head1 SEE ALSO
289
290 L<Reaction::UI::ViewPort::Object>
291
292 L<Reaction::UI::ViewPort>
293
294 L<Reaction::InterfaceModel::Action>
295
296 =head1 AUTHORS
297
298 See L<Reaction::Class> for authors.
299
300 =head1 LICENSE
301
302 See L<Reaction::Class> for the license.
303
304 =cut