[CLEANUP] Ensure variables initalized and fix code smell
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / TypoScript / ConfigurationForm.php
1 <?php
2 namespace TYPO3\CMS\Core\TypoScript;
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\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Provides a simplified layer for making Constant Editor style configuration forms
22 */
23 class ConfigurationForm extends ExtendedTemplateService
24 {
25 /**
26 * @var array
27 */
28 public $categories = [];
29
30 /**
31 * @var bool
32 */
33 public $ext_dontCheckIssetValues = 1;
34
35 /**
36 * @var string
37 */
38 public $ext_CEformName = 'tsStyleConfigForm';
39
40 /**
41 * @var bool
42 */
43 public $ext_printAll = true;
44
45 /**
46 * @var array
47 */
48 public $ext_incomingValues = [];
49
50 /**
51 * @var array
52 */
53 protected $ext_realValues = [];
54
55 /**
56 * @param string $configTemplate
57 * @return array
58 */
59 public function ext_initTSstyleConfig($configTemplate)
60 {
61 // Do not log time-performance information
62 $this->tt_track = 0;
63 $this->constants = [$configTemplate, ''];
64 // The editable constants are returned in an array.
65 $theConstants = $this->generateConfig_constants();
66 return $theConstants;
67 }
68
69 /**
70 * Ext set value array
71 *
72 * @param array $theConstants
73 * @param array $valueArray
74 * @return array
75 */
76 public function ext_setValueArray($theConstants, $valueArray)
77 {
78 $temp = $this->flatSetup;
79 $this->flatSetup = [];
80 $this->flattenSetup($valueArray, '');
81 $this->objReg = $this->ext_realValues = $this->flatSetup;
82 $this->flatSetup = $temp;
83 foreach ($theConstants as $k => $p) {
84 if (isset($this->objReg[$k])) {
85 $theConstants[$k]['value'] = $this->ext_realValues[$k];
86 }
87 }
88 // Reset the default pool of categories.
89 $this->categories = [];
90 // The returned constants are sorted in categories, that goes into the $this->categories array
91 $this->ext_categorizeEditableConstants($theConstants);
92 return $theConstants;
93 }
94
95 /**
96 * @return array
97 */
98 public function ext_getCategoriesForModMenu()
99 {
100 return $this->ext_getCategoryLabelArray();
101 }
102
103 /**
104 * @param string $cat
105 * @return void
106 */
107 public function ext_makeHelpInformationForCategory($cat)
108 {
109 $this->ext_getTSCE_config($cat);
110 }
111
112 /**
113 * Get the form for extension configuration
114 *
115 * @param string $cat
116 * @param array $theConstants
117 * @param string $script
118 * @param string $addFields
119 * @param string $extKey
120 * @param bool $addFormTag Adds opening <form> tag to the output, if TRUE
121 * @return string The form
122 */
123 public function ext_getForm($cat, $theConstants, $script = '', $addFields = '', $extKey = '', $addFormTag = true)
124 {
125 $this->ext_makeHelpInformationForCategory($cat);
126 $printFields = trim($this->ext_printFields($theConstants, $cat));
127 $content = '';
128 $content .= GeneralUtility::wrapJS('
129 function uFormUrl(aname) {
130 document.' . $this->ext_CEformName . '.action = ' . GeneralUtility::quoteJSvalue(GeneralUtility::linkThisScript() . '#') . '+aname;
131 }
132 ');
133 if ($addFormTag) {
134 $content .= '<form action="' . htmlspecialchars(($script ?: GeneralUtility::linkThisScript())) . '" name="' . $this->ext_CEformName . '" method="post" enctype="multipart/form-data">';
135 }
136 $content .= $addFields;
137 $content .= $printFields;
138 $content .= '<input class="btn btn-default" type="submit" name="submit" value="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_tsfe.xlf:update')) . '" id="configuration-submit-' . htmlspecialchars($extKey) . '" />';
139 $example = $this->ext_displayExample();
140 $content .= $example ? '<hr/>' . $example : '';
141 return $content;
142 }
143
144 /**
145 * Display example
146 *
147 * @return string
148 */
149 public function ext_displayExample()
150 {
151 $out = '';
152 if ($this->helpConfig['description'] || $this->helpConfig['header']) {
153 $out = ($this->helpConfig['description'] ? implode(explode('//', $this->helpConfig['description']), '<br />') . '<br />' : '')
154 . ($this->helpConfig['bulletlist'] ? '<ul><li>' . implode(explode('//', $this->helpConfig['bulletlist']), '<li>') . '</ul>' : '<BR>');
155 }
156 return $out;
157 }
158
159 /**
160 * Merge incoming with existing
161 *
162 * @param array $arr
163 * @return array
164 */
165 public function ext_mergeIncomingWithExisting($arr)
166 {
167 $parseObj = GeneralUtility::makeInstance(Parser\TypoScriptParser::class);
168 $parseObj->parse(implode(LF, $this->ext_incomingValues));
169 $arr2 = $parseObj->setup;
170 ArrayUtility::mergeRecursiveWithOverrule($arr, $arr2);
171 return $arr;
172 }
173
174 /**
175 * @param array $params
176 * @return array
177 */
178 public function ext_fNandV($params)
179 {
180 $fN = 'data[' . $params['name'] . ']';
181 $idName = str_replace('.', '-', $params['name']);
182 $fV = ($params['value'] = isset($this->ext_realValues[$params['name']]) ? $this->ext_realValues[$params['name']] : $params['default_value']);
183 $reg = [];
184 // Values entered from the constantsedit cannot be constants!
185 if (preg_match('/^\\{[\\$][a-zA-Z0-9\\.]*\\}$/', trim($fV), $reg)) {
186 $fV = '';
187 }
188 $fV = htmlspecialchars($fV);
189 return [$fN, $fV, $params, $idName];
190 }
191
192 /**
193 * @param string $key
194 * @param string $var
195 * @return void
196 */
197 public function ext_putValueInConf($key, $var)
198 {
199 $this->ext_incomingValues[$key] = $key . '=' . $var;
200 }
201
202 /**
203 * @param string $key
204 * @return void
205 */
206 public function ext_removeValueInConf($key)
207 {
208 }
209 }