diff --git a/Build/phpstan/Core12/phpstan-baseline.neon b/Build/phpstan/Core12/phpstan-baseline.neon index 9a0f88c0..5a3e531b 100644 --- a/Build/phpstan/Core12/phpstan-baseline.neon +++ b/Build/phpstan/Core12/phpstan-baseline.neon @@ -145,11 +145,6 @@ parameters: count: 1 path: ../../../Classes/Service/DeeplGlossaryService.php - - - message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException\\|Doctrine\\\\DBAL\\\\Driver\\\\Exception\\|Doctrine\\\\DBAL\\\\Exception\\|TYPO3\\\\CMS\\\\Core\\\\Exception\\\\SiteNotFoundException is not subtype of Throwable$#" - count: 1 - path: ../../../Classes/Service/DeeplGlossaryService.php - - message: "#^Offset 'pid' does not exist on array\\|null\\.$#" count: 1 diff --git a/Classes/Controller/GlossarySyncController.php b/Classes/Controller/GlossarySyncController.php index f007f0ce..4918feee 100644 --- a/Classes/Controller/GlossarySyncController.php +++ b/Classes/Controller/GlossarySyncController.php @@ -5,12 +5,14 @@ namespace WebVision\WvDeepltranslate\Controller; use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Backend\Utility\BackendUtility; +use TYPO3\CMS\Core\Domain\Repository\PageRepository; use TYPO3\CMS\Core\Exception; use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Messaging\FlashMessageService; -use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; +use WebVision\WvDeepltranslate\Exception\GlossaryCanNotCreatedException; use WebVision\WvDeepltranslate\Exception\InvalidArgumentException; use WebVision\WvDeepltranslate\Service\DeeplGlossaryService; @@ -18,10 +20,14 @@ class GlossarySyncController { protected DeeplGlossaryService $deeplGlossaryService; + private FlashMessageService $flashMessageService; + public function __construct( - DeeplGlossaryService $deeplGlossaryService + DeeplGlossaryService $deeplGlossaryService, + FlashMessageService $flashMessageService ) { $this->deeplGlossaryService = $deeplGlossaryService; + $this->flashMessageService = $flashMessageService; } /** @@ -33,36 +39,49 @@ public function update(ServerRequestInterface $request): RedirectResponse $processingParameters = $request->getQueryParams(); if (!isset($processingParameters['uid'])) { - throw new InvalidArgumentException( - 'No ID given for glossary synchronization', - 1676935668643 - ); + $this->flashMessageService + ->getMessageQueueByIdentifier() + ->enqueue((new FlashMessage( + 'No ID given for glossary synchronization', + '', + 2, + true + ))); + return new RedirectResponse($processingParameters['returnUrl']); } - $this->deeplGlossaryService->syncGlossaries((int)$processingParameters['uid']); - - if ((new \TYPO3\CMS\Core\Information\Typo3Version())->getMajorVersion() >= 12) { - $severity = \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK; - } else { - $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK; + // Check page configuration of glossary type + /** @var array{uid: int, doktype: string|int, module: string} $pages */ + $pages = BackendUtility::getRecord('pages', (int)$processingParameters['uid']); + if ((int)$pages['doktype'] !== PageRepository::DOKTYPE_SYSFOLDER && $pages['module'] !== 'glossary') { + $this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage( + sprintf('Page "%d" not configured for glossary synchronization.', $pages['uid']), + (string)LocalizationUtility::translate( + 'glossary.sync.title.invalid', + 'wv_deepltranslate' + ), + 2, + true + )); + return new RedirectResponse($processingParameters['returnUrl']); } - $flashMessage = GeneralUtility::makeInstance( - FlashMessage::class, - (string)LocalizationUtility::translate( - 'glossary.sync.message', - 'wv_deepltranslate' - ), - (string)LocalizationUtility::translate( - 'glossary.sync.title', - 'wv_deepltranslate' - ), - $severity, - true - ); - GeneralUtility::makeInstance(FlashMessageService::class) - ->getMessageQueueByIdentifier() - ->enqueue($flashMessage); + try { + $this->deeplGlossaryService->syncGlossaries((int)$processingParameters['uid']); + $this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage( + (string)LocalizationUtility::translate('glossary.sync.message', 'wv_deepltranslate'), + (string)LocalizationUtility::translate('glossary.sync.title', 'wv_deepltranslate'), + 0, // OK + true + )); + } catch (GlossaryCanNotCreatedException $exception) { + $this->flashMessageService->getMessageQueueByIdentifier()->enqueue(new FlashMessage( + (string)LocalizationUtility::translate('glossary.sync.message.invalid', 'wv_deepltranslate'), + (string)LocalizationUtility::translate('glossary.sync.title.invalid', 'wv_deepltranslate'), + 2, // Error + true + )); + } return new RedirectResponse($processingParameters['returnUrl']); } diff --git a/Classes/Exception/GlossaryCanNotCreatedException.php b/Classes/Exception/GlossaryCanNotCreatedException.php new file mode 100644 index 00000000..dc3c65f4 --- /dev/null +++ b/Classes/Exception/GlossaryCanNotCreatedException.php @@ -0,0 +1,10 @@ +glossaryRepository - ->getGlossaryInformationForSync($uid); + $glossaries = $this->glossaryRepository->getGlossaryInformationForSync($uid); + if (empty($glossaries)) { + throw new GlossaryCanNotCreatedException( + 'Glossary can not created, the TYPO3 information are invalide.', + 1714987594661 + ); + } foreach ($glossaries as $glossaryInformation) { if ($glossaryInformation['glossary_id'] !== '') { diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 76627e33..903ea2f1 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -115,17 +115,23 @@ Create new Glossary translation + + DeepL sync ready + Synchronization with DeepL is done. Your glossaries are ready to use. - - DeepL Sync ready + + DeepL glossary sync invalid - - Glossary synchronization with DeepL is pending. Your glossaries are not operational. + + Glossary synchronization with DeepL error. Your glossaries has no entries or is invalid. - DeepL Glossary is out of Sync + DeepL glossary is out of sync + + + Glossary synchronization with DeepL is pending. Your glossaries are not operational.