src/Controller/InfoController.php line 21

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\PageTranslation;
  4. use App\Entity\Paragraph;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class InfoController extends AbstractController
  11. {
  12.     public function __construct(
  13.         private readonly EntityManagerInterface $em
  14.     ){}
  15.     #[Route('/{_locale}/info/{slug}'name'app_info')]
  16.     public function show(string $_localestring $slug): Response
  17.     {
  18.         $entityManager $this->em;
  19.         // Fetch the page entity based on the slug
  20.         /** @var PageTranslation $pageTranslation */
  21.         $pageTranslation $entityManager->getRepository(PageTranslation::class)->findOneBy(['slug' => $slug]);
  22.         if (!$pageTranslation) {
  23.             return $this->redirectToRoute("web_homepage");
  24.         }
  25.         if(!$pageTranslation->getPage()->getIsActive()){
  26.             return $this->redirectToRoute("web_homepage");
  27.         }
  28.         // Get the translations for the page
  29.         $translations $pageTranslation->getPage()->getTranslations();
  30.         // Find the translation based on the desired locale
  31.         $translation $translations->filter(function (PageTranslation $translation) use ($_locale) {
  32.             return $translation->getLocale() === $_locale;
  33.         })->first();
  34.         if (!$translation) {
  35.             // Find the translation that matches the provided slug, regardless of locale
  36.             $matchingTranslation $translations->filter(function (PageTranslation $translation) use ($slug) {
  37.                 return $translation->getSlug() === $slug;
  38.             })->first();
  39.             if ($matchingTranslation) {
  40.                 // Redirect to the correct URL for the matching translation
  41.                 $desiredSlug $matchingTranslation->getSlugByLocale($_locale);
  42.                 if ($desiredSlug) {
  43.                     $url $this->generateUrl('app_info', ['_locale' => $_locale'slug' => $desiredSlug]);
  44.                     return new RedirectResponse($url301);
  45.                 }
  46.             }
  47.             throw $this->createNotFoundException('Translation not found');
  48.         }
  49.         $paragraphs $this->em->getRepository(Paragraph::class)->findBy(["page" => $translation->getPage()]);
  50.         // Render the page with the retrieved title and text
  51.         return $this->render('info/show.html.twig', [
  52.             "page" => $translation,
  53.             "paragraphs" => $paragraphs
  54.         ]);
  55.     }
  56. }