1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Controller; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Form\FormInterface; |
17
|
|
|
use Romm\Formz\Form\FormObject; |
18
|
|
|
use Romm\Formz\Form\FormObjectFactory; |
19
|
|
|
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait; |
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
21
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\Argument; |
22
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; |
23
|
|
|
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException; |
24
|
|
|
use TYPO3\CMS\Extbase\Mvc\Request as MvcRequest; |
25
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
26
|
|
|
|
27
|
|
|
class ControllerState implements SingletonInterface |
28
|
|
|
{ |
29
|
|
|
use ExtendedSelfInstantiateTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FormObjectFactory |
33
|
|
|
*/ |
34
|
|
|
protected $formObjectFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var FormObject[] |
38
|
|
|
*/ |
39
|
|
|
protected $formArguments = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $dispatched = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Request |
48
|
|
|
*/ |
49
|
|
|
protected $request; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Request |
53
|
|
|
*/ |
54
|
|
|
protected $originalRequest; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Arguments |
58
|
|
|
*/ |
59
|
|
|
protected $arguments; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $settings = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @todo |
68
|
|
|
* |
69
|
|
|
* @param MvcRequest $request |
70
|
|
|
* @param Arguments $arguments |
71
|
|
|
* @param array $settings |
72
|
|
|
* @throws StopActionException |
73
|
|
|
*/ |
74
|
|
|
public function dispatch(MvcRequest $request, Arguments $arguments, array $settings) |
75
|
|
|
{ |
76
|
|
|
if (false === $this->dispatched) { |
77
|
|
|
$this->dispatched = true; |
78
|
|
|
|
79
|
|
|
/** @var Request $request */ |
80
|
|
|
$this->request = clone $request; |
81
|
|
|
$this->originalRequest = clone $request; |
82
|
|
|
$this->arguments = $arguments; |
83
|
|
|
$this->settings = $settings; |
84
|
|
|
|
85
|
|
|
$request->setDispatched(false); |
86
|
|
|
$request->setControllerVendorName('Romm'); |
87
|
|
|
$request->setControllerName('Form'); |
88
|
|
|
$request->setControllerExtensionName('Formz'); |
89
|
|
|
$request->setControllerActionName('processForm'); |
90
|
|
|
$request->setArguments([ |
91
|
|
|
'originalRequest' => $this->originalRequest |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->checkFormObjectsErrors($request); |
95
|
|
|
|
96
|
|
|
throw new StopActionException; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Will check if the form objects found in the request arguments contain |
102
|
|
|
* configuration errors. If they do, we dispatch the request to the error |
103
|
|
|
* view, where all errors will be explained properly to the user. |
104
|
|
|
* |
105
|
|
|
* @param MvcRequest $request |
106
|
|
|
*/ |
107
|
|
|
protected function checkFormObjectsErrors(MvcRequest $request) |
108
|
|
|
{ |
109
|
|
|
foreach ($this->getRequestForms() as $formObject) { |
110
|
|
|
if ($formObject->getConfigurationValidationResult()->hasErrors()) { |
111
|
|
|
$request->setControllerActionName('formObjectError'); |
112
|
|
|
$request->setArguments(['formObject' => $formObject]); |
113
|
|
|
|
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Loops on the request arguments, and pick up each one that is a form |
121
|
|
|
* instance (it implements `FormInterface`). |
122
|
|
|
* |
123
|
|
|
* @return FormObject[] |
124
|
|
|
*/ |
125
|
|
|
public function getRequestForms() |
126
|
|
|
{ |
127
|
|
|
if (empty($this->formArguments)) { |
128
|
|
|
/** @var Argument $argument */ |
129
|
|
|
foreach ($this->arguments as $argument) { |
130
|
|
|
$type = $argument->getDataType(); |
131
|
|
|
|
132
|
|
|
if (class_exists($type) |
133
|
|
|
&& in_array(FormInterface::class, class_implements($type)) |
134
|
|
|
) { |
135
|
|
|
$formClassName = $argument->getDataType(); |
136
|
|
|
$formName = $argument->getName(); |
137
|
|
|
|
138
|
|
|
$formObject = $this->formObjectFactory->getInstanceFromClassName($formClassName, $formName); |
139
|
|
|
$this->formArguments[$formName] = $formObject; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->formArguments; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return Request |
149
|
|
|
*/ |
150
|
|
|
public function getRequest() |
151
|
|
|
{ |
152
|
|
|
return $this->request; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Arguments |
157
|
|
|
*/ |
158
|
|
|
public function getArguments() |
159
|
|
|
{ |
160
|
|
|
return $this->arguments; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getSettings() |
167
|
|
|
{ |
168
|
|
|
return $this->settings; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param FormObjectFactory $formObjectFactory |
173
|
|
|
*/ |
174
|
|
|
public function injectFormObjectFactory(FormObjectFactory $formObjectFactory) |
175
|
|
|
{ |
176
|
|
|
$this->formObjectFactory = $formObjectFactory; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|