[TASK] Get rid of special fePreview=2 for workspaces
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Hook / TypoScriptFrontendControllerHook.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Hook;
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\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20
21 /**
22 * Frontend hooks
23 */
24 class TypoScriptFrontendControllerHook
25 {
26 /**
27 * Renders a message at the bottom of the HTML page, can be modified via
28 *
29 * config.disablePreviewNotification = 1 (to disable the additional info text)
30 *
31 * and
32 *
33 * config.message_preview_workspace = This is not the online version but the version of "%s" workspace (ID: %s).
34 *
35 * via TypoScript.
36 *
37 * @param array $params
38 * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $pObj
39 * @return string
40 */
41 public function renderPreviewInfo(array $params, \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $pObj)
42 {
43 if (!$pObj->doWorkspacePreview()) {
44 return '';
45 }
46
47 if (empty($this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain'))) {
48 $backendDomain = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
49 } else {
50 $backendDomain = $this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain');
51 }
52
53 $content = $pObj->cObj->cObjGetSingle('FLUIDTEMPLATE', [
54 'file' => 'EXT:workspaces/Resources/Private/Templates/Preview/Preview.html',
55 'variables.' => [
56 'backendDomain' => 'TEXT',
57 'backendDomain.' => ['value' => $backendDomain]
58 ]
59 ]);
60
61 if (!isset($pObj->config['config']['disablePreviewNotification']) || (int)$pObj->config['config']['disablePreviewNotification'] !== 1) {
62 // get the title of the current workspace
63 $currentWorkspaceId = $pObj->whichWorkspace();
64 $currentWorkspaceTitle = $this->getWorkspaceTitle($currentWorkspaceId);
65 $currentWorkspaceTitle = htmlspecialchars($currentWorkspaceTitle);
66 if ($pObj->config['config']['message_preview_workspace']) {
67 $content .= sprintf(
68 $pObj->config['config']['message_preview_workspace'],
69 $currentWorkspaceTitle,
70 $currentWorkspaceId ?? -99
71 );
72 } else {
73 $text = LocalizationUtility::translate(
74 'LLL:EXT:workspaces/Resources/Private/Language/locallang_mod.xlf:previewText',
75 'workspaces',
76 [$currentWorkspaceTitle, $currentWorkspaceId ?? -99]
77 );
78 if ($pObj->doWorkspacePreview()) {
79 $urlForStoppingPreview = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'index.php?ADMCMD_prev=LOGOUT&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
80 $text .= '<br><a style="color: #000;" href="' . $urlForStoppingPreview . '">Stop preview</a>';
81 }
82 $styles = [];
83 $styles[] = 'position: fixed';
84 $styles[] = 'top: 15px';
85 $styles[] = 'right: 15px';
86 $styles[] = 'padding: 8px 18px';
87 $styles[] = 'background: #fff3cd';
88 $styles[] = 'border: 1px solid #ffeeba';
89 $styles[] = 'font-family: sans-serif';
90 $styles[] = 'font-size: 14px';
91 $styles[] = 'font-weight: bold';
92 $styles[] = 'color: #856404';
93 $styles[] = 'z-index: 20000';
94 $styles[] = 'user-select: none';
95 $styles[] = 'pointer-events:none';
96 $styles[] = 'text-align: center';
97 $styles[] = 'border-radius: 2px';
98 $content .= '<div id="typo3-preview-info" style="' . implode(';', $styles) . '">' . $text . '</div>';
99 }
100 }
101 return $content;
102 }
103
104 /**
105 * Fetches the title of the workspace
106 *
107 * @param $workspaceId
108 * @return string the title of the workspace
109 */
110 protected function getWorkspaceTitle(int $workspaceId): string
111 {
112 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
113 ->getQueryBuilderForTable('sys_workspace');
114 $title = $queryBuilder
115 ->select('title')
116 ->from('sys_workspace')
117 ->where(
118 $queryBuilder->expr()->eq(
119 'uid',
120 $queryBuilder->createNamedParameter($workspaceId, \PDO::PARAM_INT)
121 )
122 )
123 ->execute()
124 ->fetchColumn();
125 return $title !== false ? $title : '';
126 }
127
128 /**
129 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
130 */
131 protected function getBackendUserAuthentication()
132 {
133 return $GLOBALS['BE_USER'];
134 }
135 }