view Lib/IMPL/Web/QueryHandler/PageFormat.pm @ 121:92c850d0bdb9

Minor changes Fixed bug with templates base in the PageFormat module Added an ability to remove current security context with a specified
author wizard
date Tue, 08 Jun 2010 03:38:10 +0400
parents 1722ca51537c
children 0dce0470a3d8
line wrap: on
line source

package IMPL::Web::QueryHandler::PageFormat;
use base qw(IMPL::Web::QueryHandler IMPL::Object::Autofill);

__PACKAGE__->PassThroughArgs;

use IMPL::Class::Property;
use IMPL::Web::TT::Document;
use IMPL::Security::Context;
use File::Spec;
use Error qw(:try);

BEGIN {
	public property templatesCharset => prop_all;
	public property templatesBase => prop_all;
	public property defaultTarget => prop_all;
	public property pathinfoPrefix => prop_all;
}

sub CTOR {
	my ($this) = @_;
	
	$this->templatesCharset('utf-8') unless $this->templatesCharset;
}

sub Process {
	my ($this,$action,$nextHandler) = @_;
	
	my $doc = new IMPL::Web::TT::Document();
	
	try {

		$this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase;
		
		my $pathInfo = $ENV{PATH_INFO};
		my $prefixRoot = "";
		if (my $rx = $this->pathinfoPrefix) {
			$pathInfo =~ s/($rx)//;
			$prefixRoot = $1 if $1;
		}
		local $ENV{PATH_INFO} = $pathInfo || $this->defaultTarget;
		
		my @path = split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" );
		
		$doc->LoadFile ( File::Spec->catfile($this->templatesBase,@path), $this->templatesCharset, $this->templatesBase );
		$doc->AddVar( result => $nextHandler->() );
		$doc->AddVar( absoluteUrl => sub { "$prefixRoot/$_[0]" } );
		{
			local $@;
			$doc->AddVar( user => IMPL::Security::Context->current->principal );
			$doc->AddVar( session => IMPL::Security::Context->current );
			warn $@ if $@;
		}
		
		$action->response->contentType('text/html');
		my $hOut = $action->response->streamBody;
		print $hOut $doc->Render();
	} finally {
		$doc->Dispose;
	};
}

1;

__END__

=pod

=head1 NAME

C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона.

=head1 SYNOPSIS

В файле конфигурации приложения

=begin code xml

<handlersQuery type="IMPL::Object::List">
	<item type="IMPL::Web::QueryHandler::PageFormat">
		<charsetTemplates>utf-8</charsetTemplates>
	</item>
</handlersQuery>

=end code xml

Программно

=begin code

my $app = new IMPL::Web::Application();
$app->handlersQuery->Add(
	new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' );
);

=end

=head1 DESCRIPTION

Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется
из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>.

Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала
обработчика запроса и переопределяет метод C<Process>.

C<Serializable>

=head1 MEMBERS

=over

=item C<CTOR(%props)>

Создает новый экземпляр и заполняет свойства.

=item C<[get,set] templatesCharset>

Кодировка шаблонов. По умолчанию utf-8.

=item C<[get,set] templatesBase>

Каталог относительно которого ищется шаблон.

=item C<[override] Process($action,$nextHandler)>

Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление
для выполнения действий.

=back

=cut