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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
1 package IMPL::Web::QueryHandler::PageFormat;
64
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
2 use base qw(IMPL::Web::QueryHandler IMPL::Object::Autofill);
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
3 use strict;
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
4
c64bd1bf727d Web application
wizard
parents:
diff changeset
5 __PACKAGE__->PassThroughArgs;
c64bd1bf727d Web application
wizard
parents:
diff changeset
6
140
fb896377389f to_json and escape_string functions for the templates
wizard
parents: 137
diff changeset
7 use JSON;
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
8 use IMPL::Class::Property;
77
9d24db321029 Refactoring Web::TT
wizard
parents: 65
diff changeset
9 use IMPL::Web::TT::Document;
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
10 use Template::Plugin::URL;
97
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
11 use IMPL::Security::Context;
65
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
12 use File::Spec;
146
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
13 use HTML::TreeBuilder;
154
eb478083f72b Url support
wizard
parents: 146
diff changeset
14 use URI;
63
76b878ad6596 Added serialization support for the IMPL::Object::List
wizard
parents: 62
diff changeset
15 use Error qw(:try);
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
16
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
17 $Template::Plugin::URL::JOINT = '&';
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
18
64
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
19 BEGIN {
65
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
20 public property templatesCharset => prop_all;
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
21 public property templatesBase => prop_all;
97
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
22 public property defaultTarget => prop_all;
116
1722ca51537c minor changes.
wizard
parents: 107
diff changeset
23 public property pathinfoPrefix => prop_all;
134
44977efed303 Significant performance optimizations
wizard
parents: 129
diff changeset
24 public property cache => prop_all;
146
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
25 public property preprocess => prop_all;
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
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
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
28 }
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
29
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
30 sub CTOR {
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
31 my ($this) = @_;
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
32
65
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
33 $this->templatesCharset('utf-8') unless $this->templatesCharset;
134
44977efed303 Significant performance optimizations
wizard
parents: 129
diff changeset
34 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache;
44977efed303 Significant performance optimizations
wizard
parents: 129
diff changeset
35 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase;
64
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
36 }
259cd3df6e53 Doc generation
wizard
parents: 63
diff changeset
37
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
38 sub Process {
c64bd1bf727d Web application
wizard
parents:
diff changeset
39 my ($this,$action,$nextHandler) = @_;
c64bd1bf727d Web application
wizard
parents:
diff changeset
40
146
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
41 my $doc = new IMPL::Web::TT::Document(cache => $this->cache, preprocess => $this->preprocess);
62
c64bd1bf727d Web application
wizard
parents:
diff changeset
42
63
76b878ad6596 Added serialization support for the IMPL::Object::List
wizard
parents: 62
diff changeset
43 try {
97
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
44
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
45 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase;
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
46
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
47 my ($requestUri) = split /\?/, $ENV{REQUEST_URI};
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
48
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
49 my $pathInfo;
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
50
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
51 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) {
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
52 # CGI with path info
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
53 $pathInfo = $ENV{PATH_INFO};
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
54 } else {
137
6975cd4973d1 minor fix
wizard
parents: 136
diff changeset
55 die "REQUEST_URI: $requestUri\nPATH_INFO: $ENV{PATH_INFO}" unless $requestUri eq $ENV{PATH_INFO};
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
56 }
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
57
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
58 my @root = ('');
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
59 my @base;
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
60
116
1722ca51537c minor changes.
wizard
parents: 107
diff changeset
61 if (my $rx = $this->pathinfoPrefix) {
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
62 $requestUri =~ s/^($rx)//;
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
63 push @root, grep $_, split /\//, $1 if $1;
116
1722ca51537c minor changes.
wizard
parents: 107
diff changeset
64 }
136
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
65
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
66 $pathInfo = $requestUri unless defined $pathInfo;
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
67
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
68 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri);
f6af119ac741 url routines for templates
wizard
parents: 134
diff changeset
69
116
1722ca51537c minor changes.
wizard
parents: 107
diff changeset
70 local $ENV{PATH_INFO} = $pathInfo || $this->defaultTarget;
97
964587c5183c Added SecureCall to Web QueryHandlers stack
wizard
parents: 77
diff changeset
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
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
76
154
eb478083f72b Url support
wizard
parents: 146
diff changeset
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
eb478083f72b Url support
wizard
parents: 146
diff changeset
79 $this->templatesCharset,
eb478083f72b Url support
wizard
parents: 146
diff changeset
80 $this->templatesBase,
eb478083f72b Url support
wizard
parents: 146
diff changeset
81 {
eb478083f72b Url support
wizard
parents: 146
diff changeset
82 result => scalar($nextHandler->()),
eb478083f72b Url support
wizard
parents: 146
diff changeset
83 action => $action,
eb478083f72b Url support
wizard
parents: 146
diff changeset
84 app => $action->application,
140
fb896377389f to_json and escape_string functions for the templates
wizard
parents: 137
diff changeset
85
154
eb478083f72b Url support
wizard
parents: 146
diff changeset
86 absoluteUrl => sub { new URI(join ('/', @root, $_[0]) ) },
eb478083f72b Url support
wizard
parents: 146
diff changeset
87 baseUrl => sub { new URI (join ('/', @root, @base, $_[0]) ) },
eb478083f72b Url support
wizard
parents: 146
diff changeset
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
eb478083f72b Url support
wizard
parents: 146
diff changeset
90 user => IMPL::Security::Context->current->principal,
eb478083f72b Url support
wizard
parents: 146
diff changeset
91 session => IMPL::Security::Context->current,
140
fb896377389f to_json and escape_string functions for the templates
wizard
parents: 137
diff changeset
92
154
eb478083f72b Url support
wizard
parents: 146
diff changeset
93 to_json => \&to_json,
eb478083f72b Url support
wizard
parents: 146
diff changeset
94 escape_string => sub { $_[0] =~ s/"/"/g; $_[0] },
eb478083f72b Url support
wizard
parents: 146
diff changeset
95 }
eb478083f72b Url support
wizard
parents: 146
diff changeset
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
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
100 if ($this->formatOutput == 1) {
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
101 my $tree = new HTML::TreeBuilder();
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
102 try {
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
103 $tree->parse_content($doc->Render());
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
104 print $hOut $tree->as_HTML('<>&'," ",{});
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
105 } finally {
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
106 $tree->delete;
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
107 };
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
108 } elsif ($this->formatOutput() == 2 ) {
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
109 (my $data = $doc->Render()) =~ s/\s+/ /g;
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
110 print $hOut $data;
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
111 } else {
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
112 print $hOut $doc->Render();
60fd224f3e3c fixed cgi parameters processing
wizard
parents: 144
diff changeset
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
c64bd1bf727d Web application
wizard
parents:
diff changeset
117 }
c64bd1bf727d Web application
wizard
parents:
diff changeset
118
154
eb478083f72b Url support
wizard
parents: 146
diff changeset
119 sub URI::_query::new_params {
eb478083f72b Url support
wizard
parents: 146
diff changeset
120 my ($this,$params) = @_;
eb478083f72b Url support
wizard
parents: 146
diff changeset
121
eb478083f72b Url support
wizard
parents: 146
diff changeset
122 my $clone = $this->clone;
eb478083f72b Url support
wizard
parents: 146
diff changeset
123 if (ref $params eq 'HASH' ) {
eb478083f72b Url support
wizard
parents: 146
diff changeset
124 my %newParams = ($clone->query_form , %$params);
eb478083f72b Url support
wizard
parents: 146
diff changeset
125 $clone->query_form(map { $_, $newParams{$_} } sort keys %newParams );
eb478083f72b Url support
wizard
parents: 146
diff changeset
126 }
eb478083f72b Url support
wizard
parents: 146
diff changeset
127 return $clone;
eb478083f72b Url support
wizard
parents: 146
diff changeset
128 }
eb478083f72b Url support
wizard
parents: 146
diff changeset
129
65
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
130 1;
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
131
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
132 __END__
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
133
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
134 =pod
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
135
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
136 =head1 NAME
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
137
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
138 C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
139
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
140 =head1 SYNOPSIS
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
141
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
142 В файле конфигурации приложения
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
143
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
144 =begin code xml
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
145
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
146 <handlersQuery type="IMPL::Object::List">
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
147 <item type="IMPL::Web::QueryHandler::PageFormat">
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
148 <charsetTemplates>utf-8</charsetTemplates>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
149 </item>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
150 </handlersQuery>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
151
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
152 =end code xml
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
153
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
154 Программно
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
155
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
156 =begin code
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
157
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
158 my $app = new IMPL::Web::Application();
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
159 $app->handlersQuery->Add(
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
160 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' );
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
161 );
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
162
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
163 =end
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
164
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
165 =head1 DESCRIPTION
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
166
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
167 Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
168 из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
169
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
170 Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
171 обработчика запроса и переопределяет метод C<Process>.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
172
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
173 C<Serializable>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
174
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
175 =head1 MEMBERS
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
176
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
177 =over
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
178
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
179 =item C<CTOR(%props)>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
180
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
181 Создает новый экземпляр и заполняет свойства.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
182
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
183 =item C<[get,set] templatesCharset>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
184
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
185 Кодировка шаблонов. По умолчанию utf-8.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
186
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
187 =item C<[get,set] templatesBase>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
188
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
189 Каталог относительно которого ищется шаблон.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
190
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
191 =item C<[override] Process($action,$nextHandler)>
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
192
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
193 Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
194 для выполнения действий.
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
195
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
196 =back
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
197
2840c4c85db8 Application configuration improvements
wizard
parents: 64
diff changeset
198 =cut