[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / FormUtility.php
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Domain\Model\Configuration;
19
20 /**
21 * A utility for the form
22 */
23 class FormUtility
24 {
25 /**
26 * @param Configuration $configuration
27 * @return FormBuilder
28 */
29 public static function create(Configuration $configuration)
30 {
31 /** @var FormBuilder $formBuilder */
32 $formUtility = FormUtility::getObjectManager()->get(FormUtility::class);
33 $formUtility->setConfiguration($configuration);
34 return $formUtility;
35 }
36
37 /**
38 * @var Configuration
39 */
40 protected $configuration;
41
42 /**
43 * @param Configuration $configuration
44 */
45 public function setConfiguration(Configuration $configuration)
46 {
47 $this->configuration = $configuration;
48 }
49
50 /**
51 * Render TypoScript values
52 * There are different variants. It is possible that the TypoScript
53 * which we want to render looks like this:
54 *
55 * array(
56 * 'message' => 'TEXT',
57 * 'message.' => array(
58 * 'value' => 'blah'
59 * )
60 * )
61 *
62 * or in "short syntax" (the wizard writes some syntax partly)
63 *
64 * array(
65 * 'message.' => array(
66 * 'value' => 'blah'
67 * )
68 * )
69 *
70 * or it is simply a string.
71 *
72 * Furthermore we have 2 modes:
73 * - contentelement rendering is allowed
74 * - or contentelement rendering is not allowed
75 *
76 * This method will take care of all scenarios and provide some
77 * fallbacks.
78 * Call this method always in the following way:
79 *
80 * renderItem(
81 * $typoscript['itemToRender.'],
82 * $typoscript['itemToRender'],
83 * $optionalDefaultMessage
84 * )
85 *
86 * You dont have to handle if is $typoscript['itemToRender.'] is
87 * set or not. This function determines this.
88 * This allows us to get the value of a TypoScript construct
89 * without knowing about "short syntax", only a string, a cObject,
90 * if cObject rendering is allowed and so on.
91 *
92 * If contentelement rendering is allowed:
93 * If $type and $configuration are set
94 * render as an cObject.
95 *
96 * If $type is set but $configuration is empty
97 * only return the value of $type.
98 *
99 * If $type is empty and $configuration is an array ("short syntax")
100 * render the $configuration as content type TEXT.
101 *
102 * If $type is empty and $configuration is a string
103 * render the value of $configuration like
104 * 10 = TEXT 10.value = $configuration.
105 *
106 * If $type is empty and $configuration is empty
107 * return the $defaultMessage.
108 *
109 * If contentelement rendering is not allowed:
110 * If $type is set but $configuration is empty
111 * only return the value of $type.
112 *
113 * If $type is set and $configuration['value'] isset
114 * return the value of $configuration['value'].
115 *
116 * If $type is set and $configuration['value'] is not set
117 * return the value of $defaultMessage.
118 *
119 * If $type is empty and $configuration['value'] isset
120 * return the value of $configuration['value'].
121 *
122 * If $type is empty and $configuration['value'] is not set
123 * return the value of $defaultMessage.
124 *
125 * @param mixed $configuration a string or a TypoScript array
126 * @param NULL|string $type cObject type or simply a string value
127 * @param string $defaultMessage
128 * @return string
129 */
130 public function renderItem($configuration, $type = null, $defaultMessage = '')
131 {
132 if ($this->configuration->getContentElementRendering()) {
133 $renderedMessage = null;
134 if ($type !== null) {
135 if (is_array($configuration)) {
136 /* Direct cObject rendering */
137 $value = $configuration;
138 } else {
139 /* got only a string, no rendering required */
140 $renderedMessage = $type;
141 }
142 } else {
143 if ($configuration !== null) {
144 /* Render the "short syntax"
145 * The wizard write things like label.value
146 * The previous version of EXT:form interpreted this
147 * as a TEXT content object, so we do the same
148 * */
149 $type = 'TEXT';
150 if (is_array($configuration)) {
151 $value = $configuration;
152 } else {
153 $value['value'] = $configuration;
154 }
155 } else {
156 /* return the default message
157 * If $type === NULL and $configuration === NULL
158 * return the default message (if set).
159 * */
160 $renderedMessage = $defaultMessage;
161 }
162 }
163 if ($renderedMessage === null) {
164 $renderedMessage = $GLOBALS['TSFE']->cObj->cObjGetSingle(
165 $type,
166 $value
167 );
168 }
169 } else {
170 if ($type !== null) {
171 if ($configuration !== null) {
172 /* the wizard write things like label.value = some text
173 * so we need the handle that, even content object rendering
174 * is not allowed.
175 * */
176 if (isset($configuration['value'])) {
177 $renderedMessage = $configuration['value'];
178 } else {
179 $renderedMessage = $defaultMessage;
180 }
181 } else {
182 // string, no rendering required
183 $renderedMessage = $type;
184 }
185 } else {
186 $renderedMessage = $defaultMessage;
187 if (
188 is_array($configuration)
189 && isset($configuration['value'])
190 ) {
191 $renderedMessage = $configuration['value'];
192 }
193 }
194 }
195 return $renderedMessage;
196 }
197
198 /**
199 * Render array values recursively as cObjects using the
200 * method renderItem.
201 *
202 * @param array $arrayToRender
203 * @return array
204 */
205 public function renderArrayItems(array &$arrayToRender = [])
206 {
207 foreach ($arrayToRender as $attributeName => &$attributeValue) {
208 $attributeNameWithoutDot = rtrim($attributeName, '.');
209 if (
210 isset($arrayToRender[$attributeNameWithoutDot])
211 && isset($arrayToRender[$attributeNameWithoutDot . '.'])
212 ) {
213 $attributeValue = $this->renderItem(
214 $arrayToRender[$attributeNameWithoutDot . '.'],
215 $arrayToRender[$attributeNameWithoutDot]
216 );
217 unset($arrayToRender[$attributeNameWithoutDot . '.']);
218 } elseif (
219 !isset($arrayToRender[$attributeNameWithoutDot])
220 && isset($arrayToRender[$attributeNameWithoutDot . '.'])
221 ) {
222 $this->renderArrayItems($attributeValue);
223 }
224 }
225 }
226
227 /**
228 * If the name is not defined it is automatically generated
229 * using the following syntax: id-{element_counter}
230 * The name attribute will be transformed if it contains some
231 * non allowed characters:
232 * - spaces are changed into hyphens
233 * - remove all characters except a-z A-Z 0-9 _ -
234 *
235 * @param string $name
236 * @return string
237 */
238 public function sanitizeNameAttribute($name)
239 {
240 if (!empty($name)) {
241 // Change spaces into hyphens
242 $name = preg_replace('/\\s/', '-', $name);
243 // Remove non-word characters
244 $name = preg_replace('/[^a-zA-Z0-9_\\-]+/', '', $name);
245 }
246 return $name;
247 }
248
249 /**
250 * If the id is not defined it is automatically generated
251 * using the following syntax: field-{element_counter}
252 * The id attribute will be transformed if it contains some
253 * non allowed characters:
254 * - spaces are changed into hyphens
255 * - if the id start with a integer then transform it to field-{integer}
256 * - remove all characters expect a-z A-Z 0-9 _ - : .
257 *
258 * @param string $id
259 * @return string
260 */
261 public function sanitizeIdAttribute($id)
262 {
263 if (!empty($id)) {
264 // Change spaces into hyphens
265 $attribute = preg_replace('/\\s/', '-', $id);
266 // Change first non-letter to field-
267 if (preg_match('/^([^a-zA-Z]{1})/', $attribute)) {
268 $id = 'field-' . $attribute;
269 }
270 // Remove non-word characters
271 $id = preg_replace('/([^a-zA-Z0-9_:\\-\\.]*)/', '', $id);
272 }
273 return $id;
274 }
275
276 /**
277 * @return \TYPO3\CMS\Extbase\Object\ObjectManager
278 */
279 public static function getObjectManager()
280 {
281 return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
282 }
283 }