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