[TASK] Cleanup EXT:viewpage JavaScript
[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
19 /**
20 * Controller for viewing the frontend
21 *
22 * @author Felix Kopp <felix-source@phorax.com>
23 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
24 */
25 class ViewModuleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
26
27 /**
28 * Gets called before each action
29 *
30 * @return void
31 */
32 public function initializeAction() {
33 $GLOBALS['LANG']->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
34 $GLOBALS['TBE_TEMPLATE']->getPageRenderer()->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 }
46
47 /**
48 * Determine the url to view
49 *
50 * @return string
51 */
52 protected function getTargetUrl() {
53 $pageIdToShow = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
54 $adminCommand = $this->getAdminCommand($pageIdToShow);
55 $domainName = $this->getDomainName($pageIdToShow);
56 // Mount point overlay: Set new target page id and mp parameter
57 /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
58 $sysPage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
59 $sysPage->init(FALSE);
60 $mountPointMpParameter = '';
61 $finalPageIdToShow = $pageIdToShow;
62 $mountPointInformation = $sysPage->getMountPointInfo($pageIdToShow);
63 if ($mountPointInformation && $mountPointInformation['overlay']) {
64 // New page id
65 $finalPageIdToShow = $mountPointInformation['mount_pid'];
66 $mountPointMpParameter = '&MP=' . $mountPointInformation['MPvar'];
67 }
68 // Modify relative path to protocol with host if domain record is given
69 $protocolAndHost = '..';
70 if ($domainName) {
71 $protocol = 'http';
72 $page = (array)$sysPage->getPage($finalPageIdToShow);
73 if ($page['url_scheme'] == 2 || $page['url_scheme'] == 0 && \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL')) {
74 $protocol = 'https';
75 }
76 $protocolAndHost = $protocol . '://' . $domainName;
77 }
78 $url = $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand;
79 return $url;
80 }
81
82 /**
83 * Get admin command
84 *
85 * @param int $pageId
86 * @return string
87 */
88 protected function getAdminCommand($pageId) {
89 // The page will show only if there is a valid page and if this page may be viewed by the user
90 $pageinfo = BackendUtility::readPageAccess($pageId, $GLOBALS['BE_USER']->getPagePermsClause(1));
91 $addCommand = '';
92 if (is_array($pageinfo)) {
93 $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
94 }
95 return $addCommand;
96 }
97
98 /**
99 * With page TS config it is possible to force a specific type id via mod.web_view.type
100 * for a page id or a page tree.
101 * The method checks if a type is set for the given id and returns the additional GET string.
102 *
103 * @param int $pageId
104 * @return string
105 */
106 protected function getTypeParameterIfSet($pageId) {
107 $typeParameter = '';
108 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
109 $typeId = (int)$modTSconfig['properties']['type'];
110 if ($typeId > 0) {
111 $typeParameter = '&type=' . $typeId;
112 }
113 return $typeParameter;
114 }
115
116 /**
117 * Get domain name for requested page id
118 *
119 * @param int $pageId
120 * @return string|NULL Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
121 */
122 protected function getDomainName($pageId) {
123 $previewDomainConfig = $GLOBALS['BE_USER']->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
124 if ($previewDomainConfig['value']) {
125 $domain = $previewDomainConfig['value'];
126 } else {
127 $domain = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($pageId));
128 }
129 return $domain;
130 }
131
132 /**
133 * Get available widths for preview frame
134 *
135 * @return array
136 */
137 protected function getPreviewFrameWidths() {
138 $pageId = (int)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id');
139 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
140 $widths = array(
141 '100%|100%' => $GLOBALS['LANG']->getLL('autoSize')
142 );
143 if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
144 foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf ){
145 $label = '';
146
147 $width = substr($item, 0, -1);
148 $data = array('width' => $width);
149 $label .= $width . 'px ';
150
151 //if height is set
152 if (isset($conf['height'])) {
153 $label .= ' × ' . $conf['height'] . 'px ';
154 $data['height'] = $conf['height'];
155 }
156
157 if (substr($conf['label'], 0, 4) !== 'LLL:') {
158 $label .= $conf['label'];
159 } else {
160 $label .= $GLOBALS['LANG']->sL(trim($conf['label']));
161 }
162 $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
163 $widths[$value] = $label;
164 }
165 }
166 return $widths;
167 }
168
169 }