AnnoCPAN

 view release on metacpan or  search on metacpan

lib/AnnoCPAN/Control.pm  view on Meta::CPAN

This is the main module that handles the AnnoCPAN web application. It handles
getting the CGI parameters, running the appropriate handlers, and making sure
that the appropriate templates are processed.

=head1 METHODS

=over

=item $class->new(%options)

Create a new AnnoCPAN control object. Options:

 cgi => cgi object
 tt  => template object

=cut

sub new {
    my ($class, %args) = @_;
    my $self = bless {
        log => [],
        cgi => $args{cgi} || CGI->new, 
        tt  => $args{tt}  || Template->new(
            INCLUDE_PATH => AnnoCPAN::Config->option('template_path'),
            PRE_PROCESS => 'util.tt',
            FILTERS => { myuri => \&myuri_filter },
        ),
    }, $class;
    $self;
}

=item $obj->run

Process the request. This includes figuring out the runmode, checking if 
the user is logged in, running the handler, printing the headers, and
processing the template.

=cut

sub run {
    my ($self) = @_;
    my $mode = $self->mode;
    $self->check_login;
    my ($vars, $template, $type);

    eval {
        ($vars, $template, $type) = $self->$mode;
    };
    if ($@) {
        $vars     = { error => $@ };
        if ($self->param('fast')) {
            $template = 'error';
        } else {
            ($vars, $template, $type) = $self->Main($vars);
        }
    }
    if ($template) {
        my $default_vars = $self->default_vars;
        $vars = { %$default_vars, %$vars };
        print $self->header(
            -charset => 'UTF-8',
            -cookie => $self->cookies,
            $type ? (-type => $type) : (),
        );
        $template .= '.html' unless $template =~ /\./;
        my $output = '';
        $self->process($template, $vars, \$output) or print $@;
        print $output;
    }
}

=item $obj->mode

Return the runmode. Runmodes must be made of word characters, begin with an
uppercase letter, and be a method in $obj.

=cut

sub mode {
    my ($self) = @_;
    my $mode = ucfirst $self->param('mode');
    #$self->_log("mode?=($mode)");
    $mode = 'Main' unless $mode =~ /^[A-Z]\w+$/ and $self->can($mode);
    #$self->_log("mode=($mode)");
    $mode;
}

=item $obj->cgi

Returns the CGI object.

=cut

sub cgi { shift->{cgi} }

=item $obj->tt

Returns the Template object.

=cut

sub tt  { shift->{tt} }

=item $obj->param(@_)

Get CGI parameters. Delegated to $self->cgi.

=cut

sub param { shift->cgi->param(@_) }

sub param_obj {
    my ($self, $name, $key) = @_;
    my (@values) = $self->param(lc $name)
        or  die "unspecified $name\n";
    my $class = "AnnoCPAN::DBI::$name";
    $key ||= 'id';
    my @objs;
    for my $value (@values) {
        $key ne 'id' or $value =~ /^\d+$/
            or  die "invalid $name: '$value'\n";



( run in 2.059 seconds using v1.01-cache-2.11-cpan-6aa56a78535 )