[BUGFIX] Add UI blocker to prevent loading issues in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / ModuleTemplate.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template;
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\Template\Components\DocHeaderComponent;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Messaging\AbstractMessage;
23 use TYPO3\CMS\Core\Messaging\FlashMessageService;
24 use TYPO3\CMS\Core\Page\PageRenderer;
25 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Fluid\View\Exception\InvalidTemplateResourceException;
28 use TYPO3\CMS\Fluid\View\StandaloneView;
29 use TYPO3\CMS\Lang\LanguageService;
30
31 /**
32 * A class taking care of the "outer" HTML of a module, especially
33 * the doc header and other related parts.
34 *
35 * @internal This API is not yet carved in stone and may be adapted later.
36 */
37 class ModuleTemplate
38 {
39 /**
40 * Error Icon Constant
41 *
42 * @internal
43 */
44 const STATUS_ICON_ERROR = 3;
45
46 /**
47 * Warning Icon Constant
48 *
49 * @internal
50 */
51 const STATUS_ICON_WARNING = 2;
52
53 /**
54 * Notification Icon Constant
55 *
56 * @internal
57 */
58 const STATUS_ICON_NOTIFICATION = 1;
59
60 /**
61 * OK Icon Constant
62 *
63 * @internal
64 */
65 const STATUS_ICON_OK = -1;
66
67 /**
68 * DocHeaderComponent
69 *
70 * @var DocHeaderComponent
71 */
72 protected $docHeaderComponent;
73
74 /**
75 * Javascript Code Array
76 * Used for inline JS
77 *
78 * @var array
79 */
80 protected $javascriptCodeArray = [];
81
82 /**
83 * Expose the pageRenderer
84 *
85 * @var PageRenderer
86 */
87 protected $pageRenderer;
88
89 /**
90 * @var bool
91 */
92 protected $uiBlock = false;
93
94 /**
95 * TemplateRootPath
96 *
97 * @var string[]
98 */
99 protected $templateRootPaths = ['EXT:backend/Resources/Private/Templates'];
100
101 /**
102 * PartialRootPath
103 *
104 * @var string[]
105 */
106 protected $partialRootPaths = ['EXT:backend/Resources/Private/Partials'];
107
108 /**
109 * LayoutRootPath
110 *
111 * @var string[]
112 */
113 protected $layoutRootPaths = ['EXT:backend/Resources/Private/Layouts'];
114
115 /**
116 * Template name
117 *
118 * @var string
119 */
120 protected $templateFile = 'Module.html';
121
122 /**
123 * Fluid Standalone View
124 *
125 * @var StandaloneView
126 */
127 protected $view;
128
129 /**
130 * Content String
131 *
132 * @var string
133 */
134 protected $content = '';
135
136 /**
137 * IconFactory Member
138 *
139 * @var IconFactory
140 */
141 protected $iconFactory;
142
143 /**
144 * Module ID
145 *
146 * @var string
147 */
148 protected $moduleId = '';
149
150 /**
151 * Module Name
152 *
153 * @var string
154 */
155 protected $moduleName = '';
156
157 /**
158 * Title Tag
159 *
160 * @var string
161 */
162 protected $title = '';
163
164 /**
165 * Body Tag
166 *
167 * @var string
168 */
169 protected $bodyTag = '<body>';
170
171 /**
172 * Flash message queue
173 *
174 * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue
175 */
176 protected $flashMessageQueue;
177
178 /**
179 * Returns the current body tag
180 *
181 * @return string
182 */
183 public function getBodyTag()
184 {
185 return $this->bodyTag;
186 }
187
188 /**
189 * Sets the body tag
190 *
191 * @param string $bodyTag
192 */
193 public function setBodyTag($bodyTag)
194 {
195 $this->bodyTag = $bodyTag;
196 }
197
198 /**
199 * Gets the standalone view.
200 *
201 * @return StandaloneView
202 */
203 public function getView()
204 {
205 return $this->view;
206 }
207
208 /**
209 * Set content
210 *
211 * @param string $content Content of the module
212 */
213 public function setContent($content)
214 {
215 $this->view->assign('content', $content);
216 }
217
218 /**
219 * Set title tag
220 *
221 * @param string $title
222 */
223 public function setTitle($title)
224 {
225 $this->title = $title;
226 }
227
228 /**
229 * Returns the IconFactory
230 *
231 * @return IconFactory
232 */
233 public function getIconFactory()
234 {
235 return $this->iconFactory;
236 }
237
238 /**
239 * Class constructor
240 * Sets up view and property objects
241 *
242 * @throws InvalidTemplateResourceException In case a template is invalid
243 */
244 public function __construct()
245 {
246 $this->view = GeneralUtility::makeInstance(StandaloneView::class);
247 $this->view->setPartialRootPaths($this->partialRootPaths);
248 $this->view->setTemplateRootPaths($this->templateRootPaths);
249 $this->view->setLayoutRootPaths($this->layoutRootPaths);
250 $this->view->setTemplate($this->templateFile);
251 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
252 $this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
253 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
254 }
255
256 /**
257 * Loads all necessary Javascript Files
258 */
259 protected function loadJavaScripts()
260 {
261 $this->pageRenderer->loadJquery();
262 $this->pageRenderer->loadRequireJsModule('bootstrap');
263 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextHelp');
264 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/DocumentHeader');
265 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/SplitButtons');
266 }
267
268 /**
269 * Loads all necessary stylesheets
270 */
271 protected function loadStylesheets()
272 {
273 if ($GLOBALS['TBE_STYLES']['stylesheet']) {
274 $this->pageRenderer->addCssFile($GLOBALS['TBE_STYLES']['stylesheet']);
275 }
276 if ($GLOBALS['TBE_STYLES']['stylesheet2']) {
277 $this->pageRenderer->addCssFile($GLOBALS['TBE_STYLES']['stylesheet2']);
278 }
279 }
280
281 /**
282 * Sets mandatory parameters for the view (pageRenderer)
283 */
284 protected function setupPage()
285 {
286 // Yes, hardcoded on purpose
287 $this->pageRenderer->setXmlPrologAndDocType('<!DOCTYPE html>');
288 $this->pageRenderer->setCharSet('utf-8');
289 $this->pageRenderer->setLanguage($GLOBALS['LANG']->lang);
290 $this->pageRenderer->addMetaTag('<meta name="viewport" content="width=device-width, initial-scale=1">');
291 }
292
293 /**
294 * Wrapper function for adding JS inline blocks
295 */
296 protected function setJavaScriptCodeArray()
297 {
298 foreach ($this->javascriptCodeArray as $name => $code) {
299 $this->pageRenderer->addJsInlineCode($name, $code, false);
300 }
301 }
302
303 /**
304 * Adds JS inline blocks of code to the internal registry
305 *
306 * @param string $name Javascript code block name
307 * @param string $code Inline Javascript
308 */
309 public function addJavaScriptCode($name = '', $code = '')
310 {
311 $this->javascriptCodeArray[$name] = $code;
312 }
313
314 /**
315 * Get the DocHeader
316 *
317 * @return DocHeaderComponent
318 */
319 public function getDocHeaderComponent()
320 {
321 return $this->docHeaderComponent;
322 }
323
324 /**
325 * Returns the fully rendered view
326 *
327 * @return string
328 */
329 public function renderContent()
330 {
331 $this->setupPage();
332 $this->pageRenderer->setTitle($this->title);
333 $this->loadJavaScripts();
334 $this->setJavaScriptCodeArray();
335 $this->loadStylesheets();
336
337 $this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());
338 if ($this->moduleId) {
339 $this->view->assign('moduleId', $this->moduleId);
340 }
341 if ($this->moduleName) {
342 $this->view->assign('moduleName', $this->moduleName);
343 }
344 $this->view->assign('uiBlock', $this->uiBlock);
345 $this->view->assign('flashMessageQueueIdentifier', $this->getFlashMessageQueue()->getIdentifier());
346 $renderedPage = $this->pageRenderer->render(PageRenderer::PART_HEADER);
347 $renderedPage .= $this->bodyTag;
348 $renderedPage .= $this->view->render();
349 $this->pageRenderer->addJsFooterInlineCode('updateSignals', BackendUtility::getUpdateSignalCode());
350 $renderedPage .= $this->pageRenderer->render(PageRenderer::PART_FOOTER);
351
352 return $renderedPage;
353 }
354
355 /**
356 * Get PageRenderer
357 *
358 * @return PageRenderer
359 */
360 public function getPageRenderer()
361 {
362 return $this->pageRenderer;
363 }
364
365 /**
366 * Set form tag
367 *
368 * @param string $formTag Form tag to add
369 */
370 public function setForm($formTag = '')
371 {
372 $this->view->assign('formTag', $formTag);
373 }
374
375 /**
376 * Sets the ModuleId
377 *
378 * @param string $moduleId ID of the module
379 */
380 public function setModuleId($moduleId)
381 {
382 $this->moduleId = $moduleId;
383 $this->registerModuleMenu($moduleId);
384 }
385
386 /**
387 * Sets the ModuleName
388 *
389 * @param string $moduleName Name of the module
390 */
391 public function setModuleName($moduleName)
392 {
393 $this->moduleName = $moduleName;
394 }
395
396 /**
397 * Generates the Menu for things like Web->Info
398 *
399 * @param $moduleMenuIdentifier
400 */
401 public function registerModuleMenu($moduleMenuIdentifier)
402 {
403 if (isset($GLOBALS['TBE_MODULES_EXT'][$moduleMenuIdentifier])) {
404 $menuEntries =
405 $GLOBALS['TBE_MODULES_EXT'][$moduleMenuIdentifier]['MOD_MENU']['function'];
406 $menu = $this->getDocHeaderComponent()->getMenuRegistry()->makeMenu()->setIdentifier('MOD_FUNC');
407 foreach ($menuEntries as $menuEntry) {
408 $menuItem = $menu->makeMenuItem()
409 ->setTitle($menuEntry['title'])
410 ->setHref('#');
411 $menu->addMenuItem($menuItem);
412 }
413 $this->docHeaderComponent->getMenuRegistry()->addMenu($menu);
414 }
415 }
416
417 /**
418 * Creates a tab menu where the tabs or collapsible are rendered with bootstrap markup
419 *
420 * @param array $menuItems Tab elements, each element is an array with "label" and "content"
421 * @param string $domId DOM id attribute, will be appended with an iteration number per tab.
422 * @param int $defaultTabIndex Default tab to open (for toggle <=0). Value corresponds to integer-array index + 1
423 * (index zero is "1", index "1" is 2 etc.). A value of zero (or something non-existing
424 * will result in no default tab open.
425 * @param bool $collapsible If set, the tabs are rendered as headers instead over each sheet. Effectively this means
426 * there is no tab menu, but rather a foldout/fold-in menu.
427 * @param bool $wrapContent If set, the content is wrapped in div structure which provides a padding and border
428 * style. Set this FALSE to get unstyled content pane with fullsize content area.
429 * @param bool $storeLastActiveTab If set, the last open tab is stored in local storage and will be re-open again.
430 * If you don't need this feature, e.g. for wizards like import/export you can
431 * disable this behaviour.
432 * @return string
433 */
434 public function getDynamicTabMenu(array $menuItems, $domId, $defaultTabIndex = 1, $collapsible = false, $wrapContent = true, $storeLastActiveTab = true)
435 {
436 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Tabs');
437 $templatePath = ExtensionManagementUtility::extPath('backend')
438 . 'Resources/Private/Templates/DocumentTemplate/';
439 $view = GeneralUtility::makeInstance(StandaloneView::class);
440 $view->setTemplatePathAndFilename($templatePath . ($collapsible ? 'Collapse.html' : 'Tabs.html'));
441 $view->setPartialRootPaths([$templatePath . 'Partials']);
442 $view->assignMultiple([
443 'id' => 'DTM-' . GeneralUtility::shortMD5($domId),
444 'items' => $menuItems,
445 'defaultTabIndex' => $defaultTabIndex,
446 'wrapContent' => $wrapContent,
447 'storeLastActiveTab' => $storeLastActiveTab,
448 ]);
449 return $view->render();
450 }
451
452 /*******************************************
453 * THE FOLLOWING METHODS ARE SUBJECT TO BE DEPRECATED / DROPPED!
454 *
455 * These methods have been copied over from DocumentTemplate and enables
456 * core modules to drop the dependency to DocumentTemplate altogether without
457 * rewriting these modules now.
458 * The methods below are marked as internal and will be removed
459 * one-by-one with further refactoring of modules.
460 *
461 * Do not use these methods within own extensions if possible or
462 * be prepared to change this later again.
463 *******************************************/
464
465 /**
466 * Includes a javascript library that exists in the core /typo3/ directory
467 *
468 * @param string $lib Library name. Call it with the full path like
469 * "sysext/core/Resources/Public/JavaScript/QueryGenerator.js" to load it
470 *
471 * @internal
472 */
473 public function loadJavascriptLib($lib)
474 {
475 // @todo: maybe we can remove this one as well
476 $this->pageRenderer->addJsFile($lib);
477 }
478
479 /**
480 * Returns a linked shortcut-icon which will call the shortcut frame and set a
481 * shortcut there back to the calling page/module
482 *
483 * @param string $gvList Is the list of GET variables to store (if any)
484 * @param string $setList Is the list of SET[] variables to store
485 * (if any) - SET[] variables a stored in $GLOBALS["SOBE"]->MOD_SETTINGS
486 * for backend modules
487 * @param string $modName Module name string
488 * @param string|int $motherModName Is used to enter the "parent module
489 * name" if the module is a submodule under eg. Web>* or File>*. You
490 * can also set this value to 1 in which case the currentLoadedModule
491 * is sent to the shortcut script (so - not a fixed value!) - that is used
492 * in file_edit and wizard_rte modules where those are really running as
493 * a part of another module.
494 * @param string $displayName When given this name is used instead of the
495 * module name.
496 * @param string $classes Additional CSS classes for the link around the icon
497 *
498 * @return string HTML content
499 * @todo Make this thing return a button object
500 * @internal
501 */
502 public function makeShortcutIcon($gvList, $setList, $modName, $motherModName = '', $displayName = '', $classes = 'btn btn-default btn-sm')
503 {
504 $gvList = 'route,' . $gvList;
505 $storeUrl = $this->makeShortcutUrl($gvList, $setList);
506 $pathInfo = parse_url(GeneralUtility::getIndpEnv('REQUEST_URI'));
507 // Fallback for alt_mod. We still pass in the old xMOD... stuff,
508 // but TBE_MODULES only knows about "record_edit".
509 // We still need to pass the xMOD name to createShortcut below,
510 // since this is used for icons.
511 $moduleName = $modName === 'xMOD_alt_doc.php' ? 'record_edit' : $modName;
512 // Add the module identifier automatically if typo3/index.php is used:
513 if (GeneralUtility::_GET('M') !== null) {
514 $storeUrl = '&M=' . $moduleName . $storeUrl;
515 }
516 if ((int)$motherModName === 1) {
517 $motherModule = 'top.currentModuleLoaded';
518 } elseif ($motherModName) {
519 $motherModule = GeneralUtility::quoteJSvalue($motherModName);
520 } else {
521 $motherModule = '\'\'';
522 }
523 $confirmationText = GeneralUtility::quoteJSvalue(
524 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark')
525 );
526
527 $shortcutUrl = $pathInfo['path'] . '?' . $storeUrl;
528 $shortcutExist = BackendUtility::shortcutExists($shortcutUrl);
529
530 if ($shortcutExist) {
531 return '<a class="active ' . htmlspecialchars($classes) . '" title="">' .
532 $this->iconFactory->getIcon('actions-system-shortcut-active', Icon::SIZE_SMALL)->render() . '</a>';
533 }
534
535 $url = GeneralUtility::quoteJSvalue(rawurlencode($shortcutUrl));
536 $onClick = 'top.TYPO3.ShortcutMenu.createShortcut(' . GeneralUtility::quoteJSvalue(rawurlencode($modName)) .
537 ', ' . $url . ', ' . $confirmationText . ', ' . $motherModule . ', this, ' . GeneralUtility::quoteJSvalue($displayName) . ');return false;';
538
539 return '<a href="#" class="' . htmlspecialchars($classes) . '" onclick="' . htmlspecialchars($onClick) . '" title="' .
540 htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark')) . '">' .
541 $this->iconFactory->getIcon('actions-system-shortcut-new', Icon::SIZE_SMALL)->render() . '</a>';
542 }
543
544 /**
545 * MAKE url for storing
546 * Internal func
547 *
548 * @param string $gvList Is the list of GET variables to store (if any)
549 * @param string $setList Is the list of SET[] variables to store (if any)
550 * - SET[] variables a stored in $GLOBALS["SOBE"]->MOD_SETTINGS for backend
551 * modules
552 *
553 * @return string
554 * @internal
555 */
556 public function makeShortcutUrl($gvList, $setList)
557 {
558 $getParams = GeneralUtility::_GET();
559 $storeArray = array_merge(
560 GeneralUtility::compileSelectedGetVarsFromArray($gvList, $getParams),
561 ['SET' => GeneralUtility::compileSelectedGetVarsFromArray($setList, (array)$GLOBALS['SOBE']->MOD_SETTINGS)]
562 );
563 return GeneralUtility::implodeArrayForUrl('', $storeArray);
564 }
565
566 /**
567 * Returns the BE USER Object
568 *
569 * @return BackendUserAuthentication
570 */
571 protected function getBackendUserAuthentication()
572 {
573 return $GLOBALS['BE_USER'];
574 }
575
576 /**
577 * Returns the LanguageService
578 *
579 * @return LanguageService
580 */
581 protected function getLanguageService()
582 {
583 return $GLOBALS['LANG'];
584 }
585
586 /**
587 * Returns an image-tag with an 18x16 icon of the following types:
588 *
589 * $type:
590 * -1:» OK icon (Check-mark)
591 * 1:» Notice (Speach-bubble)
592 * 2:» Warning (Yellow triangle)
593 * 3:» Fatal error (Red stop sign)
594 *
595 * @param int $type See description
596 *
597 * @return string HTML image tag (if applicable)
598 * @internal
599 */
600 public function icons($type)
601 {
602 $icon = '';
603 switch ($type) {
604 case self::STATUS_ICON_ERROR:
605 $icon = 'status-dialog-error';
606 break;
607 case self::STATUS_ICON_WARNING:
608 $icon = 'status-dialog-warning';
609 break;
610 case self::STATUS_ICON_NOTIFICATION:
611 $icon = 'status-dialog-notification';
612 break;
613 case self::STATUS_ICON_OK:
614 $icon = 'status-dialog-ok';
615 break;
616 default:
617 // Do nothing
618 }
619 if ($icon != '') {
620 return $this->iconFactory->getIcon($icon, Icon::SIZE_SMALL)->render();
621 }
622 return '';
623 }
624
625 /**
626 * Returns JavaScript variables setting the returnUrl and thisScript location for use by JavaScript on the page.
627 * Used in fx. db_list.php (Web>List)
628 *
629 * @param string $thisLocation URL to "this location" / current script
630 * @return string Urls are returned as JavaScript variables T3_RETURN_URL and T3_THIS_LOCATION
631 * @see typo3/db_list.php
632 * @internal
633 */
634 public function redirectUrls($thisLocation = '')
635 {
636 $thisLocation = $thisLocation ? $thisLocation : GeneralUtility::linkThisScript([
637 'CB' => '',
638 'SET' => '',
639 'cmd' => '',
640 'popViewId' => ''
641 ]);
642 $out = '
643 var T3_RETURN_URL = ' . GeneralUtility::quoteJSvalue(str_replace('%20', '', rawurlencode(GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'))))) . ';
644 var T3_THIS_LOCATION = ' . GeneralUtility::quoteJSvalue(str_replace('%20', '', rawurlencode($thisLocation))) . '
645 ';
646 return $out;
647 }
648
649 /**
650 * Returns the header-bar in the top of most backend modules
651 * Closes section if open.
652 *
653 * @param string $text The text string for the header
654 * @return string HTML content
655 * @internal
656 */
657 public function header($text)
658 {
659 return '
660
661 <!-- MAIN Header in page top -->
662 <h1 class="t3js-title-inlineedit">' . htmlspecialchars($text) . '</h1>
663 ';
664 }
665
666 /**
667 * Creates a Message object and adds it to the FlashMessageQueue.
668 *
669 * @param string $messageBody The message
670 * @param string $messageTitle Optional message title
671 * @param int $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\FlashMessage constants
672 * @param bool $storeInSession Optional, defines whether the message should be stored in the session (default)
673 * @throws \InvalidArgumentException if the message body is no string
674 */
675 public function addFlashMessage($messageBody, $messageTitle = '', $severity = AbstractMessage::OK, $storeInSession = true)
676 {
677 if (!is_string($messageBody)) {
678 throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1446483133);
679 }
680 /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
681 $flashMessage = GeneralUtility::makeInstance(
682 \TYPO3\CMS\Core\Messaging\FlashMessage::class,
683 $messageBody,
684 $messageTitle,
685 $severity,
686 $storeInSession
687 );
688 $this->getFlashMessageQueue()->enqueue($flashMessage);
689 }
690
691 /**
692 * @param \TYPO3\CMS\Core\Messaging\FlashMessageQueue $flashMessageQueue
693 */
694 public function setFlashMessageQueue($flashMessageQueue)
695 {
696 $this->flashMessageQueue = $flashMessageQueue;
697 }
698
699 /**
700 * @return \TYPO3\CMS\Core\Messaging\FlashMessageQueue
701 */
702 protected function getFlashMessageQueue()
703 {
704 if (!isset($this->flashMessageQueue)) {
705 /** @var FlashMessageService $service */
706 $service = GeneralUtility::makeInstance(FlashMessageService::class);
707 $this->flashMessageQueue = $service->getMessageQueueByIdentifier();
708 }
709 return $this->flashMessageQueue;
710 }
711
712 /**
713 * @return bool
714 */
715 public function isUiBlock() : bool
716 {
717 return $this->uiBlock;
718 }
719
720 /**
721 * @param bool $uiBlock
722 */
723 public function setUiBlock(bool $uiBlock)
724 {
725 $this->uiBlock = $uiBlock;
726 }
727 }