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