[TASK] Doctrine: migrate EXT:viewpage
[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/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 = array('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 * @return void
93 */
94 public function initializeAction()
95 {
96 $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
97 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
98 $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
99 }
100
101 /**
102 * Show selected page from pagetree in iframe
103 *
104 * @return void
105 */
106 public function showAction()
107 {
108 $this->view->assign('widths', $this->getPreviewFrameWidths());
109 $this->view->assign('url', $this->getTargetUrl());
110 $this->view->assign('languages', $this->getPreviewLanguages());
111 }
112
113 /**
114 * Determine the url to view
115 *
116 * @return string
117 */
118 protected function getTargetUrl()
119 {
120 $pageIdToShow = (int)GeneralUtility::_GP('id');
121
122 $permissionClause = $this->getBackendUser()->getPagePermsClause(1);
123 $pageRecord = BackendUtility::readPageAccess($pageIdToShow, $permissionClause);
124 if ($pageRecord) {
125 $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($pageRecord);
126
127 $adminCommand = $this->getAdminCommand($pageIdToShow);
128 $domainName = $this->getDomainName($pageIdToShow);
129 $languageParameter = $this->getLanguageParameter();
130 // Mount point overlay: Set new target page id and mp parameter
131 /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
132 $sysPage = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
133 $sysPage->init(false);
134 $mountPointMpParameter = '';
135 $finalPageIdToShow = $pageIdToShow;
136 $mountPointInformation = $sysPage->getMountPointInfo($pageIdToShow);
137 if ($mountPointInformation && $mountPointInformation['overlay']) {
138 // New page id
139 $finalPageIdToShow = $mountPointInformation['mount_pid'];
140 $mountPointMpParameter = '&MP=' . $mountPointInformation['MPvar'];
141 }
142 // Modify relative path to protocol with host if domain record is given
143 $protocolAndHost = '..';
144 if ($domainName) {
145 // TCEMAIN.previewDomain can contain the protocol, check prevents double protocol URLs
146 if (strpos($domainName, '://') !== false) {
147 $protocolAndHost = $domainName;
148 } else {
149 $protocol = 'http';
150 $page = (array)$sysPage->getPage($finalPageIdToShow);
151 if ($page['url_scheme'] == 2 || $page['url_scheme'] == 0 && GeneralUtility::getIndpEnv('TYPO3_SSL')) {
152 $protocol = 'https';
153 }
154 $protocolAndHost = $protocol . '://' . $domainName;
155 }
156 }
157 return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand . $languageParameter;
158 } else {
159 return '#';
160 }
161 }
162
163 /**
164 * Get admin command
165 *
166 * @param int $pageId
167 * @return string
168 */
169 protected function getAdminCommand($pageId)
170 {
171 // The page will show only if there is a valid page and if this page may be viewed by the user
172 $pageinfo = BackendUtility::readPageAccess($pageId, $this->getBackendUser()->getPagePermsClause(1));
173 $addCommand = '';
174 if (is_array($pageinfo)) {
175 $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
176 }
177 return $addCommand;
178 }
179
180 /**
181 * With page TS config it is possible to force a specific type id via mod.web_view.type
182 * for a page id or a page tree.
183 * The method checks if a type is set for the given id and returns the additional GET string.
184 *
185 * @param int $pageId
186 * @return string
187 */
188 protected function getTypeParameterIfSet($pageId)
189 {
190 $typeParameter = '';
191 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
192 $typeId = (int)$modTSconfig['properties']['type'];
193 if ($typeId > 0) {
194 $typeParameter = '&type=' . $typeId;
195 }
196 return $typeParameter;
197 }
198
199 /**
200 * Get domain name for requested page id
201 *
202 * @param int $pageId
203 * @return string|NULL Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
204 */
205 protected function getDomainName($pageId)
206 {
207 $previewDomainConfig = $this->getBackendUser()->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
208 if ($previewDomainConfig['value']) {
209 $domain = $previewDomainConfig['value'];
210 } else {
211 $domain = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($pageId));
212 }
213 return $domain;
214 }
215
216 /**
217 * Get available widths for preview frame
218 *
219 * @return array
220 */
221 protected function getPreviewFrameWidths()
222 {
223 $pageId = (int)GeneralUtility::_GP('id');
224 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
225 $widths = array(
226 '100%|100%' => $this->getLanguageService()->getLL('autoSize')
227 );
228 if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
229 foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf) {
230 $label = '';
231
232 $width = substr($item, 0, -1);
233 $data = array('width' => $width);
234 $label .= $width . 'px ';
235
236 //if height is set
237 if (isset($conf['height'])) {
238 $label .= ' × ' . $conf['height'] . 'px ';
239 $data['height'] = $conf['height'];
240 }
241
242 if (substr($conf['label'], 0, 4) !== 'LLL:') {
243 $label .= $conf['label'];
244 } else {
245 $label .= $this->getLanguageService()->sL(trim($conf['label']));
246 }
247 $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
248 $widths[$value] = $label;
249 }
250 }
251 return $widths;
252 }
253
254 /**
255 * Returns the preview languages
256 *
257 * @return array
258 */
259 protected function getPreviewLanguages()
260 {
261 $pageIdToShow = (int)GeneralUtility::_GP('id');
262 $modSharedTSconfig = BackendUtility::getModTSconfig($pageIdToShow, 'mod.SHARED');
263 if ($modSharedTSconfig['properties']['view.']['disableLanguageSelector'] === '1') {
264 return [];
265 }
266 $languages = array(
267 0 => isset($modSharedTSconfig['properties']['defaultLanguageLabel'])
268 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')'
269 : $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage')
270 );
271 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
272 $queryBuilder->getRestrictions()
273 ->removeAll()
274 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
275
276 if (!$this->getBackendUser()->isAdmin()) {
277 $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
278 }
279
280 $result = $queryBuilder->select('sys_language.uid', 'sys_language.title')
281 ->from('sys_language')
282 ->join(
283 'sys_language',
284 'pages_language_overlay',
285 'o',
286 $queryBuilder->expr()->eq('o.sys_language_uid', $queryBuilder->quoteIdentifier('sys_language.uid'))
287 )
288 ->where($queryBuilder->expr()->eq('o.pid', (int)$pageIdToShow))
289 ->groupBy('sys_language.uid', 'sys_language.title')
290 ->orderBy('sys_language.title')
291 ->execute();
292
293 while ($row = $result->fetch()) {
294 if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
295 $languages[$row['uid']] = $row['title'];
296 }
297 }
298 return $languages;
299 }
300
301 /**
302 * Gets the L parameter from the user session
303 *
304 * @return string
305 */
306 protected function getLanguageParameter()
307 {
308 $states = $this->getBackendUser()->uc['moduleData']['web_view']['States'];
309 $languages = $this->getPreviewLanguages();
310 $languageParameter = '';
311 if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
312 $languageParameter = '&L=' . (int)$states['languageSelectorValue'];
313 }
314 return $languageParameter;
315 }
316
317 /**
318 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
319 */
320 protected function getBackendUser()
321 {
322 return $GLOBALS['BE_USER'];
323 }
324
325 /**
326 * @return \TYPO3\CMS\Lang\LanguageService
327 */
328 protected function getLanguageService()
329 {
330 return $GLOBALS['LANG'];
331 }
332 }