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