src/Controller/TranslationModerationController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Translation;
  4. use App\Service\CustomTranslatorService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class TranslationModerationController extends AbstractController
  13. {
  14.     private $translationService;
  15.     public function __construct(CustomTranslatorService $translatorService)
  16.     {
  17.         $this->translationService $translatorService;
  18.     }
  19.     /**
  20.      * @Route("/moderation/translations", name="translation_moderation", methods={"GET"})
  21.      */
  22.     public function moderation(Request $requestSessionInterface $session): Response
  23.     {
  24.         // Récupère l'URL de retour si disponible
  25.         $redirectUrl $session->get('translation_review_redirect'$this->generateUrl('homepage'));
  26.         $pendingTranslations $this->translationService->getPendingTranslations($redirectUrl);
  27.         // Supprime la redirection stockée après modération
  28.         $session->remove('translation_review_redirect');
  29.         //if ($request->isMethod('POST')) {
  30.         //    foreach ($pendingTranslations as $key => $data) {
  31.         //        $newTranslation = $request->request->get("translation_{$key}");
  32.         //        if ($newTranslation) {
  33.         //            $this->translationService->saveTranslation($key, $newTranslation, $data['locale']);
  34.         //        }
  35.         //    }
  36.         //    return $this->redirectToRoute('homepage'); // Remet le rendu normal
  37.         //}
  38.         return $this->render('moderation/translations.html.twig', [
  39.             'translations' => $pendingTranslations,
  40.             'redirectUrl' => $redirectUrl
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/moderation/translation/validate/{id}", name="translation_validate", methods={"POST"})
  45.      */
  46.     public function validateTranslation(Translation $translationEntityManagerInterface $entityManagerRequest $requestSessionInterface $session): JsonResponse
  47.     {
  48.         // Récupère la traduction modifiée
  49.         $data           json_decode($request->getContent(), true);
  50.         $newTranslation $data['translation'] ?? null;
  51.         $redirectUrl    $data['redirectUrl'] ?? null;
  52.         if ($newTranslation) {
  53.             $this->translationService->saveTranslationToDb($translation->getKeyName(), $translation->getLocale(), $newTranslation$redirectUrl''0);
  54.         // supprime les doublons
  55.             $pendingTranslations $entityManager->getRepository(Translation::class)->findBy(['locale' => $translation->getLocale(), 'keyName' => $translation->getKeyName()]);
  56.         foreach ($pendingTranslations as $pendingTranslation) {
  57.                 $entityManager->remove($pendingTranslation);
  58.         }
  59.         // Supprime la translation elle même
  60.             $entityManager->remove($translation);
  61.             $entityManager->flush();
  62.         // Check la session
  63.         $pendingTranslations $entityManager->getRepository(Translation::class)->findBy(['redirectUrl' => $redirectUrl]);
  64.         if (count($pendingTranslations) == 0) {
  65.             $items $session->get('translation_review_items', []);
  66.         if (($key array_search($redirectUrl$items)) !== false) {
  67.                     unset($items[$key]);
  68.                 }
  69.             $session->set('translation_review_items'$items);
  70.         }
  71.         }
  72.         return new JsonResponse(['status' => 'success''message' => 'Traduction validée !']);
  73.     }
  74.     /**
  75.      * @Route("/moderation/translation/reject/{id}", name="translation_reject", methods={"POST"})
  76.      */
  77.     public function rejectTranslation(Translation $translationEntityManagerInterface $entityManager): JsonResponse
  78.     {
  79.         // Supprime la traduction non validée
  80.         $entityManager->remove($translation);
  81.         $entityManager->flush();
  82.         $this->translationService->reject($translation);
  83.         return new JsonResponse(['status' => 'success''message' => 'Traduction rejetée !']);
  84.     }
  85. }