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