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

241 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 * 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 $arguments = $this->php->arrayFill(0, $count, $objects[$objectKey]->labels->name);
95 return vsprintf($text, $arguments);
96 }
97 }
98
99 return (defined($ident) === true) ? constant($ident) : $ident;
100 }
101
102 /**
103 * @param string $key
104 * @param bool $description
105 *
106 * @return string
107 */
108 public function getText($key, $description = false)
109 {
110 return $this->getObjectText(
111 'TXT_UAM_'.strtoupper($key).'_SETTING',
112 $description,
113 $key
114 );
115 }
116
117 /**
118 * Returns the label for the parameter.
119 *
120 * @param ConfigParameter $configParameter
121 * @param bool $description
122 * @param string $objectKey
123 *
124 * @return string
125 */
126 public function getParameterText(ConfigParameter $configParameter, $description = false, $objectKey = null)
127 {
128 $ident = 'TXT_UAM_'.strtoupper($configParameter->getId());
129
130 return $this->getObjectText(
131 $ident,
132 $description,
133 $objectKey
134 );
135 }
136
137 /**
138 * Converts a config parameter to a form element.
139 *
140 * @param ConfigParameter $configParameter
141 * @param string $objectKey
142 *
143 * @return null|Input|Radio|Select
144 */
145 public function convertConfigParameter(ConfigParameter $configParameter, $objectKey = null)
146 {
147 if (($configParameter instanceof StringConfigParameter) === true) {
148 return $this->formFactory->createInput(
149 $configParameter->getId(),
150 $configParameter->getValue(),
151 $this->getParameterText($configParameter, false, $objectKey),
152 $this->getParameterText($configParameter, true, $objectKey)
153 );
154 } elseif (($configParameter instanceof BooleanConfigParameter) === true) {
155 $yes = $this->formFactory->createMultipleFormElementValue(true, TXT_UAM_YES);
156 $no = $this->formFactory->createMultipleFormElementValue(false, TXT_UAM_NO);
157
158 return $this->formFactory->createRadio(
159 $configParameter->getId(),
160 [$yes, $no],
161 $configParameter->getValue(),
162 $this->getParameterText($configParameter, false, $objectKey),
163 $this->getParameterText($configParameter, true, $objectKey)
164 );
165 } elseif (($configParameter instanceof SelectionConfigParameter) === true) {
166 $possibleValues = [];
167 /**
168 * @var SelectionConfigParameter $configParameter
169 */
170 $selections = $configParameter->getSelections();
171
172 foreach ($selections as $selection) {
173 $optionNameKey = 'TXT_UAM_'.strtoupper($configParameter->getId().'_'.$selection);
174 $label = (defined($optionNameKey) === true) ? constant($optionNameKey) : $optionNameKey;
175 $possibleValues[] = $this->formFactory->createValueSetFromElementValue($selection, $label);
176 }
177
178 return $this->formFactory->createSelect(
179 $configParameter->getId(),
180 $possibleValues,
181 $configParameter->getValue(),
182 $this->getParameterText($configParameter, false, $objectKey),
183 $this->getParameterText($configParameter, true, $objectKey)
184 );
185 }
186
187 return null;
188 }
189
190 /**
191 * Returns the settings form for the given config parameters.
192 *
193 * @param array $parameters
194 * @param string|null $objectKey
195 *
196 * @return \UserAccessManager\Form\Form
197 */
198 public function getSettingsForm(array $parameters, $objectKey = null)
199 {
200 $configParameters = $this->config->getConfigParameters();
201 $form = $this->formFactory->createFrom();
202
203 foreach ($parameters as $parameter) {
204 if (is_string($parameter) === true && isset($configParameters[$parameter]) === true) {
205 $formElement = $this->convertConfigParameter($configParameters[$parameter], $objectKey);
206
207 if ($formElement !== null) {
208 $form->addElement($formElement);
209 }
210 } elseif (($parameter instanceof FormElement) === true) {
211 $form->addElement($parameter);
212 }
213 }
214
215 return $form;
216 }
217
218 /**
219 * Converts config parameters to a form.
220 *
221 * @param Config $config
222 *
223 * @return Form
224 */
225 public function getSettingsFormByConfig(Config $config)
226 {
227 $form = $this->formFactory->createFrom();
228 $configParameters = $config->getConfigParameters();
229
230 foreach ($configParameters as $configParameter) {
231 $formElement = $this->convertConfigParameter($configParameter);
232
233 if ($formElement !== null) {
234 $form->addElement($formElement);
235 }
236 }
237
238 return $form;
239 }
240 }
241