From 2e526cc1a0a59c7cae396c816cb7183b71f93a7a Mon Sep 17 00:00:00 2001
From: Robert Vock <robert.vock@4wdmedia.de>
Date: Fri, 2 Sep 2016 09:45:04 +0200
Subject: [PATCH] [BUGFIX] Added missing workspace preview info

The workspace preview info got lost from TYPO3 4.5 to 4.6. This commit
readds the info and respects the same TypoScript config options.

Resolves: #77467
Releases: master, 8.7
Change-Id: Ie1696a5f86714c6ef9a61c10c41b0397abb95ab5
Reviewed-on: https://review.typo3.org/49740
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Hook/TypoScriptFrontendControllerHook.php | 64 +++++++++++++++++--
 .../Private/Language/locallang_mod.xlf        |  3 +
 typo3/sysext/workspaces/ext_localconf.php     |  2 +-
 3 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/typo3/sysext/workspaces/Classes/Hook/TypoScriptFrontendControllerHook.php b/typo3/sysext/workspaces/Classes/Hook/TypoScriptFrontendControllerHook.php
index 0d103983b58f..46daee2e5d0f 100644
--- a/typo3/sysext/workspaces/Classes/Hook/TypoScriptFrontendControllerHook.php
+++ b/typo3/sysext/workspaces/Classes/Hook/TypoScriptFrontendControllerHook.php
@@ -14,20 +14,35 @@ namespace TYPO3\CMS\Workspaces\Hook;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+
 /**
  * Frontend hooks
  */
 class TypoScriptFrontendControllerHook
 {
     /**
+     * Renders a message at the bottom of the HTML page, can be modified via
+     *
+     *   config.disablePreviewNotification = 1 (to disable the additional info text)
+     *
+     * and
+     *
+     *   config.message_preview_workspace = This is not the online version but the version of "%s" workspace (ID: %s).
+     *
+     * via TypoScript.
+     *
      * @param array $params
      * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $pObj
+     * @return string
      */
-    public function hook_eofe($params, $pObj)
+    public function renderPreviewInfo(array $params, \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $pObj)
     {
         // 2 means preview of a non-live workspace
         if ($pObj->fePreview !== 2) {
-            return;
+            return '';
         }
 
         if (empty($this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain'))) {
@@ -36,22 +51,57 @@ class TypoScriptFrontendControllerHook
             $backendDomain = $this->getBackendUserAuthentication()->getSessionData('workspaces.backend_domain');
         }
 
-        $previewParts = $this->getTypoScriptFrontendController()->cObj->cObjGetSingle('FLUIDTEMPLATE', [
+        $content = $pObj->cObj->cObjGetSingle('FLUIDTEMPLATE', [
             'file' => 'EXT:workspaces/Resources/Private/Templates/Preview/Preview.html',
             'variables.' => [
                 'backendDomain' => 'TEXT',
                 'backendDomain.' => ['value' => $backendDomain]
             ]
         ]);
-        $this->getTypoScriptFrontendController()->content = str_ireplace('</body>', $previewParts . '</body>', $this->getTypoScriptFrontendController()->content);
+
+        if (!isset($pObj->config['config']['disablePreviewNotification']) || (int)$pObj->config['config']['disablePreviewNotification'] !== 1) {
+            // get the title of the current workspace
+            $currentWorkspaceId = $pObj->whichWorkspace();
+            $currentWorkspaceTitle = $this->getWorkspaceTitle($currentWorkspaceId);
+            $currentWorkspaceTitle = htmlspecialchars($currentWorkspaceTitle);
+            if ($pObj->config['config']['message_preview_workspace']) {
+                $content .= sprintf($pObj->config['config']['message_preview_workspace'], $currentWorkspaceTitle,
+                    $currentWorkspaceId ?? -1);
+            } else {
+                $text = LocalizationUtility::translate('LLL:EXT:workspaces/Resources/Private/Language/locallang_mod.xlf:previewText',
+                    'workspaces', [$currentWorkspaceTitle, $currentWorkspaceId ?? -1]);
+                if ($pObj->doWorkspacePreview()) {
+                    $urlForStoppingPreview = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'index.php?ADMCMD_prev=LOGOUT&returnUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI'));
+                    $text .= '<br><a style="color: #000;" href="' . $urlForStoppingPreview . '">Stop preview</a>';
+                }
+                $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>';
+            }
+        }
+        return $content;
     }
 
     /**
-     * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
+     * Fetches the title of the workspace
+     *
+     * @param $workspaceId
+     * @return string the title of the workspace
      */
-    protected function getTypoScriptFrontendController()
+    protected function getWorkspaceTitle(int $workspaceId): string
     {
-        return $GLOBALS['TSFE'];
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_workspace');
+        $title = $queryBuilder
+            ->select('title')
+            ->from('sys_workspace')
+            ->where(
+                $queryBuilder->expr()->eq(
+                    'uid',
+                    $queryBuilder->createNamedParameter($workspaceId, \PDO::PARAM_INT)
+                )
+            )
+            ->execute()
+            ->fetchColumn();
+        return $title !== false ? $title : '';
     }
 
     /**
diff --git a/typo3/sysext/workspaces/Resources/Private/Language/locallang_mod.xlf b/typo3/sysext/workspaces/Resources/Private/Language/locallang_mod.xlf
index c6b215299706..da322ec6c493 100644
--- a/typo3/sysext/workspaces/Resources/Private/Language/locallang_mod.xlf
+++ b/typo3/sysext/workspaces/Resources/Private/Language/locallang_mod.xlf
@@ -27,6 +27,9 @@
 			<trans-unit id="stage_ready_to_publish">
 				<source>Ready to publish</source>
 			</trans-unit>
+			<trans-unit id="previewText">
+				<source>Preview of workspace %s (%s)</source>
+			</trans-unit>
 		</body>
 	</file>
 </xliff>
diff --git a/typo3/sysext/workspaces/ext_localconf.php b/typo3/sysext/workspaces/ext_localconf.php
index ad67131908b4..be5576b8fabb 100644
--- a/typo3/sysext/workspaces/ext_localconf.php
+++ b/typo3/sysext/workspaces/ext_localconf.php
@@ -17,7 +17,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Works
 
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass']['workspaces'] = \TYPO3\CMS\Workspaces\Hook\DataHandlerHook::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['viewOnClickClass']['workspaces'] = \TYPO3\CMS\Workspaces\Hook\BackendUtilityHook::class;
-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe']['workspaces'] = \TYPO3\CMS\Workspaces\Hook\TypoScriptFrontendControllerHook::class . '->hook_eofe';
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_previewInfo']['workspaces'] = \TYPO3\CMS\Workspaces\Hook\TypoScriptFrontendControllerHook::class . '->renderPreviewInfo';
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/alt_doc.php']['makeEditForm_accessCheck']['workspaces'] = \TYPO3\CMS\Workspaces\Hook\BackendUtilityHook::class . '->makeEditForm_accessCheck';
 
 // Register hook to check for the preview mode in the FE
-- 
GitLab