Apache-App-Mercury

 view release on metacpan or  search on metacpan

Mercury/Controller.pm  view on Meta::CPAN


=cut

sub initialize {
    my ($self, $r) = @_;
    $self->{r} = $r;
    $self->{q} = CGI->new;
    $self->{mercury} = Apache::App::Mercury->new;
    $self->{mercury}->initialize($self);

    $self->{time} = time;
}

sub cleanup {
    my ($self) = @_;
    foreach (qw(r q)) {
	delete $self->{$_};
    }
    $self->{mercury}->cleanup;
}

# mod_perl handler
sub handler {
    my ($self, $r) = @_;
    # if called directly from mod_perl PerlHandler, swap $self and $r
    unless (ref $self eq __PACKAGE__ and ref $r eq "Apache") {
	$r = $self;
	$self = __PACKAGE__->new;
    }
    $self->initialize($r);
    eval { $self->{mercury}->content_handler; };
    if ($@) {
	$self->log_error;
	$self->write_response();
    } else {
	$self->write_response();
    }
    $self->cleanup;
}


=head1 ACCESSORS

=over 4

=item * infomsg([$msg])

Set or get a page-specific informational message.  The controller should
display this message in some prominent location on the resulting HTML page.

=item * pagetitle([$title])

Set or get the HTML page title.

=item * pagebody([$body])

Set or get the page body content.

=item * get_time()

Return the current unixtime, as returned by the Perl time() function.
This accessor is used for time synchronization throughout the application,
so your controller can keep a single time for each http request.

=item * sitemark([$mark])

Set or get a page-specific location mark, for logging purposes.

=back

=cut
sub get_time { return $_[0]->{time}; }
sub sitemark { $_[0]->{sitemark} = $_[1] if $_[1]; $_[0]->{sitemark}; }

sub infomsg { $_[0]->{msg} = $_[1] if $_[1]; $_[0]->{msg}; }
sub pagetitle { $_[0]->{title} = $_[1] if $_[1]; $_[0]->{title}; }
sub pagebody { $_[0]->{body} = $_[1] if $_[1]; $_[0]->{body}; }


sub write_response {
    my ($self) = @_;
    my $r = $self->{r};
    my $q = $self->{q};

    $r->content_type("text/html");

    if ($r->status != REDIRECT) {
	$self->{out} = '<html><head>';
	if ($self->{error_title}) {
	    $self->{out} .= $q->title($self->{error_title});
	} elsif ($self->{error}) {
	    $self->{out} .= $q->title("Apache::App::Mercury - Error");
	} else {
	    $self->{out} .= $q->title($self->{title});
	}
	$self->{out} .= '</head><body>';

	if ($self->{error}) {
	    $self->{out} .= $self->{error};
	} else {
	    $self->{out} .= $self->process_msg if defined $self->{msg};
	    $self->{out} .= $self->{body};
	}
	$self->{out} .= '</body></html>';

	$r->header_out("Location" => $r->uri);
	$r->header_out("Content-Length" => length($self->{out}));

	$r->status(DOCUMENT_FOLLOWS);

	if ($self->{cgi_headers}) {
	    $r->send_cgi_header($self->{cgi_headers} . "\n");
	} else {
	    $r->send_http_header;
	}
	$r->print($self->{out});
    } else {
	$r->send_http_header;
    }

    $self->{out} = '';



( run in 3.692 seconds using v1.01-cache-2.11-cpan-fe3c2283af0 )