memory leaks on CRUD fixed
[catagits/Reaction.git] / lib / Reaction / UI / Controller.pm
CommitLineData
7adfd53f 1package Reaction::UI::Controller;
2
1810d302 3use base qw(
4 Catalyst::Controller
5 Catalyst::Component::ACCEPT_CONTEXT
6 Reaction::Object
7);
8
7adfd53f 9use Reaction::Class;
10
11sub push_viewport {
12 my $self = shift;
1810d302 13 my $c = $self->context;
14 my $focus_stack = $c->stash->{focus_stack};
7adfd53f 15 my ($class, @proto_args) = @_;
16 my %args;
7adfd53f 17 if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
18 if (ref($vp_attr) eq 'ARRAY') {
19 $vp_attr = $vp_attr->[0];
20 }
21 if (ref($vp_attr) eq 'HASH') {
22 if (my $conf_class = delete $vp_attr->{class}) {
23 $class = $conf_class;
24 }
504e2c44 25 %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
7adfd53f 26 } else {
27 $class = $vp_attr;
28 %args = @proto_args;
29 }
30 } else {
31 %args = @proto_args;
32 }
33
34 $args{ctx} = $c;
35
36 if (exists $args{next_action} && !ref($args{next_action})) {
37 $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
38 }
39 $focus_stack->push_viewport($class, %args);
40}
41
42sub pop_viewport {
1810d302 43 return shift->context->stash->{focus_stack}->pop_viewport;
7adfd53f 44}
45
46sub pop_viewports_to {
47 my ($self, $vp) = @_;
1810d302 48 return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
7adfd53f 49}
50
51sub redirect_to {
52 my ($self, $c, $to, $cap, $args, $attrs) = @_;
53
54 #the confess calls could be changed later to $c->log ?
55 my $action;
9cf5bc0b 56 my $reftype = ref($to);
57 if( $reftype eq '' ){
58 $action = $self->action_for($to);
59 confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
60 } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
61 $action = $c->controller($to->[0])->action_for($to->[1]);
62 confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
63 } elsif( blessed $to && $to->isa('Catalyst::Action') ){
64 $action = $to;
7adfd53f 65 } else{
9cf5bc0b 66 confess("Failed to locate action from ${to}");
7adfd53f 67 }
68
69 $cap ||= $c->req->captures;
70 $args ||= $c->req->args;
71 $attrs ||= {};
72 my $uri = $c->uri_for($action, $cap, @$args, $attrs);
73 $c->res->redirect($uri);
74}
75
761;
f2756356 77
78__END__;
79
80=head1 NAME
81
bd1aa0c2 82Reaction::UI::Controller
f2756356 83
84=head1 DESCRIPTION
85
86Base Reaction Controller class. Inherits from:
87
88=over 4
89
90=item L<Catalyst::Controller>
91=item L<Catalyst::Component::ACCEPT_CONTEXT>
92=item L<Reaction::Object>
93
94=back
95
96=head1 METHODS
97
98=head2 push_viewport $vp_class, %args
99
f1cd5548 100Creates a new instance of the L<Reaction::UI::ViewPort> class
101($vp_class) using the rest of the arguments given (%args). Defaults of
102the action can be overridden by using the C<ViewPort> key in the
103controller configuration. For example to override the default number
104of items in a CRUD list action:
105
106__PACKAGE__->config(
107 action => {
108 list => { ViewPort => { per_page => 50 } },
109 }
110 );
111
112The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
113the stash, and also returned to the calling code.
114
115Related items:
116
117=over
118
119=item L<Reaction::UI::Controller::Root>
120=item L<Reaction::UI::Window>
121
122=back
f2756356 123
124TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing
125
126=head2 pop_viewport
127
128=head2 pop_viewport_to $vp
129
f1cd5548 130Call L<Reaction::UI::FocusStack/pop_viewport> or
131L<Reaction::UI::FocusStack/pop_viewport_to> on
132the C<< $c->stash->{focus_stack} >>.
f2756356 133
134=head2 redirect_to $c, $to, $captures, $args, $attrs
135
f1cd5548 136Construct a URI and redirect to it.
137
138$to can be:
139
140=over
f2756356 141
f1cd5548 142=item The name of an action in the current controller.
f2756356 143
f1cd5548 144=item A L<Catalyst::Action> instance.
145
146=item An arrayref of controller name and the name of an action in that
147controller.
148
149=back
f2756356 150
f1cd5548 151$captures and $args default to the current requests $captures and
152$args if not supplied.
f2756356 153
154=head1 AUTHORS
155
156See L<Reaction::Class> for authors.
157
158=head1 LICENSE
159
160See L<Reaction::Class> for the license.
161
162=cut