FormHelper.php
9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* FormHelper.php
*
* The FormHelper class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Form;
use Exception;
use UserAccessManager\Config\BooleanConfigParameter;
use UserAccessManager\Config\Config;
use UserAccessManager\Config\ConfigParameter;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Config\SelectionConfigParameter;
use UserAccessManager\Config\StringConfigParameter;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class FormHelper
*
* @package UserAccessManager\Form
*/
class FormHelper
{
/**
* @var Php
*/
private $php;
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var MainConfig
*/
private $config;
/**
* @var FormFactory
*/
private $formFactory;
/**
* FormHelper constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param MainConfig $config
* @param FormFactory $formFactory
*/
public function __construct(
Php $php,
Wordpress $wordpress,
MainConfig $config,
FormFactory $formFactory
) {
$this->php = $php;
$this->wordpress = $wordpress;
$this->config = $config;
$this->formFactory = $formFactory;
}
/**
* Returns the right translation string.
* @param string $ident
* @param bool $description
* @param null $objectKey
* @return mixed|string
*/
private function getObjectText(string $ident, $description = false, $objectKey = null): string
{
$ident .= ($description === true) ? '_DESC' : '';
if ($objectKey !== null) {
$objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
+ $this->wordpress->getTaxonomies(['public' => true], 'objects');
if (isset($objects[$objectKey]) === true) {
$ident = str_replace(strtoupper($objectKey), 'OBJECT', $ident);
$text = (defined($ident) === true) ? constant($ident) : $ident;
$count = substr_count($text, '%s');
if ($count > 0) {
$arguments = $this->php->arrayFill(0, $count, $objects[$objectKey]->labels->name);
$text = vsprintf($text, $arguments);
}
return $text;
}
}
return (defined($ident) === true) ? constant($ident) : $ident;
}
/**
* @param string $key
* @param bool $description
* @return string
*/
public function getText(string $key, $description = false): string
{
return $this->getObjectText(
'TXT_UAM_' . strtoupper($key) . '_SETTING',
$description,
$key
);
}
/**
* Returns the label for the parameter.
* @param ConfigParameter $configParameter
* @param bool $description
* @param string $objectKey
* @return string
*/
public function getParameterText(ConfigParameter $configParameter, $description = false, $objectKey = null): string
{
$ident = 'TXT_UAM_' . strtoupper($configParameter->getId());
return $this->getObjectText(
$ident,
$description,
$objectKey
);
}
/**
* Creates a multiple form element.
* @param string $value
* @param string $label
* @param ConfigParameter|null $parameter
* @return MultipleFormElementValue
* @throws Exception
*/
public function createMultipleFromElement(
string $value,
string $label,
?ConfigParameter $parameter = null
): MultipleFormElementValue {
$value = $this->formFactory->createMultipleFormElementValue($value, $label);
if ($parameter !== null) {
$convertedParameter = $this->convertConfigParameter($parameter);
if ($convertedParameter !== null) {
$value->setSubElement($convertedParameter);
}
}
return $value;
}
/**
* @param SelectionConfigParameter $configParameter
* @param null|string $objectKey
* @param array $overwrittenValues
* @return mixed
* @throws Exception
*/
private function convertSelectionParameter(
SelectionConfigParameter $configParameter,
$objectKey = null,
array $overwrittenValues = []
) {
$values = [];
foreach ($configParameter->getSelections() as $selection) {
$optionNameKey = 'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection);
$label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey;
if ($overwrittenValues === []) {
$values[] = $this->formFactory->createValueSetFromElementValue($selection, $label);
} else {
$parameter = (isset($overwrittenValues[$selection]) === true) ? $overwrittenValues[$selection] : null;
$values[] = $this->createMultipleFromElement($selection, $label, $parameter);
}
}
$objectMethod = $overwrittenValues === [] ? 'createSelect' : 'createRadio';
return $this->formFactory->{$objectMethod}(
$configParameter->getId(),
$values,
$configParameter->getValue(),
$this->getParameterText($configParameter, false, $objectKey),
$this->getParameterText($configParameter, true, $objectKey)
);
}
/**
* @param ConfigParameter $configParameter
* @param null|string $objectKey
* @param array $overwrittenValues
* @return null|Input|Radio|Select
* @throws Exception
*/
public function convertConfigParameter(
ConfigParameter $configParameter,
$objectKey = null,
array $overwrittenValues = []
) {
if (($configParameter instanceof StringConfigParameter) === true) {
return $this->formFactory->createInput(
$configParameter->getId(),
$configParameter->getValue(),
$this->getParameterText($configParameter, false, $objectKey),
$this->getParameterText($configParameter, true, $objectKey)
);
} elseif (($configParameter instanceof BooleanConfigParameter) === true) {
$yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
$no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
return $this->formFactory->createRadio(
$configParameter->getId(),
[$yes, $no],
$configParameter->getValue(),
$this->getParameterText($configParameter, false, $objectKey),
$this->getParameterText($configParameter, true, $objectKey)
);
} elseif (($configParameter instanceof SelectionConfigParameter) === true) {
/** @var SelectionConfigParameter $configParameter */
return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues);
}
return null;
}
/**
* Returns the settings form for the given config parameters.
* @param array $parameters
* @param string|null $objectKey
* @return Form
* @throws Exception
*/
public function getSettingsForm(array $parameters, $objectKey = null): Form
{
$configParameters = $this->config->getConfigParameters();
$form = $this->formFactory->createFrom();
foreach ($parameters as $key => $parameter) {
$overwrittenValues = [];
if (is_array($parameter) === true) {
$overwrittenValues = array_map(
function ($parameterKey) use ($configParameters) {
return $configParameters[$parameterKey] ?? null;
},
$parameter
);
$parameter = $key;
}
if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
$formElement = $this->convertConfigParameter(
$configParameters[$parameter],
$objectKey,
$overwrittenValues
);
if ($formElement !== null) {
$form->addElement($formElement);
}
} elseif (($parameter instanceof FormElement) === true) {
$form->addElement($parameter);
}
}
return $form;
}
/**
* Converts config parameters to a form.
* @param Config $config
* @return Form
* @throws Exception
*/
public function getSettingsFormByConfig(Config $config): Form
{
$form = $this->formFactory->createFrom();
$configParameters = $config->getConfigParameters();
foreach ($configParameters as $configParameter) {
$formElement = $this->convertConfigParameter($configParameter);
if ($formElement !== null) {
$form->addElement($formElement);
}
}
return $form;
}
}