Revision 3 (by ahitrov@rambler.ru, 2010/03/24 15:19:32) The CORE
package Contenido::MVC::Controller;

use strict;
use Contenido::Globals;
use Data::Dumper;
use HTTP::Status;

sub init {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self = {
            comp => undef,
            mason_args => {},
            action => undef,
            result => undef
            
        };
        return bless $self, $class;
}

# get path actions in format: [ { sub =>  sub_name, path => location }, .. ]
sub get_path_actions {
        return [];
}

# get regex actions in format: [ { sub => sub_name, regex => regex, sorder => undef or integer (0 - max priority) ], .. ]
sub get_regex_actions {
        return [];
}


sub comp {
        my $self = shift;
        my $comp = shift;
        my %mason_args = @_;

        $self->{comp} = $comp;
        if (%mason_args) {
            foreach (keys %mason_args) {
                next unless defined $_;
                $self->{mason_args}->{$_} = $mason_args{$_};
            }
        }

        return $self->{comp};
}

# ��������� ���� ����������, ������� ����� �������� � ARGS ���������� ����������
sub set_mason_args {
        my $self = shift;

        return undef unless $self->{mason_args} && ref($self->{mason_args}) eq 'HASH';

        my $apr = $self->get_r();

        foreach ( keys %{ $self->{mason_args} } ) {
                $request->{'_mason_request_args_'}{$_} = $self->{mason_args}->{$_};
        }

        return 1;
}

# ������� ������� �������� args
sub mason_args {
        my $self = shift;
        return $self->{mason_args};
}

# ������� $r - ������ Apache
sub get_r {
        my $self = shift;
        return $self->{r};
}

# ���������� ������ �������
sub prepare_request {
        my $self = shift;

        $self->{r}		= shift;
        $self->{comp}		= undef;
        $self->{mason_args}	= {};
        $self->{action}		= undef;
        $self->{_result}	= undef;
}

# ��������� action
sub set_action {
        my $self = shift;
        $self->{action} = shift;
}

# ������� ������� action
sub get_action {
        my $self = shift;
        return $self->{action};
}


# �������, ������� ����� ������� � ������ ��������� ������ action
sub begin {
        return undef;
}

# �������, ������� ����� ������� � ����� ��������� ������ action
sub end {
        return undef;
}


sub get_response {
    my $self = shift;
    return $self->{_result} ? $self->{_result} : undef;
}

sub is_response {
    my $self = shift;
    return defined $self->{_result} ? 1 : undef;
}

sub set_response {
    my $self = shift;
    my $status = shift;

    $self->set_mason_args();
    
    if ($status && HTTP::Status::status_message($status)) {
        $self->{_result} = $status;
    } else {
        $self->{_result} = 0;
    }

    return undef;
}


# �������� ���������� �����������
sub redirect {
    my $self = shift;
    my $url = shift;
    
    my $r = $self->get_r();
    $r->header_out("Location", $url);
    $r->status(302);

    return $self->set_response(302);
}


1;