Mercurial > pub > Impl
comparison Lib/IMPL/Web/Application/ControllerUnit.pm @ 112:0ed8e2541b1c
Form processing mechanism
| author | wizard |
|---|---|
| date | Tue, 18 May 2010 17:59:31 +0400 |
| parents | 6c25ea91c985 |
| children | 7b14e0122b79 |
comparison
equal
deleted
inserted
replaced
| 111:6c25ea91c985 | 112:0ed8e2541b1c |
|---|---|
| 1 package IMPL::Web::Application::ControllerUnit; | 1 package IMPL::Web::Application::ControllerUnit; |
| 2 | 2 use strict; |
| 3 use base qw(IMPL::Object); | 3 use base qw(IMPL::Object); |
| 4 | 4 |
| 5 use IMPL::Class::Property; | 5 use IMPL::Class::Property; |
| 6 use IMPL::DOM::Transform::PostToDOM; | |
| 7 use IMPL::DOM::Schema; | |
| 8 | |
| 9 use constant { | |
| 10 CONTROLLER_METHODS => 'controller_methods', | |
| 11 STATE_CORRECT => 'correct', | |
| 12 STATE_NEW => 'new', | |
| 13 STATE_INVALID => 'invalid' | |
| 14 }; | |
| 6 | 15 |
| 7 BEGIN { | 16 BEGIN { |
| 8 public property action => prop_get | owner_set; | 17 public property action => prop_get | owner_set; |
| 9 public property application => prop_get | owner_set; | 18 public property application => prop_get | owner_set; |
| 10 public property query => prop_get | owner_set; | 19 public property query => prop_get | owner_set; |
| 12 public property formSchema => prop_get | owner_set; | 21 public property formSchema => prop_get | owner_set; |
| 13 public property formErrors => prop_get | owner_set; | 22 public property formErrors => prop_get | owner_set; |
| 14 } | 23 } |
| 15 | 24 |
| 16 sub CTOR { | 25 sub CTOR { |
| 17 my ($this,$action) = @_; | 26 my ($this,$action,$args) = @_; |
| 18 | 27 |
| 19 $this->action($action); | 28 $this->action($action); |
| 20 $this->application($action->application); | 29 $this->application($action->application); |
| 21 $this->query($action->query); | 30 $this->query($action->query); |
| 31 | |
| 32 $this->$_($args->{$_}) foreach qw(formData formSchema formErrors); | |
| 33 } | |
| 34 | |
| 35 sub forms { | |
| 36 my ($self,%forms) = @_; | |
| 37 | |
| 38 while ( my ($method,$info) = each %forms ) { | |
| 39 die new IMPL::Exception("A method doesn't exists in the controller",$self,$method) unless $self->can($method); | |
| 40 if ( not ref $info ) { | |
| 41 $self->class_data(CONTROLLER_METHODS)->{$method} = { | |
| 42 wrapper => 'FormWrapper', | |
| 43 schema => $info | |
| 44 }; | |
| 45 } elsif (ref $info eq 'HASH') { | |
| 46 die new IMPL::Exception("A schema must be specified",$self,$method) unless $info->{schema}; | |
| 47 | |
| 48 $self->class_data(CONTROLLER_METHODS)->{$method} = { | |
| 49 wrapper => 'FormWrapper', | |
| 50 schema => $info->{schema} | |
| 51 }; | |
| 52 } else { | |
| 53 die new IMPL::Exception("Unsupported method information",$self,$method); | |
| 54 } | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 sub transactions { | |
| 59 | |
| 22 } | 60 } |
| 23 | 61 |
| 24 sub InvokeAction { | 62 sub InvokeAction { |
| 25 my ($self,$method,$action) = @_; | 63 my ($self,$method,$action) = @_; |
| 26 | 64 |
| 27 if ($self->can($method)) { | 65 if (my $methodInfo = $self->class_data(CONTROLLER_METHODS)->{$method}) { |
| 28 my $unit = $self->new($action); | 66 if (my $wrapper = $methodInfo->{wrapper}) { |
| 29 $unit->$method(); | 67 return $self->$wrapper($method,$action,$methodInfo); |
| 68 } else { | |
| 69 return $self->TransactionWrapper($method,$action,$methodInfo); | |
| 70 } | |
| 30 } else { | 71 } else { |
| 31 die new IMPL::InvalidOperationException("Invalid method call",$self,$method); | 72 die new IMPL::InvalidOperationException("Invalid method call",$self,$method); |
| 32 } | 73 } |
| 74 } | |
| 75 | |
| 76 sub TransactionWrapper { | |
| 77 my ($self,$method,$action,$methodInfo) = @_; | |
| 78 | |
| 79 my $unit = $self->new($action); | |
| 80 return $unit->$method(); | |
| 81 } | |
| 82 | |
| 83 sub FormWrapper { | |
| 84 my ($this,$method,$action,$methodInfo) = @_; | |
| 85 | |
| 86 my $schema = $this->loadSchema($methodInfo->{schema}); | |
| 87 | |
| 88 my $process = $this->query->param('process') || 0; | |
| 89 | |
| 90 | |
| 91 | |
| 92 my %result = ( | |
| 93 | |
| 94 ); | |
| 33 } | 95 } |
| 34 | 96 |
| 35 1; | 97 1; |
| 36 | 98 |
| 37 __END__ | 99 __END__ |
| 140 =item C<[get] formErrors> | 202 =item C<[get] formErrors> |
| 141 | 203 |
| 142 =item C<InvokeAction($method,$action)> | 204 =item C<InvokeAction($method,$action)> |
| 143 | 205 |
| 144 Конструирует контекст выполнения транзакции, может быть переопределен для конструирования контекста по | 206 Конструирует контекст выполнения транзакции, может быть переопределен для конструирования контекста по |
| 145 своимправилам. | 207 своим правилам. |
| 208 | |
| 209 =item C<TransactionWrapper($method,$action,$methodInfo)> | |
| 210 | |
| 211 Обертка для конструирования простых транзакций, может быть переопределен для конструирования контекста по | |
| 212 своим правилам. | |
| 213 | |
| 214 =item C<FormWrapper($method,$action,$methodInfo)> | |
| 215 | |
| 216 Обертка для конструирования форм, может быть переопределен для конструирования контекста по | |
| 217 своим правилам. | |
| 146 | 218 |
| 147 =back | 219 =back |
| 148 | 220 |
| 149 =head1 EXAMPLE | 221 =head1 EXAMPLE |
| 150 | 222 |
