[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\View\BackendTemplateView;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Page\PageRenderer;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
27 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
28
29 /**
30 * Controller for viewing the frontend
31 */
32 class ViewModuleController extends ActionController
33 {
34 /**
35 * @var string
36 */
37 protected $defaultViewObjectName = BackendTemplateView::class;
38
39 /**
40 * @var BackendTemplateView
41 */
42 protected $view;
43
44 /**
45 * Set up the doc header properly here
46 *
47 * @param ViewInterface $view
48 */
49 protected function initializeView(ViewInterface $view)
50 {
51 /** @var BackendTemplateView $view */
52 parent::initializeView($view);
53 $this->registerButtons();
54 }
55
56 /**
57 * Registers the docheader buttons
58 */
59 protected function registerButtons()
60 {
61 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
62 $showButton = $buttonBar->makeLinkButton()
63 ->setHref($this->getTargetUrl())
64 ->setOnClick('window.open(this.href, \'newTYPO3frontendWindow\').focus();return false;')
65 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
66 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-view', Icon::SIZE_SMALL));
67 $buttonBar->addButton($showButton);
68
69 $refreshButton = $buttonBar->makeLinkButton()
70 ->setHref('javascript:document.getElementById(\'tx_viewpage_iframe\').contentWindow.location.reload(true);')
71 ->setTitle($this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:refreshPage'))
72 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-refresh', Icon::SIZE_SMALL));
73 $buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT, 1);
74
75 $currentRequest = $this->request;
76 $moduleName = $currentRequest->getPluginName();
77 $getVars = $this->request->getArguments();
78 $extensionName = $currentRequest->getControllerExtensionName();
79 if (count($getVars) === 0) {
80 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
81 $getVars = ['id', 'M', $modulePrefix];
82 }
83 $shortcutButton = $buttonBar->makeShortcutButton()
84 ->setModuleName($moduleName)
85 ->setGetVariables($getVars);
86 $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT, 2);
87 }
88
89 /**
90 * Gets called before each action
91 */
92 public function initializeAction()
93 {
94 $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
95 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
96 $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
97 }
98
99 /**
100 * Show selected page from pagetree in iframe
101 */
102 public function showAction()
103 {
104 $this->view->assign('widths', $this->getPreviewFrameWidths());
105 $this->view->assign('url', $this->getTargetUrl());
106 $this->view->assign('languages', $this->getPreviewLanguages());
107 $this->view->assign('pageTitle', $this->getPageTitle());
108 }
109
110 /**
111 * Determine the url to view
112 *
113 * @return string
114 */
115 protected function getTargetUrl()
116 {
117 $pageIdToShow = (int)GeneralUtility::_GP('id');
118
119 $permissionClause = $this->getBackendUser()->getPagePermsClause(1);
120 $pageRecord = BackendUtility::readPageAccess($pageIdToShow, $permissionClause);
121 if ($pageRecord) {
122 $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($pageRecord);
123
124 $adminCommand = $this->getAdminCommand($pageIdToShow);
125 $domainName = $this->getDomainName($pageIdToShow);
126 $languageParameter = $this->getLanguageParameter();
127 // Mount point overlay: Set new target page id and mp parameter
128 /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
129 $sysPage = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
130 $sysPage->init(false);
131 $mountPointMpParameter = '';
132 $finalPageIdToShow = $pageIdToShow;
133 $mountPointInformation = $sysPage->getMountPointInfo($pageIdToShow);
134 if ($mountPointInformation && $mountPointInformation['overlay']) {
135 // New page id
136 $finalPageIdToShow = $mountPointInformation['mount_pid'];
137 $mountPointMpParameter = '&MP=' . $mountPointInformation['MPvar'];
138 }
139 // Modify relative path to protocol with host if domain record is given
140 $protocolAndHost = '..';
141 if ($domainName) {
142 // TCEMAIN.previewDomain can contain the protocol, check prevents double protocol URLs
143 if (strpos($domainName, '://') !== false) {
144 $protocolAndHost = $domainName;
145 } else {
146 $protocol = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https' : 'http';
147 $protocolAndHost = $protocol . '://' . $domainName;
148 }
149 }
150 return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand . $languageParameter;
151 }
152 return '#';
153 }
154
155 /**
156 * Get admin command
157 *
158 * @param int $pageId
159 * @return string
160 */
161 protected function getAdminCommand($pageId)
162 {
163 // The page will show only if there is a valid page and if this page may be viewed by the user
164 $pageinfo = BackendUtility::readPageAccess($pageId, $this->getBackendUser()->getPagePermsClause(1));
165 $addCommand = '';
166 if (is_array($pageinfo)) {
167 $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
168 }
169 return $addCommand;
170 }
171
172 /**
173 * With page TS config it is possible to force a specific type id via mod.web_view.type
174 * for a page id or a page tree.
175 * The method checks if a type is set for the given id and returns the additional GET string.
176 *
177 * @param int $pageId
178 * @return string
179 */
180 protected function getTypeParameterIfSet($pageId)
181 {
182 $typeParameter = '';
183 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
184 $typeId = (int)$modTSconfig['properties']['type'];
185 if ($typeId > 0) {
186 $typeParameter = '&type=' . $typeId;
187 }
188 return $typeParameter;
189 }
190
191 /**
192 * Get domain name for requested page id
193 *
194 * @param int $pageId
195 * @return string|null Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
196 */
197 protected function getDomainName($pageId)
198 {
199 $previewDomainConfig = $this->getBackendUser()->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
200 if ($previewDomainConfig['value']) {
201 $domain = $previewDomainConfig['value'];
202 } else {
203 $domain = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($pageId));
204 }
205 return $domain;
206 }
207
208 /**
209 * Get available widths for preview frame
210 *
211 * @return array
212 */
213 protected function getPreviewFrameWidths()
214 {
215 $pageId = (int)GeneralUtility::_GP('id');
216 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
217 $widths = [
218 '100%|100%' => $this->getLanguageService()->getLL('autoSize')
219 ];
220 if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
221 foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf) {
222 $label = '';
223
224 $width = substr($item, 0, -1);
225 $data = ['width' => $width];
226 $label .= $width . 'px ';
227
228 //if height is set
229 if (isset($conf['height'])) {
230 $label .= ' × ' . $conf['height'] . 'px ';
231 $data['height'] = $conf['height'];
232 }
233
234 if (substr($conf['label'], 0, 4) !== 'LLL:') {
235 $label .= $conf['label'];
236 } else {
237 $label .= $this->getLanguageService()->sL(trim($conf['label']));
238 }
239 $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
240 $widths[$value] = $label;
241 }
242 }
243 return $widths;
244 }
245
246 /**
247 * Returns the preview languages
248 *
249 * @return array
250 */
251 protected function getPreviewLanguages()
252 {
253 $pageIdToShow = (int)GeneralUtility::_GP('id');
254 $modSharedTSconfig = BackendUtility::getModTSconfig($pageIdToShow, 'mod.SHARED');
255 if ($modSharedTSconfig['properties']['view.']['disableLanguageSelector'] === '1') {
256 return [];
257 }
258 $languages = [
259 0 => isset($modSharedTSconfig['properties']['defaultLanguageLabel'])
260 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage') . ')'
261 : $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage')
262 ];
263 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
264 $queryBuilder->getRestrictions()
265 ->removeAll()
266 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
267
268 if (!$this->getBackendUser()->isAdmin()) {
269 $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
270 }
271
272 $result = $queryBuilder->select('sys_language.uid', 'sys_language.title')
273 ->from('sys_language')
274 ->join(
275 'sys_language',
276 'pages_language_overlay',
277 'o',
278 $queryBuilder->expr()->eq('o.sys_language_uid', $queryBuilder->quoteIdentifier('sys_language.uid'))
279 )
280 ->where(
281 $queryBuilder->expr()->eq(
282 'o.pid',
283 $queryBuilder->createNamedParameter($pageIdToShow, \PDO::PARAM_INT)
284 )
285 )
286 ->groupBy('sys_language.uid', 'sys_language.title', 'sys_language.sorting')
287 ->orderBy('sys_language.sorting')
288 ->execute();
289
290 while ($row = $result->fetch()) {
291 if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
292 $languages[$row['uid']] = $row['title'];
293 }
294 }
295 return $languages;
296 }
297
298 /**
299 * Returns the page title
300 *
301 * @return string
302 */
303 protected function getPageTitle()
304 {
305 $pageIdToShow = (int)GeneralUtility::_GP('id');
306 $pageRecord = BackendUtility::getRecord('pages', $pageIdToShow);
307 $pageRecordTitle = is_array($pageRecord)
308 ? BackendUtility::getRecordTitle('pages', $pageRecord)
309 : '';
310
311 return $pageRecordTitle;
312 }
313
314 /**
315 * Gets the L parameter from the user session
316 *
317 * @return string
318 */
319 protected function getLanguageParameter()
320 {
321 $states = $this->getBackendUser()->uc['moduleData']['web_view']['States'];
322 $languages = $this->getPreviewLanguages();
323 $languageParameter = '';
324 if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
325 $languageParameter = '&L=' . (int)$states['languageSelectorValue'];
326 }
327 return $languageParameter;
328 }
329
330 /**
331 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
332 */
333 protected function getBackendUser()
334 {
335 return $GLOBALS['BE_USER'];
336 }
337
338 /**
339 * @return \TYPO3\CMS\Lang\LanguageService
340 */
341 protected function getLanguageService()
342 {
343 return $GLOBALS['LANG'];
344 }
345 }