[TASK] Extract html into fluid template from preview module in admin panel 67/56167/9
authorTobi Kretschmann <tobi@tobishome.de>
Thu, 15 Mar 2018 13:56:32 +0000 (14:56 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 15 Mar 2018 19:37:17 +0000 (20:37 +0100)
The preview module should use a fluid template instead of
the old php-html mixed data structure.

Resolves: #84211
Releases: master
Change-Id: I6b0a449216ff5b591eb123fdd2f9641fafeba3d8
Reviewed-on: https://review.typo3.org/56167
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Tested-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/adminpanel/Classes/Modules/PreviewModule.php
typo3/sysext/adminpanel/Classes/Repositories/FrontendGroupsRepository.php [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Language/locallang_preview.xlf [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FluidDebug.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FrontendUserGroup.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenPages.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenRecords.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/ShowHiddenElements.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/SimulateDate.html [new file with mode: 0644]
typo3/sysext/adminpanel/Resources/Private/Templates/Modules/Preview.html [new file with mode: 0644]
typo3/sysext/lang/Resources/Private/Language/locallang_tsfe.xlf

index 1e7d96d..74f33d5 100644 (file)
@@ -16,10 +16,10 @@ namespace TYPO3\CMS\Adminpanel\Modules;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Adminpanel\Repositories\FrontendGroupsRepository;
 use TYPO3\CMS\Core\Type\Bitmask\Permission;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\View\StandaloneView;
 use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
@@ -52,99 +52,28 @@ class PreviewModule extends AbstractModule
      */
     public function getContent(): string
     {
-        $output = [];
-        if ($this->getBackendUser()->uc['TSFE_adminConfig']['display_preview']) {
-            $output[] = '<div class="typo3-adminPanel-form-group">';
-            $output[] = '  <div class="typo3-adminPanel-form-group-checkbox">';
-            $output[] = '    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showHiddenPages]" value="0" />';
-            $output[] = '    <label for="preview_showHiddenPages">';
-            $output[] = '      <input type="checkbox" id="preview_showHiddenPages" name="TSFE_ADMIN_PANEL[preview_showHiddenPages]" value="1"' .
-                        ($this->getBackendUser(
-                        )->uc['TSFE_adminConfig']['preview_showHiddenPages'] ? ' checked="checked"' : '') .
-                        ' />';
-            $output[] = '      ' . $this->extGetLL('preview_showHiddenPages');
-            $output[] = '    </label>';
-            $output[] = '  </div>';
-            $output[] = '  <div class="typo3-adminPanel-form-group-checkbox">';
-            $output[] = '    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showHiddenRecords]" value="0" />';
-            $output[] = '    <label for="preview_showHiddenRecords">';
-            $output[] = '      <input type="checkbox" id="preview_showHiddenRecords" name="TSFE_ADMIN_PANEL[preview_showHiddenRecords]" value="1"' .
-                        ($this->getBackendUser(
-                        )->uc['TSFE_adminConfig']['preview_showHiddenRecords'] ? ' checked="checked"' : '') .
-                        ' />';
-            $output[] = '      ' . $this->extGetLL('preview_showHiddenRecords');
-            $output[] = '    </label>';
-            $output[] = '  </div>';
-            $output[] = '  <div class="typo3-adminPanel-form-group-checkbox">';
-            $output[] = '    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showFluidDebug]" value="0" />';
-            $output[] = '    <label for="preview_showFluidDebug">';
-            $output[] = '      <input type="checkbox" id="preview_showFluidDebug" name="TSFE_ADMIN_PANEL[preview_showFluidDebug]" value="1"' .
-                        ($this->getBackendUser(
-                        )->uc['TSFE_adminConfig']['preview_showFluidDebug'] ? ' checked="checked"' : '') .
-                        ' />';
-            $output[] = '      ' . $this->extGetLL('preview_showFluidDebug');
-            $output[] = '    </label>';
-            $output[] = '  </div>';
-            $output[] = '</div>';
-
-            // Simulate date
-            $output[] = '<div class="typo3-adminPanel-form-group">';
-            $output[] = '  <label for="preview_simulateDate">';
-            $output[] = '    ' . $this->extGetLL('preview_simulateDate');
-            $output[] = '  </label>';
-            $output[] = '  <input type="text" id="preview_simulateDate" name="TSFE_ADMIN_PANEL[preview_simulateDate]_hr" onchange="TSFEtypo3FormFieldGet(\'TSFE_ADMIN_PANEL[preview_simulateDate]\', \'datetime\', \'\', 1,0);" />';
-            // the hidden field must be placed after the _hr field to avoid the timestamp being overridden by the date string
-            $output[] = '  <input type="hidden" name="TSFE_ADMIN_PANEL[preview_simulateDate]" value="' .
-                        $this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateDate'] .
-                        '" />';
-            $output[] = '</div>';
-
-            // Frontend Usergroups
-            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
-                ->getQueryBuilderForTable('fe_groups');
-            $queryBuilder->getRestrictions()
-                ->removeAll()
-                ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
-            $optionCount = $queryBuilder->count('fe_groups.uid')
-                ->from('fe_groups')
-                ->from('pages')
-                ->where(
-                    $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid')),
-                    $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
-                )
-                ->execute()
-                ->fetchColumn(0);
-            if ($optionCount > 0) {
-                $result = $queryBuilder->select('fe_groups.uid', 'fe_groups.title')
-                    ->from('fe_groups')
-                    ->from('pages')
-                    ->where(
-                        $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid')),
-                        $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
-                    )
-                    ->orderBy('fe_groups.title')
-                    ->execute();
-                $output[] = '<div class="typo3-adminPanel-form-group">';
-                $output[] = '  <label for="preview_simulateUserGroup">';
-                $output[] = '    ' . $this->extGetLL('preview_simulateUserGroup');
-                $output[] = '  </label>';
-                $output[] = '  <select id="preview_simulateUserGroup" name="TSFE_ADMIN_PANEL[preview_simulateUserGroup]">';
-                $output[] = '    <option value="0">&nbsp;</option>';
-                while ($row = $result->fetch()) {
-                    $output[] = '<option value="' .
-                                (int)$row['uid'] .
-                                '" ' .
-                                ($this->getBackendUser()->uc['TSFE_adminConfig']['preview_simulateUserGroup'] ===
-                                 $row['uid'] ? ' selected="selected"' : '') .
-                                '>';
-                    $output[] = htmlspecialchars($row['title'] . ' [' . $row['uid'] . ']');
-                    $output[] = '</option>';
-                }
-                $output[] = '  </select>';
-                $output[] = '</div>';
-            }
-        }
-        return implode('', $output);
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $templateNameAndPath = $this->extResources . '/Templates/Modules/Preview.html';
+        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
+        $view->setPartialRootPaths([$this->extResources . '/Partials']);
+
+        $frontendGroupsRepository = GeneralUtility::makeInstance(FrontendGroupsRepository::class);
+
+        $view->assignMultiple(
+            [
+                'show' => [
+                    'hiddenPages' => $this->getConfigurationOption('showHiddenPages'),
+                    'hiddenRecords' => $this->getConfigurationOption('showHiddenRecords'),
+                    'fluidDebug' => $this->getConfigurationOption('showFluidDebug'),
+                ],
+                'simulateDate' => $this->getConfigurationOption('simulateDate'),
+                'frontendUserGroups' => [
+                    'availableGroups' => $frontendGroupsRepository->getAvailableFrontendUserGroups(),
+                    'selected' => $this->getConfigurationOption('simulateUserGroup'),
+                ],
+            ]
+        );
+        return $view->render();
     }
 
     /**
diff --git a/typo3/sysext/adminpanel/Classes/Repositories/FrontendGroupsRepository.php b/typo3/sysext/adminpanel/Classes/Repositories/FrontendGroupsRepository.php
new file mode 100644 (file)
index 0000000..35881d2
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+declare(strict_types=1);
+
+namespace TYPO3\CMS\Adminpanel\Repositories;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Type\Bitmask\Permission;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Admin Panel Frontend Groups Repository
+ */
+class FrontendGroupsRepository
+{
+    /**
+     * returns an array of all available frontend user groups including hidden ones.
+     *
+     * @return array
+     */
+    public function getAvailableFrontendUserGroups(): array
+    {
+        $optionCount = $this->getUserGroupOptionCountByBackendUser($this->getBackendUser());
+
+        $frontendGroups = [];
+        if ($optionCount > 0) {
+            $frontendGroups = $this->getUserGroupsForPagesByBackendUser($this->getBackendUser());
+        }
+
+        return $frontendGroups;
+    }
+
+    /**
+     * fetches the amount of user groups
+     *
+     * @param FrontendBackendUserAuthentication $beUser
+     * @return int
+     */
+    protected function getUserGroupOptionCountByBackendUser(FrontendBackendUserAuthentication $beUser): int
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('fe_groups');
+
+        $queryBuilder->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+
+        $optionCount = $queryBuilder->count('fe_groups.uid')
+            ->from('fe_groups', 'fe_groups')
+            ->innerJoin(
+                'fe_groups',
+                'pages',
+                'pages',
+                $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
+            )
+            ->where(
+                $beUser->getPagePermsClause(Permission::PAGE_SHOW)
+            )
+            ->execute()
+            ->fetchColumn(0);
+
+        return (int)$optionCount;
+    }
+
+    /**
+     * fetches all frontend user groups, except deleted, for pages
+     *
+     * @param FrontendBackendUserAuthentication $beUser
+     * @return array
+     */
+    protected function getUserGroupsForPagesByBackendUser(FrontendBackendUserAuthentication $beUser): array
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('fe_groups');
+
+        $queryBuilder->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+
+        return $queryBuilder->select('fe_groups.uid', 'fe_groups.title')
+            ->from('fe_groups')
+            ->innerJoin(
+                'fe_groups',
+                'pages',
+                'pages',
+                $queryBuilder->expr()->eq('pages.uid', $queryBuilder->quoteIdentifier('fe_groups.pid'))
+            )
+            ->where(
+                $beUser->getPagePermsClause(Permission::PAGE_SHOW)
+            )
+            ->orderBy('fe_groups.title')
+            ->execute()
+            ->fetchAll();
+    }
+
+    /**
+     * Returns the current BE user.
+     *
+     * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
+     */
+    protected function getBackendUser(): FrontendBackendUserAuthentication
+    {
+        return $GLOBALS['BE_USER'];
+    }
+}
diff --git a/typo3/sysext/adminpanel/Resources/Private/Language/locallang_preview.xlf b/typo3/sysext/adminpanel/Resources/Private/Language/locallang_preview.xlf
new file mode 100644 (file)
index 0000000..60411f0
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xliff version="1.0" xmlns:t3="http://typo3.org/schemas/xliff">
+    <file t3:id="1520838433" source-language="en" datatype="plaintext" original="messages" date="2018-03-13T08:08:00Z"
+          product-name="adminpanel">
+        <header/>
+
+        <body>
+            <trans-unit id="module.label">
+                <source>Preview</source>
+            </trans-unit>
+            <trans-unit id="showHiddenPages">
+                <source>Show hidden pages</source>
+            </trans-unit>
+            <trans-unit id="showHiddenRecords">
+                <source>Show hidden records</source>
+            </trans-unit>
+            <trans-unit id="showFluidDebug">
+                <source>Show fluid debug output</source>
+            </trans-unit>
+            <trans-unit id="simulateDate">
+                <source>Simulate time</source>
+            </trans-unit>
+            <trans-unit id="simulateUserGroup">
+                <source>Simulate user group</source>
+            </trans-unit>
+        </body>
+    </file>
+</xliff>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FluidDebug.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FluidDebug.html
new file mode 100644 (file)
index 0000000..3a63ee3
--- /dev/null
@@ -0,0 +1,12 @@
+<div class="typo3-adminPanel-form-group-checkbox">
+    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showFluidDebug]" value="0"/>
+    <label for="preview_showFluidDebug">
+        <input type="checkbox"
+               id="preview_showFluidDebug"
+               name="TSFE_ADMIN_PANEL[preview_showFluidDebug]"
+               value="1"
+               {f:if(condition: '{showFluidDebug}', then:'checked="checked"')}
+        />
+        <f:translate key="LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:showFluidDebug"/>
+    </label>
+</div>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FrontendUserGroup.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/FrontendUserGroup.html
new file mode 100644 (file)
index 0000000..d529e30
--- /dev/null
@@ -0,0 +1,17 @@
+<div class="typo3-adminPanel-form-group">
+    <label for="preview_simulateUserGroup">
+        <f:translate
+                key="LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:simulateUserGroup"/>
+    </label>
+
+    <select name="TSFE_ADMIN_PANEL[preview_simulateUserGroup]"
+            id="preview_simulateUserGroup">
+        <option value="0"></option>
+        <f:for each="{frontendUserGroups.availableGroups}" as="group">
+            <option value="{group.uid}"
+                    {f:if(condition:'{group.uid} == {frontendUserGroups.selected}', then: 'selected="selected"')}>
+            {group.title}
+            </option>
+        </f:for>
+    </select>
+</div>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenPages.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenPages.html
new file mode 100644 (file)
index 0000000..b4c9b02
--- /dev/null
@@ -0,0 +1,12 @@
+<div class="typo3-adminPanel-form-group-checkbox">
+    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showHiddenPages]" value="0"/>
+    <label for="preview_showHiddenPages">
+        <input type="checkbox"
+               id="preview_showHiddenPages"
+               name="TSFE_ADMIN_PANEL[preview_showHiddenPages]"
+               value="1"
+               {f:if(condition: '{showHiddenPages}', then:'checked="checked"')}
+        />
+        <f:translate key="LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:showHiddenPages"/>
+    </label>
+</div>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenRecords.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/HiddenRecords.html
new file mode 100644 (file)
index 0000000..336aeb3
--- /dev/null
@@ -0,0 +1,13 @@
+<div class="typo3-adminPanel-form-group-checkbox">
+    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_showHiddenRecords]" value="0"/>
+    <label for="preview_showHiddenRecords">
+        <input type="checkbox"
+               id="preview_showHiddenRecords"
+               name="TSFE_ADMIN_PANEL[preview_showHiddenRecords]"
+               value="1"
+               {f:if(condition: '{showHiddenRecords}', then:'checked="checked"')}
+        />
+        <f:translate
+                key="LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:showHiddenRecords"/>
+    </label>
+</div>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/ShowHiddenElements.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/ShowHiddenElements.html
new file mode 100644 (file)
index 0000000..9e204a3
--- /dev/null
@@ -0,0 +1,5 @@
+<div class="typo3-adminPanel-form-group">
+    <f:render partial="Modules/Preview/HiddenPages" arguments="{showHiddenPages: show.hiddenPages}" debug="false"/>
+    <f:render partial="Modules/Preview/HiddenRecords" arguments="{showHiddenRecords: show.hiddenRecords}" debug="false"/>
+    <f:render partial="Modules/Preview/FluidDebug" arguments="{showFluidDebug: show.fluidDebug}" debug="false"/>
+</div>
\ No newline at end of file
diff --git a/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/SimulateDate.html b/typo3/sysext/adminpanel/Resources/Private/Partials/Modules/Preview/SimulateDate.html
new file mode 100644 (file)
index 0000000..af3b9b9
--- /dev/null
@@ -0,0 +1,10 @@
+<div class="typo3-adminPanel-form-group">
+    <label for="preview_simulateDate">
+        <f:translate key="LLL:EXT:adminpanel/Resources/Private/Language/locallang_preview.xlf:simulateDate"/>
+    </label>
+    <input type="text"
+           id="preview_simulateDate"
+           name="TSFE_ADMIN_PANEL[preview_simulateDate]_hr"
+           onchange="TSFEtypo3FormFieldGet('TSFE_ADMIN_PANEL[preview_simulateDate]', 'datetime', '', 1,0);"/>
+    <input type="hidden" name="TSFE_ADMIN_PANEL[preview_simulateDate]" value="{simulateDate}"/>
+</div>
diff --git a/typo3/sysext/adminpanel/Resources/Private/Templates/Modules/Preview.html b/typo3/sysext/adminpanel/Resources/Private/Templates/Modules/Preview.html
new file mode 100644 (file)
index 0000000..2eac4ee
--- /dev/null
@@ -0,0 +1,3 @@
+<f:render partial="Modules/Preview/ShowHiddenElements" arguments="{show: show}" debug="false" />
+<f:render partial="Modules/Preview/SimulateDate" arguments="{simulateDate: simulateDate}" debug="false" />
+<f:render partial="Modules/Preview/FrontendUserGroup.html" arguments="{frontendUserGroups: frontendUserGroups}" debug="false"/>
index c406204..5368d88 100644 (file)
@@ -9,24 +9,6 @@
                        <trans-unit id="adminPanelTitle">
                                <source>TYPO3 ADMIN PANEL</source>
                        </trans-unit>
-                       <trans-unit id="preview">
-                               <source>Preview</source>
-                       </trans-unit>
-                       <trans-unit id="preview_showHiddenPages">
-                               <source>Show hidden pages</source>
-                       </trans-unit>
-                       <trans-unit id="preview_showHiddenRecords">
-                               <source>Show hidden records</source>
-                       </trans-unit>
-                       <trans-unit id="preview_showFluidDebug">
-                               <source>Show fluid debug output</source>
-                       </trans-unit>
-                       <trans-unit id="preview_simulateDate">
-                               <source>Simulate time</source>
-                       </trans-unit>
-                       <trans-unit id="preview_simulateUserGroup">
-                               <source>Simulate user group</source>
-                       </trans-unit>
                        <trans-unit id="publish">
                                <source>Publish</source>
                        </trans-unit>