<?php
namespace App\Controller;
use App\Entity\Translation;
use App\Service\CustomTranslatorService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
class TranslationModerationController extends AbstractController
{
private $translationService;
public function __construct(CustomTranslatorService $translatorService)
{
$this->translationService = $translatorService;
}
/**
* @Route("/moderation/translations", name="translation_moderation", methods={"GET"})
*/
public function moderation(Request $request, SessionInterface $session): Response
{
// Récupère l'URL de retour si disponible
$redirectUrl = $session->get('translation_review_redirect', $this->generateUrl('homepage'));
$pendingTranslations = $this->translationService->getPendingTranslations($redirectUrl);
// Supprime la redirection stockée après modération
$session->remove('translation_review_redirect');
//if ($request->isMethod('POST')) {
// foreach ($pendingTranslations as $key => $data) {
// $newTranslation = $request->request->get("translation_{$key}");
// if ($newTranslation) {
// $this->translationService->saveTranslation($key, $newTranslation, $data['locale']);
// }
// }
// return $this->redirectToRoute('homepage'); // Remet le rendu normal
//}
return $this->render('moderation/translations.html.twig', [
'translations' => $pendingTranslations,
'redirectUrl' => $redirectUrl
]);
}
/**
* @Route("/moderation/translation/validate/{id}", name="translation_validate", methods={"POST"})
*/
public function validateTranslation(Translation $translation, EntityManagerInterface $entityManager, Request $request, SessionInterface $session): JsonResponse
{
// Récupère la traduction modifiée
$data = json_decode($request->getContent(), true);
$newTranslation = $data['translation'] ?? null;
$redirectUrl = $data['redirectUrl'] ?? null;
if ($newTranslation) {
$this->translationService->saveTranslationToDb($translation->getKeyName(), $translation->getLocale(), $newTranslation, $redirectUrl, '', 0);
// supprime les doublons
$pendingTranslations = $entityManager->getRepository(Translation::class)->findBy(['locale' => $translation->getLocale(), 'keyName' => $translation->getKeyName()]);
foreach ($pendingTranslations as $pendingTranslation) {
$entityManager->remove($pendingTranslation);
}
// Supprime la translation elle même
$entityManager->remove($translation);
$entityManager->flush();
// Check la session
$pendingTranslations = $entityManager->getRepository(Translation::class)->findBy(['redirectUrl' => $redirectUrl]);
if (count($pendingTranslations) == 0) {
$items = $session->get('translation_review_items', []);
if (($key = array_search($redirectUrl, $items)) !== false) {
unset($items[$key]);
}
$session->set('translation_review_items', $items);
}
}
return new JsonResponse(['status' => 'success', 'message' => 'Traduction validée !']);
}
/**
* @Route("/moderation/translation/reject/{id}", name="translation_reject", methods={"POST"})
*/
public function rejectTranslation(Translation $translation, EntityManagerInterface $entityManager): JsonResponse
{
// Supprime la traduction non validée
$entityManager->remove($translation);
$entityManager->flush();
$this->translationService->reject($translation);
return new JsonResponse(['status' => 'success', 'message' => 'Traduction rejetée !']);
}
}