Mercurial > pub > Impl
annotate Lib/IMPL/Web/QueryHandler/PageFormat.pm @ 162:39c8788eded5
corrected PATH_INFO handling
author | wizard |
---|---|
date | Wed, 29 Dec 2010 16:55:24 +0300 |
parents | 3f09584bf189 |
children | 4267a2ac3d46 |
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; |
154 | 14 use URI; |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
15 use Error qw(:try); |
62 | 16 |
136 | 17 $Template::Plugin::URL::JOINT = '&'; |
18 | |
64 | 19 BEGIN { |
65 | 20 public property templatesCharset => prop_all; |
21 public property templatesBase => prop_all; | |
160 | 22 public property includes => prop_all | prop_list; |
116 | 23 public property pathinfoPrefix => prop_all; |
134 | 24 public property cache => prop_all; |
146 | 25 public property preprocess => prop_all; |
26 public property formatOutput => prop_all; | |
156
8638dd1374bf
Added template property to IMPL::Web::QueryHandler::PageFormat (this allows to specify exact template (filename, ref to a scalar, ref to a file handle)).
wizard
parents:
154
diff
changeset
|
27 public property template => prop_all; |
64 | 28 } |
29 | |
30 sub CTOR { | |
31 my ($this) = @_; | |
32 | |
65 | 33 $this->templatesCharset('utf-8') unless $this->templatesCharset; |
134 | 34 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; |
35 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; | |
64 | 36 } |
37 | |
62 | 38 sub Process { |
39 my ($this,$action,$nextHandler) = @_; | |
40 | |
146 | 41 my $doc = new IMPL::Web::TT::Document(cache => $this->cache, preprocess => $this->preprocess); |
62 | 42 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
43 try { |
97 | 44 |
45 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; | |
46 | |
136 | 47 my ($requestUri) = split /\?/, $ENV{REQUEST_URI}; |
48 | |
49 my $pathInfo; | |
50 my @root = (''); | |
51 my @base; | |
52 | |
162 | 53 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { |
54 # CGI with path info, for example | |
55 # /base/cgi-bin/myscript.cgi/path/info | |
56 # PATH_INFO will be /path/info | |
57 $pathInfo = $ENV{PATH_INFO}; | |
58 } else { | |
59 # usual url, for exmaple | |
60 # /base/script.cgi will have PATH_INFO /base/script.cgi | |
61 # /base/ will have PATH_INFO /base/index.cgi (if index.cgi is a DirectoryIndex) | |
62 $pathInfo = $ENV{PATH_INFO}; | |
63 | |
64 if (my $rx = $this->pathinfoPrefix) { | |
65 $requestUri =~ s/^($rx)//; | |
66 $pathInfo =~ s/^($rx)//; | |
67 push @root, grep $_, split /\//, $1 if $1; | |
68 } | |
116 | 69 } |
136 | 70 |
71 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); | |
72 | |
162 | 73 local $ENV{PATH_INFO} = $pathInfo; |
97 | 74 |
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
|
75 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
|
76 |
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
|
77 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
|
78 pop @pathContainer; |
65 | 79 |
154 | 80 $doc->LoadFile ( |
156
8638dd1374bf
Added template property to IMPL::Web::QueryHandler::PageFormat (this allows to specify exact template (filename, ref to a scalar, ref to a file handle)).
wizard
parents:
154
diff
changeset
|
81 ($this->template || File::Spec->catfile($this->templatesBase,@path)), |
154 | 82 $this->templatesCharset, |
160 | 83 [$this->templatesBase, $this->includes], |
154 | 84 { |
85 result => scalar($nextHandler->()), | |
86 action => $action, | |
87 app => $action->application, | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
88 |
154 | 89 absoluteUrl => sub { new URI(join ('/', @root, $_[0]) ) }, |
90 baseUrl => sub { new URI (join ('/', @root, @base, $_[0]) ) }, | |
91 relativeUrl => sub { new URI(join ('/', @root, @base, @pathContainer,$_[0]) ) }, | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
92 |
154 | 93 user => IMPL::Security::Context->current->principal, |
94 session => IMPL::Security::Context->current, | |
140
fb896377389f
to_json and escape_string functions for the templates
wizard
parents:
137
diff
changeset
|
95 |
154 | 96 to_json => \&to_json, |
97 escape_string => sub { $_[0] =~ s/"/"/g; $_[0] }, | |
98 } | |
99 ); | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
100 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
101 $action->response->contentType('text/html'); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
102 my $hOut = $action->response->streamBody; |
146 | 103 if ($this->formatOutput == 1) { |
104 my $tree = new HTML::TreeBuilder(); | |
105 try { | |
106 $tree->parse_content($doc->Render()); | |
107 print $hOut $tree->as_HTML('<>&'," ",{}); | |
108 } finally { | |
109 $tree->delete; | |
110 }; | |
111 } elsif ($this->formatOutput() == 2 ) { | |
112 (my $data = $doc->Render()) =~ s/\s+/ /g; | |
113 print $hOut $data; | |
114 } else { | |
115 print $hOut $doc->Render(); | |
116 } | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
117 } finally { |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
118 $doc->Dispose; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
119 }; |
62 | 120 } |
121 | |
154 | 122 sub URI::_query::new_params { |
123 my ($this,$params) = @_; | |
124 | |
125 my $clone = $this->clone; | |
126 if (ref $params eq 'HASH' ) { | |
127 my %newParams = ($clone->query_form , %$params); | |
128 $clone->query_form(map { $_, $newParams{$_} } sort keys %newParams ); | |
129 } | |
130 return $clone; | |
131 } | |
132 | |
65 | 133 1; |
134 | |
135 __END__ | |
136 | |
137 =pod | |
138 | |
139 =head1 NAME | |
140 | |
141 C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона. | |
142 | |
143 =head1 SYNOPSIS | |
144 | |
145 В файле конфигурации приложения | |
146 | |
147 =begin code xml | |
148 | |
149 <handlersQuery type="IMPL::Object::List"> | |
150 <item type="IMPL::Web::QueryHandler::PageFormat"> | |
151 <charsetTemplates>utf-8</charsetTemplates> | |
152 </item> | |
153 </handlersQuery> | |
154 | |
155 =end code xml | |
156 | |
157 Программно | |
158 | |
159 =begin code | |
160 | |
161 my $app = new IMPL::Web::Application(); | |
162 $app->handlersQuery->Add( | |
163 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); | |
164 ); | |
165 | |
166 =end | |
167 | |
168 =head1 DESCRIPTION | |
169 | |
170 Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется | |
171 из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>. | |
172 | |
173 Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала | |
174 обработчика запроса и переопределяет метод C<Process>. | |
175 | |
176 C<Serializable> | |
177 | |
178 =head1 MEMBERS | |
179 | |
180 =over | |
181 | |
182 =item C<CTOR(%props)> | |
183 | |
184 Создает новый экземпляр и заполняет свойства. | |
185 | |
186 =item C<[get,set] templatesCharset> | |
187 | |
188 Кодировка шаблонов. По умолчанию utf-8. | |
189 | |
190 =item C<[get,set] templatesBase> | |
191 | |
192 Каталог относительно которого ищется шаблон. | |
193 | |
194 =item C<[override] Process($action,$nextHandler)> | |
195 | |
196 Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление | |
197 для выполнения действий. | |
198 | |
199 =back | |
200 | |
201 =cut |