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