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
|
|
|
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($hash, '$hash'); |
57
|
|
|
|
58
|
|
|
if ($hash) { |
|
|
|
|
59
|
|
|
$this->metadata = $this->metadataRepository->findOneByHash($hash); |
60
|
|
|
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->metadata, '$this->metadata'); |
61
|
|
|
|
62
|
|
|
if (!$this->metadata) { |
63
|
|
|
// @todo forward |
64
|
|
|
$action = $this->options->getForwardToActionOnHashError(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @see PersistenceFetchingMiddleware |
71
|
|
|
* |
72
|
|
|
* @param Arguments $arguments |
73
|
|
|
*/ |
74
|
|
|
public function before(Arguments $arguments) |
75
|
|
|
{ |
76
|
|
|
if ($this->metadata |
77
|
|
|
&& false === $this->getFormObject()->formWasSubmitted() |
78
|
|
|
) { |
79
|
|
|
$formObject = $this->getFormObject(); |
80
|
|
|
$form = $formObject->getPersistenceManager()->fetchFirst(); |
81
|
|
|
|
82
|
|
|
if ($form) { |
83
|
|
|
$formObject->setForm($form); |
84
|
|
|
$formObject->setFormHash($this->metadata->getHash()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* This function tries to fetch the form identifier hash for the current |
91
|
|
|
* form, in the request arguments. |
92
|
|
|
* |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
protected function getFormHash() |
96
|
|
|
{ |
97
|
|
|
if ($this->getRequest()->hasArgument('fz-hash')) { |
98
|
|
|
$formName = $this->getFormObject()->getName(); |
99
|
|
|
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($formName, '$formName'); |
100
|
|
|
$identifierList = $this->getRequest()->getArgument('fz-hash'); |
101
|
|
|
|
102
|
|
|
if (is_array($identifierList) |
103
|
|
|
&& isset($identifierList[$formName]) |
104
|
|
|
) { |
105
|
|
|
return (string)$identifierList[$formName]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param FormMetadataRepository $metadataRepository |
114
|
|
|
*/ |
115
|
|
|
public function injectMetadataRepository(FormMetadataRepository $metadataRepository) |
116
|
|
|
{ |
117
|
|
|
$this->metadataRepository = $metadataRepository; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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: