[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputSlugElement.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\Element;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\Controller\FormSlugAjaxController;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Site\Entity\SiteInterface;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24 use TYPO3\CMS\Core\Utility\StringUtility;
25
26 /**
27 * General type=input element with some additional value.
28 */
29 class InputSlugElement extends AbstractFormElement
30 {
31
32 /**
33 * Default field information enabled for this element.
34 *
35 * @var array
36 */
37 protected $defaultFieldInformation = [
38 'tcaDescription' => [
39 'renderType' => 'tcaDescription',
40 ],
41 ];
42
43 /**
44 * Default field wizards enabled for this element.
45 *
46 * @var array
47 */
48 protected $defaultFieldWizard = [
49 'localizationStateSelector' => [
50 'renderType' => 'localizationStateSelector',
51 ],
52 'otherLanguageContent' => [
53 'renderType' => 'otherLanguageContent',
54 'after' => [
55 'localizationStateSelector'
56 ],
57 ],
58 'defaultLanguageDifferences' => [
59 'renderType' => 'defaultLanguageDifferences',
60 'after' => [
61 'otherLanguageContent',
62 ],
63 ],
64 ];
65
66 /**
67 * This will render a single-line input form field, possibly with various control/validation features
68 *
69 * @return array As defined in initializeResultArray() of AbstractNode
70 */
71 public function render()
72 {
73 $table = $this->data['tableName'];
74 $row = $this->data['databaseRow'];
75 $parameterArray = $this->data['parameterArray'];
76 $resultArray = $this->initializeResultArray();
77
78 $languageId = 0;
79 if (isset($GLOBALS['TCA'][$table]['ctrl']['languageField']) && !empty($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
80 $languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
81 $languageId = (int)((is_array($row[$languageField]) ? $row[$languageField][0] : $row[$languageField]) ?? 0);
82 }
83 $baseUrl = $this->getPrefix($this->data['site'], $languageId);
84
85 $itemValue = $parameterArray['itemFormElValue'];
86 $config = $parameterArray['fieldConf']['config'];
87 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
88 $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
89 $width = (int)$this->formMaxWidth($size);
90
91 // Convert UTF-8 characters back (that is important, see Slug class when sanitizing)
92 $itemValue = rawurldecode($itemValue);
93
94 $fieldInformationResult = $this->renderFieldInformation();
95 $fieldInformationHtml = $fieldInformationResult['html'];
96 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
97
98 $fieldControlResult = $this->renderFieldControl();
99 $fieldControlHtml = $fieldControlResult['html'];
100 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
101
102 $fieldWizardResult = $this->renderFieldWizard();
103 $fieldWizardHtml = $fieldWizardResult['html'];
104 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
105 $toggleButtonTitle = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.toggleSlugExplanation');
106 $recreateButtonTitle = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.recreateSlugExplanation');
107
108 $thisSlugId = 't3js-form-field-slug-id' . StringUtility::getUniqueId();
109 $mainFieldHtml = [];
110 $mainFieldHtml[] = '<div class="formengine-field-item t3js-formengine-field-item">';
111 $mainFieldHtml[] = $fieldInformationHtml;
112 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px" id="' . htmlspecialchars($thisSlugId) . '">';
113 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
114 $mainFieldHtml[] = '<div class="form-wizards-element">';
115 $mainFieldHtml[] = '<div class="input-group">';
116 $mainFieldHtml[] = ($baseUrl ? '<span class="input-group-addon">' . htmlspecialchars($baseUrl) . '</span>' : '');
117 // We deal with 3 fields here: a readonly field for current / default values, an input
118 // field to manipulate the value, and the final hidden field used to send the value
119 $mainFieldHtml[] = '<input';
120 $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-readonly"';
121 $mainFieldHtml[] = ' data-toggle="tooltip"';
122 $mainFieldHtml[] = ' data-title="' . htmlspecialchars($itemValue) . '"';
123 $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
124 $mainFieldHtml[] = ' readonly';
125 $mainFieldHtml[] = ' />';
126 $mainFieldHtml[] = '<input type="text"';
127 $mainFieldHtml[] = ' id="' . htmlspecialchars(StringUtility::getUniqueId('formengine-input-')) . '"';
128 $mainFieldHtml[] = ' class="form-control t3js-form-field-slug-input hidden"';
129 $mainFieldHtml[] = ' placeholder="' . htmlspecialchars($row['slug'] ?? '/') . '"';
130 $mainFieldHtml[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->getValidationDataAsJsonString($config)) . '"';
131 $mainFieldHtml[] = ' data-formengine-input-params="' . htmlspecialchars(json_encode(['field' => $parameterArray['itemFormElName'], 'evalList' => implode(',', $evalList)])) . '"';
132 $mainFieldHtml[] = ' data-formengine-input-name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
133 $mainFieldHtml[] = ' />';
134 $mainFieldHtml[] = '<span class="input-group-btn">';
135 $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
136 $mainFieldHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL)->render();
137 $mainFieldHtml[] = '</button>';
138 $mainFieldHtml[] = '<button class="btn btn-default t3js-form-field-slug-recreate" type="button" title="' . htmlspecialchars($recreateButtonTitle) . '">';
139 $mainFieldHtml[] = $this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL)->render();
140 $mainFieldHtml[] = '</button>';
141 $mainFieldHtml[] = '</span>';
142 $mainFieldHtml[] = '<input type="hidden"';
143 $mainFieldHtml[] = ' class="t3js-form-field-slug-hidden"';
144 $mainFieldHtml[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
145 $mainFieldHtml[] = ' value="' . htmlspecialchars($itemValue) . '"';
146 $mainFieldHtml[] = ' />';
147 $mainFieldHtml[] = '</div>';
148 $mainFieldHtml[] = '</div>';
149 if (!empty($fieldControlHtml)) {
150 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
151 $mainFieldHtml[] = '<div class="btn-group">';
152 $mainFieldHtml[] = $fieldControlHtml;
153 $mainFieldHtml[] = '</div>';
154 $mainFieldHtml[] = '</div>';
155 }
156 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
157 $mainFieldHtml[] = '<span class="t3js-form-proposal-accepted hidden label label-success">Congrats, this page will look like ' . htmlspecialchars($baseUrl) . '<span>/abc/</span></span>';
158 $mainFieldHtml[] = '<span class="t3js-form-proposal-different hidden label label-warning">Hmm, that is taken, how about ' . htmlspecialchars($baseUrl) . '<span>/abc/</span></span>';
159 $mainFieldHtml[] = $fieldWizardHtml;
160 $mainFieldHtml[] = '</div>';
161 $mainFieldHtml[] = '</div>';
162 $mainFieldHtml[] = '</div>';
163 $mainFieldHtml[] = '</div>';
164
165 $resultArray['html'] = implode(LF, $mainFieldHtml);
166
167 [$commonElementPrefix] = GeneralUtility::revExplode('[', $parameterArray['itemFormElName'], 2);
168 $validInputNamesToListenTo = [];
169 foreach ($config['generatorOptions']['fields'] ?? [] as $fieldNameParts) {
170 if (is_string($fieldNameParts)) {
171 $fieldNameParts = GeneralUtility::trimExplode(',', $fieldNameParts);
172 }
173 foreach ($fieldNameParts as $listenerFieldName) {
174 $validInputNamesToListenTo[$listenerFieldName] = $commonElementPrefix . '[' . htmlspecialchars($listenerFieldName) . ']';
175 }
176 }
177 $parentPageId = $this->data['parentPageRow']['uid'] ?? 0;
178 $signature = GeneralUtility::hmac(
179 implode(
180 '',
181 [
182 $table,
183 $this->data['effectivePid'],
184 $row['uid'],
185 $languageId,
186 $this->data['fieldName'],
187 $this->data['command'],
188 $parentPageId
189 ]
190 ),
191 FormSlugAjaxController::class
192 );
193 $optionsForModule = [
194 'pageId' => $this->data['effectivePid'],
195 'recordId' => $row['uid'],
196 'tableName' => $table,
197 'fieldName' => $this->data['fieldName'],
198 'config' => $config,
199 'listenerFieldNames' => $validInputNamesToListenTo,
200 'language' => $languageId,
201 'originalValue' => $itemValue,
202 'signature' => $signature,
203 'command' => $this->data['command'],
204 'parentPageId' => $parentPageId,
205 ];
206 $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SlugElement' => '
207 function(SlugElement) {
208 new SlugElement(' . GeneralUtility::quoteJSvalue('#' . $thisSlugId) . ', ' . json_encode($optionsForModule) . ');
209 }'
210 ];
211 return $resultArray;
212 }
213
214 /**
215 * Render the prefix for the input field.
216 *
217 * @param SiteInterface $site
218 * @param int $requestLanguageId
219 * @return string
220 */
221 protected function getPrefix(SiteInterface $site, int $requestLanguageId = 0): string
222 {
223 try {
224 $language = ($requestLanguageId < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($requestLanguageId);
225 $base = $language->getBase();
226 $baseUrl = (string)$base;
227 $baseUrl = rtrim($baseUrl, '/');
228 if (!empty($baseUrl) && empty($base->getScheme()) && $base->getHost() !== '') {
229 $baseUrl = 'http:' . $baseUrl;
230 }
231 } catch (\InvalidArgumentException $e) {
232 // No site found
233 $baseUrl = '';
234 }
235 return $baseUrl;
236 }
237
238 /**
239 * @return LanguageService
240 */
241 protected function getLanguageService(): LanguageService
242 {
243 return $GLOBALS['LANG'];
244 }
245 }