Mercurial > pub > Impl
annotate Lib/IMPL/Web/QueryHandler/PageFormat.pm @ 141:7245c39e941e
(no commit message)
author | wizard |
---|---|
date | Tue, 06 Jul 2010 17:26:48 +0400 |
parents | fb896377389f |
children | b56ebc31bf18 |
rev | line source |
---|---|
62 | 1 package IMPL::Web::QueryHandler::PageFormat; |
64 | 2 use base qw(IMPL::Web::QueryHandler IMPL::Object::Autofill); |
136 | 3 use strict; |
62 | 4 |
5 __PACKAGE__->PassThroughArgs; | |
6 | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
7 use JSON; |
62 | 8 use IMPL::Class::Property; |
77 | 9 use IMPL::Web::TT::Document; |
136 | 10 use Template::Plugin::URL; |
97 | 11 use IMPL::Security::Context; |
65 | 12 use File::Spec; |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
13 use Error qw(:try); |
62 | 14 |
136 | 15 $Template::Plugin::URL::JOINT = '&'; |
16 | |
64 | 17 BEGIN { |
65 | 18 public property templatesCharset => prop_all; |
19 public property templatesBase => prop_all; | |
97 | 20 public property defaultTarget => prop_all; |
116 | 21 public property pathinfoPrefix => prop_all; |
134 | 22 public property cache => prop_all; |
64 | 23 } |
24 | |
25 sub CTOR { | |
26 my ($this) = @_; | |
27 | |
65 | 28 $this->templatesCharset('utf-8') unless $this->templatesCharset; |
134 | 29 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; |
30 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; | |
64 | 31 } |
32 | |
62 | 33 sub Process { |
34 my ($this,$action,$nextHandler) = @_; | |
35 | |
134 | 36 my $doc = new IMPL::Web::TT::Document(cache => $this->cache); |
62 | 37 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
38 try { |
97 | 39 |
40 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; | |
41 | |
136 | 42 my ($requestUri) = split /\?/, $ENV{REQUEST_URI}; |
43 | |
44 my $pathInfo; | |
45 | |
46 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { | |
47 # CGI with path info | |
48 $pathInfo = $ENV{PATH_INFO}; | |
49 } else { | |
137 | 50 die "REQUEST_URI: $requestUri\nPATH_INFO: $ENV{PATH_INFO}" unless $requestUri eq $ENV{PATH_INFO}; |
136 | 51 } |
52 | |
53 my @root = (''); | |
54 my @base; | |
55 | |
116 | 56 if (my $rx = $this->pathinfoPrefix) { |
136 | 57 $requestUri =~ s/^($rx)//; |
58 push @root, grep $_, split /\//, $1 if $1; | |
116 | 59 } |
136 | 60 |
61 $pathInfo = $requestUri unless defined $pathInfo; | |
62 | |
63 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); | |
64 | |
116 | 65 local $ENV{PATH_INFO} = $pathInfo || $this->defaultTarget; |
97 | 66 |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
67 my @path = grep $_, split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" ); |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
68 |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
69 my @pathContainer = @path; |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
70 pop @pathContainer; |
65 | 71 |
121 | 72 $doc->LoadFile ( File::Spec->catfile($this->templatesBase,@path), $this->templatesCharset, $this->templatesBase ); |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
73 |
97 | 74 $doc->AddVar( result => $nextHandler->() ); |
129 | 75 $doc->AddVar( app => $action->application ); |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
76 |
136 | 77 $doc->AddVar( absoluteUrl => sub { join '/', @root, $_[0] } ); |
78 $doc->AddVar( baseUrl => sub { join '/', @root, @base, $_[0] } ); | |
79 $doc->AddVar( relativeUrl => sub { join '/', @root, @base, @pathContainer,$_[0] } ); | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
80 |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
81 $doc->AddVar( user => IMPL::Security::Context->current->principal ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
82 $doc->AddVar( session => IMPL::Security::Context->current ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
83 |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
84 $doc->AddVar( to_json => \&to_json ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
85 $doc->AddVar( escape_string => sub { $_[0] =~ s/"/"/g; $_[0] } ); |
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
86 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
87 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
88 $action->response->contentType('text/html'); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
89 my $hOut = $action->response->streamBody; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
90 print $hOut $doc->Render(); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
91 } finally { |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
92 $doc->Dispose; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
93 }; |
62 | 94 } |
95 | |
65 | 96 1; |
97 | |
98 __END__ | |
99 | |
100 =pod | |
101 | |
102 =head1 NAME | |
103 | |
104 C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона. | |
105 | |
106 =head1 SYNOPSIS | |
107 | |
108 В файле конфигурации приложения | |
109 | |
110 =begin code xml | |
111 | |
112 <handlersQuery type="IMPL::Object::List"> | |
113 <item type="IMPL::Web::QueryHandler::PageFormat"> | |
114 <charsetTemplates>utf-8</charsetTemplates> | |
115 </item> | |
116 </handlersQuery> | |
117 | |
118 =end code xml | |
119 | |
120 Программно | |
121 | |
122 =begin code | |
123 | |
124 my $app = new IMPL::Web::Application(); | |
125 $app->handlersQuery->Add( | |
126 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); | |
127 ); | |
128 | |
129 =end | |
130 | |
131 =head1 DESCRIPTION | |
132 | |
133 Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется | |
134 из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>. | |
135 | |
136 Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала | |
137 обработчика запроса и переопределяет метод C<Process>. | |
138 | |
139 C<Serializable> | |
140 | |
141 =head1 MEMBERS | |
142 | |
143 =over | |
144 | |
145 =item C<CTOR(%props)> | |
146 | |
147 Создает новый экземпляр и заполняет свойства. | |
148 | |
149 =item C<[get,set] templatesCharset> | |
150 | |
151 Кодировка шаблонов. По умолчанию utf-8. | |
152 | |
153 =item C<[get,set] templatesBase> | |
154 | |
155 Каталог относительно которого ищется шаблон. | |
156 | |
157 =item C<[override] Process($action,$nextHandler)> | |
158 | |
159 Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление | |
160 для выполнения действий. | |
161 | |
162 =back | |
163 | |
164 =cut |