src/EventSubscriber/ContentSubscriber.php line 102

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: admin
  5.  * Date: 12/06/2020
  6.  * Time: 14:49
  7.  *
  8.  * Copyright 2018-2019, RĂ©mi Fongaufier, All rights reserved.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Controller\API\ApiController;
  12. use App\Controller\Utils\ContentController;
  13. use App\Entity\Color;
  14. use App\Entity\CustomRedirection;
  15. use App\Entity\KindProject;
  16. use App\Entity\Section;
  17. use App\Entity\SeoPage;
  18. use App\Entity\Shape;
  19. use App\Event\ContentEvent;
  20. use App\Service\DocumentFileUploader;
  21. use App\Service\FilePathAndUrlService;
  22. use App\Service\LocaleService;
  23. use App\Utils\ContentParser;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  28. use Symfony\Component\HttpKernel\Event\RequestEvent;
  29. use Symfony\Component\HttpKernel\KernelEvents;
  30. class ContentSubscriber implements EventSubscriberInterface
  31. {
  32.     const MENUS = [
  33.         '01' => ['title' => 'Collections''route' => 'pages_collections_overview''params' => []],
  34.         '02' => ['title' => 'Woven vinyl flooring''route' => 'pages_products_overview''params' => []],
  35.         '03' => ['title' => 'Projects''route' => 'pages_projects_overview''params' => []],
  36.         '04' => ['title' => '2tec2 play''route' => 'pages_2tec2Play_index''params' => []],
  37.         '05' => ['title' => '3D Simulation''route' => 'pages_3d_simulation_index''params' => []],
  38.         '06' => ['title' => 'About our floors''route' => 'pages_aboutOurFloors_overview''params' => []],
  39.         '07' => ['title' => 'Acoustic comfort backing''route' => 'pages_aboutOurFloors_detail''params' => ['sectionSlug' => 'section_acoustic']],
  40.         '08' => ['title' => 'Sustainability''route' => 'pages_aboutOurFloors_detail''params' => ['sectionSlug' => 'section_sustainability']],
  41.         '09' => ['title' => 'Support''route' => 'pages_support_index''params' => []],
  42.         '10' => ['title' => 'Contact''route' => 'pages_contact_index''params' => []],
  43.         // '11' => ['title' => 'Custom made luxury rugs', 'route' => 'pages_LE_project_index', 'params' => []],
  44.     ];
  45.     /**
  46.      * @var EntityManagerInterface
  47.      */
  48.     protected $em;
  49.     /**
  50.      * @var LocaleService
  51.      */
  52.     protected $localeService;
  53.     /**
  54.      * @var DocumentFileUploader
  55.      */
  56.     private $documentFileUploader;
  57.     /**
  58.      * @var ApiController
  59.      */
  60.     private $apiController;
  61.     /**
  62.      * @var FilePathAndUrlService
  63.      */
  64.     private $filePathAndUrl;
  65.     private $apiPrefix;
  66.     public function __construct(EntityManagerInterface $entityManagerLocaleService $localeService,
  67.                                 ApiController $apiControllerFilePathAndUrlService $filePathAndUrl,
  68.                                 DocumentFileUploader $documentFileUploader$apiPrefix)
  69.     {
  70.         $this->em $entityManager;
  71.         $this->localeService $localeService;
  72.         $this->apiController $apiController;
  73.         $this->filePathAndUrl $filePathAndUrl;
  74.         $this->apiPrefix $apiPrefix;
  75.         $this->documentFileUploader $documentFileUploader;
  76.     }
  77.     public static function getSubscribedEvents(): array
  78.     {
  79.         return [
  80.             ContentEvent::PRE_RENDER => 'addContent',
  81.             KernelEvents::CONTROLLER => 'generateParametersParseContent',
  82.             KernelEvents::REQUEST => 'redirectionFromOldwebSite'
  83.         ];
  84.     }
  85.     public function redirectionFromOldwebSite(RequestEvent $event)
  86.     {
  87.         $redirection $this->em->getRepository(CustomRedirection::class)->findOneBy(['oldUrl' => $event->getRequest()->getRequestUri()]);
  88.         if($redirection)
  89.             $event->setResponse(new RedirectResponse($redirection->getNewUrl(), 301));
  90.     }
  91.     public function addContent(ContentEvent $event)
  92.     {
  93.         if (isset($event->getParameters()['locale'])) {
  94.             $locale $event->getParameters()['locale'];
  95.             $parameters $event->getParameters();
  96.             $parameters['menus'] = self::MENUS;
  97.             $parameters['colors_global'] = $this->localeService->getAllEntitiesTranslated(Color::class, $locale);
  98.             $parameters['kindsProject_global'] = $this->localeService->getAllEntitiesTranslated(KindProject::class, $locale);
  99.             $parameters['shapes_global'] = $this->localeService->getAllEntitiesTranslated(Shape::class, $locale);
  100.             usort($parameters['shapes_global'], function ($a$b) {
  101.                 return strcmp($a->getId(), $b->getId());
  102.             });
  103.             $aboutOurFloorSeoPage $this->em->getRepository(SeoPage::class)->findOneBy(['name' => "About our floors"]);
  104.             $parameters['aboutSections_global'] = $this->localeService->getEntitiesTranslated(Section::class,
  105.                 array_map(function ($section) {
  106.                     return $section->getId();
  107.                 }, $aboutOurFloorSeoPage->getSections()->getValues()), $locale);
  108.             $sectionsAboutOurFloor $this->localeService->getEntitiesTranslated(Section::class,
  109.                 array_map(function ($section) {
  110.                     return $section->getId();
  111.                 }, $aboutOurFloorSeoPage->getSections()->getValues()), 'en');
  112.             foreach ($sectionsAboutOurFloor as $section) {
  113.                 if ($section->getDesignation() === 'History of 2tec2') {
  114.                     $parameters['section_history'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  115.                 } elseif ($section->getDesignation() === 'Sustainability') {
  116.                     $parameters['section_sustainability'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  117.                 } elseif ($section->getDesignation() === 'Flooring for any function') {
  118.                     $parameters['section_flooring'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  119.                 } elseif ($section->getDesignation() === 'Cleanability') {
  120.                     $parameters['section_cleanability'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  121.                 } elseif ($section->getDesignation() === 'Acoustic Comfort Backing') {
  122.                     $parameters['section_acoustic'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  123.                 } elseif ($section->getDesignation() === 'Worldwide Distribution') {
  124.                     $parameters['section_worldwide'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  125.                 } elseif ($section->getDesignation() === '2tec2 Play') {
  126.                     $parameters['section_play'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  127.                 }
  128.             }
  129.             if (isset($parameters['seoPage'])) {
  130.                 $parameters['metaTitle'] = $parameters['seoPage']->getMetaTitle();
  131.                 $parameters['metaDescription'] = $parameters['seoPage']->getMetaDescription();
  132.             }
  133.             $parameters['apiUrl'] = $this->apiPrefix;
  134.             $event->setParameters($parameters);
  135.         }
  136.     }
  137.     public function generateParametersParseContent(ControllerEvent $event)
  138.     {
  139.         $controllers $event->getController();
  140.         $locale $event->getRequest()->getLocale();
  141.         $sectionSlug $event->getRequest()->get('sectionSlug');
  142.         $currentSection null;
  143.         if (is_array($controllers) && isset($controllers[0]) && get_parent_class($controllers[0]) === ContentController::class && get_class($controllers[0]) !== "App\Controller\Security\SecurityController") {
  144.             $controller $controllers[0];
  145.             $pageSlug $controller->getPageSlug();
  146.             $seoPages $this->em->getRepository(SeoPage::class)->getPagesBySlug($pageSlug);
  147.             $parameters = [];
  148.             if (count($seoPages) === 0) {
  149.                 if ($sectionSlug === null) {
  150.                     $resultSeoPages = array();
  151.                     $seoPages $this->localeService->getAllEntitiesTranslated(SeoPage::class, $locale);
  152.                     foreach ($seoPages as $seoPage) {
  153.                         $slugSeoPage str_replace(['-''_''2''3'], ['''''two''three'], $seoPage->getSlug());
  154.                         if ($slugSeoPage == $pageSlug)
  155.                             $resultSeoPages[] = $seoPage;
  156.                     }
  157.                     $seoPages $resultSeoPages;
  158.                 } else {
  159.                     $sections $this->apiController->getAllSectionsOrderByID($localetrue);
  160.                     foreach ($sections as $section) {
  161.                         if ($section->getSlug() === $sectionSlug) {
  162.                             $currentSection $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
  163.                         }
  164.                     }
  165.                     if ($currentSection)
  166.                         $seoPages[] = $currentSection->getSeoPage();
  167.                 }
  168.                 foreach ($seoPages as $i => $seoPage) {
  169.                     $seoPageKey str_replace(['-''2''3'], ['_''two''three'], $seoPage->getSlug() . '_seopage');
  170.                     if (!isset($parameters['metaTitle']) || $parameters['metaTitle'] === null)
  171.                         $parameters['metaTitle'] = $seoPage->getMetaTitle() !== null $seoPage->getMetaTitle() : null;
  172.                     if (!isset($parameters['metaDescription']) || $parameters['metaDescription'] === null)
  173.                         $parameters['metaDescription'] = $seoPage->getMetaDescription() !== null $seoPage->getMetaDescription() : null;
  174.                     foreach ($seoPage->getSections()->getValues() as $section) {
  175.                         $this->localeService->setEntityTranslated($section$event->getRequest()->getLocale());
  176.                         $contentParsed ContentParser::getParseContent($section->getContent() == !null $section->getContent() : '');
  177.                         $sectionKey strtolower(str_replace([' '], ['_'], $section->getName()));
  178.                         $seoPageKey 'seopage_' . ($i 1);
  179.                         $parameters[$seoPageKey][$sectionKey] = $contentParsed;
  180.                         $parameters[$seoPageKey][$sectionKey]['slug'] = $section->getSlug();
  181.                         $parameters[$seoPageKey][$sectionKey]['urlVideo'] = $section->getUrlVideo();
  182.                         if (count($section->getAttachments()) == 0)
  183.                             $this->initDefaultImg($parameters$seoPageKey$sectionKey);
  184.                         foreach ($section->getAttachments()->getValues() as $key => $attachment) {
  185.                             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['alt'] = $this->localeService->getFieldTranslatedFromObject('caption'$localeget_class($attachment), $attachment->getId());
  186.                             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['url']['webp'] = $this->filePathAndUrl->getAttachmentFullUrl($attachment);
  187.                             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['url']['jpg'] = $this->filePathAndUrl->getAttachmentFullUrlJPG($attachment);
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.             foreach ($seoPages as $seoPage) {
  193.                 $seoPageKey str_replace(['-''2''3'], ['_''two''three'], $seoPage->getSlug() . '_seopage');
  194.                 if (!isset($parameters['metaTitle']) || $parameters['metaTitle'] === null)
  195.                     $parameters['metaTitle'] = $seoPage->getMetaTitle() !== null $seoPage->getMetaTitle() : null;
  196.                 if (!isset($parameters['metaDescription']) || $parameters['metaDescription'] === null)
  197.                     $parameters['metaDescription'] = $seoPage->getMetaDescription() !== null $seoPage->getMetaDescription() : null;
  198.                 foreach ($seoPage->getSections()->getValues() as $section) {
  199.                     $this->localeService->setEntityTranslated($section$event->getRequest()->getLocale());
  200.                     $contentParsed ContentParser::getParseContent($section->getContent() == !null $section->getContent() : '');
  201.                     $sectionKey strtolower(str_replace([' '], ['_'], $section->getName()));
  202.                     $parameters[$seoPageKey][$sectionKey] = $contentParsed;
  203.                     $parameters[$seoPageKey][$sectionKey]['slug'] = $section->getSlug();
  204.                     $parameters[$seoPageKey][$sectionKey]['urlVideo'] = $section->getUrlVideo();
  205.                     if (count($section->getAttachments()) == 0)
  206.                         $this->initDefaultImg($parameters$seoPageKey$sectionKey);
  207.                     foreach ($section->getAttachments()->getValues() as $key => $attachment) {
  208.                         $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['alt'] = $this->localeService->getFieldTranslatedFromObject('caption'$localeget_class($attachment), $attachment->getId());
  209.                         $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['url']['webp'] = $this->filePathAndUrl->getAttachmentFullUrl($attachment);
  210.                         $parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key 1)]['url']['jpg'] = $this->filePathAndUrl->getAttachmentFullUrlJPG($attachment);
  211.                     }
  212.                 }
  213.             }
  214.             $parameters['locale'] = $locale;
  215.             $parameters['url_technical_sheet'] = $this->documentFileUploader->getTechnicalPDF($event->getRequest())[0];
  216.             $event->getRequest()->request->add($parameters);
  217.         }
  218.     }
  219.     public function initDefaultImg(&$parametersstring $seoPageKeystring $sectionKey)
  220.     {
  221.         for ($i 1$i 11$i++) {
  222.             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' $i]['alt'] = "Il n'y a pas encore enregistrĂ© d'image pour cette section";
  223.             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' $i]['url']['webp'] = "/uploads/default" . ((($i 1) % 7) + 1) . ".webp";
  224.             $parameters[$seoPageKey][$sectionKey]['pictures']['image_' $i]['url']['jpg'] = "/uploads/default" . ((($i 1) % 7) + 1) . ".jpg";
  225.         }
  226.     }
  227. }