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\Step\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Form\Definition\Step\Step\Substep\Substep; |
17
|
|
|
use Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition; |
18
|
|
|
use Romm\Formz\Form\FormObject\FormObject; |
19
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
20
|
|
|
use Romm\Formz\Middleware\Item\Step\Service\Exception\SubstepException; |
21
|
|
|
use Romm\Formz\Service\Traits\SelfInstantiateTrait; |
22
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
23
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @todo |
27
|
|
|
*/ |
28
|
|
|
class SubstepMiddlewareService implements SingletonInterface |
29
|
|
|
{ |
30
|
|
|
use SelfInstantiateTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FormObject |
34
|
|
|
*/ |
35
|
|
|
protected $formObject; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Request |
39
|
|
|
*/ |
40
|
|
|
protected $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param FormObject $formObject |
44
|
|
|
* @param Request $request |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function reset(FormObject $formObject, Request $request) |
48
|
|
|
{ |
49
|
|
|
$this->formObject = $formObject; |
50
|
|
|
$this->request = $request; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function manageSubstepPathData() |
56
|
|
|
{ |
57
|
|
|
$currentStep = $this->formObject->fetchCurrentStep($this->request)->getCurrentStep(); |
58
|
|
|
|
59
|
|
|
if ($currentStep |
60
|
|
|
&& $currentStep->hasSubsteps() |
61
|
|
|
) { |
62
|
|
|
$this->validateAndFillSubstepPathData(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function validateAndFillSubstepPathData() |
67
|
|
|
{ |
68
|
|
|
$substepPath = $this->getSubstepPathDataFromRequest(); |
69
|
|
|
$stepService = FormObjectFactory::get()->getStepService($this->formObject); |
70
|
|
|
|
71
|
|
|
if (null !== $substepPath) { |
72
|
|
|
try { |
73
|
|
|
$path = isset($substepPath['path']) && is_array($substepPath['path']) |
74
|
|
|
? $substepPath['path'] |
75
|
|
|
: []; |
76
|
|
|
|
77
|
|
|
$currentSubstepDefinition = $this->fetchCurrentSubstep($substepPath['current']); |
78
|
|
|
$substepPath = $this->fetchSubstepPath($path); |
79
|
|
|
|
80
|
|
|
$stepService->setCurrentSubstepDefinition($currentSubstepDefinition); |
81
|
|
|
$stepService->setSubstepsPath($substepPath); |
82
|
|
|
} catch (SubstepException $exception) { |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function fetchCurrentSubstep($identifier) |
88
|
|
|
{ |
89
|
|
|
$currentSubstep = $this->getSubstep($identifier); |
90
|
|
|
|
91
|
|
|
if ($currentSubstep) { |
92
|
|
|
$currentSubstepDefinition = $this->getSubstepDefinition($currentSubstep); |
93
|
|
|
|
94
|
|
|
if ($currentSubstepDefinition) { |
95
|
|
|
return $currentSubstepDefinition; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new SubstepException; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $path |
104
|
|
|
* @return Substep[] |
105
|
|
|
* @throws SubstepException |
106
|
|
|
*/ |
107
|
|
|
protected function fetchSubstepPath(array $path) |
108
|
|
|
{ |
109
|
|
|
$substepsPath = []; |
110
|
|
|
/** @var SubstepDefinition $lastSubstepDefinition */ |
111
|
|
|
$lastSubstepDefinition = null; |
112
|
|
|
|
113
|
|
|
foreach ($path as $substepIdentifier) { |
114
|
|
|
$substepIdentifier = (string)$substepIdentifier; |
115
|
|
|
$substep = $this->getSubstep($substepIdentifier); |
116
|
|
|
|
117
|
|
|
if ($substep) { |
118
|
|
|
$substepDefinition = $this->getSubstepDefinition($substep); |
119
|
|
|
|
120
|
|
|
if ($substepDefinition) { |
121
|
|
|
$flag = true; |
122
|
|
|
|
123
|
|
|
if ($lastSubstepDefinition) { |
124
|
|
|
$flag = ($lastSubstepDefinition->hasNextSubstep() |
125
|
|
|
&& $lastSubstepDefinition->getNextSubstep()->getSubstep()->getIdentifier() === $substepDefinition->getSubstep()->getIdentifier() |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
if (false === $flag) { |
129
|
|
|
if ($lastSubstepDefinition->hasDivergence()) { |
130
|
|
|
foreach ($lastSubstepDefinition->getDivergenceSubsteps() as $divergenceSubstep) { |
131
|
|
|
if ($divergenceSubstep->getSubstep()->getIdentifier() === $substepDefinition->getSubstep()->getIdentifier()) { |
132
|
|
|
$flag = true; |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (true === $flag) { |
141
|
|
|
$substepsPath[] = $substep; |
142
|
|
|
$lastSubstepDefinition = $substepDefinition; |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
throw new SubstepException; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $substepsPath; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $identifier |
156
|
|
|
* @return Substep|null |
157
|
|
|
*/ |
158
|
|
|
protected function getSubstep($identifier) |
159
|
|
|
{ |
160
|
|
|
$currentStep = $this->formObject->fetchCurrentStep($this->request)->getCurrentStep(); |
161
|
|
|
|
162
|
|
|
return ($currentStep->getSubsteps()->hasEntry($identifier)) |
163
|
|
|
? $currentStep->getSubsteps()->getEntry($identifier) |
164
|
|
|
: null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Substep $substep |
169
|
|
|
* @return SubstepDefinition|null |
170
|
|
|
*/ |
171
|
|
|
public function getSubstepDefinition(Substep $substep) |
172
|
|
|
{ |
173
|
|
|
return $this->findSubstepDefinition($substep, $this->getFirstSubstepDefinition()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Substep $substep |
178
|
|
|
* @param SubstepDefinition $substepDefinition |
179
|
|
|
* @return SubstepDefinition|null |
180
|
|
|
*/ |
181
|
|
|
protected function findSubstepDefinition(Substep $substep, SubstepDefinition $substepDefinition) |
182
|
|
|
{ |
183
|
|
|
if ($substepDefinition->getSubstep() === $substep) { |
184
|
|
|
return $substepDefinition; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($substepDefinition->hasNextSubstep()) { |
188
|
|
|
$result = $this->findSubstepDefinition($substep, $substepDefinition->getNextSubstep()); |
189
|
|
|
|
190
|
|
|
if ($result instanceof SubstepDefinition) { |
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($substepDefinition->hasDivergence()) { |
196
|
|
|
foreach ($substepDefinition->getDivergenceSubsteps() as $divergenceStep) { |
197
|
|
|
$result = $this->findSubstepDefinition($substep, $divergenceStep); |
198
|
|
|
|
199
|
|
|
if ($result instanceof SubstepDefinition) { |
200
|
|
|
return $result; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return null; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return array|null |
210
|
|
|
*/ |
211
|
|
|
protected function getSubstepPathDataFromRequest() |
212
|
|
|
{ |
213
|
|
|
if ($this->request->hasArgument('substepsPath')) { |
214
|
|
|
$substepPath = json_decode($this->request->getArgument('substepsPath'), true); |
215
|
|
|
|
216
|
|
|
if (null !== $substepPath) { |
217
|
|
|
return $substepPath; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return SubstepDefinition |
226
|
|
|
*/ |
227
|
|
|
public function getFirstSubstepDefinition() |
228
|
|
|
{ |
229
|
|
|
return $this->formObject->fetchCurrentStep($this->request)->getCurrentStep()->getSubsteps()->getFirstSubstepDefinition(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|