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