Mercurial > pub > Impl
changeset 112:0ed8e2541b1c
Form processing mechanism
author | wizard |
---|---|
date | Tue, 18 May 2010 17:59:31 +0400 |
parents | 6c25ea91c985 |
children | 7b14e0122b79 |
files | Lib/IMPL/DOM/Navigator/Builder.pm Lib/IMPL/DOM/Transform/PostToDOM.pm Lib/IMPL/Web/Application/ControllerUnit.pm |
diffstat | 3 files changed, 111 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Navigator/Builder.pm Tue May 18 01:33:37 2010 +0400 +++ b/Lib/IMPL/DOM/Navigator/Builder.pm Tue May 18 17:59:31 2010 +0400 @@ -17,7 +17,7 @@ } our %CTOR = ( - 'IMPL::DOM::Navigator' => sub { IMPL::DOM::Document::Empty; } + 'IMPL::DOM::Navigator' => sub { IMPL::DOM::Document->Empty; } ); sub CTOR { @@ -108,6 +108,20 @@ $this->SUPER::Back(); } +sub saveState { + my ($this) = @_; + + $this->{$_schemaNavi}->saveState; + $this->SUPER::saveState; +} + +sub restoreState { + my ($this) = @_; + + $this->{$_schemaNavi}->restoreState; + $this->SUPER::restoreState; +} + 1; __END__
--- a/Lib/IMPL/DOM/Transform/PostToDOM.pm Tue May 18 01:33:37 2010 +0400 +++ b/Lib/IMPL/DOM/Transform/PostToDOM.pm Tue May 18 17:59:31 2010 +0400 @@ -1,8 +1,8 @@ -package IMPL::DOM::PostToDOM; +package IMPL::DOM::Transform::PostToDOM; use strict; use warnings; -use IMPL::DOM::Navigator; +use IMPL::DOM::Navigator::Builder; use IMPL::Class::Property; use base qw(IMPL::Transform); @@ -21,43 +21,38 @@ sub CTOR { my ($this,$docClass,$docSchema) = @_; - $docClass ||= 'IMPL::DOM::Document' + $docClass ||= 'IMPL::DOM::Document'; + + $this->_navi( + IMPL::DOM::Navigator::Builder->new( + $docClass, + $docSchema + ) + ); } sub TransformPostData { my ($this,$data) = @_; - my $navi = $this->Navigator; + my $navi = $this->_navi; + + my % while (my ($key,$value) = each %$data) { # TODO: review + $navi->save; my $node = $navi->Navigate(split /\//, $key); $node->nodeValue($value); + $navi->resore; } return $navi->Document; } -package IMPL::DOM::PostToDOM::Navigator; -use base qw(IMPL::DOM::Navigator::Builder); - -__PACKAGE__->PassThroughArgs; +sub -sub Navigate { - my ($this,@path) = @_; - - if (@path > 1) { - my $node; - foreach my $query (@path) { - unless($this->dosafe(sub { - $node = $this->SUPER::Navigate($query); - })) { - $node = $this->NavigateCreate($query); - } - } - } else { - die new IMPL::InvalidArgumentException("A path is a required parameter"); - } +sub TransformErrors { + return $_[0]->_navi->BuildErrors; } 1;
--- a/Lib/IMPL/Web/Application/ControllerUnit.pm Tue May 18 01:33:37 2010 +0400 +++ b/Lib/IMPL/Web/Application/ControllerUnit.pm Tue May 18 17:59:31 2010 +0400 @@ -1,8 +1,17 @@ package IMPL::Web::Application::ControllerUnit; - +use strict; use base qw(IMPL::Object); use IMPL::Class::Property; +use IMPL::DOM::Transform::PostToDOM; +use IMPL::DOM::Schema; + +use constant { + CONTROLLER_METHODS => 'controller_methods', + STATE_CORRECT => 'correct', + STATE_NEW => 'new', + STATE_INVALID => 'invalid' +}; BEGIN { public property action => prop_get | owner_set; @@ -14,24 +23,77 @@ } sub CTOR { - my ($this,$action) = @_; + my ($this,$action,$args) = @_; $this->action($action); $this->application($action->application); $this->query($action->query); + + $this->$_($args->{$_}) foreach qw(formData formSchema formErrors); +} + +sub forms { + my ($self,%forms) = @_; + + while ( my ($method,$info) = each %forms ) { + die new IMPL::Exception("A method doesn't exists in the controller",$self,$method) unless $self->can($method); + if ( not ref $info ) { + $self->class_data(CONTROLLER_METHODS)->{$method} = { + wrapper => 'FormWrapper', + schema => $info + }; + } elsif (ref $info eq 'HASH') { + die new IMPL::Exception("A schema must be specified",$self,$method) unless $info->{schema}; + + $self->class_data(CONTROLLER_METHODS)->{$method} = { + wrapper => 'FormWrapper', + schema => $info->{schema} + }; + } else { + die new IMPL::Exception("Unsupported method information",$self,$method); + } + } +} + +sub transactions { + } sub InvokeAction { my ($self,$method,$action) = @_; - if ($self->can($method)) { - my $unit = $self->new($action); - $unit->$method(); + if (my $methodInfo = $self->class_data(CONTROLLER_METHODS)->{$method}) { + if (my $wrapper = $methodInfo->{wrapper}) { + return $self->$wrapper($method,$action,$methodInfo); + } else { + return $self->TransactionWrapper($method,$action,$methodInfo); + } } else { die new IMPL::InvalidOperationException("Invalid method call",$self,$method); } } +sub TransactionWrapper { + my ($self,$method,$action,$methodInfo) = @_; + + my $unit = $self->new($action); + return $unit->$method(); +} + +sub FormWrapper { + my ($this,$method,$action,$methodInfo) = @_; + + my $schema = $this->loadSchema($methodInfo->{schema}); + + my $process = $this->query->param('process') || 0; + + + + my %result = ( + + ); +} + 1; __END__ @@ -142,7 +204,17 @@ =item C<InvokeAction($method,$action)> Конструирует контекст выполнения транзакции, может быть переопределен для конструирования контекста по -своимправилам. +своим правилам. + +=item C<TransactionWrapper($method,$action,$methodInfo)> + +Обертка для конструирования простых транзакций, может быть переопределен для конструирования контекста по +своим правилам. + +=item C<FormWrapper($method,$action,$methodInfo)> + +Обертка для конструирования форм, может быть переопределен для конструирования контекста по +своим правилам. =back