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