PluginProbe
User Access Manager / 2.3.12
User Access Manager v2.3.12
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Form / FormHelper.php

FormHelper.php in User Access Manager 2.3.12, at src/Form/FormHelper.php

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