|
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\Middleware\Item\Begin; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
|
17
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
|
18
|
|
|
use Romm\Formz\Middleware\BasicMiddlewareInterface; |
|
19
|
|
|
use Romm\Formz\Middleware\Item\Begin\Service\FormService; |
|
20
|
|
|
use Romm\Formz\Middleware\Processor\MiddlewareProcessor; |
|
21
|
|
|
use Romm\Formz\Middleware\Signal\After; |
|
22
|
|
|
use Romm\Formz\Middleware\Signal\SignalObject; |
|
23
|
|
|
|
|
24
|
|
|
final class BeginMiddleware implements BasicMiddlewareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var MiddlewareProcessor |
|
28
|
|
|
*/ |
|
29
|
|
|
private $processor; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var FormService |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $formService; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialization of this middleware. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function initialize() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->formService = Core::instantiate(FormService::class, $this->processor); |
|
42
|
|
|
|
|
43
|
|
|
$this->checkFormSubmission(); |
|
44
|
|
|
$this->fetchCurrentStep(); |
|
45
|
|
|
$this->fetchSubstepsLevel(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* This is the first middleware being called, it will send a signal to all |
|
50
|
|
|
* middlewares that depend on it. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute() |
|
53
|
|
|
{ |
|
54
|
|
|
$signalObject = new SignalObject($this->processor, BeginSignal::class, After::class); |
|
55
|
|
|
$signalObject->dispatch(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Will check if the current form was submitted by the user. If it is found, |
|
60
|
|
|
* the form instance is injected in the form object. |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function checkFormSubmission() |
|
63
|
|
|
{ |
|
64
|
|
|
$formObject = $this->processor->getFormObject(); |
|
65
|
|
|
|
|
66
|
|
|
if ($formObject->hasForm()) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$request = $this->processor->getRequest(); |
|
71
|
|
|
$formName = $formObject->getName(); |
|
72
|
|
|
|
|
73
|
|
|
if ($this->requestWasSubmitted() |
|
74
|
|
|
&& null === $request->getOriginalRequest() |
|
75
|
|
|
&& $this->processor->getRequestArguments()->hasArgument($formName) |
|
76
|
|
|
) { |
|
77
|
|
|
if (false === $request->hasArgument('fz-hash')) { |
|
78
|
|
|
throw new \Exception('todo fz-hash'); // @todo |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (false === $request->hasArgument('formzData')) { |
|
82
|
|
|
throw new \Exception('todo formzData'); // @todo |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$form = $this->formService->getFormInstance(); |
|
86
|
|
|
|
|
87
|
|
|
$formObject->setForm($form); |
|
88
|
|
|
|
|
89
|
|
|
$formzData = $request->getArgument('formzData'); |
|
90
|
|
|
$formObject->getRequestData()->fillFromHash($formzData); |
|
91
|
|
|
|
|
92
|
|
|
$proxy = FormObjectFactory::get()->getProxy($form); |
|
93
|
|
|
$proxy->markFormAsSubmitted(); |
|
94
|
|
|
|
|
95
|
|
|
$this->injectFormHashInProxy(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @todo |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function fetchCurrentStep() |
|
103
|
|
|
{ |
|
104
|
|
|
$formObject = $this->processor->getFormObject(); |
|
105
|
|
|
$request = ($formObject->formWasSubmitted()) |
|
106
|
|
|
? $this->processor->getRequest()->getReferringRequest() |
|
107
|
|
|
: $this->processor->getRequest(); |
|
108
|
|
|
|
|
109
|
|
|
$formObject->fetchCurrentStep($request); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @todo |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function fetchSubstepsLevel() |
|
116
|
|
|
{ |
|
117
|
|
|
$request = $this->processor->getRequest(); |
|
118
|
|
|
|
|
119
|
|
|
if ($this->requestWasSubmitted() |
|
120
|
|
|
&& $request->hasArgument('substepsLevel') |
|
121
|
|
|
) { |
|
122
|
|
|
$substepLevel = $request->getArgument('substepsLevel'); |
|
123
|
|
|
FormObjectFactory::get() |
|
124
|
|
|
->getStepService($this->processor->getFormObject()) |
|
125
|
|
|
->setSubstepsLevel($substepLevel); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Fetches the form hash from the request data that has been submitted with |
|
131
|
|
|
* the form, and injects it in the form proxy. |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function injectFormHashInProxy() |
|
134
|
|
|
{ |
|
135
|
|
|
$formObject = $this->processor->getFormObject(); |
|
136
|
|
|
$hash = $formObject->getRequestData()->getFormHash(); |
|
137
|
|
|
|
|
138
|
|
|
$proxy = FormObjectFactory::get()->getProxy($formObject->getForm()); |
|
139
|
|
|
$proxy->setFormHash($hash); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param MiddlewareProcessor $middlewareProcessor |
|
144
|
|
|
*/ |
|
145
|
|
|
final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->processor = $middlewareProcessor; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function requestWasSubmitted() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->processor->getRequest()->getMethod() === 'POST'; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|