From f4f4725aacb8e2990617a5e1c5fe6cbfc4209672 Mon Sep 17 00:00:00 2001 From: thisismeonmounteverest Date: Mon, 30 Oct 2023 21:10:47 +0100 Subject: [PATCH] Search in translation text as well. --- .../Admin/TranslationController.php | 25 +++++++++++-------- src/Model/TranslationModel.php | 15 +++++------ src/Pagerfanta/TranslationAdapter.php | 1 + 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Controller/Admin/TranslationController.php b/src/Controller/Admin/TranslationController.php index c051399e9e..3aebdfcb24 100644 --- a/src/Controller/Admin/TranslationController.php +++ b/src/Controller/Admin/TranslationController.php @@ -18,6 +18,7 @@ use App\Utilities\TranslatedFlashTrait; use App\Utilities\TranslatorTrait; use DateTime; +use Doctrine\ORM\EntityManagerInterface; use Exception; use MessageFormatter; use Pagerfanta\Pagerfanta; @@ -31,6 +32,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Contracts\Translation\TranslatorInterface; /** * Class TranslationController. @@ -44,11 +46,13 @@ class TranslationController extends AbstractController private TranslationModel $translationModel; private string $enabledLocales; + private EntityManagerInterface $entityManager; - public function __construct(TranslationModel $translationModel, string $locales) + public function __construct(TranslationModel $translationModel, EntityManagerInterface $entityManager, string $locales) { $this->translationModel = $translationModel; $this->enabledLocales = $locales; + $this->entityManager = $entityManager; } /** @@ -85,7 +89,7 @@ public function editTranslation( $request->getSession()->set('originalReferrer', $request->headers->get('referer')); } - $translationRepository = $this->getDoctrine()->getRepository(Word::class); + $translationRepository = $this->entityManager->getRepository(Word::class); /** @var Word $original */ $original = $translationRepository->findOneBy([ 'code' => $code, @@ -120,7 +124,7 @@ public function editTranslation( if ('' === $invalidMessage) { $originalDomain = $translation->getDomain(); - $em = $this->getDoctrine()->getManager(); + $em = $this->entityManager; // Make sure the ID of the translations match $translation->setCode($original->getCode()); $translation->setDomain($data->domain); @@ -246,7 +250,7 @@ public function createTranslationForId( $createForm->handleRequest($request); if ($createForm->isSubmitted() && $createForm->isValid()) { - $em = $this->getDoctrine()->getManager(); + $em = $this->entityManager; /** @var TranslationRequest $data */ $data = $createForm->getData(); $invalidMessage = $this->checkIfICUFormatIsValid($data, $data->englishText); @@ -319,7 +323,7 @@ public function createTranslationDirect( $createForm->handleRequest($request); if ($createForm->isSubmitted() && $createForm->isValid()) { - $em = $this->getDoctrine()->getManager(); + $em = $this->entityManager; /** @var TranslationRequest $data */ $data = $createForm->getData(); $invalidMessage = $this->checkIfICUFormatIsValid($data, $data->englishText); @@ -417,7 +421,6 @@ public function addTranslation(Request $request, Language $language, $code) $data = $addForm->getData(); $invalidMessage = $this->checkIfICUFormatIsValid($data, $data->translatedText); if ('' === $invalidMessage) { - $em = $this->getDoctrine()->getManager(); $translation->setDomain($original->getDomain()); $translation->setSentence($data->translatedText); $translation->setLanguage($language); @@ -425,8 +428,8 @@ public function addTranslation(Request $request, Language $language, $code) $translation->setAuthor($translator); // No need for a description as the English original has one $translation->setDescription(''); - $em->persist($translation); - $em->flush(); + $this->entityManager->persist($translation); + $this->entityManager->flush(); $this->translationModel->refreshTranslationsCacheForLocale($language->getShortCode()); $this->addTranslatedFlash('notice', 'translation.add', [ 'translationId' => $original->getCode(), @@ -568,7 +571,7 @@ public function listTranslations(Request $request, Language $language, $type, $c $translations->setCurrentPage($page); /** @var WordRepository $translationRepository */ - $translationRepository = $this->getDoctrine()->getRepository(Word::class); + $translationRepository = $this->entityManager->getRepository(Word::class); $countAll = $translationRepository->getTranslatableItemsCount('en'); $countTranslated = $translationRepository->getTranslatableItemsCount($language->getShortCode()); @@ -597,10 +600,10 @@ public function listTranslations(Request $request, Language $language, $type, $c * * @return Response */ - public function statistics(Request $request) + public function statistics(Request $request, EntityManagerInterface $entityManager) { /** @var WordRepository $translationRepository */ - $translationRepository = $this->getDoctrine()->getRepository(Word::class); + $translationRepository = $entityManager->getRepository(Word::class); $countAll = $translationRepository->getTranslatableItemsCount('en'); $translationDetails = $translationRepository->getTranslationDetails($this->enabledLocales); diff --git a/src/Model/TranslationModel.php b/src/Model/TranslationModel.php index abd7ba9c3f..03ba77a5e8 100644 --- a/src/Model/TranslationModel.php +++ b/src/Model/TranslationModel.php @@ -9,6 +9,7 @@ use App\Pagerfanta\TranslationAdapter; use App\Pagerfanta\UpdateTranslationAdapter; use App\Utilities\ManagerTrait; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; @@ -16,8 +17,6 @@ class TranslationModel { - use ManagerTrait; - /** @var TranslatorInterface */ private $translator; @@ -29,9 +28,11 @@ class TranslationModel /** @var string */ private $locales; + private EntityManagerInterface $entityManager; public function __construct( TranslatorInterface $translator, + EntityManagerInterface $entityManager, Filesystem $filesystem, string $cacheDirectory, string $locales @@ -40,6 +41,7 @@ public function __construct( $this->cacheDirectory = $cacheDirectory; $this->filesystem = $filesystem; $this->locales = $locales; + $this->entityManager = $entityManager; } /** @@ -64,7 +66,7 @@ public function refreshTranslationsCache(): void public function getAdapter($type, $locale, $code) { $translationAdapter = null; - $connection = $this->getManager()->getConnection(); + $connection = $this->entityManager->getConnection(); switch ($type) { case 'missing': @@ -89,15 +91,14 @@ public function getAdapter($type, $locale, $code) public function updateDomainOfTranslations(Word $updatedTranslation) { - $em = $this->getManager(); - $translationRepository = $em->getRepository(Word::class); + $translationRepository = $this->entityManager->getRepository(Word::class); $translations = $translationRepository->findBy(['code' => $updatedTranslation->getCode()]); foreach ($translations as $translation) { $translation->setDomain($updatedTranslation->getDomain()); - $em->persist($translation); + $this->entityManager->persist($translation); } - $em->flush(); + $this->entityManager->flush(); } private function removeAndWarmupCache(string $locale): void diff --git a/src/Pagerfanta/TranslationAdapter.php b/src/Pagerfanta/TranslationAdapter.php index ee8b38e609..ee3a92ffae 100644 --- a/src/Pagerfanta/TranslationAdapter.php +++ b/src/Pagerfanta/TranslationAdapter.php @@ -48,6 +48,7 @@ public function __construct(Connection $connection, string $locale, string $code "; if (!empty($code)) { $this->query .= " WHERE (pi_lang.code LIKE '%" . $code . "%' OR pi_dflt.code LIKE '%" . $code . "%')"; + $this->query .= " OR (pi_lang.Sentence LIKE '%" . $code . "%' OR pi_dflt.Sentence LIKE '%" . $code . "%')"; } $this->query .= ' ORDER BY created desc';