1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\ViewHelpers\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Error\FormResult; |
17
|
|
|
use Romm\Formz\Form\FormInterface; |
18
|
|
|
use Romm\Formz\Form\FormObject; |
19
|
|
|
use Romm\Formz\ViewHelpers\FormViewHelper; |
20
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This class contains methods that help view helpers to manipulate data and |
24
|
|
|
* know more things concerning the current form state. |
25
|
|
|
* |
26
|
|
|
* It is mainly configured inside the `FormViewHelper`, and used in other |
27
|
|
|
* view helpers. |
28
|
|
|
*/ |
29
|
|
|
class FormService implements SingletonInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $formContext; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array|FormInterface |
38
|
|
|
*/ |
39
|
|
|
protected $formInstance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $formWasSubmitted; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var FormResult |
48
|
|
|
*/ |
49
|
|
|
protected $formResult; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var FormObject |
53
|
|
|
*/ |
54
|
|
|
protected $formObject; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Reset every state that can be used by this service. |
58
|
|
|
*/ |
59
|
|
|
public function resetState() |
60
|
|
|
{ |
61
|
|
|
$this->formContext = false; |
62
|
|
|
$this->formInstance = null; |
63
|
|
|
$this->formResult = null; |
64
|
|
|
$this->formWasSubmitted = false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Will activate the form context, changing the result returned by the |
69
|
|
|
* function `formContextExists`. |
70
|
|
|
* |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
public function activateFormContext() |
74
|
|
|
{ |
75
|
|
|
if (true === $this->formContext) { |
76
|
|
|
throw new \Exception( |
77
|
|
|
'You can not use a form view helper inside another one.', |
78
|
|
|
1465242575 |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->formContext = true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns `true` if the `FormViewHelper` context exists. |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function formContextExists() |
91
|
|
|
{ |
92
|
|
|
return $this->formContext; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Will mark the form as submitted (change the result returned by the |
97
|
|
|
* function `formWasSubmitted()`). |
98
|
|
|
*/ |
99
|
|
|
public function markFormAsSubmitted() |
100
|
|
|
{ |
101
|
|
|
$this->formWasSubmitted = true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns `true` if the form was submitted by the user. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function formWasSubmitted() |
110
|
|
|
{ |
111
|
|
|
return $this->formWasSubmitted; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks that the current `FormViewHelper` exists. If not, an exception is |
116
|
|
|
* thrown. |
117
|
|
|
* |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
|
|
public function checkIsInsideFormViewHelper() |
121
|
|
|
{ |
122
|
|
|
if (false === $this->formContextExists()) { |
123
|
|
|
throw new \Exception( |
124
|
|
|
'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FormViewHelper::class . '".', |
125
|
|
|
1465243085 |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* If the form was submitted by the user, contains the array containing the |
132
|
|
|
* submitted values. |
133
|
|
|
* |
134
|
|
|
* @param array|FormInterface $formInstance |
135
|
|
|
*/ |
136
|
|
|
public function setFormInstance($formInstance) |
137
|
|
|
{ |
138
|
|
|
$this->formInstance = $formInstance; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array|FormInterface |
143
|
|
|
*/ |
144
|
|
|
public function getFormInstance() |
145
|
|
|
{ |
146
|
|
|
return $this->formInstance; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return FormResult |
151
|
|
|
*/ |
152
|
|
|
public function getFormResult() |
153
|
|
|
{ |
154
|
|
|
return $this->formResult; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param FormResult $formResult |
159
|
|
|
*/ |
160
|
|
|
public function setFormResult(FormResult $formResult) |
161
|
|
|
{ |
162
|
|
|
$this->formResult = $formResult; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return FormObject |
167
|
|
|
*/ |
168
|
|
|
public function getFormObject() |
169
|
|
|
{ |
170
|
|
|
return $this->formObject; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param FormObject $formObject |
175
|
|
|
*/ |
176
|
|
|
public function setFormObject(FormObject $formObject) |
177
|
|
|
{ |
178
|
|
|
$this->formObject = $formObject; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|