PluginProbe
User Access Manager / 2.2.1
User Access Manager v2.2.1
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.2.1, at src/Form/FormHelper.php

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