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