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\Domain\Model\FormMetadata; |
17
|
|
|
use Romm\Formz\Domain\Repository\FormMetadataRepository; |
18
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
19
|
|
|
use Romm\Formz\Middleware\Argument\Arguments; |
20
|
|
|
use Romm\Formz\Middleware\Item\AbstractMiddleware; |
21
|
|
|
use Romm\Formz\Middleware\Item\FormInjection\FormInjectionSignal; |
22
|
|
|
use Romm\Formz\Middleware\Signal\Before; |
23
|
|
|
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal; |
24
|
|
|
use Romm\Formz\Middleware\State\PresetMiddlewareInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This middleware will try to fetch a form instance using the persistence |
28
|
|
|
* manager. |
29
|
|
|
* |
30
|
|
|
* If a form identifier hash is found in the request arguments, it is used to |
31
|
|
|
* search for a form instance in every persistence service bound to the form |
32
|
|
|
* object. |
33
|
|
|
*/ |
34
|
|
|
class PersistenceFetchingMiddleware extends AbstractMiddleware implements Before, FormInjectionSignal, SendsMiddlewareSignal, PresetMiddlewareInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var \Romm\Formz\Middleware\Item\Persistence\PersistenceFetchingMiddlewareOption |
38
|
|
|
*/ |
39
|
|
|
protected $options; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var FormMetadata |
43
|
|
|
*/ |
44
|
|
|
protected $metadata; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var FormMetadataRepository |
48
|
|
|
*/ |
49
|
|
|
protected $metadataRepository; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks for a form identifier hash in the request arguments, and tries to |
53
|
|
|
* get a form identifier object with it. |
54
|
|
|
* |
55
|
|
|
* @param Arguments $arguments |
56
|
|
|
*/ |
57
|
|
|
public function before(Arguments $arguments) |
58
|
|
|
{ |
59
|
|
|
$this->beforeSignal()->dispatch(); |
60
|
|
|
|
61
|
|
|
$hash = $this->getFormHash(); |
62
|
|
|
|
63
|
|
|
if ($hash) { |
|
|
|
|
64
|
|
|
$this->metadata = $this->metadataRepository->findOneByHash($hash); |
65
|
|
|
|
66
|
|
|
if ($this->metadata |
67
|
|
|
&& $this->metadata->getClassName() !== $this->getFormObject()->getClassName() |
68
|
|
|
) { |
69
|
|
|
$this->metadata = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$this->metadata) { |
73
|
|
|
// @todo forward |
74
|
|
|
$action = $this->options->getForwardToActionOnHashError(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->metadata |
78
|
|
|
&& false === $this->getFormObject()->formWasSubmitted() |
79
|
|
|
) { |
80
|
|
|
$formObject = $this->getFormObject(); |
81
|
|
|
$persistenceManager = $formObject->getPersistenceManager(); |
82
|
|
|
|
83
|
|
|
$form = $persistenceManager->fetchFirst($this->metadata); |
84
|
|
|
|
85
|
|
|
if ($form) { |
86
|
|
|
$formObject->setForm($form); |
87
|
|
|
|
88
|
|
|
$proxy = FormObjectFactory::get()->getProxy($formObject, $form); |
89
|
|
|
$proxy->setFormHash($this->metadata->getHash()); |
90
|
|
|
|
91
|
|
|
$this->afterSignal()->dispatch(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* This function tries to fetch the form identifier hash for the current |
99
|
|
|
* form, in the request arguments. |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
protected function getFormHash() |
104
|
|
|
{ |
105
|
|
|
if (null !== $this->options->getFormHash()) { |
106
|
|
|
return $this->options->getFormHash(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$formObject = $this->getFormObject(); |
110
|
|
|
|
111
|
|
|
if ($this->getRequest()->hasArgument('fz-hash')) { |
112
|
|
|
$formName = $formObject->getName(); |
113
|
|
|
$identifierList = $this->getRequest()->getArgument('fz-hash'); |
114
|
|
|
|
115
|
|
|
if (is_array($identifierList) |
116
|
|
|
&& isset($identifierList[$formName]) |
117
|
|
|
) { |
118
|
|
|
return (string)$identifierList[$formName]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $formObject->hasForm() |
123
|
|
|
? $formObject->getFormHash() |
124
|
|
|
: null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param FormMetadataRepository $metadataRepository |
129
|
|
|
*/ |
130
|
|
|
public function injectMetadataRepository(FormMetadataRepository $metadataRepository) |
131
|
|
|
{ |
132
|
|
|
$this->metadataRepository = $metadataRepository; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getAllowedSignals() |
139
|
|
|
{ |
140
|
|
|
return [PersistenceFetchingSignal::class]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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: