vendor/akyos/cms-bundle/Service/FrontControllerService.php line 126

Open in your IDE?
  1. <?php
  2. namespace Akyos\CmsBundle\Service;
  3. use Akyos\BuilderBundle\AkyosBuilderBundle;
  4. use Akyos\BuilderBundle\Entity\BuilderOptions;
  5. use Akyos\BuilderBundle\Entity\Component;
  6. use Akyos\CmsBundle\Entity\Page;
  7. use Akyos\CmsBundle\Entity\Redirect301;
  8. use Akyos\CmsBundle\Entity\Seo;
  9. use DateTime;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Gedmo\Translatable\Entity\Translation;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. use Twig\Environment;
  19. use Twig\Error\LoaderError;
  20. use Twig\Error\RuntimeError;
  21. use Twig\Error\SyntaxError;
  22. class FrontControllerService
  23. {
  24.     private EntityManagerInterface $em;
  25.     private RouterInterface $router;
  26.     private Filesystem $filesystem;
  27.     private KernelInterface $kernel;
  28.     private Environment $environment;
  29.     private CmsService $cmsService;
  30.     private AuthorizationCheckerInterface $checker;
  31.     public function __construct(EntityManagerInterface $emRouterInterface $routerFilesystem $filesystemKernelInterface $kernelEnvironment $environmentCmsService $cmsServiceAuthorizationCheckerInterface $checker)
  32.     {
  33.         $this->em $em;
  34.         $this->router $router;
  35.         $this->filesystem $filesystem;
  36.         $this->kernel $kernel;
  37.         $this->environment $environment;
  38.         $this->cmsService $cmsService;
  39.         $this->checker $checker;
  40.     }
  41.     /**
  42.      * @param string $entitySlug
  43.      * @param string $slug
  44.      * @param string $route
  45.      * @return string|RedirectResponse
  46.      * @throws LoaderError
  47.      * @throws RuntimeError
  48.      * @throws SyntaxError
  49.      */
  50.     public function singleAndPreview(string $entitySlugstring $slugstring $route)
  51.     {
  52.         // GET ENTITY NAME AND FULLNAME FROM SLUG
  53.         [$entityFullName$entity] = $this->cmsService->getEntityAndFullString($entitySlug);
  54.         if (!$entityFullName || !$entity || !$this->cmsService->checkIfSingleEnable($entityFullName)) {
  55.             throw new NotFoundHttpException("Cette page n'existe pas! ( Détail )");
  56.         }
  57.         $slug substr($slug, -1) === "/" substr($slug0, -1) : $slug;
  58.         // GET ELEMENT
  59.         $element $this->em->getRepository($entityFullName)->findOneBy(['slug' => $slug]) ?? (!$this->em->getMetadataFactory()->isTransient(Translation::class) ? $this->em->getRepository(Translation::class)->findObjectByTranslatedField('slug'$slug$entityFullName) : null);
  60.         $now = new DateTime();
  61.         if (!$element) {
  62.             $redirect301 $this->em->getRepository(Redirect301::class)->findOneBy(['oldSlug' => $slug'objectType' => $entityFullName]);
  63.             if ($redirect301) {
  64.                 $element $this->em->getRepository($entityFullName)->find($redirect301->getObjectId());
  65.                 if ($element) {
  66.                     $redirectUrl $this->router->generate($route, ['entitySlug' => $entitySlug'slug' => $element->getSlug()]);
  67.                     return new RedirectResponse($redirectUrl301);
  68.                 }
  69.             }
  70.             throw new NotFoundHttpException("Cette page n'existe pas! ( ${route} )");
  71.         }
  72.         if (property_exists($element'published') && $route !== 'single_preview') {
  73.             if (!$element->getPublished()) {
  74.                 if ($this->checker->isGranted('ROLE_ADMIN')) {
  75.                     return new RedirectResponse($this->router->generate('single_preview', ['entitySlug' => $entitySlug'slug' => $slug]));
  76.                 }
  77.                 throw new NotFoundHttpException("Cette page n'existe pas! ( ${entity} )");
  78.             }
  79.             if (property_exists($element'publishedAt') && ($element->getPublishedAt() > $now)) {
  80.                 if ($this->checker->isGranted('ROLE_ADMIN')) {
  81.                     return new RedirectResponse($this->router->generate('single_preview', ['entitySlug' => $entitySlug'slug' => $slug]));
  82.                 }
  83.                 throw new NotFoundHttpException("Cette page n'existe pas! ( ${entity} )");
  84.             }
  85.         }
  86.         // GET COMPONENTS OR CONTENT
  87.         $components null;
  88.         if ($this->cmsService->checkIfBundleEnable(AkyosBuilderBundle::class, BuilderOptions::class, $entityFullName)) {
  89.             $components $this->em->getRepository(Component::class)->findBy(['type' => $entityFullName'typeId' => $element->getId(), 'isTemp' => ($route === 'single_preview'), 'parentComponent' => null], ['position' => 'ASC']);
  90.         }
  91.         // GET TEMPLATE
  92.         $view $this->filesystem->exists($this->kernel->getProjectDir() . "/templates/${entity}/single.html.twig") ? "${entity}/single.html.twig" '@AkyosCms/front/single.html.twig';
  93.         $this->environment->addGlobal('global_element'$element);
  94.         // RENDER
  95.         return $this->environment->render($view, ['seo' => $this->em->getRepository(Seo::class)->findOneBy(['type' => $entityFullName'typeId' => $element->getId()]), 'element' => $element'components' => $components'entity' => $entity'slug' => $slug]);
  96.     }
  97.     /**
  98.      * @param string $slug
  99.      * @param string $route
  100.      * @return string|RedirectResponse
  101.      * @throws LoaderError
  102.      * @throws RuntimeError
  103.      * @throws SyntaxError
  104.      */
  105.     public function pageAndPreview(string $slugstring $route)
  106.     {
  107.         // FIND PAGE
  108.         $entity Page::class;
  109.         $slug substr($slug, -1) === "/" substr($slug0, -1) : $slug;
  110.         /** @var Page $page */
  111.         $page $this->em->getRepository($entity)->findOneBy(['slug' => $slug]) ?? (!$this->em->getMetadataFactory()->isTransient(Translation::class) ? $this->em->getRepository(Translation::class)->findObjectByTranslatedField('slug'$slug$entity) : null);
  112.         $now = new DateTime();
  113.         if (!$page) {
  114.             $redirect301 $this->em->getRepository(Redirect301::class)->findOneBy(['oldSlug' => $slug'objectType' => $entity]);
  115.             if ($redirect301) {
  116.                 $page $this->em->getRepository($entity)->find($redirect301->getObjectId());
  117.                 $redirectUrl $this->router->generate($route, ['slug' => $page->getSlug()]);
  118.                 return new RedirectResponse($redirectUrl301);
  119.             }
  120.             throw new NotFoundHttpException("Cette page n'existe pas! ( ${entity} )");
  121.         }
  122.         if ($route !== 'page_preview') {
  123.             if (!$page->getPublished()) {
  124.                 if ($this->checker->isGranted('ROLE_ADMIN')) {
  125.                     return new RedirectResponse($this->router->generate('page_preview', ['slug' => $slug]));
  126.                 }
  127.                 throw new NotFoundHttpException("Cette page n'existe pas! ( ${entity} )");
  128.             }
  129.             if ($page->getPublishedAt() > $now) {
  130.                 if ($this->checker->isGranted('ROLE_ADMIN')) {
  131.                     return new RedirectResponse($this->router->generate('page_preview', ['slug' => $slug]));
  132.                 }
  133.                 throw new NotFoundHttpException("Cette page n'existe pas! ( ${entity} )");
  134.             }
  135.         }
  136.         // GET COMPONENTS OR CONTENT
  137.         $components null;
  138.         if ($this->cmsService->checkIfBundleEnable(AkyosBuilderBundle::class, BuilderOptions::class, $entity)) {
  139.             $components $this->em->getRepository(Component::class)->findBy(['type' => $entity'typeId' => $page->getId(), 'isTemp' => ($route === 'page_preview'), 'parentComponent' => null], ['position' => 'ASC']);
  140.         }
  141.         // GET TEMPLATE
  142.         $view $page->getTemplate() ? 'page/' $page->getTemplate() . '.html.twig' '@AkyosCms/front/content.html.twig';
  143.         $this->environment->addGlobal('global_page'$page);
  144.         return $this->environment->render($view, ['seo' => $this->em->getRepository(Seo::class)->findOneBy(['type' => $entity'typeId' => $page->getId()]), 'page' => $page'components' => $components'content' => $page->getContent(), 'slug' => $slug]);
  145.     }
  146. }