-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationFormTrait.php
More file actions
195 lines (164 loc) · 5.88 KB
/
Copy pathConfigurationFormTrait.php
File metadata and controls
195 lines (164 loc) · 5.88 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
<?php
namespace ADT\FancyAdmin\UI\Components\Forms\Configuration;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\Model\Entities\Configuration;
use ADT\FancyAdmin\Model\Entities\Enums\ConfigurationTypeEnum;
use ADT\FancyAdmin\Model\Entities\File;
use ADT\FancyAdmin\Model\FileUploadRules;
use ADT\Forms\Form;
use App\Model\Entities\Account;
use Nette\Http\FileUpload;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
/**
* @property Account $entity
*/
trait ConfigurationFormTrait
{
use EntityManagerInject;
private const string JSON_VALUE_PREFIX = 'value_';
public function initForm(Form $form): void
{
$form->addText('key', 'Key')
->setRequired()
->setDisabled();
$form->addSelect('type', 'Type', ConfigurationTypeEnum::list())
->setRequired()
->setDisabled();
$form['type']
->addCondition(Form::Equal, ConfigurationTypeEnum::TYPE_SELECT)
->toggle('optionsInput')
->endCondition()
->addCondition(Form::Equal, ConfigurationTypeEnum::TYPE_FILE)
->toggle('fileInput')
->elseCondition()
->toggle('valueInput');
$form->addSection(function() use ($form) {
$form->addTextArea('options', 'Options')
->setHtmlAttribute('rows', 5);
}, 'optionsInput');
$form->addSection(function() use ($form) {
$form->addUpload('_file', 'File')
->addRule(Form::MaxFileSize, 'fcadmin.forms.fileUpload.errors.tooLarge', FileUploadRules::MAX_FILE_SIZE)
->addRule(Form::MimeType, 'fcadmin.forms.fileUpload.errors.mimeTypeMismatch', FileUploadRules::ALLOWED_MIME_TYPES)
->addConditionOn($form['type'], Form::EQUAL, ConfigurationTypeEnum::TYPE_FILE)
->setRequired();
}, 'fileInput');
$jsonValues = $this->getDecodedJsonValue();
$form->addSection(function() use ($form, $jsonValues) {
// pro JSON typ s rozpoznanou strukturou vygenerujeme jednotlivá pole dle klíčů,
// jinak fallback na původní textarea
if ($jsonValues !== null) {
foreach ($jsonValues as $key => $value) {
$this->addJsonValueField($form, (string) $key, $value);
}
} else {
$form->addTextArea('value', 'Value')
->setHtmlAttribute('rows', 5);
}
}, 'valueInput');
$form->addSubmit("submit", 'Save');
}
public function validateForm(Configuration $entity, array $inputs): void
{
// pro JSON skládáme hodnotu z jednotlivých polí (value_*), validní JSON tedy vznikne vždy
if ($entity->getType() === ConfigurationTypeEnum::TYPE_JSON->value && isset($inputs['value']) && !is_array(json_decode($inputs['value'], true))) {
$this->form->addError('Input value not contains valid JSON');
}
if ($entity->getType() === ConfigurationTypeEnum::TYPE_SELECT->value) {
$options = json_decode($inputs['options'], true);
if (!is_array($options)) {
$this->form->addError('Input options not contains valid JSON');
return;
}
if ($inputs['value'] && !array_key_exists($inputs['value'], $options)) {
$this->form->addError('Input value not contains valid option from options input.');
}
}
}
public function processForm(Configuration $entity, array $inputs): void
{
if ($entity->getType() === ConfigurationTypeEnum::TYPE_JSON) {
$original = $this->getDecodedJsonValue();
if ($original !== null) {
// poskládáme JSON zpět z jednotlivých polí (value_*) se zachováním původních typů
$value = [];
foreach ($original as $key => $originalValue) {
$value[$key] = $this->castJsonValue($inputs[self::JSON_VALUE_PREFIX . $key] ?? null, $originalValue);
}
} else {
$value = json_decode($inputs['value']);
}
$entity->setValue(json_encode($value, JSON_PRETTY_PRINT));
} elseif ($entity->getType() === ConfigurationTypeEnum::TYPE_FILE) {
/** @var FileUpload $fileUpload */
$fileUpload = $inputs['_file'];
/** @var File $fileEntity */
$fileEntity = new ($this->em->findEntityClassByInterface(File::class));
$fileEntity->setTemporaryFile($fileUpload->getTemporaryFile(), $fileUpload->getUntrustedName());
$entity->setFile($fileEntity);
}
$this->_em->flush();
}
protected function getEntityClass(): ?string
{
return Configuration::class;
}
/**
* Vrátí dekódovanou JSON hodnotu jako asociativní pole (klíč => hodnota),
* nebo null, pokud nejde o JSON typ / hodnotu nelze rozparsovat.
*
* @return array<string, mixed>|null
*/
private function getDecodedJsonValue(): ?array
{
$entity = $this->getEntity();
if (!$entity instanceof Configuration || $entity->getType() !== ConfigurationTypeEnum::TYPE_JSON) {
return null;
}
try {
// hodnota může mít v DB navíc koncový středník (legacy serializace)
$rawValue = rtrim(trim((string) $entity->getValue()), ';');
$decoded = Json::decode($rawValue, Json::FORCE_ARRAY);
// hodnota mohla být v DB uložena dvojitě zakódovaná (string místo objektu)
if (is_string($decoded)) {
$decoded = Json::decode($decoded, Json::FORCE_ARRAY);
}
} catch (JsonException) {
return null;
}
return is_array($decoded) ? $decoded : null;
}
private function addJsonValueField(Form $form, string $key, mixed $value): void
{
$name = self::JSON_VALUE_PREFIX . $key;
$label = 'fcadmin.presenters.configurations.valueKeys.' . $key;
if (is_bool($value)) {
$form->addCheckbox($name, $label)
->setDefaultValue($value);
} elseif (is_int($value)) {
$form->addInteger($name, $label)
->setDefaultValue($value);
} else {
$form->addText($name, $label)
->setDefaultValue($value === null ? '' : (string) $value);
}
}
/**
* Převede hodnotu z formuláře zpět na typ odpovídající původní hodnotě.
*/
private function castJsonValue(mixed $input, mixed $original): mixed
{
if (is_bool($original)) {
return (bool) $input;
}
if (is_int($original)) {
return $input === '' || $input === null ? null : (int) $input;
}
// původně null a prázdný vstup → zůstává null
if ($original === null && ($input === '' || $input === null)) {
return null;
}
return $input;
}
}