| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Form; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\Config\Parameter\BooleanConfigParameter; |
| 9 |
use UserAccessManager\Config\Config; |
| 10 |
use UserAccessManager\Config\Parameter\ConfigParameter; |
| 11 |
use UserAccessManager\Config\MainConfig; |
| 12 |
use UserAccessManager\Config\Parameter\SelectionConfigParameter; |
| 13 |
use UserAccessManager\Config\Parameter\StringConfigParameter; |
| 14 |
use UserAccessManager\Form\Element\FormElement; |
| 15 |
use UserAccessManager\Form\Element\Input; |
| 16 |
use UserAccessManager\Form\Element\MultipleFormElementValue; |
| 17 |
use UserAccessManager\Form\Element\Radio; |
| 18 |
use UserAccessManager\Form\Element\Select; |
| 19 |
use UserAccessManager\Wrapper\Php; |
| 20 |
use UserAccessManager\Wrapper\Wordpress; |
| 21 |
|
| 22 |
class FormHelper |
| 23 |
{ |
| 24 |
public function __construct( |
| 25 |
private Php $php, |
| 26 |
private Wordpress $wordpress, |
| 27 |
private MainConfig $config, |
| 28 |
private FormFactory $formFactory |
| 29 |
) { |
| 30 |
} |
| 31 |
|
| 32 |
private function resolveTextConstant(string $ident): string |
| 33 |
{ |
| 34 |
return defined($ident) ? constant($ident) : $ident; |
| 35 |
} |
| 36 |
|
| 37 |
private function getPublicObject(string $objectKey): ?object |
| 38 |
{ |
| 39 |
$objects = $this->wordpress->getPostTypes(['public' => true], 'objects') |
| 40 |
+ $this->wordpress->getTaxonomies(['public' => true], 'objects'); |
| 41 |
|
| 42 |
return $objects[$objectKey] ?? null; |
| 43 |
} |
| 44 |
|
| 45 |
private function getObjectText(string $ident, bool $description, ?string $objectKey = null): string |
| 46 |
{ |
| 47 |
$ident .= ($description === true) ? '_DESC' : ''; |
| 48 |
$object = ($objectKey !== null) ? $this->getPublicObject($objectKey) : null; |
| 49 |
|
| 50 |
if ($object === null) { |
| 51 |
return $this->resolveTextConstant($ident); |
| 52 |
} |
| 53 |
|
| 54 |
$text = $this->resolveTextConstant(str_replace(strtoupper($objectKey), 'OBJECT', $ident)); |
| 55 |
$placeholderCount = substr_count($text, '%s'); |
| 56 |
|
| 57 |
if ($placeholderCount === 0) { |
| 58 |
return $text; |
| 59 |
} |
| 60 |
|
| 61 |
return vsprintf($text, $this->php->arrayFill(0, $placeholderCount, $object->labels->name)); |
| 62 |
} |
| 63 |
|
| 64 |
public function getText(string $key, bool $description = false): string |
| 65 |
{ |
| 66 |
return $this->getObjectText('TXT_UAM_' . strtoupper($key) . '_SETTING', $description, $key); |
| 67 |
} |
| 68 |
|
| 69 |
public function getParameterText( |
| 70 |
ConfigParameter $configParameter, |
| 71 |
bool $description = false, |
| 72 |
?string $objectKey = null |
| 73 |
): string { |
| 74 |
return $this->getObjectText('TXT_UAM_' . strtoupper($configParameter->getId()), $description, $objectKey); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @throws Exception |
| 79 |
*/ |
| 80 |
public function createMultipleFormElementValue( |
| 81 |
string $value, |
| 82 |
string $label, |
| 83 |
?ConfigParameter $parameter = null |
| 84 |
): MultipleFormElementValue { |
| 85 |
$elementValue = $this->formFactory->createMultipleFormElementValue($value, $label); |
| 86 |
$subElement = ($parameter !== null) ? $this->convertConfigParameter($parameter) : null; |
| 87 |
|
| 88 |
if ($subElement !== null) { |
| 89 |
$elementValue->setSubElement($subElement); |
| 90 |
} |
| 91 |
|
| 92 |
return $elementValue; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @throws Exception |
| 97 |
*/ |
| 98 |
private function convertSelectionParameter( |
| 99 |
SelectionConfigParameter $configParameter, |
| 100 |
?string $objectKey = null, |
| 101 |
array $overwrittenValues = [] |
| 102 |
): Select|Radio { |
| 103 |
$values = []; |
| 104 |
|
| 105 |
foreach ($configParameter->getSelections() as $selection) { |
| 106 |
$optionLabel = $this->resolveTextConstant( |
| 107 |
'TXT_UAM_' . strtoupper($configParameter->getId() . '_' . $selection) |
| 108 |
); |
| 109 |
|
| 110 |
$overwrittenValue = $overwrittenValues[$selection] ?? null; |
| 111 |
$values[] = ($overwrittenValues === []) |
| 112 |
? $this->formFactory->createValueSetFormElementValue($selection, $optionLabel) |
| 113 |
: $this->createMultipleFormElementValue($selection, $optionLabel, $overwrittenValue); |
| 114 |
} |
| 115 |
|
| 116 |
$label = $this->getParameterText($configParameter, false, $objectKey); |
| 117 |
$description = $this->getParameterText($configParameter, true, $objectKey); |
| 118 |
|
| 119 |
if ($overwrittenValues === []) { |
| 120 |
return $this->formFactory->createSelect( |
| 121 |
$configParameter->getId(), |
| 122 |
$values, |
| 123 |
$configParameter->getValue(), |
| 124 |
$label, |
| 125 |
$description |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
return $this->formFactory->createRadio( |
| 130 |
$configParameter->getId(), |
| 131 |
$values, |
| 132 |
$configParameter->getValue(), |
| 133 |
$label, |
| 134 |
$description |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @throws Exception |
| 140 |
*/ |
| 141 |
public function convertConfigParameter( |
| 142 |
ConfigParameter $configParameter, |
| 143 |
?string $objectKey = null, |
| 144 |
array $overwrittenValues = [] |
| 145 |
): Input|Radio|Select|null { |
| 146 |
if ($configParameter instanceof StringConfigParameter) { |
| 147 |
return $this->formFactory->createInput( |
| 148 |
$configParameter->getId(), |
| 149 |
$configParameter->getValue(), |
| 150 |
$this->getParameterText($configParameter, false, $objectKey), |
| 151 |
$this->getParameterText($configParameter, true, $objectKey) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
if ($configParameter instanceof BooleanConfigParameter) { |
| 156 |
return $this->formFactory->createRadio( |
| 157 |
$configParameter->getId(), |
| 158 |
[ |
| 159 |
$this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES), |
| 160 |
$this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO) |
| 161 |
], |
| 162 |
$configParameter->getValue(), |
| 163 |
$this->getParameterText($configParameter, false, $objectKey), |
| 164 |
$this->getParameterText($configParameter, true, $objectKey) |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
if ($configParameter instanceof SelectionConfigParameter) { |
| 169 |
return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues); |
| 170 |
} |
| 171 |
|
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @throws Exception |
| 177 |
*/ |
| 178 |
private function addConvertedParameter( |
| 179 |
Form $form, |
| 180 |
ConfigParameter $configParameter, |
| 181 |
?string $objectKey = null, |
| 182 |
array $overwrittenValues = [] |
| 183 |
): void { |
| 184 |
$formElement = $this->convertConfigParameter($configParameter, $objectKey, $overwrittenValues); |
| 185 |
|
| 186 |
if ($formElement !== null) { |
| 187 |
$form->addElement($formElement); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @throws Exception |
| 193 |
*/ |
| 194 |
public function getSettingsForm(array $parameters, ?string $objectKey = null): Form |
| 195 |
{ |
| 196 |
$configParameters = $this->config->getConfigParameters(); |
| 197 |
$form = $this->formFactory->createForm(); |
| 198 |
|
| 199 |
foreach ($parameters as $key => $parameter) { |
| 200 |
$overwrittenValues = []; |
| 201 |
|
| 202 |
if (is_array($parameter) === true) { |
| 203 |
$overwrittenValues = array_map( |
| 204 |
fn($parameterKey) => $configParameters[$parameterKey] ?? null, |
| 205 |
$parameter |
| 206 |
); |
| 207 |
$parameter = $key; |
| 208 |
} |
| 209 |
|
| 210 |
if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) { |
| 211 |
$this->addConvertedParameter($form, $configParameters[$parameter], $objectKey, $overwrittenValues); |
| 212 |
} elseif ($parameter instanceof FormElement) { |
| 213 |
$form->addElement($parameter); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return $form; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* @throws Exception |
| 222 |
*/ |
| 223 |
public function getSettingsFormByConfig(Config $config): Form |
| 224 |
{ |
| 225 |
$form = $this->formFactory->createForm(); |
| 226 |
|
| 227 |
foreach ($config->getConfigParameters() as $configParameter) { |
| 228 |
$this->addConvertedParameter($form, $configParameter); |
| 229 |
} |
| 230 |
|
| 231 |
return $form; |
| 232 |
} |
| 233 |
} |
| 234 |
|