[FEATURE] Viewpage language selector 62/27862/7
authorErik Frister <erik.frister@aijko.com>
Wed, 26 Feb 2014 10:02:06 +0000 (11:02 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 18 Aug 2015 06:32:45 +0000 (08:32 +0200)
Adds a language selector to the viewpage module.

Resolves: #56282
Releases: master
Change-Id: Iae19f533f447c20aab7d672eff9aa1d202365363
Reviewed-on: http://review.typo3.org/27862
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-56282-LanguageSelectorForPageviewModule.rst [new file with mode: 0644]
typo3/sysext/viewpage/Classes/Controller/ViewModuleController.php
typo3/sysext/viewpage/Resources/Private/Language/locallang.xlf
typo3/sysext/viewpage/Resources/Private/Layouts/DocHeader.html
typo3/sysext/viewpage/Resources/Public/JavaScript/Main.js

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-56282-LanguageSelectorForPageviewModule.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-56282-LanguageSelectorForPageviewModule.rst
new file mode 100644 (file)
index 0000000..a9d19bf
--- /dev/null
@@ -0,0 +1,8 @@
+=======================================================
+Feature: #56282 - Language selector for pageview module
+=======================================================
+
+Description
+===========
+
+The pageview module now has a dropdown to select a language for the page preview.
index 94dac09..fd7cdc8 100644 (file)
@@ -29,7 +29,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
         * @return void
         */
        public function initializeAction() {
-               $GLOBALS['LANG']->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
+               $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
                $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
                $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
        }
@@ -42,6 +42,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
        public function showAction() {
                $this->view->assign('widths', $this->getPreviewFrameWidths());
                $this->view->assign('url', $this->getTargetUrl());
+               $this->view->assign('languages', $this->getPreviewLanguages());
        }
 
        /**
@@ -53,6 +54,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
                $pageIdToShow = (int)GeneralUtility::_GP('id');
                $adminCommand = $this->getAdminCommand($pageIdToShow);
                $domainName = $this->getDomainName($pageIdToShow);
+               $languageParameter = $this->getLanguageParameter();
                // Mount point overlay: Set new target page id and mp parameter
                /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
                $sysPage = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
@@ -80,7 +82,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
                                $protocolAndHost = $protocol . '://' . $domainName;
                        }
                }
-               return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand;
+               return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand . $languageParameter;
        }
 
        /**
@@ -91,7 +93,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
         */
        protected function getAdminCommand($pageId) {
                // The page will show only if there is a valid page and if this page may be viewed by the user
-               $pageinfo = BackendUtility::readPageAccess($pageId, $GLOBALS['BE_USER']->getPagePermsClause(1));
+               $pageinfo = BackendUtility::readPageAccess($pageId, $this->getBackendUser()->getPagePermsClause(1));
                $addCommand = '';
                if (is_array($pageinfo)) {
                        $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
@@ -124,7 +126,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
         * @return string|NULL Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
         */
        protected function getDomainName($pageId) {
-               $previewDomainConfig = $GLOBALS['BE_USER']->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
+               $previewDomainConfig = $this->getBackendUser()->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
                if ($previewDomainConfig['value']) {
                        $domain = $previewDomainConfig['value'];
                } else {
@@ -142,7 +144,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
                $pageId = (int)GeneralUtility::_GP('id');
                $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
                $widths = array(
-                       '100%|100%' => $GLOBALS['LANG']->getLL('autoSize')
+                       '100%|100%' => $this->getLanguageService()->getLL('autoSize')
                );
                if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
                        foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf ){
@@ -161,7 +163,7 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
                                if (substr($conf['label'], 0, 4) !== 'LLL:') {
                                        $label .= $conf['label'];
                                } else {
-                                       $label .= $GLOBALS['LANG']->sL(trim($conf['label']));
+                                       $label .= $this->getLanguageService()->sL(trim($conf['label']));
                                }
                                $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
                                $widths[$value] = $label;
@@ -170,4 +172,70 @@ class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionContr
                return $widths;
        }
 
+       /**
+        * Returns the preview languages
+        *
+        * @return array
+        */
+       protected function getPreviewLanguages() {
+               $pageIdToShow = (int)GeneralUtility::_GP('id');
+               $modSharedTSconfig = BackendUtility::getModTSconfig($pageIdToShow, 'mod.SHARED');
+               $languages = array(
+                       0 => isset($modSharedTSconfig['properties']['defaultLanguageLabel'])
+                                       ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')'
+                                       : $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage')
+               );
+               $excludeHidden = $this->getBackendUser()->isAdmin() ? '' : ' AND sys_language.hidden=0';
+               $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
+                       'sys_language.*',
+                       'pages_language_overlay JOIN sys_language ON pages_language_overlay.sys_language_uid = sys_language.uid',
+                       'pages_language_overlay.pid = ' . (int)$pageIdToShow . BackendUtility::deleteClause('pages_language_overlay') . $excludeHidden,
+                       'pages_language_overlay.sys_language_uid, sys_language.uid, sys_language.pid, sys_language.tstamp, sys_language.hidden, sys_language.title, sys_language.static_lang_isocode, sys_language.flag',
+                       'sys_language.title'
+               );
+               if (!empty($rows)) {
+                       foreach ($rows as $row) {
+                               if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
+                                       $languages[$row['uid']] = $row['title'];
+                               }
+                       }
+               }
+               return $languages;
+       }
+
+       /**
+        * Gets the L parameter from the user session
+        *
+        * @return string
+        */
+       protected function getLanguageParameter() {
+               $states = $this->getBackendUser()->uc['moduleData']['web_view']['States'];
+               $languages = $this->getPreviewLanguages();
+               $languageParameter = '';
+               if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
+                       $languageParameter = '&L=' . (int)$states['languageSelectorValue'];
+               }
+               return $languageParameter;
+       }
+
+       /**
+        * @return \TYPO3\CMS\Core\Database\DatabaseConnection
+        */
+       protected function getDatabaseConnection() {
+               return $GLOBALS['TYPO3_DB'];
+       }
+
+       /**
+        * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
+        */
+       protected function getBackendUser() {
+               return $GLOBALS['BE_USER'];
+       }
+
+       /**
+        * @return \TYPO3\CMS\Lang\LanguageService
+        */
+       protected function getLanguageService() {
+               return $GLOBALS['LANG'];
+       }
 }
index 963007b..f6bda33 100644 (file)
@@ -24,6 +24,9 @@
                        <trans-unit id="refreshPage">
                                <source>Refresh page</source>
                        </trans-unit>
+                       <trans-unit id="language">
+                               <source>Language</source>
+                       </trans-unit>
                </body>
        </file>
 </xliff>
index eaad421..116f0d6 100644 (file)
                                                <f:translate key="width" />
                                        </label>
                                        <f:form.select options="{widths}" id="width" name="width" />
+                                       <f:if condition="{languages -> f:count()} > 1">
+                                               <label for="language">
+                                                       <f:translate key="language" />
+                                               </label>
+                                               <f:form.select options="{languages}" id="language" name="language" />
+                                       </f:if>
                                </form>
                        </div>
                        <div class="typo3-docheader-buttons">
index 6dae885..4f940e2 100644 (file)
@@ -14,6 +14,7 @@
  * Main logic for resizing the view of the frame
  */
 define(['jquery', 'jquery-ui/resizable'], function($) {
+       "use strict";
 
        // fetch the storage from the outer frame
        var Storage = top.TYPO3.Storage;
@@ -26,6 +27,8 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
 
        ViewPage.initialize = function() {
                var me = this;
+               me.$iframe = $('#tx_viewpage_iframe');
+               me.$languageSelector = $('#language');
                me.$resizableContainer = $(me.resizableContainerIdentifier);
                me.$widthSelector = $(me.widthSelectorIdentifier);
 
@@ -57,7 +60,7 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                var storedWidth = Storage.Persistent.get(me.storagePrefix + 'widthSelectorValue');
                // Check for the " symbol is done in order to avoid problems with the old (non-jQuery) syntax which might be stored inside
                // the UC from previous versions, can be removed with TYPO3 CMS9 again
-               if (storedWidth && storedWidth.indexOf('"') == -1) {
+               if (storedWidth && storedWidth.indexOf('"') === -1) {
                        me.$widthSelector.val(storedWidth).trigger('change');
                }
 
@@ -67,7 +70,7 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                });
 
                // Create and select custom option
-               me.$resizableContainer.on('resizestart', function(evt, ui) {
+               me.$resizableContainer.on('resizestart', function() {
                        // Check custom option is there, if not, add it
                        if (me.$widthSelector.find('#customOption').length === 0) {
                                me.addCustomWidthOption('100%|100%');
@@ -76,7 +79,7 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                        me.$widthSelector.find('#customOption').prop('selected', true);
 
                        // Add iframe overlay to prevent loosing the mouse focus to the iframe while resizing fast
-                       $(this).append('<div id="iframeCover" style="zindex:99;position:absolute;width:100%;top:0px;left:0px;height:100%;"></div>');
+                       $(this).append('<div id="iframeCover" style="z-index:99;position:absolute;width:100%;top:0;left:0;height:100%;"></div>');
                });
 
                me.$resizableContainer.on('resize', function(evt, ui) {
@@ -86,7 +89,7 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                        me.$widthSelector.find('#customOption').text(label).val(value);
                });
 
-               me.$resizableContainer.on('resizestop', function(evt, ui) {
+               me.$resizableContainer.on('resizestop', function() {
                        Storage.Persistent.set(me.storagePrefix + 'widthSelectorCustomValue', me.$widthSelector.val()).done(function() {
                                Storage.Persistent.set(me.storagePrefix + 'widthSelectorValue', me.$widthSelector.val());
                        });
@@ -94,6 +97,25 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                        // Remove iframe overlay
                        $('#iframeCover').remove();
                });
+
+               // select stored language
+               var storedLanguage = Storage.Persistent.get(me.storagePrefix + 'languageSelectorValue');
+               if (storedLanguage) {
+                       // select it
+                       me.$languageSelector.val(storedLanguage);
+               }
+
+               // Add event to language selector
+               me.$languageSelector.on('change',function() {
+                       var iframeUrl = me.$iframe.attr('src');
+                       var iframeParameters = ViewPage.getUrlVars(iframeUrl);
+                       // change language
+                       iframeParameters.L = me.$languageSelector.val();
+                       var newIframeUrl = iframeUrl.slice(0, iframeUrl.indexOf('?') + 1) + $.param(iframeParameters);
+                       // load new url into iframe
+                       me.$iframe.attr('src', newIframeUrl);
+                       Storage.Persistent.set(me.storagePrefix + 'languageSelectorValue', me.$languageSelector.val());
+               });
        };
 
        ViewPage.addCustomWidthOption = function(value) {
@@ -105,9 +127,20 @@ define(['jquery', 'jquery-ui/resizable'], function($) {
                return data[0] + 'px ' + (data[1] ? '× ' + data[1] + 'px ' : '') + TYPO3.lang['customWidth'];
        };
 
-       return function() {
-               $(document).ready(function() {
-                       ViewPage.initialize();
-               });
-       }();
+       ViewPage.getUrlVars = function(url) {
+               var vars = {};
+               var hash;
+               var hashes = url.slice(url.indexOf('?') + 1).split('&');
+               for (var i = 0; i < hashes.length; i ++) {
+                       hash = hashes[i].split('=');
+                       vars[hash[0]] = hash[1];
+               }
+               return vars;
+       };
+
+       $(document).ready(function() {
+               ViewPage.initialize();
+       });
+
+       return ViewPage;
 });