Parley

 view release on metacpan or  search on metacpan

lib/Parley/Controller/Root.pm  view on Meta::CPAN


use Parley::App::Terms qw( :terms );
use Parley::App::Error qw( :methods );

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

sub begin :Private {
    my ($self, $c) = @_;

    # deal with access banned by IP
    my $ip = $c->request->address;
    my $access_banned =
        $c->model('ParleyDB::IpBan')->is_access_banned($ip);
    if ($access_banned) {
        $c->stash->{template} = 'user/access_ip_banned';
        return;
    }

    return 1;
}

# pre-populate values in the stash if we're given "appropriate" information:
# - _authed_user
# - _current_post
# - _current_thread
# - _current_forum
sub auto : Private {
    my ($self, $c) = @_;

    # the cookie name for language choice(s)
    my $cookie_name = $c->config->{name} . q{_languages};

    ##################################################
    # do we have a request for a chosen language?
    ##################################################
    if (defined $c->request->param('lang')) {
        $c->response->cookies->{ $cookie_name } = {
            value       => $c->request->param('lang'),
            expires     => '+14d',
        };
        # redirect back to the page they were on
        $c->response->redirect(
            $c->request->referer()
        );
        return;
    }

    # preserve cookies
    if ($c->request->cookie($cookie_name)) {
        $c->response->cookies->{ $cookie_name } = {
            value       => $c->request->cookie($cookie_name)->value,
            expires     => '+14d',
        };

        # push cookie saved languages onto list of i18n languages the user accepts
        my (@languages);
        # fetch cookie language prefs (if any)
        push @languages,
            split(
                /\s+/,
                $c->request->cookie($cookie_name)->value
            )
        ;
        # get current list of accepted languages
        push @languages, @{$c->languages};
        # make the list have unique entries
        @languages = uniq @languages;
        # set new list of accepted languages
        $c->languages(
            \@languages
        );
    }

    # get a list of (all/available) forums
    $c->stash->{available_forums} =
        $c->model('ParleyDB::Forum')->available_list();

    ##################################################
    # do we have a post id in the URL?
    ##################################################
    if (defined $c->request->param('post')) {
        # make sure the paramter looks sane
        if (not $c->request->param('post') =~ m{\A\d+\z}) {
            $c->stash->{error}{message} =
                  $c->localize('non-integer post id passed')
                . ': ['
                . $c->request->param('post')
                . ']';
            return;
        }

        # get the matching post
        $c->_current_post(
            $c->model('ParleyDB::Post')->record_from_id(
                $c->request->param('post')
            )
        );

        # set the current_thread from the current_post
        $c->_current_thread(
            $c->_current_post->thread()
        );

        # set the current_forum from the current thread
        $c->_current_forum(
            $c->_current_thread->forum()
        );
    }
    ##################################################
    # (elsif) do we have a thread id in the URL?
    ##################################################
    elsif (defined $c->request->param('thread')) {
        # make sure the paramter looks sane
        if (not $c->request->param('thread') =~ m{\A\d+\z}) {
            $c->stash->{error}{message} =
                  $c->localize('non-integer thread id passed')
                . ': ['



( run in 0.582 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )