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

246 lines 7.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 * Converts a config parameter to a form element.
144 *
145 * @param ConfigParameter $configParameter
146 * @param string $objectKey
147 *
148 * @return null|Input|Radio|Select
149 */
150 public function convertConfigParameter(ConfigParameter $configParameter, $objectKey = null)
151 {
152 if (($configParameter instanceof StringConfigParameter) === true) {
153 return $this->formFactory->createInput(
154 $configParameter->getId(),
155 $configParameter->getValue(),
156 $this->getParameterText($configParameter, false, $objectKey),
157 $this->getParameterText($configParameter, true, $objectKey)
158 );
159 } elseif (($configParameter instanceof BooleanConfigParameter) === true) {
160 $yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
161 $no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
162
163 return $this->formFactory->createRadio(
164 $configParameter->getId(),
165 [$yes, $no],
166 $configParameter->getValue(),
167 $this->getParameterText($configParameter, false, $objectKey),
168 $this->getParameterText($configParameter, true, $objectKey)
169 );
170 } elseif (($configParameter instanceof SelectionConfigParameter) === true) {
171 $possibleValues = [];
172 /**
173 * @var SelectionConfigParameter $configParameter
174 */
175 $selections = $configParameter->getSelections();
176
177 foreach ($selections as $selection) {
178 $optionNameKey = 'TXT_UAM_'.strtoupper($configParameter->getId().'_'.$selection);
179 $label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey;
180 $possibleValues[] = $this->formFactory->createValueSetFromElementValue($selection, $label);
181 }
182
183 return $this->formFactory->createSelect(
184 $configParameter->getId(),
185 $possibleValues,
186 $configParameter->getValue(),
187 $this->getParameterText($configParameter, false, $objectKey),
188 $this->getParameterText($configParameter, true, $objectKey)
189 );
190 }
191
192 return null;
193 }
194
195 /**
196 * Returns the settings form for the given config parameters.
197 *
198 * @param array $parameters
199 * @param string|null $objectKey
200 *
201 * @return \UserAccessManager\Form\Form
202 */
203 public function getSettingsForm(array $parameters, $objectKey = null)
204 {
205 $configParameters = $this->config->getConfigParameters();
206 $form = $this->formFactory->createFrom();
207
208 foreach ($parameters as $parameter) {
209 if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
210 $formElement = $this->convertConfigParameter($configParameters[$parameter], $objectKey);
211
212 if ($formElement !== null) {
213 $form->addElement($formElement);
214 }
215 } elseif (($parameter instanceof FormElement) === true) {
216 $form->addElement($parameter);
217 }
218 }
219
220 return $form;
221 }
222
223 /**
224 * Converts config parameters to a form.
225 *
226 * @param Config $config
227 *
228 * @return Form
229 */
230 public function getSettingsFormByConfig(Config $config)
231 {
232 $form = $this->formFactory->createFrom();
233 $configParameters = $config->getConfigParameters();
234
235 foreach ($configParameters as $configParameter) {
236 $formElement = $this->convertConfigParameter($configParameter);
237
238 if ($formElement !== null) {
239 $form->addElement($formElement);
240 }
241 }
242
243 return $form;
244 }
245 }
246