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

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