[BUGFIX] Use Sitematcher to fetch available languages 38/60338/3
authorGeorg Ringer <georg.ringer@gmail.com>
Tue, 26 Mar 2019 07:02:44 +0000 (08:02 +0100)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Fri, 5 Apr 2019 14:10:43 +0000 (16:10 +0200)
EXT:viewpage must use the SiteMatcher to get the available languages.

The join functionality has been removed as well to let users view a page
in a fallback language.

Resolves: #88001
Releaes: master, 9.5
Change-Id: Ibd0be7b2d169a7e8fb029905e3221fdd3ee5d69c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/60338
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Tested-by: André Schließer <andy.schliesser@gmail.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: André Schließer <andy.schliesser@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
typo3/sysext/viewpage/Classes/Controller/ViewModuleController.php

index b5ee12c..54e008c 100644 (file)
@@ -21,9 +21,7 @@ use TYPO3\CMS\Backend\Template\Components\ButtonBar;
 use TYPO3\CMS\Backend\Template\ModuleTemplate;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
-use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
+use TYPO3\CMS\Core\Context\LanguageAspectFactory;
 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
 use TYPO3\CMS\Core\Http\HtmlResponse;
 use TYPO3\CMS\Core\Imaging\Icon;
@@ -353,48 +351,27 @@ class ViewModuleController
      */
     protected function getPreviewLanguages(int $pageId): array
     {
-        $localizationParentField = $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'];
-        $languageField = $GLOBALS['TCA']['pages']['ctrl']['languageField'];
+        $languages = [];
         $modSharedTSconfig = BackendUtility::getPagesTSconfig($pageId)['mod.']['SHARED.'] ?? [];
         if ($modSharedTSconfig['view.']['disableLanguageSelector'] === '1') {
-            return [];
+            return $languages;
         }
-        $languages = [
-            0 => isset($modSharedTSconfig['defaultLanguageLabel'])
-                    ? $modSharedTSconfig['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage') . ')'
-                    : $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage')
-        ];
-        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
-        $queryBuilder->getRestrictions()
-            ->removeAll()
-            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
 
-        if (!$this->getBackendUser()->isAdmin()) {
-            $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
-        }
+        try {
+            $pageRepository = GeneralUtility::makeInstance(PageRepository::class);
+            $site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId($pageId);
+            $siteLanguages = $site->getAvailableLanguages($this->getBackendUser(), false, $pageId);
 
-        $result = $queryBuilder->select('sys_language.uid', 'sys_language.title')
-            ->from('sys_language')
-            ->join(
-                'sys_language',
-                'pages',
-                'o',
-                $queryBuilder->expr()->eq('o.' . $languageField, $queryBuilder->quoteIdentifier('sys_language.uid'))
-            )
-            ->where(
-                $queryBuilder->expr()->eq(
-                    'o.' . $localizationParentField,
-                    $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
-                )
-            )
-            ->groupBy('sys_language.uid', 'sys_language.title', 'sys_language.sorting')
-            ->orderBy('sys_language.sorting')
-            ->execute();
-
-        while ($row = $result->fetch()) {
-            if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
-                $languages[$row['uid']] = $row['title'];
+            foreach ($siteLanguages as $siteLanguage) {
+                $languageAspectToTest = LanguageAspectFactory::createFromSiteLanguage($siteLanguage);
+                $page = $pageRepository->getPageOverlay($pageRepository->getPage($pageId), $siteLanguage->getLanguageId());
+
+                if ($pageRepository->isPageSuitableForLanguage($page, $languageAspectToTest)) {
+                    $languages[$siteLanguage->getLanguageId()] = $siteLanguage->getTitle();
+                }
             }
+        } catch (SiteNotFoundException $e) {
+            // do nothing
         }
         return $languages;
     }