6e47b8081c0a5927922e6f0e93dc0525368c3bb1
[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 // 2 means preview of a non-live workspace
44 if ($pObj->fePreview !== 2) {
45 return '';
46 }
47
48 if (empty($this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain'))) {
49 $backendDomain = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY');
50 } else {
51 $backendDomain = $this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain');
52 }
53
54 $content = $pObj->cObj->cObjGetSingle('FLUIDTEMPLATE', [
55 'file' => 'EXT:workspaces/Resources/Private/Templates/Preview/Preview.html',
56 'variables.' => [
57 'backendDomain' => 'TEXT',
58 'backendDomain.' => ['value' => $backendDomain]
59 ]
60 ]);
61
62 if (!isset($pObj->config['config']['disablePreviewNotification']) || (int)$pObj->config['config']['disablePreviewNotification'] !== 1) {
63 // get the title of the current workspace
64 $currentWorkspaceId = $pObj->whichWorkspace();
65 $currentWorkspaceTitle = $this->getWorkspaceTitle($currentWorkspaceId);
66 $currentWorkspaceTitle = htmlspecialchars($currentWorkspaceTitle);
67 if ($pObj->config['config']['message_preview_workspace']) {
68 $content .= sprintf(
69 $pObj->config['config']['message_preview_workspace'],
70 $currentWorkspaceTitle,
71 $currentWorkspaceId ?? -1
72 );
73 } else {
74 $text = LocalizationUtility::translate(
75 'LLL:EXT:workspaces/Resources/Private/Language/locallang_mod.xlf:previewText',
76 'workspaces',
77 [$currentWorkspaceTitle, $currentWorkspaceId ?? -1]
78 );
79 if ($pObj->doWorkspacePreview()) {
80 $urlForStoppingPreview = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'index.php?ADMCMD_prev=LOGOUT&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
81 $text .= '<br><a style="color: #000;" href="' . $urlForStoppingPreview . '">Stop preview</a>';
82 }
83 $content .= '<div id="typo3-previewInfo" style="position: absolute; top: 20px; right: 20px; border: 2px solid #000; padding: 5px; background: #f00; font: 1em Verdana; color: #000; font-weight: bold; z-index: 10001">' . $text . '</div>';
84 }
85 }
86 return $content;
87 }
88
89 /**
90 * Fetches the title of the workspace
91 *
92 * @param $workspaceId
93 * @return string the title of the workspace
94 */
95 protected function getWorkspaceTitle(int $workspaceId): string
96 {
97 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
98 ->getQueryBuilderForTable('sys_workspace');
99 $title = $queryBuilder
100 ->select('title')
101 ->from('sys_workspace')
102 ->where(
103 $queryBuilder->expr()->eq(
104 'uid',
105 $queryBuilder->createNamedParameter($workspaceId, \PDO::PARAM_INT)
106 )
107 )
108 ->execute()
109 ->fetchColumn();
110 return $title !== false ? $title : '';
111 }
112
113 /**
114 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
115 */
116 protected function getBackendUserAuthentication()
117 {
118 return $GLOBALS['BE_USER'];
119 }
120 }