|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WebVision\DeeplWrite\Controller; |
| 6 | + |
| 7 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use Psr\Http\Message\ServerRequestInterface; |
| 10 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 11 | +use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory; |
| 12 | +use TYPO3\CMS\Fluid\View\StandaloneView; |
| 13 | +use WebVision\DeeplWrite\Configuration\ConfigurationInterface; |
| 14 | +use WebVision\DeeplWrite\Domain\Enum\RephraseToneDeepL; |
| 15 | +use WebVision\DeeplWrite\Domain\Enum\RephraseWritingStyleDeepL; |
| 16 | +use WebVision\DeeplWrite\Service\DeeplService; |
| 17 | +use WebVision\DeeplWrite\Service\HtmlParser; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + * This class is meant to be used within the DeepL write extension and therefore |
| 22 | + * no public API. Endpoints can change without further information. |
| 23 | + */ |
| 24 | +final class CkEditorController |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly ResponseFactoryInterface $responseFactory, |
| 28 | + private readonly ConfigurationInterface $configuration, |
| 29 | + private readonly DeeplService $deeplService, |
| 30 | + private readonly HtmlParser $htmlParser, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function deeplConfiguredAction(ServerRequestInterface $request): ResponseInterface |
| 35 | + { |
| 36 | + $configured = true; |
| 37 | + if ($this->configuration->getApiKey() === '') { |
| 38 | + $configured = false; |
| 39 | + } |
| 40 | + $response = $this->responseFactory->createResponse() |
| 41 | + ->withHeader('Content-Type', 'application/json; charset=utf-8'); |
| 42 | + $response->getBody()->write( |
| 43 | + json_encode(['configured' => $configured], JSON_THROW_ON_ERROR), |
| 44 | + ); |
| 45 | + return $response; |
| 46 | + } |
| 47 | + |
| 48 | + public function optimizeTextAction(ServerRequestInterface $request): ResponseInterface |
| 49 | + { |
| 50 | + $data = $request->getParsedBody(); |
| 51 | + $splittedText = $this->htmlParser->splitHtml($data['text']); |
| 52 | + foreach ($splittedText as $node => $text) { |
| 53 | + $optimizedText = $this->deeplService->rephraseText( |
| 54 | + $data['text'], |
| 55 | + null, |
| 56 | + RephraseWritingStyleDeepL::tryFrom($data['style']), |
| 57 | + RephraseToneDeepL::tryFrom($data['tone']) |
| 58 | + ); |
| 59 | + $splittedText[$node] = $optimizedText; |
| 60 | + } |
| 61 | + $response = $this->responseFactory->createResponse() |
| 62 | + ->withHeader('Content-Type', 'application/json; charset=utf-8'); |
| 63 | + $response->getBody()->write( |
| 64 | + json_encode(['result' => $this->htmlParser->buildHtml($splittedText)], JSON_THROW_ON_ERROR), |
| 65 | + ); |
| 66 | + return $response; |
| 67 | + } |
| 68 | + |
| 69 | + public function getEditMaskAction(ServerRequestInterface $request): ResponseInterface |
| 70 | + { |
| 71 | + $renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create( |
| 72 | + templatePathsArray: [ |
| 73 | + 'templateRootPaths' => ['EXT:deepl_write/Resources/Private/Backend/Templates/'], |
| 74 | + ] |
| 75 | + ); |
| 76 | + $renderingContext->setRequest($request); |
| 77 | + $renderingContext->setControllerAction('Edit'); |
| 78 | + $renderingContext->setControllerName('CkEditor'); |
| 79 | + $view = GeneralUtility::makeInstance(StandaloneView::class, $renderingContext); |
| 80 | + $view->assignMultiple([ |
| 81 | + 'styles' => RephraseWritingStyleDeepL::cases(), |
| 82 | + 'tones' => RephraseToneDeepL::cases(), |
| 83 | + ]); |
| 84 | + $response = $this->responseFactory->createResponse() |
| 85 | + ->withHeader('Content-Type', 'text/html; charset=utf-8'); |
| 86 | + $response->getBody()->write($view->render()); |
| 87 | + return $response; |
| 88 | + } |
| 89 | +} |
0 commit comments