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\Persistence; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Form\Service\DataObject\WrongFormIdentifierObjectDataException; |
17
|
|
|
use Romm\Formz\Middleware\Argument\Arguments; |
18
|
|
|
use Romm\Formz\Middleware\Item\AbstractMiddleware; |
19
|
|
|
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal; |
20
|
|
|
use Romm\Formz\Middleware\Signal\Before; |
21
|
|
|
use Romm\Formz\Middleware\State\PresetMiddlewareInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This middleware will try to fetch a form instance using the persistence |
25
|
|
|
* manager. |
26
|
|
|
* |
27
|
|
|
* If a form identifier hash is found in the request arguments, it is used to |
28
|
|
|
* search for a form instance in every persistence service bound to the form |
29
|
|
|
* object. |
30
|
|
|
*/ |
31
|
|
|
class PersistenceFetchingMiddleware extends AbstractMiddleware implements Before, FormInjectionSignal, PresetMiddlewareInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var \Romm\Formz\Middleware\Item\Persistence\PersistenceFetchingMiddlewareOption |
35
|
|
|
*/ |
36
|
|
|
protected $options; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PersistenceMiddlewareService |
40
|
|
|
*/ |
41
|
|
|
protected $service; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Checks for a form identifier hash in the request arguments, and tries to |
45
|
|
|
* get a form identifier object with it. |
46
|
|
|
*/ |
47
|
|
|
public function initializeMiddleware() |
48
|
|
|
{ |
49
|
|
|
$identifier = $this->getFormIdentifierHash(); |
50
|
|
|
|
51
|
|
|
if ($identifier) { |
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$identifierObject = $this->getFormObject()->getFormIdentifierObject($identifier); |
54
|
|
|
$this->service->setIdentifierObject($identifierObject); |
55
|
|
|
} catch (WrongFormIdentifierObjectDataException $exception) { |
56
|
|
|
// @todo forward |
57
|
|
|
$action = $this->options->getForwardToActionOnHashError(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @see PersistenceFetchingMiddleware |
64
|
|
|
* |
65
|
|
|
* @param Arguments $arguments |
66
|
|
|
*/ |
67
|
|
|
public function before(Arguments $arguments) |
68
|
|
|
{ |
69
|
|
|
if (false === $this->getFormObject()->formWasSubmitted() |
70
|
|
|
&& $this->service->hasIdentifierObject() |
71
|
|
|
) { |
72
|
|
|
$form = $this->getFormObject() |
73
|
|
|
->getPersistenceManager() |
74
|
|
|
->fetchFirst($this->service->getIdentifierObject()); |
75
|
|
|
|
76
|
|
|
if ($form) { |
77
|
|
|
$this->getFormObject()->setForm($form); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* This function tries to fetch the form identifier hash for the current |
84
|
|
|
* form, in the request arguments. |
85
|
|
|
* |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
|
|
protected function getFormIdentifierHash() |
89
|
|
|
{ |
90
|
|
|
if ($this->getRequest()->hasArgument('fz-identifier')) { |
91
|
|
|
$formName = $this->getFormObject()->getName(); |
92
|
|
|
$identifierList = $this->getRequest()->getArgument('fz-identifier'); |
93
|
|
|
|
94
|
|
|
if (is_array($identifierList) |
95
|
|
|
&& isset($identifierList[$formName]) |
96
|
|
|
) { |
97
|
|
|
return (string)$identifierList[$formName]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param PersistenceMiddlewareService $service |
106
|
|
|
*/ |
107
|
|
|
public function injectService(PersistenceMiddlewareService $service) |
108
|
|
|
{ |
109
|
|
|
$this->service = $service; |
110
|
|
|
$this->service->reset(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: