[TASK] Use null coalescing operator where possible
[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 * Deprecate this class
57 */
58 public function __construct()
59 {
60 trigger_error('Class ConfigurationForm has been deprecated with v9 and will be removed in v10', E_USER_DEPRECATED);
61 }
62
63 /**
64 * @param string $configTemplate
65 * @return array
66 */
67 public function ext_initTSstyleConfig($configTemplate)
68 {
69 // Do not log time-performance information
70 $this->tt_track = 0;
71 $this->constants = [$configTemplate, ''];
72 // The editable constants are returned in an array.
73 $theConstants = $this->generateConfig_constants();
74 return $theConstants;
75 }
76
77 /**
78 * Ext set value array
79 *
80 * @param array $theConstants
81 * @param array $valueArray
82 * @return array
83 */
84 public function ext_setValueArray($theConstants, $valueArray)
85 {
86 $temp = $this->flatSetup;
87 $this->flatSetup = [];
88 $this->flattenSetup($valueArray, '');
89 $this->objReg = $this->ext_realValues = $this->flatSetup;
90 $this->flatSetup = $temp;
91 foreach ($theConstants as $k => $p) {
92 if (isset($this->objReg[$k])) {
93 $theConstants[$k]['value'] = $this->ext_realValues[$k];
94 }
95 }
96 // Reset the default pool of categories.
97 $this->categories = [];
98 // The returned constants are sorted in categories, that goes into the $this->categories array
99 $this->ext_categorizeEditableConstants($theConstants);
100 return $theConstants;
101 }
102
103 /**
104 * @return array
105 */
106 public function ext_getCategoriesForModMenu()
107 {
108 return $this->ext_getCategoryLabelArray();
109 }
110
111 /**
112 * Get the form for extension configuration
113 *
114 * @param string $cat
115 * @param array $theConstants
116 * @param string $script
117 * @param string $addFields
118 * @param string $extKey
119 * @param bool $addFormTag Adds opening <form> tag to the output, if TRUE
120 * @return string The form
121 */
122 public function ext_getForm($cat, $theConstants, $script = '', $addFields = '', $extKey = '', $addFormTag = true)
123 {
124 $printFields = trim($this->ext_printFields($theConstants, $cat));
125 $content = '';
126 $content .= GeneralUtility::wrapJS('
127 function uFormUrl(aname) {
128 document.' . $this->ext_CEformName . '.action = ' . GeneralUtility::quoteJSvalue(GeneralUtility::linkThisScript() . '#') . '+aname;
129 }
130 ');
131 if ($addFormTag) {
132 $content .= '<form action="' . htmlspecialchars(($script ?: GeneralUtility::linkThisScript())) . '" name="' . $this->ext_CEformName . '" method="post" enctype="multipart/form-data">';
133 }
134 $content .= $addFields;
135 $content .= $printFields;
136 $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) . '" />';
137 return $content;
138 }
139
140 /**
141 * Merge incoming with existing
142 *
143 * @param array $arr
144 * @return array
145 */
146 public function ext_mergeIncomingWithExisting($arr)
147 {
148 $parseObj = GeneralUtility::makeInstance(Parser\TypoScriptParser::class);
149 $parseObj->parse(implode(LF, $this->ext_incomingValues));
150 $arr2 = $parseObj->setup;
151 ArrayUtility::mergeRecursiveWithOverrule($arr, $arr2);
152 return $arr;
153 }
154
155 /**
156 * @param array $params
157 * @return array
158 */
159 public function ext_fNandV($params)
160 {
161 $fN = 'data[' . $params['name'] . ']';
162 $idName = str_replace('.', '-', $params['name']);
163 $fV = ($params['value'] = $this->ext_realValues[$params['name']] ?? $params['default_value']);
164 $reg = [];
165 // Values entered from the constantsedit cannot be constants!
166 if (preg_match('/^\\{[\\$][a-zA-Z0-9\\.]*\\}$/', trim($fV), $reg)) {
167 $fV = '';
168 }
169 $fV = htmlspecialchars($fV);
170 return [$fN, $fV, $params, $idName];
171 }
172
173 /**
174 * @param string $key
175 * @param string $var
176 */
177 public function ext_putValueInConf($key, $var)
178 {
179 $this->ext_incomingValues[$key] = $key . '=' . $var;
180 }
181
182 /**
183 * @param string $key
184 */
185 public function ext_removeValueInConf($key)
186 {
187 }
188 }