Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Action; |
2 | |
3 | use Reaction::Class; |
4 | |
08451970 |
5 | use aliased 'Reaction::UI::ViewPort::Object'; |
6 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Text'; |
7 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Array'; |
8 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::String'; |
9 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Number'; |
10 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Integer'; |
11 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Boolean'; |
12 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::Password'; |
13 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::DateTime'; |
14 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseOne'; |
15 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::ChooseMany'; |
16 | |
17 | use aliased 'Reaction::UI::ViewPort::Field::Mutable::File'; |
18 | #use aliased 'Reaction::UI::ViewPort::Field::Mutable::TimeRange'; |
19 | |
20 | use MooseX::Types::Moose qw/Int/; |
21 | use Reaction::Types::Core qw/NonEmptySimpleStr/; |
22 | |
23 | use namespace::clean -except => [ qw(meta) ]; |
24 | extends Object; |
25 | with 'Reaction::UI::ViewPort::Action::Role::OK'; |
26 | |
27 | has model => ( |
28 | is => 'ro', |
29 | isa => 'Reaction::InterfaceModel::Action', |
30 | required => 1 |
31 | ); |
32 | |
33 | has 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. |
41 | has method => ( |
42 | is => 'rw', |
43 | isa => NonEmptySimpleStr, |
44 | default => sub { 'post' } |
45 | ); |
46 | |
47 | sub 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 | |
58 | sub do_apply { |
59 | shift->model->do_apply; |
60 | } |
61 | |
62 | after apply_child_events => sub { |
63 | # interrupt here because fields will have been updated |
64 | my ($self) = @_; |
65 | $self->sync_action_from_fields; |
66 | }; |
67 | |
68 | sub 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 | |
79 | sub _build_fields_for_type_Num { |
80 | my ($self, $attr, $args) = @_; |
81 | $self->_build_simple_field(attribute => $attr, class => Number, %$args); |
82 | } |
83 | |
84 | sub _build_fields_for_type_Int { |
85 | my ($self, $attr, $args) = @_; |
86 | $self->_build_simple_field(attribute => $attr, class => Integer, %$args); |
87 | } |
88 | |
89 | sub _build_fields_for_type_Bool { |
90 | my ($self, $attr, $args) = @_; |
91 | $self->_build_simple_field(attribute => $attr, class => Boolean, %$args); |
92 | } |
93 | |
94 | sub _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 | |
99 | sub _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 | |
104 | sub _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 | |
113 | sub _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 | |
118 | sub _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 | |
123 | sub _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 { |
130 | sub _build_fields_for_type_DBIx_Class_Row { |
131 | my ($self, $attr, $args) = @_; |
132 | $self->_build_simple_field(attribute => $attr, class => ChooseOne, %$args); |
133 | } |
134 | |
135 | sub _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 |
151 | 1; |
ddccc6a2 |
152 | |
114916fc |
153 | __END__; |
ddccc6a2 |
154 | |
08451970 |
155 | =head1 NAME |
156 | |
157 | Reaction::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 | |
175 | This subclass of L<Reaction::UI::ViewPort::Object> is used for rendering a |
176 | collection of C<Reaction::UI::ViewPort::Field::Mutable::*> objects for user editing. |
177 | |
178 | =head1 ATTRIBUTES |
179 | |
180 | =head2 model |
181 | |
182 | L<Reaction::InterfaceModel::Action> |
183 | |
184 | =head2 ok_label |
185 | |
186 | Default: 'ok' |
187 | |
188 | =head2 apply_label |
189 | |
190 | Default: 'apply' |
191 | |
192 | =head2 close_label_close |
193 | |
194 | Default: 'close' |
195 | |
196 | =head2 close_label_cancel |
197 | |
198 | This label is only shown when C<changed> is true. |
199 | |
200 | Default: 'cancel' |
201 | |
202 | =head2 fields |
203 | |
204 | =head2 can_apply |
205 | |
206 | =head2 can_close |
207 | |
208 | =head2 changed |
209 | |
210 | Returns true if a field has been edited. |
211 | |
212 | =head2 next_action |
213 | |
214 | =head2 on_apply_callback |
215 | |
216 | CodeRef. |
217 | |
218 | =head1 METHODS |
219 | |
220 | =head2 ok |
221 | |
222 | Calls C<apply>, and then C<close> if successful. |
223 | |
224 | =head2 close |
225 | |
226 | Pop viewport and proceed to C<next_action>. |
227 | |
228 | =head2 apply |
229 | |
230 | Attempt to save changes and update C<changed> attribute if required. |
231 | |
232 | =head1 SEE ALSO |
233 | |
234 | L<Reaction::UI::ViewPort::Object> |
235 | |
236 | L<Reaction::UI::ViewPort> |
237 | |
238 | L<Reaction::InterfaceModel::Action> |
239 | |
240 | =head1 AUTHORS |
241 | |
242 | See L<Reaction::Class> for authors. |
243 | |
244 | =head1 LICENSE |
245 | |
246 | See L<Reaction::Class> for the license. |
247 | |
248 | =cut |
249 | |