[TASK] Migrate EXT:form Backend Wizard to Routing
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / View / Wizard / WizardView.php
1 <?php
2 namespace TYPO3\CMS\Form\View\Wizard;
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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Page\PageRenderer;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Form\Domain\Repository\ContentRepository;
25
26 /**
27 * The form wizard view
28 */
29 class WizardView
30 {
31 /**
32 * Is the referenced record available
33 *
34 * @var bool TRUE if available, FALSE if not
35 */
36 protected $recordIsAvailable = false;
37
38 /**
39 * @var ContentRepository
40 */
41 protected $repository;
42
43 /**
44 * The document template object
45 *
46 * Needs to be a local variable of the class.
47 *
48 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
49 */
50 public $doc;
51
52 /**
53 * @var PageRenderer
54 */
55 protected $pageRenderer;
56
57 /**
58 * Constructs this view
59 *
60 * Defines the global variable SOBE. Normally this is used by the wizards
61 * which are one file only. This view is now the class with the global
62 * variable name SOBE.
63 *
64 * Defines the document template object.
65 *
66 * @param ContentRepository $repository
67 * @see \TYPO3\CMS\Backend\Template\DocumentTemplate
68 */
69 public function __construct(ContentRepository $repository)
70 {
71 $this->setRepository($repository);
72 $this->getLanguageService()->includeLLFile('EXT:form/Resources/Private/Language/locallang_wizard.xlf');
73 $GLOBALS['SOBE'] = $this;
74 // Define the document template object
75 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
76 $this->doc->setModuleTemplate('EXT:form/Resources/Private/Templates/Wizard.html');
77 }
78
79 /**
80 * Sets the content repository to be used.
81 *
82 * @param ContentRepository $repository
83 * @return void
84 */
85 public function setRepository(ContentRepository $repository)
86 {
87 $this->repository = $repository;
88 }
89 /**
90 * The main render method
91 *
92 * Gathers all content and returns it
93 *
94 * @return string
95 */
96 public function render()
97 {
98 $docHeaderButtons = array();
99 // Check if the referenced record is available
100 $this->recordIsAvailable = $this->repository->hasRecord();
101 if ($this->recordIsAvailable) {
102 // Load necessary JavaScript
103 $this->loadJavascript();
104 // Load necessary CSS
105 $this->loadCss();
106 // Load the settings
107 $this->loadSettings();
108 // Localization
109 $this->loadLocalization();
110 // Setting up the buttons and markers for docheader
111 $docHeaderButtons = $this->getButtons();
112 $markers['CSH'] = $docHeaderButtons['csh'];
113 // Hook
114 $this->callRenderHook();
115 }
116 // Getting the body content
117 $markers['CONTENT'] = $this->getBodyContent();
118 // Build the HTML for the module
119 $content = $this->doc->startPage($this->getLanguageService()->getLL('title', true));
120 $content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markers);
121 $content .= $this->doc->endPage();
122 return $this->doc->insertStylesAndJS($content);
123 }
124
125 /**
126 * Load the necessary javascript
127 *
128 * This will only be done when the referenced record is available
129 *
130 * @return void
131 */
132 protected function loadJavascript()
133 {
134 $compress = true;
135 $javascriptFiles = array(
136 'Initialize.js',
137 'Ux/Ext.ux.merge.js',
138 'Ux/Ext.ux.isemptyobject.js',
139 'Ux/Ext.ux.spinner.js',
140 'Ux/Ext.ux.form.spinnerfield.js',
141 'Ux/Ext.ux.form.textfieldsubmit.js',
142 'Ux/Ext.ux.form.ValueCheckbox.js', // required by Viewport/Left/Options/Forms/Attributes.js
143 'Ux/Ext.ux.grid.CheckColumn.js',
144 'Ux/Ext.ux.grid.SingleSelectCheckColumn.js',
145 'Ux/Ext.ux.grid.ItemDeleter.js',
146 'Helpers/History.js',
147 'Helpers/Element.js',
148 'Elements/ButtonGroup.js',
149 'Elements/Container.js',
150 'Elements/Elements.js',
151 'Elements/Dummy.js',
152 'Elements/Basic/Button.js',
153 'Elements/Basic/Checkbox.js',
154 'Elements/Basic/Fieldset.js',
155 'Elements/Basic/Fileupload.js',
156 'Elements/Basic/Form.js',
157 'Elements/Basic/Hidden.js',
158 'Elements/Basic/Password.js',
159 'Elements/Basic/Radio.js',
160 'Elements/Basic/Reset.js',
161 'Elements/Basic/Select.js',
162 'Elements/Basic/Submit.js',
163 'Elements/Basic/Textarea.js',
164 'Elements/Basic/Textline.js',
165 'Elements/Predefined/Email.js',
166 'Elements/Predefined/CheckboxGroup.js',
167 'Elements/Predefined/Name.js',
168 'Elements/Predefined/RadioGroup.js',
169 'Elements/Content/Header.js',
170 'Elements/Content/Textblock.js',
171 'Viewport.js',
172 'Viewport/Left.js',
173 'Viewport/Right.js',
174 'Viewport/Left/Elements.js',
175 'Viewport/Left/Elements/ButtonGroup.js',
176 'Viewport/Left/Elements/Basic.js',
177 'Viewport/Left/Elements/Predefined.js',
178 'Viewport/Left/Elements/Content.js',
179 'Viewport/Left/Options.js',
180 'Viewport/Left/Options/Dummy.js',
181 'Viewport/Left/Options/Panel.js',
182 'Viewport/Left/Options/Forms/Attributes.js',
183 'Viewport/Left/Options/Forms/Label.js',
184 'Viewport/Left/Options/Forms/Legend.js',
185 'Viewport/Left/Options/Forms/Options.js',
186 'Viewport/Left/Options/Forms/Various.js',
187 'Viewport/Left/Options/Forms/Filters.js',
188 'Viewport/Left/Options/Forms/Filters/Filter.js',
189 'Viewport/Left/Options/Forms/Filters/Dummy.js',
190 'Viewport/Left/Options/Forms/Filters/Alphabetic.js',
191 'Viewport/Left/Options/Forms/Filters/Alphanumeric.js',
192 'Viewport/Left/Options/Forms/Filters/Currency.js',
193 'Viewport/Left/Options/Forms/Filters/Digit.js',
194 'Viewport/Left/Options/Forms/Filters/Integer.js',
195 'Viewport/Left/Options/Forms/Filters/LowerCase.js',
196 'Viewport/Left/Options/Forms/Filters/RegExp.js',
197 'Viewport/Left/Options/Forms/Filters/RemoveXSS.js',
198 'Viewport/Left/Options/Forms/Filters/StripNewLines.js',
199 'Viewport/Left/Options/Forms/Filters/TitleCase.js',
200 'Viewport/Left/Options/Forms/Filters/Trim.js',
201 'Viewport/Left/Options/Forms/Filters/UpperCase.js',
202 'Viewport/Left/Options/Forms/Validation.js',
203 'Viewport/Left/Options/Forms/Validation/Rule.js',
204 'Viewport/Left/Options/Forms/Validation/Dummy.js',
205 'Viewport/Left/Options/Forms/Validation/Alphabetic.js',
206 'Viewport/Left/Options/Forms/Validation/Alphanumeric.js',
207 'Viewport/Left/Options/Forms/Validation/Between.js',
208 'Viewport/Left/Options/Forms/Validation/Date.js',
209 'Viewport/Left/Options/Forms/Validation/Digit.js',
210 'Viewport/Left/Options/Forms/Validation/Email.js',
211 'Viewport/Left/Options/Forms/Validation/Equals.js',
212 'Viewport/Left/Options/Forms/Validation/FileAllowedTypes.js',
213 'Viewport/Left/Options/Forms/Validation/FileMaximumSize.js',
214 'Viewport/Left/Options/Forms/Validation/FileMinimumSize.js',
215 'Viewport/Left/Options/Forms/Validation/Float.js',
216 'Viewport/Left/Options/Forms/Validation/GreaterThan.js',
217 'Viewport/Left/Options/Forms/Validation/InArray.js',
218 'Viewport/Left/Options/Forms/Validation/Integer.js',
219 'Viewport/Left/Options/Forms/Validation/Ip.js',
220 'Viewport/Left/Options/Forms/Validation/Length.js',
221 'Viewport/Left/Options/Forms/Validation/LessThan.js',
222 'Viewport/Left/Options/Forms/Validation/RegExp.js',
223 'Viewport/Left/Options/Forms/Validation/Required.js',
224 'Viewport/Left/Options/Forms/Validation/Uri.js',
225 'Viewport/Left/Form.js',
226 'Viewport/Left/Form/Behaviour.js',
227 'Viewport/Left/Form/Attributes.js',
228 'Viewport/Left/Form/Prefix.js',
229 'Viewport/Left/Form/PostProcessor.js',
230 'Viewport/Left/Form/PostProcessors/PostProcessor.js',
231 'Viewport/Left/Form/PostProcessors/Dummy.js',
232 'Viewport/Left/Form/PostProcessors/Mail.js',
233 'Viewport/Left/Form/PostProcessors/Redirect.js'
234 );
235 // Load ExtJS
236 $this->getPageRenderer()->loadExtJS();
237 // Load the wizards javascript
238 $baseUrl = ExtensionManagementUtility::extRelPath('form') . 'Resources/Public/JavaScript/Wizard/';
239 foreach ($javascriptFiles as $javascriptFile) {
240 $this->getPageRenderer()->addJsFile($baseUrl . $javascriptFile, 'text/javascript', $compress, false);
241 }
242 }
243
244 /**
245 * Load the necessary css
246 *
247 * This will only be done when the referenced record is available
248 *
249 * @return void
250 */
251 protected function loadCss()
252 {
253 // @todo Set to TRUE when finished
254 $compress = false;
255 $cssFiles = array(
256 'Wizard/Form.css',
257 'Wizard/Wizard.css'
258 );
259 $baseUrl = ExtensionManagementUtility::extRelPath('form') . 'Resources/Public/CSS/';
260 // Load the wizards css
261 foreach ($cssFiles as $cssFile) {
262 $this->getPageRenderer()->addCssFile($baseUrl . $cssFile, 'stylesheet', 'all', '', $compress, false);
263 }
264 }
265
266 /**
267 * Load the settings
268 *
269 * The settings are defined in pageTSconfig mod.wizards.form
270 *
271 * @return void
272 */
273 protected function loadSettings()
274 {
275 $record = $this->repository->getRecord();
276 $pageId = $record->getPageId();
277 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.wizards.form');
278 $settings = $modTSconfig['properties'];
279 $this->removeTrailingDotsFromTyposcript($settings);
280 $this->doc->JScode .= $this->doc->wrapScriptTags('TYPO3.Form.Wizard.Settings = ' . json_encode($settings) . ';');
281 }
282
283 /**
284 * Reads locallang file into array (for possible include in header)
285 *
286 * @return void
287 */
288 protected function loadLocalization()
289 {
290 $wizardLabels = $this->getLanguageService()->includeLLFile('EXT:form/Resources/Private/Language/locallang_wizard.xlf', false, true);
291 $controllerLabels = $this->getLanguageService()->includeLLFile('EXT:form/Resources/Private/Language/locallang.xlf', false, true);
292 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($controllerLabels, $wizardLabels);
293 $this->getPageRenderer()->addInlineLanguageLabelArray($controllerLabels['default']);
294 }
295
296 /**
297 * Hook to extend the wizard interface.
298 *
299 * The hook is called just before content rendering. Use it by adding your function to the array
300 * $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['form']['hooks']['renderWizard']
301 *
302 * @return void
303 */
304 protected function callRenderHook()
305 {
306 $params = array();
307 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['form']['hooks']['renderWizard'])) {
308 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['form']['hooks']['renderWizard'] as $funcRef) {
309 GeneralUtility::callUserFunction($funcRef, $params, $this);
310 }
311 }
312 }
313
314 /**
315 * Remove the trailing dots from the values in Typoscript
316 *
317 * @param array $array The array with the trailing dots
318 * @return void
319 */
320 protected function removeTrailingDotsFromTyposcript(array &$array)
321 {
322 foreach ($array as $key => $value) {
323 if (is_array($value)) {
324 $this->removeTrailingDotsFromTyposcript($value);
325 }
326 if (substr($key, -1) === '.') {
327 $newKey = substr($key, 0, -1);
328 unset($array[$key]);
329 $array[$newKey] = $value;
330 }
331 }
332 }
333
334 /**
335 * Create the panel of buttons for submitting the form or otherwise perform
336 * operations.
337 *
338 * @return array all available buttons as an assoc. array
339 */
340 protected function getButtons()
341 {
342 /** @var IconFactory $iconFactory */
343 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
344
345 $buttons = array(
346 'csh' => '',
347 'csh_buttons' => '',
348 'close' => '',
349 'save' => '',
350 'save_close' => '',
351 'reload' => ''
352 );
353 // CSH
354 $buttons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'wizard_forms_wiz');
355 // CSH Buttons
356 $buttons['csh_buttons'] = BackendUtility::cshItem('xMOD_csh_corebe', 'wizard_forms_wiz_buttons');
357 // Close
358 $getPostVariables = GeneralUtility::_GP('P');
359 $title = 'title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc', true) . '"';
360 $onClick = htmlspecialchars('jumpToUrl(unescape(\'' .
361 rawurlencode(GeneralUtility::sanitizeLocalUrl($getPostVariables['returnUrl'])) . '\')); return false;');
362 $buttons['close'] = '<a href="#" onclick="' . $onClick . '" ' . $title . '>' .
363 $iconFactory->getIcon('actions-document-close', Icon::SIZE_SMALL)->render() . '</a>';
364 return $buttons;
365 }
366
367 /**
368 * Generate the body content
369 *
370 * If there is an error, no reference to a record, a Flash Message will be
371 * displayed
372 *
373 * @return string The body content
374 */
375 protected function getBodyContent()
376 {
377 if ($this->recordIsAvailable) {
378 $bodyContent = '';
379 } else {
380 /** @var $flashMessage FlashMessage */
381 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class,
382 $this->getLanguageService()->getLL('errorMessage', true),
383 $this->getLanguageService()->getLL('errorTitle', true),
384 FlashMessage::ERROR);
385 $bodyContent = $flashMessage->render();
386 }
387 return $bodyContent;
388 }
389
390 /**
391 * @return PageRenderer
392 */
393 protected function getPageRenderer()
394 {
395 if (!isset($this->pageRenderer)) {
396 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
397 }
398
399 return $this->pageRenderer;
400 }
401
402 /**
403 * Returns an instance of LanguageService
404 *
405 * @return \TYPO3\CMS\Lang\LanguageService
406 */
407 protected function getLanguageService()
408 {
409 return $GLOBALS['LANG'];
410 }
411 }