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