| Total Complexity | 51 |
| Total Lines | 363 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ViewModuleController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ViewModuleController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class ViewModuleController |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * ModuleTemplate object |
||
| 51 | * |
||
| 52 | * @var ModuleTemplate |
||
| 53 | */ |
||
| 54 | protected $moduleTemplate; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * View |
||
| 58 | * |
||
| 59 | * @var ViewInterface |
||
| 60 | */ |
||
| 61 | protected $view; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Initialize module template and language service |
||
| 65 | */ |
||
| 66 | public function __construct() |
||
| 67 | { |
||
| 68 | $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); |
||
| 69 | $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf'); |
||
| 70 | $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); |
||
| 71 | $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Initialize view |
||
| 76 | * |
||
| 77 | * @param string $templateName |
||
| 78 | */ |
||
| 79 | protected function initializeView(string $templateName) |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Register the doc header |
||
| 91 | |||
| 92 | * @param int $pageId |
||
| 93 | * @param int $languageId |
||
| 94 | * @param string $targetUrl |
||
| 95 | * @param string $route |
||
| 96 | */ |
||
| 97 | protected function registerDocHeader(int $pageId, int $languageId, string $targetUrl, string $route) |
||
| 98 | { |
||
| 99 | $languages = $this->getPreviewLanguages($pageId); |
||
| 100 | if (count($languages) > 1) { |
||
| 101 | $languageMenu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu(); |
||
| 102 | $languageMenu->setIdentifier('_langSelector'); |
||
| 103 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
| 104 | foreach ($languages as $value => $label) { |
||
| 105 | $href = (string)$uriBuilder->buildUriFromRoute( |
||
| 106 | 'web_ViewpageView', |
||
| 107 | [ |
||
| 108 | 'id' => $pageId, |
||
| 109 | 'language' => (int)$value |
||
| 110 | ] |
||
| 111 | ); |
||
| 112 | $menuItem = $languageMenu->makeMenuItem() |
||
| 113 | ->setTitle($label) |
||
| 114 | ->setHref($href); |
||
| 115 | if ($languageId === (int)$value) { |
||
| 116 | $menuItem->setActive(true); |
||
| 117 | } |
||
| 118 | $languageMenu->addMenuItem($menuItem); |
||
| 119 | } |
||
| 120 | $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($languageMenu); |
||
| 121 | } |
||
| 122 | |||
| 123 | $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar(); |
||
| 124 | $showButton = $buttonBar->makeLinkButton() |
||
| 125 | ->setHref($targetUrl) |
||
| 126 | ->setOnClick('window.open(this.href, \'newTYPO3frontendWindow\').focus();return false;') |
||
| 127 | ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.showPage')) |
||
| 128 | ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-page', Icon::SIZE_SMALL)); |
||
| 129 | $buttonBar->addButton($showButton); |
||
| 130 | |||
| 131 | $refreshButton = $buttonBar->makeLinkButton() |
||
| 132 | ->setHref('javascript:document.getElementById(\'tx_viewpage_iframe\').contentWindow.location.reload(true);') |
||
| 133 | ->setTitle($this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:refreshPage')) |
||
| 134 | ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-refresh', Icon::SIZE_SMALL)); |
||
| 135 | $buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT, 1); |
||
| 136 | |||
| 137 | // Shortcut |
||
| 138 | $shortcutButton = $buttonBar->makeShortcutButton() |
||
| 139 | ->setModuleName('web_ViewpageView') |
||
| 140 | ->setDisplayName($this->getShortcutTitle($pageId)) |
||
| 141 | ->setArguments([ |
||
| 142 | 'route' => $route, |
||
| 143 | 'id' => $pageId, |
||
| 144 | ]); |
||
| 145 | $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Show selected page from pagetree in iframe |
||
| 150 | * |
||
| 151 | * @param ServerRequestInterface $request |
||
| 152 | * @return ResponseInterface |
||
| 153 | * @throws \TYPO3\CMS\Core\Exception |
||
| 154 | */ |
||
| 155 | public function showAction(ServerRequestInterface $request): ResponseInterface |
||
| 156 | { |
||
| 157 | $pageId = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0); |
||
| 158 | |||
| 159 | $this->initializeView('show'); |
||
| 160 | $this->moduleTemplate->setBodyTag('<body class="typo3-module-viewpage">'); |
||
| 161 | $this->moduleTemplate->setModuleName('typo3-module-viewpage'); |
||
| 162 | $this->moduleTemplate->setModuleId('typo3-module-viewpage'); |
||
| 163 | |||
| 164 | if (!$this->isValidDoktype($pageId)) { |
||
| 165 | $flashMessage = GeneralUtility::makeInstance( |
||
| 166 | FlashMessage::class, |
||
| 167 | $this->getLanguageService()->getLL('noValidPageSelected'), |
||
| 168 | '', |
||
| 169 | FlashMessage::INFO |
||
| 170 | ); |
||
| 171 | return $this->renderFlashMessage($flashMessage); |
||
| 172 | } |
||
| 173 | |||
| 174 | $languageId = $this->getCurrentLanguage($pageId, $request->getParsedBody()['language'] ?? $request->getQueryParams()['language'] ?? null); |
||
| 175 | try { |
||
| 176 | $targetUrl = BackendUtility::getPreviewUrl( |
||
| 177 | $pageId, |
||
| 178 | '', |
||
| 179 | null, |
||
| 180 | '', |
||
| 181 | '', |
||
| 182 | $this->getTypeParameterIfSet($pageId) . '&L=' . $languageId |
||
| 183 | ); |
||
| 184 | } catch (UnableToLinkToPageException $e) { |
||
| 185 | $flashMessage = GeneralUtility::makeInstance( |
||
| 186 | FlashMessage::class, |
||
| 187 | $this->getLanguageService()->getLL('noSiteConfiguration'), |
||
| 188 | '', |
||
| 189 | FlashMessage::WARNING |
||
| 190 | ); |
||
| 191 | return $this->renderFlashMessage($flashMessage); |
||
| 192 | } |
||
| 193 | |||
| 194 | $this->registerDocHeader($pageId, $languageId, $targetUrl, $request->getQueryParams()['route']); |
||
| 195 | |||
| 196 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 197 | $icons = []; |
||
| 198 | $icons['orientation'] = $iconFactory->getIcon('actions-device-orientation-change', Icon::SIZE_SMALL)->render('inline'); |
||
| 199 | $icons['fullscreen'] = $iconFactory->getIcon('actions-fullscreen', Icon::SIZE_SMALL)->render('inline'); |
||
| 200 | $icons['expand'] = $iconFactory->getIcon('actions-expand', Icon::SIZE_SMALL)->render('inline'); |
||
| 201 | $icons['desktop'] = $iconFactory->getIcon('actions-device-desktop', Icon::SIZE_SMALL)->render('inline'); |
||
| 202 | $icons['tablet'] = $iconFactory->getIcon('actions-device-tablet', Icon::SIZE_SMALL)->render('inline'); |
||
| 203 | $icons['mobile'] = $iconFactory->getIcon('actions-device-mobile', Icon::SIZE_SMALL)->render('inline'); |
||
| 204 | $icons['unidentified'] = $iconFactory->getIcon('actions-device-unidentified', Icon::SIZE_SMALL)->render('inline'); |
||
| 205 | |||
| 206 | $current = ($this->getBackendUser()->uc['moduleData']['web_view']['States']['current'] ?: []); |
||
| 207 | $current['label'] = ($current['label'] ?? $this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:custom')); |
||
| 208 | $current['width'] = (isset($current['width']) && (int)$current['width'] >= 300 ? (int)$current['width'] : 320); |
||
| 209 | $current['height'] = (isset($current['height']) && (int)$current['height'] >= 300 ? (int)$current['height'] : 480); |
||
| 210 | |||
| 211 | $custom = ($this->getBackendUser()->uc['moduleData']['web_view']['States']['custom'] ?: []); |
||
| 212 | $custom['width'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 320); |
||
| 213 | $custom['height'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 480); |
||
| 214 | |||
| 215 | $this->view->assign('icons', $icons); |
||
| 216 | $this->view->assign('current', $current); |
||
| 217 | $this->view->assign('custom', $custom); |
||
| 218 | $this->view->assign('presetGroups', $this->getPreviewPresets($pageId)); |
||
| 219 | $this->view->assign('url', $targetUrl); |
||
| 220 | |||
| 221 | $this->moduleTemplate->setContent($this->view->render()); |
||
| 222 | return new HtmlResponse($this->moduleTemplate->renderContent()); |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function renderFlashMessage(FlashMessage $flashMessage): HtmlResponse |
||
| 226 | { |
||
| 227 | $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); |
||
| 228 | $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier(); |
||
| 229 | $defaultFlashMessageQueue->enqueue($flashMessage); |
||
| 230 | |||
| 231 | $this->moduleTemplate->setContent($this->view->render()); |
||
| 232 | return new HtmlResponse($this->moduleTemplate->renderContent()); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * With page TS config it is possible to force a specific type id via mod.web_view.type |
||
| 237 | * for a page id or a page tree. |
||
| 238 | * The method checks if a type is set for the given id and returns the additional GET string. |
||
| 239 | * |
||
| 240 | * @param int $pageId |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | protected function getTypeParameterIfSet(int $pageId): string |
||
| 244 | { |
||
| 245 | $typeParameter = ''; |
||
| 246 | $typeId = (int)(BackendUtility::getPagesTSconfig($pageId)['mod.']['web_view.']['type'] ?? 0); |
||
| 247 | if ($typeId > 0) { |
||
| 248 | $typeParameter = '&type=' . $typeId; |
||
| 249 | } |
||
| 250 | return $typeParameter; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get available presets for page id |
||
| 255 | * |
||
| 256 | * @param int $pageId |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected function getPreviewPresets(int $pageId): array |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the preview languages |
||
| 298 | * |
||
| 299 | * @param int $pageId |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function getPreviewLanguages(int $pageId): array |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns the current language |
||
| 331 | * |
||
| 332 | * @param int $pageId |
||
| 333 | * @param string $languageParam |
||
| 334 | * @return int |
||
| 335 | */ |
||
| 336 | protected function getCurrentLanguage(int $pageId, string $languageParam = null): int |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Verifies if doktype of given page is valid |
||
| 354 | * |
||
| 355 | * @param int $pageId |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | protected function isValidDoktype(int $pageId = 0): bool |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns the shortcut title for the current page |
||
| 377 | * |
||
| 378 | * @param int $pageId |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function getShortcutTitle(int $pageId): string |
||
| 382 | { |
||
| 383 | $pageTitle = ''; |
||
| 384 | $pageRow = BackendUtility::getRecord('pages', $pageId); |
||
| 385 | if ($pageRow !== []) { |
||
| 386 | $pageTitle = BackendUtility::getRecordTitle('pages', $pageRow); |
||
| 387 | } |
||
| 388 | return sprintf( |
||
| 389 | '%s: %s [%d]', |
||
| 390 | $this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tablabel'), |
||
| 391 | $pageTitle, |
||
| 392 | $pageId |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return BackendUserAuthentication |
||
| 398 | */ |
||
| 399 | protected function getBackendUser(): BackendUserAuthentication |
||
| 400 | { |
||
| 401 | return $GLOBALS['BE_USER']; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return LanguageService |
||
| 406 | */ |
||
| 407 | protected function getLanguageService(): LanguageService |
||
| 410 | } |
||
| 411 | } |
||
| 412 |