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