Line # Revision Author
1 3 ahitrov@rambler.ru package Contenido::MVC::Controller;
2
3 use strict;
4 use Contenido::Globals;
5 use Data::Dumper;
6 use HTTP::Status;
7
8 sub init {
9 my $proto = shift;
10 my $class = ref($proto) || $proto;
11 my $self = {
12 comp => undef,
13 mason_args => {},
14 action => undef,
15 result => undef
16
17 };
18 return bless $self, $class;
19 }
20
21 # get path actions in format: [ { sub => sub_name, path => location }, .. ]
22 sub get_path_actions {
23 return [];
24 }
25
26 # get regex actions in format: [ { sub => sub_name, regex => regex, sorder => undef or integer (0 - max priority) ], .. ]
27 sub get_regex_actions {
28 return [];
29 }
30
31
32 sub comp {
33 my $self = shift;
34 my $comp = shift;
35 my %mason_args = @_;
36
37 $self->{comp} = $comp;
38 if (%mason_args) {
39 foreach (keys %mason_args) {
40 next unless defined $_;
41 $self->{mason_args}->{$_} = $mason_args{$_};
42 }
43 }
44
45 return $self->{comp};
46 }
47
48 # ��������� ���� ����������, ������� ����� �������� � ARGS ���������� ����������
49 sub set_mason_args {
50 my $self = shift;
51
52 return undef unless $self->{mason_args} && ref($self->{mason_args}) eq 'HASH';
53
54 my $apr = $self->get_r();
55
56 foreach ( keys %{ $self->{mason_args} } ) {
57 $request->{'_mason_request_args_'}{$_} = $self->{mason_args}->{$_};
58 }
59
60 return 1;
61 }
62
63 # ������� ������� �������� args
64 sub mason_args {
65 my $self = shift;
66 return $self->{mason_args};
67 }
68
69 # ������� $r - ������ Apache
70 sub get_r {
71 my $self = shift;
72 return $self->{r};
73 }
74
75 # ���������� ������ �������
76 sub prepare_request {
77 my $self = shift;
78
79 $self->{r} = shift;
80 $self->{comp} = undef;
81 $self->{mason_args} = {};
82 $self->{action} = undef;
83 $self->{_result} = undef;
84 }
85
86 # ��������� action
87 sub set_action {
88 my $self = shift;
89 $self->{action} = shift;
90 }
91
92 # ������� ������� action
93 sub get_action {
94 my $self = shift;
95 return $self->{action};
96 }
97
98
99 # �������, ������� ����� ������� � ������ ��������� ������ action
100 sub begin {
101 return undef;
102 }
103
104 # �������, ������� ����� ������� � ����� ��������� ������ action
105 sub end {
106 return undef;
107 }
108
109
110 sub get_response {
111 my $self = shift;
112 return $self->{_result} ? $self->{_result} : undef;
113 }
114
115 sub is_response {
116 my $self = shift;
117 return defined $self->{_result} ? 1 : undef;
118 }
119
120 sub set_response {
121 my $self = shift;
122 my $status = shift;
123
124 $self->set_mason_args();
125
126 if ($status && HTTP::Status::status_message($status)) {
127 $self->{_result} = $status;
128 } else {
129 $self->{_result} = 0;
130 }
131
132 return undef;
133 }
134
135
136 # �������� ���������� �����������
137 sub redirect {
138 my $self = shift;
139 my $url = shift;
140
141 my $r = $self->get_r();
142 $r->header_out("Location", $url);
143 $r->status(302);
144
145 return $self->set_response(302);
146 }
147
148
149 1;
150