|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WebVision\WvDeepltranslate\Event\Listener; |
| 6 | + |
| 7 | +use TYPO3\CMS\Core\Context\Context; |
| 8 | +use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
| 9 | +use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent; |
| 10 | +use WebVision\WvDeepltranslate\Hooks\DeeplPreviewFlagGeneratePageHook; |
| 11 | + |
| 12 | +/** |
| 13 | + * Event listener to render the frontend preview flag information. |
| 14 | + * |
| 15 | + * TYPO3 v12+ only and this is the counter-part of the {@see DeeplPreviewFlagGeneratePageHook} for older TYPO3 versions. |
| 16 | + * https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Breaking-97862-HooksRelatedToGeneratingPageContentRemoved.html |
| 17 | + */ |
| 18 | +final class RenderTranslatedFlagInFrontendPreviewMode |
| 19 | +{ |
| 20 | + public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void |
| 21 | + { |
| 22 | + $controller = $this->getTypoScriptFrontendController($event); |
| 23 | + $context = $controller->getContext(); |
| 24 | + if ( |
| 25 | + !$this->isInPreviewMode($context) |
| 26 | + || $this->processWorkspacePreview($context) |
| 27 | + || ($controller->config['config']['disablePreviewNotification'] ?? false) |
| 28 | + || ( |
| 29 | + isset($controller->page['tx_wvdeepltranslate_translated_time']) |
| 30 | + && $controller->page['tx_wvdeepltranslate_translated_time'] === 0 |
| 31 | + ) |
| 32 | + ) { |
| 33 | + // Preview flag must not be inserted. Return early. |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $messagePreviewLabel = ($controller->config['config']['deepl_message_preview'] ?? '') |
| 38 | + ?: 'Translated with DeepL'; |
| 39 | + |
| 40 | + $styles = []; |
| 41 | + $styles[] = 'position: fixed'; |
| 42 | + $styles[] = 'top: 65px'; |
| 43 | + $styles[] = 'right: 15px'; |
| 44 | + $styles[] = 'padding: 8px 18px'; |
| 45 | + $styles[] = 'background: #006494'; |
| 46 | + $styles[] = 'border: 1px solid #006494'; |
| 47 | + $styles[] = 'font-family: sans-serif'; |
| 48 | + $styles[] = 'font-size: 14px'; |
| 49 | + $styles[] = 'font-weight: bold'; |
| 50 | + $styles[] = 'color: #fff'; |
| 51 | + $styles[] = 'z-index: 20000'; |
| 52 | + $styles[] = 'user-select: none'; |
| 53 | + $styles[] = 'pointer-events: none'; |
| 54 | + $styles[] = 'text-align: center'; |
| 55 | + $styles[] = 'border-radius: 2px'; |
| 56 | + $message = '<div id="deepl-preview-info" style="' . implode(';', $styles) . '">' . htmlspecialchars($messagePreviewLabel) . '</div>'; |
| 57 | + |
| 58 | + $controller->content = str_ireplace('</body>', $message . '</body>', $controller->content); |
| 59 | + } |
| 60 | + |
| 61 | + private function isInPreviewMode(Context $context): bool |
| 62 | + { |
| 63 | + return $context->hasAspect('frontend.preview') |
| 64 | + && $context->getPropertyFromAspect('frontend.preview', 'isPreview', false); |
| 65 | + } |
| 66 | + |
| 67 | + private function processWorkspacePreview(Context $context): bool |
| 68 | + { |
| 69 | + return $context->hasAspect('workspace') |
| 70 | + && $context->getPropertyFromAspect('workspace', 'isOffline', false); |
| 71 | + } |
| 72 | + |
| 73 | + private function getTypoScriptFrontendController(AfterCacheableContentIsGeneratedEvent $event): TypoScriptFrontendController |
| 74 | + { |
| 75 | + return $event->getController(); |
| 76 | + } |
| 77 | +} |
0 commit comments