[TASK] Remove old language labels in EXT:setup
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / View / Wizard / Element / FormWizardElement.php
1 <?php
2 namespace TYPO3\CMS\Form\View\Wizard\Element;
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\Backend\Form\Element\AbstractFormElement;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\StringUtility;
20 use TYPO3\CMS\Extbase\Service\TypoScriptService;
21 use TYPO3\CMS\Form\Domain\Repository\ContentRepository;
22 use TYPO3\CMS\Form\Utility\TypoScriptToJsonConverter;
23
24 /**
25 * form Wizard widget
26 */
27 class FormWizardElement extends AbstractFormElement
28 {
29 /**
30 * @var array
31 */
32 protected $resultArray;
33
34 /**
35 * Store initialized resultArray
36 */
37 protected function initializeResultArray()
38 {
39 $this->resultArray = parent::initializeResultArray();
40 }
41
42 /**
43 * @return int
44 */
45 protected function getCurrentPageId()
46 {
47 // $this->data used to be globalOptions
48 return (int)$this->data['inlineFirstPid'];
49 }
50
51 /**
52 * @return int
53 */
54 protected function getCurrentUid()
55 {
56 return (int)$this->data['databaseRow']['uid'];
57 }
58
59 /**
60 * @return array
61 */
62 protected function getPlainPageWizardModTsConfigSettingsProperties()
63 {
64 $settings = $this->data['pageTsConfig']['mod.']['wizards.']['form.'];
65 return $this->getTypoScriptService()->convertTypoScriptArrayToPlainArray($settings);
66 }
67
68 /**
69 * Gets the repository object.
70 *
71 * @return ContentRepository
72 */
73 protected function getRepository()
74 {
75 return GeneralUtility::makeInstance(ContentRepository::class);
76 }
77
78 /**
79 * Read and convert the content record to JSON
80 *
81 * @see \TYPO3\CMS\Form\Domain\Repository\ContentRepository::getRecordAsJson
82 * @return TYPO3\CMS\Form\Domain\Model\Json\FormJsonElement|false The JSON object if record exists, FALSE if not
83 */
84 protected function getRecordAsJson()
85 {
86 $json = false;
87 $record = $this->getRepository()->getRecord($this->getCurrentUid(), 'tt_content');
88 if ($record) {
89 $typoscript = $record->getTyposcript();
90 /** @var $converter TypoScriptToJsonConverter */
91 $converter = GeneralUtility::makeInstance(TypoScriptToJsonConverter::class);
92 $json = $converter->convert($typoscript);
93 }
94 return $json;
95 }
96
97 /**
98 * @return string
99 */
100 protected function getAjaxUrl()
101 {
102 /**
103 * @see TYPO3.CMS/src/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php:267 for wizard type=popup
104 */
105 $parameterArray = $this->data['parameterArray'];
106 $table = $this->data['tableName'];
107 $fieldName = $this->data['fieldName'];
108 $row = $this->data['databaseRow'];
109 $itemName = $parameterArray['itemFormElName'];
110 // Resolving script filename and setting URL.
111
112 $params = [];
113 $params['fieldConfig'] = $parameterArray['fieldConf'];
114 $params['table'] = $table;
115 $params['uid'] = $row['uid'];
116 $params['pid'] = $row['pid'];
117 $params['field'] = $fieldName;
118
119 $params['formName'] = 'editform';
120 $params['itemName'] = $itemName;
121 $params['hmac'] = GeneralUtility::hmac($params['formName'] . $params['itemName'], 'wizard_js');
122
123 return GeneralUtility::implodeArrayForUrl('', ['P' => $params]);
124 }
125
126 /**
127 * locallang files for return array
128 *
129 * @return array strings
130 */
131 protected function getLocalization()
132 {
133 $wizardLabels = 'EXT:form/Resources/Private/Language/locallang_wizard.xlf';
134 $controllerLabels = 'EXT:form/Resources/Private/Language/locallang.xlf';
135 return [$controllerLabels, $wizardLabels];
136 }
137
138 /**
139 * @param array $settings
140 * @return string
141 */
142 protected function resultAddWizardSettingsJson(array $settings)
143 {
144 $recordJson = $this->getRecordAsJson();
145 $settings['Configuration'] = $recordJson;
146 $settings['ajaxUrl'] = $this->getAjaxUrl();
147 $settingsCommand = 'TYPO3.Form.Wizard.Settings = ' . json_encode($settings) . ';';
148 // enhance global variable for requireJs shim "exports: 'TYPO3.Form.Wizard.Settings'" to work
149 $this->resultArray['additionalJavaScriptPost'][] = ';TYPO3.Form = TYPO3.Form || {Wizard:{}};';
150 $this->resultArray['additionalJavaScriptPost'][] =
151 ';define("TYPO3/CMS/Form/Wizard/Settings", function() {'
152 . "\n" . ' TYPO3.Form.Wizard.Settings = ' . json_encode($settings) . ';'
153 . "\n" . ' return TYPO3.Form.Wizard.Settings;'
154 . "\n" . '});';
155 return $settingsCommand;
156 }
157
158 /**
159 * Load the wizards css
160 */
161 protected function resultAddWizardCss()
162 {
163 $this->resultArray['stylesheetFiles'][] = 'EXT:form/Resources/Public/Css/form.css';
164 }
165
166 /**
167 * @return array
168 */
169 public function render()
170 {
171 $this->initializeResultArray();
172
173 /** @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication $beUser */
174 $beUser = $GLOBALS['BE_USER'];
175 /** @var string $showWizardByDefault */
176 $showWizardByDefault = $beUser->getTSConfigVal('setup.default.tx_form.showWizardByDefault');
177 if ((int)$showWizardByDefault == 0) {
178 $content = '';
179 $record = $this->getRepository()->getRecord($this->getCurrentUid(), 'tt_content');
180 if ($record) {
181 $content = $record->getBodytext();
182 }
183
184 $id = StringUtility::getUniqueId('formengine-textarea-');
185 $this->resultArray['html'] = '<textarea id="formengine-textarea-' . $id . '"'
186 . ' class="form-control t3js-formengine-textarea formengine-textarea" wrap="off"'
187 . ' onchange="TBE_EDITOR.fieldChanged(\'tt_content\',\'' . $this->getCurrentUid() . '\',\'bodytext\',\'data[tt_content][' . $this->getCurrentUid() . '][bodytext]\');"'
188 . ' rows="15" style="" name="data[tt_content][' . $this->getCurrentUid() . '][bodytext]">' . $content . '</textarea>';
189 return $this->resultArray;
190 }
191
192 $this->resultAddWizardCss();
193 $this->resultArray['additionalInlineLanguageLabelFiles'] += $this->getLocalization();
194 $settings = $this->getPlainPageWizardModTsConfigSettingsProperties();
195 $settingsCommand = $this->resultAddWizardSettingsJson($settings);
196 $this->resultArray['requireJsModules'][] = [
197 'TYPO3/CMS/Form/Wizard' => "function(){\n"
198 //. "\t" . 'console.log(this, arguments);' . "\n"
199 . "\t" . $settingsCommand . "\n"
200 . '}'
201 ];
202 $attributes = [];
203 $attributes['id'] = StringUtility::getUniqueId('formengine-form-wizard-');
204 /**
205 * @see TYPO3.CMS/src/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php:267 for wizard type=popup
206 */
207 $parameterArray = $this->data['parameterArray'];
208 $attributes['name'] = $parameterArray['itemFormElName'];
209
210 $attributeString = '';
211 foreach ($attributes as $attributeName => $attributeValue) {
212 $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
213 }
214
215 $input = '<input ' . $attributeString . ' type="hidden" />' . "\n";
216 $content = $input . '<div id="form-wizard-element"></div>';
217 $this->resultArray['html'] = '<div id="form-wizard-element-container" rel="' . $attributes['id'] . '">'
218 . "\n" . $content
219 . "\n" . '</div>';
220 return $this->resultArray;
221 }
222
223 /**
224 * @return TypoScriptService
225 */
226 protected function getTypoScriptService()
227 {
228 return GeneralUtility::makeInstance(TypoScriptService::class);
229 }
230 }