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