1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\execution\ExecutionResult; |
6
|
|
|
use rtens\domin\execution\FailedResult; |
7
|
|
|
use rtens\domin\execution\MissingParametersResult; |
8
|
|
|
use rtens\domin\execution\NoResult; |
9
|
|
|
use rtens\domin\execution\NotPermittedResult; |
10
|
|
|
use rtens\domin\execution\RedirectResult; |
11
|
|
|
use rtens\domin\execution\ValueResult; |
12
|
|
|
use rtens\domin\Executor; |
13
|
|
|
use watoki\collections\Map; |
14
|
|
|
use watoki\curir\delivery\WebRequest; |
15
|
|
|
|
16
|
|
|
class ActionResult { |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $model; |
20
|
|
|
|
21
|
|
|
/** @var Element[] */ |
22
|
|
|
private $headElements = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param WebRequest $request |
26
|
|
|
* @param WebApplication $app |
27
|
|
|
* @param Action $action |
28
|
|
|
* @param string $actionId |
29
|
|
|
* @param BreadCrumbs $crumbs |
30
|
|
|
*/ |
31
|
|
|
public function __construct(WebRequest $request, WebApplication $app, Action $action, $actionId, BreadCrumbs $crumbs) { |
32
|
|
|
$reader = new RequestParameterReader($request); |
33
|
|
|
|
34
|
|
|
$executor = new Executor($app->actions, $app->fields, $reader); |
35
|
|
|
$executor->restrictAccess($app->getAccessControl($request)); |
36
|
|
|
$result = $executor->execute($actionId); |
37
|
|
|
|
38
|
|
|
$this->model = $this->assembleResult($result, $app, $request, $crumbs, $action, $actionId); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function assembleResult(ExecutionResult $result, WebApplication $app, WebRequest $request, |
42
|
|
|
BreadCrumbs $crumbs, Action $action, $actionId) { |
43
|
|
|
$model = [ |
44
|
|
|
'error' => null, |
45
|
|
|
'missing' => null, |
46
|
|
|
'success' => null, |
47
|
|
|
'redirect' => null, |
48
|
|
|
'output' => null |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($result instanceof FailedResult) { |
52
|
|
|
$model['error'] = htmlentities($result->getMessage()); |
53
|
|
|
|
54
|
|
|
} else if ($result instanceof MissingParametersResult) { |
55
|
|
|
$model['missing'] = $result->getMissingNames(); |
56
|
|
|
|
57
|
|
|
} else if ($result instanceof NoResult) { |
58
|
|
|
$model['success'] = true; |
59
|
|
|
$model['redirect'] = $crumbs->getLastCrumb(); |
60
|
|
|
|
61
|
|
|
} else if ($result instanceof NotPermittedResult) { |
62
|
|
|
$model['error'] = 'You are not permitted to execute this action.'; |
63
|
|
|
$model['redirect'] = $app->getAccessControl($request)->acquirePermission(); |
64
|
|
|
|
65
|
|
|
} else if ($result instanceof RedirectResult) { |
66
|
|
|
$model['success'] = true; |
67
|
|
|
$model['redirect'] = $request->getContext() |
68
|
|
|
->appended($result->getActionId()) |
69
|
|
|
->withParameters(new Map($result->getParameters())); |
70
|
|
|
|
71
|
|
|
} else if ($result instanceof ValueResult) { |
72
|
|
|
$value = $result->getValue(); |
73
|
|
|
$renderer = $app->renderers->getRenderer($value); |
74
|
|
|
|
75
|
|
|
if ($renderer instanceof WebRenderer) { |
76
|
|
|
$this->headElements = $renderer->headElements($value); |
77
|
|
|
} |
78
|
|
|
$model['output'] = $renderer->render($value); |
79
|
|
|
|
80
|
|
|
if (!$action->isModifying()) { |
81
|
|
|
$crumbs->updateCrumbs($action, $actionId); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $model; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getModel() { |
89
|
|
|
return $this->model; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getHeadElements() { |
93
|
|
|
return $this->headElements; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function wasExecuted() { |
97
|
|
|
return !$this->model['error'] && !$this->model['missing']; |
98
|
|
|
} |
99
|
|
|
} |