[FEATURE] Viewpage language selector
[Packages/TYPO3.CMS.git] / typo3 / sysext / viewpage / Classes / Controller / ViewModuleController.php
1 <?php
2 namespace TYPO3\CMS\Viewpage\Controller;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Page\PageRenderer;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Controller for viewing the frontend
23 */
24 class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
25
26 /**
27 * Gets called before each action
28 *
29 * @return void
30 */
31 public function initializeAction() {
32 $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
33 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
34 $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
35 }
36
37 /**
38 * Show selected page from pagetree in iframe
39 *
40 * @return void
41 */
42 public function showAction() {
43 $this->view->assign('widths', $this->getPreviewFrameWidths());
44 $this->view->assign('url', $this->getTargetUrl());
45 $this->view->assign('languages', $this->getPreviewLanguages());
46 }
47
48 /**
49 * Determine the url to view
50 *
51 * @return string
52 */
53 protected function getTargetUrl() {
54 $pageIdToShow = (int)GeneralUtility::_GP('id');
55 $adminCommand = $this->getAdminCommand($pageIdToShow);
56 $domainName = $this->getDomainName($pageIdToShow);
57 $languageParameter = $this->getLanguageParameter();
58 // Mount point overlay: Set new target page id and mp parameter
59 /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
60 $sysPage = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
61 $sysPage->init(FALSE);
62 $mountPointMpParameter = '';
63 $finalPageIdToShow = $pageIdToShow;
64 $mountPointInformation = $sysPage->getMountPointInfo($pageIdToShow);
65 if ($mountPointInformation && $mountPointInformation['overlay']) {
66 // New page id
67 $finalPageIdToShow = $mountPointInformation['mount_pid'];
68 $mountPointMpParameter = '&MP=' . $mountPointInformation['MPvar'];
69 }
70 // Modify relative path to protocol with host if domain record is given
71 $protocolAndHost = '..';
72 if ($domainName) {
73 // TCEMAIN.previewDomain can contain the protocol, check prevents double protocol URLs
74 if (strpos($domainName, '://') !== FALSE) {
75 $protocolAndHost = $domainName;
76 } else {
77 $protocol = 'http';
78 $page = (array)$sysPage->getPage($finalPageIdToShow);
79 if ($page['url_scheme'] == 2 || $page['url_scheme'] == 0 && GeneralUtility::getIndpEnv('TYPO3_SSL')) {
80 $protocol = 'https';
81 }
82 $protocolAndHost = $protocol . '://' . $domainName;
83 }
84 }
85 return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand . $languageParameter;
86 }
87
88 /**
89 * Get admin command
90 *
91 * @param int $pageId
92 * @return string
93 */
94 protected function getAdminCommand($pageId) {
95 // The page will show only if there is a valid page and if this page may be viewed by the user
96 $pageinfo = BackendUtility::readPageAccess($pageId, $this->getBackendUser()->getPagePermsClause(1));
97 $addCommand = '';
98 if (is_array($pageinfo)) {
99 $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
100 }
101 return $addCommand;
102 }
103
104 /**
105 * With page TS config it is possible to force a specific type id via mod.web_view.type
106 * for a page id or a page tree.
107 * The method checks if a type is set for the given id and returns the additional GET string.
108 *
109 * @param int $pageId
110 * @return string
111 */
112 protected function getTypeParameterIfSet($pageId) {
113 $typeParameter = '';
114 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
115 $typeId = (int)$modTSconfig['properties']['type'];
116 if ($typeId > 0) {
117 $typeParameter = '&type=' . $typeId;
118 }
119 return $typeParameter;
120 }
121
122 /**
123 * Get domain name for requested page id
124 *
125 * @param int $pageId
126 * @return string|NULL Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
127 */
128 protected function getDomainName($pageId) {
129 $previewDomainConfig = $this->getBackendUser()->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
130 if ($previewDomainConfig['value']) {
131 $domain = $previewDomainConfig['value'];
132 } else {
133 $domain = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($pageId));
134 }
135 return $domain;
136 }
137
138 /**
139 * Get available widths for preview frame
140 *
141 * @return array
142 */
143 protected function getPreviewFrameWidths() {
144 $pageId = (int)GeneralUtility::_GP('id');
145 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
146 $widths = array(
147 '100%|100%' => $this->getLanguageService()->getLL('autoSize')
148 );
149 if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
150 foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf ){
151 $label = '';
152
153 $width = substr($item, 0, -1);
154 $data = array('width' => $width);
155 $label .= $width . 'px ';
156
157 //if height is set
158 if (isset($conf['height'])) {
159 $label .= ' × ' . $conf['height'] . 'px ';
160 $data['height'] = $conf['height'];
161 }
162
163 if (substr($conf['label'], 0, 4) !== 'LLL:') {
164 $label .= $conf['label'];
165 } else {
166 $label .= $this->getLanguageService()->sL(trim($conf['label']));
167 }
168 $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
169 $widths[$value] = $label;
170 }
171 }
172 return $widths;
173 }
174
175 /**
176 * Returns the preview languages
177 *
178 * @return array
179 */
180 protected function getPreviewLanguages() {
181 $pageIdToShow = (int)GeneralUtility::_GP('id');
182 $modSharedTSconfig = BackendUtility::getModTSconfig($pageIdToShow, 'mod.SHARED');
183 $languages = array(
184 0 => isset($modSharedTSconfig['properties']['defaultLanguageLabel'])
185 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')'
186 : $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage')
187 );
188 $excludeHidden = $this->getBackendUser()->isAdmin() ? '' : ' AND sys_language.hidden=0';
189 $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
190 'sys_language.*',
191 'pages_language_overlay JOIN sys_language ON pages_language_overlay.sys_language_uid = sys_language.uid',
192 'pages_language_overlay.pid = ' . (int)$pageIdToShow . BackendUtility::deleteClause('pages_language_overlay') . $excludeHidden,
193 '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',
194 'sys_language.title'
195 );
196 if (!empty($rows)) {
197 foreach ($rows as $row) {
198 if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
199 $languages[$row['uid']] = $row['title'];
200 }
201 }
202 }
203 return $languages;
204 }
205
206 /**
207 * Gets the L parameter from the user session
208 *
209 * @return string
210 */
211 protected function getLanguageParameter() {
212 $states = $this->getBackendUser()->uc['moduleData']['web_view']['States'];
213 $languages = $this->getPreviewLanguages();
214 $languageParameter = '';
215 if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
216 $languageParameter = '&L=' . (int)$states['languageSelectorValue'];
217 }
218 return $languageParameter;
219 }
220
221 /**
222 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
223 */
224 protected function getDatabaseConnection() {
225 return $GLOBALS['TYPO3_DB'];
226 }
227
228 /**
229 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
230 */
231 protected function getBackendUser() {
232 return $GLOBALS['BE_USER'];
233 }
234
235 /**
236 * @return \TYPO3\CMS\Lang\LanguageService
237 */
238 protected function getLanguageService() {
239 return $GLOBALS['LANG'];
240 }
241 }