r20898@agaton (orig r755): wreis | 2008-07-24 00:32:56 +0100
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Action;
2
3use Reaction::Class;
4
d9a3266f 5use aliased 'Reaction::UI::ViewPort::Object';
6
7BEGIN { *DEBUG_EVENTS = \&Reaction::UI::ViewPort::DEBUG_EVENTS; }
8
ddccc6a2 9use aliased 'Reaction::UI::ViewPort::Field::Mutable::Text';
10use aliased 'Reaction::UI::ViewPort::Field::Mutable::Array';
11use aliased 'Reaction::UI::ViewPort::Field::Mutable::String';
12use aliased 'Reaction::UI::ViewPort::Field::Mutable::Number';
13use aliased 'Reaction::UI::ViewPort::Field::Mutable::Integer';
14use aliased 'Reaction::UI::ViewPort::Field::Mutable::Boolean';
15use aliased 'Reaction::UI::ViewPort::Field::Mutable::Password';
16use aliased 'Reaction::UI::ViewPort::Field::Mutable::DateTime';
17use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseOne';
18use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseMany';
19
ddd1dc65 20use aliased 'Reaction::UI::ViewPort::Field::Mutable::File';
bd1283bd 21#use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange';
ddccc6a2 22
2e07908b 23use Reaction::Types::Core qw/NonEmptySimpleStr/;
24
d9a3266f 25class Action is Object, which {
c8fbb8ad 26 has model => (is => 'ro', isa => 'Reaction::InterfaceModel::Action', required => 1);
27 #has '+model' => (isa => 'Reaction::InterfaceModel::Action');
2e07908b 28 has method => ( isa => NonEmptySimpleStr, is => 'rw', default => sub { 'post' } );
ddccc6a2 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' };
36d54b14 47 implements _build_apply_label => as{ 'apply' };
ddccc6a2 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) = @_;
36d54b14 53 foreach my $field ( @{ $self->fields } ) {
d9a3266f 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 }
5344cd2b 61 return 0;
d9a3266f 62 }
ddccc6a2 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 }
d9a3266f 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 }
ddccc6a2 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) = @_;
36d54b14 122 foreach my $field (@{$self->fields}) {
ddccc6a2 123 $field->sync_to_action; # get the field to populate the $action if possible
124 }
36d54b14 125 $self->model->sync_all;
126 foreach my $field (@{$self->fields}) {
ddccc6a2 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
d9a3266f 147 implements _build_fields_for_type_Reaction_Types_Core_SimpleStr => as {
ddccc6a2 148 my ($self, $attr, $args) = @_;
149 $self->_build_simple_field(attribute => $attr, class => String, %$args);
150 };
151
5ae92ec0 152 implements _build_fields_for_type_Reaction_Types_File_File => as {
ddd1dc65 153 my ($self, $attr, $args) = @_;
154 $self->_build_simple_field(attribute => $attr, class => File, %$args);
155 };
ddccc6a2 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);
cf1dfeb5 161 } else {
162 $self->_build_simple_field(attribute => $attr, class => Text, %$args);
ddccc6a2 163 }
ddccc6a2 164 };
165
d9a3266f 166 implements _build_fields_for_type_Reaction_Types_Core_Password => as {
ddccc6a2 167 my ($self, $attr, $args) = @_;
168 $self->_build_simple_field(attribute => $attr, class => Password, %$args);
169 };
170
abc28589 171 implements _build_fields_for_type_Reaction_Types_DateTime_DateTime => as {
ddccc6a2 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 {
5976ddc4 183 implements _build_fields_for_type_DBIx_Class_Row => as {
ddccc6a2 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,
13427367 197 layout => 'field/mutable/hidden_array',
ddccc6a2 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
bd1283bd 213Reaction::UI::ViewPort::Action
ddccc6a2 214
215=head1 SYNOPSIS
216
2f670e13 217 use aliased 'Reaction::UI::ViewPort::Action';
ddccc6a2 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
2dba7201 231This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a
232collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing.
ddccc6a2 233
234=head1 ATTRIBUTES
235
236=head2 model
237
238L<Reaction::InterfaceModel::Action>
239
240=head2 ok_label
241
242Default: 'ok'
243
244=head2 apply_label
245
246Default: 'apply'
247
248=head2 close_label_close
249
250Default: 'close'
251
252=head2 close_label_cancel
253
254This label is only shown when C<changed> is true.
255
256Default: 'cancel'
257
258=head2 fields
259
260=head2 can_apply
261
262=head2 can_close
263
264=head2 changed
265
266Returns true if a field has been edited.
267
268=head2 next_action
269
270=head2 on_apply_callback
271
272CodeRef.
273
274=head1 METHODS
275
276=head2 ok
277
278Calls C<apply>, and then C<close> if successful.
279
280=head2 close
281
282Pop viewport and proceed to C<next_action>.
283
284=head2 apply
285
286Attempt to save changes and update C<changed> attribute if required.
287
288=head1 SEE ALSO
289
2dba7201 290L<Reaction::UI::ViewPort::Object>
291
ddccc6a2 292L<Reaction::UI::ViewPort>
293
294L<Reaction::InterfaceModel::Action>
295
296=head1 AUTHORS
297
298See L<Reaction::Class> for authors.
299
300=head1 LICENSE
301
302See L<Reaction::Class> for the license.
303
304=cut