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

310 lines 9.3 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 public function createMultipleFromElement($value, $label, ConfigParameter $parameter = null)
152 {
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 *
171 * @return mixed
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 * Converts a config parameter to a form element.
205 *
206 * @param ConfigParameter $configParameter
207 * @param string $objectKey
208 * @param array $overwrittenValues
209 *
210 * @return null|Input|Radio|Select
211 */
212 public function convertConfigParameter(
213 ConfigParameter $configParameter,
214 $objectKey = null,
215 array $overwrittenValues = []
216 ) {
217 if (($configParameter instanceof StringConfigParameter) === true) {
218 return $this->formFactory->createInput(
219 $configParameter->getId(),
220 $configParameter->getValue(),
221 $this->getParameterText($configParameter, false, $objectKey),
222 $this->getParameterText($configParameter, true, $objectKey)
223 );
224 } elseif (($configParameter instanceof BooleanConfigParameter) === true) {
225 $yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
226 $no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
227
228 return $this->formFactory->createRadio(
229 $configParameter->getId(),
230 [$yes, $no],
231 $configParameter->getValue(),
232 $this->getParameterText($configParameter, false, $objectKey),
233 $this->getParameterText($configParameter, true, $objectKey)
234 );
235 } elseif (($configParameter instanceof SelectionConfigParameter) === true) {
236 /** @var SelectionConfigParameter $configParameter */
237 return $this->convertSelectionParameter($configParameter, $objectKey, $overwrittenValues);
238 }
239
240 return null;
241 }
242
243 /**
244 * Returns the settings form for the given config parameters.
245 *
246 * @param array $parameters
247 * @param string|null $objectKey
248 *
249 * @return \UserAccessManager\Form\Form
250 */
251 public function getSettingsForm(array $parameters, $objectKey = null)
252 {
253 $configParameters = $this->config->getConfigParameters();
254 $form = $this->formFactory->createFrom();
255
256 foreach ($parameters as $key => $parameter) {
257 $overwrittenValues = [];
258
259 if (is_array($parameter) === true) {
260 $overwrittenValues = array_map(
261 function ($parameterKey) use ($configParameters) {
262 return isset($configParameters[$parameterKey]) ? $configParameters[$parameterKey] : null;
263 },
264 $parameter
265 );
266 $parameter = $key;
267 }
268
269 if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
270 $formElement = $this->convertConfigParameter(
271 $configParameters[$parameter],
272 $objectKey,
273 $overwrittenValues
274 );
275
276 if ($formElement !== null) {
277 $form->addElement($formElement);
278 }
279 } elseif (($parameter instanceof FormElement) === true) {
280 $form->addElement($parameter);
281 }
282 }
283
284 return $form;
285 }
286
287 /**
288 * Converts config parameters to a form.
289 *
290 * @param Config $config
291 *
292 * @return Form
293 */
294 public function getSettingsFormByConfig(Config $config)
295 {
296 $form = $this->formFactory->createFrom();
297 $configParameters = $config->getConfigParameters();
298
299 foreach ($configParameters as $configParameter) {
300 $formElement = $this->convertConfigParameter($configParameter);
301
302 if ($formElement !== null) {
303 $form->addElement($formElement);
304 }
305 }
306
307 return $form;
308 }
309 }
310