‪TYPO3CMS  9.5
ViewModuleController.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
38 
44 {
50  protected ‪$moduleTemplate;
51 
57  protected ‪$view;
58 
62  public function ‪__construct()
63  {
64  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
65  $this->‪getLanguageService()->‪includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
66  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
67  $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
68  }
69 
75  protected function ‪initializeView(string $templateName)
76  {
77  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
78  $this->view->getRequest()->setControllerExtensionName('Viewpage');
79  $this->view->setTemplate($templateName);
80  $this->view->setTemplateRootPaths(['EXT:viewpage/Resources/Private/Templates/ViewModule']);
81  $this->view->setPartialRootPaths(['EXT:viewpage/Resources/Private/Partials']);
82  $this->view->setLayoutRootPaths(['EXT:viewpage/Resources/Private/Layouts']);
83  }
84 
92  protected function ‪registerDocHeader(int $pageId, int $languageId, string $targetUrl)
93  {
94  $languages = $this->‪getPreviewLanguages($pageId);
95  if (count($languages) > 1) {
96  $languageMenu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
97  $languageMenu->setIdentifier('_langSelector');
99  $uriBuilder = GeneralUtility::makeInstance(\‪TYPO3\CMS\Backend\Routing\UriBuilder::class);
100  foreach ($languages as $value => $label) {
101  $href = (string)$uriBuilder->buildUriFromRoute(
102  'web_ViewpageView',
103  [
104  'id' => $pageId,
105  'language' => (int)$value
106  ]
107  );
108  $menuItem = $languageMenu->makeMenuItem()
109  ->setTitle($label)
110  ->setHref($href);
111  if ($languageId === (int)$value) {
112  $menuItem->setActive(true);
113  }
114  $languageMenu->addMenuItem($menuItem);
115  }
116  $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($languageMenu);
117  }
118 
119  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
120  $showButton = $buttonBar->makeLinkButton()
121  ->setHref($targetUrl)
122  ->setOnClick('window.open(this.href, \'newTYPO3frontendWindow\').focus();return false;')
123  ->setTitle($this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
124  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-page', ‪Icon::SIZE_SMALL));
125  $buttonBar->addButton($showButton);
126 
127  $refreshButton = $buttonBar->makeLinkButton()
128  ->setHref('javascript:document.getElementById(\'tx_viewpage_iframe\').contentWindow.location.reload(true);')
129  ->setTitle($this->‪getLanguageService()->‪sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:refreshPage'))
130  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-refresh', ‪Icon::SIZE_SMALL));
131  $buttonBar->addButton($refreshButton, ‪ButtonBar::BUTTON_POSITION_RIGHT, 1);
132 
133  // Shortcut
134  $mayMakeShortcut = $this->‪getBackendUser()->‪mayMakeShortcut();
135  if ($mayMakeShortcut) {
136  $getVars = ['id', 'route'];
137 
138  $shortcutButton = $buttonBar->makeShortcutButton()
139  ->setModuleName('web_ViewpageView')
140  ->setGetVariables($getVars);
141  $buttonBar->addButton($shortcutButton, ‪ButtonBar::BUTTON_POSITION_RIGHT);
142  }
143  }
144 
152  public function ‪showAction(ServerRequestInterface $request): ResponseInterface
153  {
154  $pageId = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
155 
156  $this->‪initializeView('show');
157  $this->moduleTemplate->setBodyTag('<body class="typo3-module-viewpage">');
158  $this->moduleTemplate->setModuleName('typo3-module-viewpage');
159  $this->moduleTemplate->setModuleId('typo3-module-viewpage');
160 
161  if (!$this->‪isValidDoktype($pageId)) {
162  $flashMessage = GeneralUtility::makeInstance(
163  FlashMessage::class,
164  $this->‪getLanguageService()->getLL('noValidPageSelected'),
165  '',
167  );
168  $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
169  $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
170  $defaultFlashMessageQueue->enqueue($flashMessage);
171  } else {
172  $languageId = $this->‪getCurrentLanguage($pageId, $request->getParsedBody()['language'] ?? $request->getQueryParams()['language'] ?? null);
174  $pageId,
175  '',
176  null,
177  '',
178  '',
179  $this->‪getTypeParameterIfSet($pageId) . '&L=' . $languageId
180  );
181  $this->‪registerDocHeader($pageId, $languageId, $targetUrl);
182 
183  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
184  ‪$icons = [];
185  ‪$icons['orientation'] = $iconFactory->getIcon('actions-device-orientation-change', ‪Icon::SIZE_SMALL)->render('inline');
186  ‪$icons['fullscreen'] = $iconFactory->getIcon('actions-fullscreen', ‪Icon::SIZE_SMALL)->render('inline');
187  ‪$icons['expand'] = $iconFactory->getIcon('actions-expand', ‪Icon::SIZE_SMALL)->render('inline');
188  ‪$icons['desktop'] = $iconFactory->getIcon('actions-device-desktop', ‪Icon::SIZE_SMALL)->render('inline');
189  ‪$icons['tablet'] = $iconFactory->getIcon('actions-device-tablet', ‪Icon::SIZE_SMALL)->render('inline');
190  ‪$icons['mobile'] = $iconFactory->getIcon('actions-device-mobile', ‪Icon::SIZE_SMALL)->render('inline');
191  ‪$icons['unidentified'] = $iconFactory->getIcon('actions-device-unidentified', ‪Icon::SIZE_SMALL)->render('inline');
192 
193  $current = ($this->‪getBackendUser()->uc['moduleData']['web_view']['States']['current'] ?: []);
194  $current['label'] = ($current['label'] ?? $this->‪getLanguageService()->‪sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:custom'));
195  $current['width'] = (isset($current['width']) && (int)$current['width'] >= 300 ? (int)$current['width'] : 320);
196  $current['height'] = (isset($current['height']) && (int)$current['height'] >= 300 ? (int)$current['height'] : 480);
197 
198  $custom = ($this->‪getBackendUser()->uc['moduleData']['web_view']['States']['custom'] ?: []);
199  $custom['width'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 320);
200  $custom['height'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 480);
201 
202  $this->view->assign('icons', ‪$icons);
203  $this->view->assign('current', $current);
204  $this->view->assign('custom', $custom);
205  $this->view->assign('presetGroups', $this->‪getPreviewPresets($pageId));
206  $this->view->assign('url', $targetUrl);
207  }
208 
209  $this->moduleTemplate->setContent($this->view->render());
210  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
211  }
212 
221  protected function ‪getTypeParameterIfSet(int $pageId): string
222  {
223  $typeParameter = '';
224  $typeId = (int)(‪BackendUtility::getPagesTSconfig($pageId)['mod.']['web_view.']['type'] ?? 0);
225  if ($typeId > 0) {
226  $typeParameter = '&type=' . $typeId;
227  }
228  return $typeParameter;
229  }
230 
237  protected function ‪getDomainName(int $pageId)
238  {
239  $previewDomainConfig = ‪BackendUtility::getPagesTSconfig($pageId)['TCEMAIN.']['previewDomain'] ?? '';
240  return $previewDomainConfig ?: ‪BackendUtility::firstDomainRecord(‪BackendUtility::BEgetRootLine($pageId));
241  }
242 
249  protected function ‪getPreviewPresets(int $pageId): array
250  {
251  $presetGroups = [
252  'desktop' => [],
253  'tablet' => [],
254  'mobile' => [],
255  'unidentified' => []
256  ];
257  $previewFrameWidthConfig = ‪BackendUtility::getPagesTSconfig($pageId)['mod.']['web_view.']['previewFrameWidths.'] ?? [];
258  foreach ($previewFrameWidthConfig as $item => $conf) {
259  $data = [
260  'key' => substr($item, 0, -1),
261  'label' => $conf['label'] ?? null,
262  'type' => $conf['type'] ?? 'unknown',
263  'width' => (isset($conf['width']) && (int)$conf['width'] > 0 && strpos($conf['width'], '%') === false) ? (int)$conf['width'] : null,
264  'height' => (isset($conf['height']) && (int)$conf['height'] > 0 && strpos($conf['height'], '%') === false) ? (int)$conf['height'] : null,
265  ];
266  $width = (int)substr($item, 0, -1);
267  if (!isset($data['width']) && $width > 0) {
268  $data['width'] = $width;
269  }
270  if (!isset($data['label'])) {
271  $data['label'] = $data['key'];
272  } elseif (strpos($data['label'], 'LLL:') === 0) {
273  $data['label'] = $this->‪getLanguageService()->‪sL(trim($data['label']));
274  }
275 
276  if (array_key_exists($data['type'], $presetGroups)) {
277  $presetGroups[$data['type']][$data['key']] = $data;
278  } else {
279  $presetGroups['unidentified'][$data['key']] = $data;
280  }
281  }
282 
283  return $presetGroups;
284  }
285 
292  protected function ‪getPreviewLanguages(int $pageId): array
293  {
294  $localizationParentField = ‪$GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'];
295  $languageField = ‪$GLOBALS['TCA']['pages']['ctrl']['languageField'];
296  $modSharedTSconfig = ‪BackendUtility::getPagesTSconfig($pageId)['mod.']['SHARED.'] ?? [];
297  if ($modSharedTSconfig['view.']['disableLanguageSelector'] === '1') {
298  return [];
299  }
300  $languages = [
301  0 => isset($modSharedTSconfig['defaultLanguageLabel'])
302  ? $modSharedTSconfig['defaultLanguageLabel'] . ' (' . $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage') . ')'
303  : $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage')
304  ];
305  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
306  $queryBuilder->getRestrictions()
307  ->removeAll()
308  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
309 
310  if (!$this->‪getBackendUser()->isAdmin()) {
311  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
312  }
313 
314  $result = $queryBuilder->select('sys_language.uid', 'sys_language.title')
315  ->from('sys_language')
316  ->join(
317  'sys_language',
318  'pages',
319  'o',
320  $queryBuilder->expr()->eq('o.' . $languageField, $queryBuilder->quoteIdentifier('sys_language.uid'))
321  )
322  ->where(
323  $queryBuilder->expr()->eq(
324  'o.' . $localizationParentField,
325  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
326  )
327  )
328  ->groupBy('sys_language.uid', 'sys_language.title', 'sys_language.sorting')
329  ->orderBy('sys_language.sorting')
330  ->execute();
331 
332  while ($row = $result->fetch()) {
333  if ($this->‪getBackendUser()->checkLanguageAccess($row['uid'])) {
334  $languages[$row['uid']] = $row['title'];
335  }
336  }
337  return $languages;
338  }
339 
347  protected function ‪getCurrentLanguage(int $pageId, string $languageParam = null): int
348  {
349  $languageId = (int)$languageParam;
350  if ($languageParam === null) {
351  $states = $this->‪getBackendUser()->uc['moduleData']['web_view']['States'];
352  $languages = $this->‪getPreviewLanguages($pageId);
353  if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
354  $languageId = (int)$states['languageSelectorValue'];
355  }
356  } else {
357  $this->‪getBackendUser()->uc['moduleData']['web_view']['States']['languageSelectorValue'] = $languageId;
358  $this->‪getBackendUser()->‪writeUC($this->‪getBackendUser()->uc);
359  }
360  return $languageId;
361  }
362 
369  protected function ‪isValidDoktype(int $pageId = 0): bool
370  {
371  if ($pageId === 0) {
372  return false;
373  }
374 
375  $page = ‪BackendUtility::getRecord('pages', $pageId);
376  $pageType = (int)$page['doktype'] ?? 0;
377 
378  return $page !== null
379  && $pageType !== ‪PageRepository::DOKTYPE_SPACER
380  && $pageType !== ‪PageRepository::DOKTYPE_SYSFOLDER
381  && $pageType !== ‪PageRepository::DOKTYPE_RECYCLER;
382  }
383 
387  protected function ‪getBackendUser(): ‪BackendUserAuthentication
388  {
389  return ‪$GLOBALS['BE_USER'];
390  }
391 
395  protected function ‪getLanguageService(): ‪LanguageService
396  {
397  return ‪$GLOBALS['LANG'];
398  }
399 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:25
‪TYPO3\CMS\Core\Localization\LanguageService\includeLLFile
‪mixed includeLLFile($fileRef, $setGlobal=true, $mergeLocalOntoDefault=false)
Definition: LanguageService.php:260
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:31
‪TYPO3\CMS\Viewpage\Controller
Definition: ViewModuleController.php:3
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\writeUC
‪writeUC($variable='')
Definition: AbstractUserAuthentication.php:1172
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getTypeParameterIfSet
‪string getTypeParameterIfSet(int $pageId)
Definition: ViewModuleController.php:219
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\__construct
‪__construct()
Definition: ViewModuleController.php:60
‪TYPO3
‪TYPO3\CMS\Frontend\Page\PageRepository\DOKTYPE_SPACER
‪const DOKTYPE_SPACER
Definition: PageRepository.php:173
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\initializeView
‪initializeView(string $templateName)
Definition: ViewModuleController.php:73
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\isValidDoktype
‪bool isValidDoktype(int $pageId=0)
Definition: ViewModuleController.php:367
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Backend\Utility\BackendUtility\BEgetRootLine
‪static array BEgetRootLine($uid, $clause='', $workspaceOL=false, array $additionalFields=[])
Definition: BackendUtility.php:374
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPreviewUrl
‪static string getPreviewUrl( $pageUid, $backPath='', $rootLine=null, $anchorSection='', $alternativeUrl='', $additionalGetVars='', &$switchFocus=true)
Definition: BackendUtility.php:2656
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\showAction
‪ResponseInterface showAction(ServerRequestInterface $request)
Definition: ViewModuleController.php:150
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\mayMakeShortcut
‪bool mayMakeShortcut()
Definition: BackendUserAuthentication.php:939
‪TYPO3\CMS\Backend\Utility\BackendUtility\firstDomainRecord
‪static string null firstDomainRecord($rootLine)
Definition: BackendUtility.php:3623
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getBackendUser
‪BackendUserAuthentication getBackendUser()
Definition: ViewModuleController.php:385
‪TYPO3\CMS\Frontend\Page\PageRepository\DOKTYPE_SYSFOLDER
‪const DOKTYPE_SYSFOLDER
Definition: PageRepository.php:174
‪TYPO3\CMS\Frontend\Page\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getLanguageService
‪LanguageService getLanguageService()
Definition: ViewModuleController.php:393
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\$view
‪ViewInterface $view
Definition: ViewModuleController.php:55
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:21
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
Definition: BackendUtility.php:864
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Frontend\Page\PageRepository\DOKTYPE_RECYCLER
‪const DOKTYPE_RECYCLER
Definition: PageRepository.php:175
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getPreviewPresets
‪array getPreviewPresets(int $pageId)
Definition: ViewModuleController.php:247
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ViewModuleController.php:49
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_RIGHT
‪const BUTTON_POSITION_RIGHT
Definition: ButtonBar.php:40
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController
Definition: ViewModuleController.php:44
‪$icons
‪$icons
Definition: ext_localconf.php:77
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getPreviewLanguages
‪array getPreviewLanguages(int $pageId)
Definition: ViewModuleController.php:290
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:25
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\registerDocHeader
‪registerDocHeader(int $pageId, int $languageId, string $targetUrl)
Definition: ViewModuleController.php:90
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getCurrentLanguage
‪int getCurrentLanguage(int $pageId, string $languageParam=null)
Definition: ViewModuleController.php:345
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:25
‪TYPO3\CMS\Viewpage\Controller\ViewModuleController\getDomainName
‪string null getDomainName(int $pageId)
Definition: ViewModuleController.php:235