<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 12/06/2020
* Time: 14:49
*
* Copyright 2018-2019, Rémi Fongaufier, All rights reserved.
*/
namespace App\EventSubscriber;
use App\Controller\API\ApiController;
use App\Controller\Utils\ContentController;
use App\Entity\Color;
use App\Entity\CustomRedirection;
use App\Entity\KindProject;
use App\Entity\Section;
use App\Entity\SeoPage;
use App\Entity\Shape;
use App\Event\ContentEvent;
use App\Service\DocumentFileUploader;
use App\Service\FilePathAndUrlService;
use App\Service\LocaleService;
use App\Utils\ContentParser;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ContentSubscriber implements EventSubscriberInterface
{
const MENUS = [
'01' => ['title' => 'Collections', 'route' => 'pages_collections_overview', 'params' => []],
'02' => ['title' => 'Woven vinyl flooring', 'route' => 'pages_products_overview', 'params' => []],
'03' => ['title' => 'Projects', 'route' => 'pages_projects_overview', 'params' => []],
'04' => ['title' => '2tec2 play', 'route' => 'pages_2tec2Play_index', 'params' => []],
'05' => ['title' => '3D Simulation', 'route' => 'pages_3d_simulation_index', 'params' => []],
'06' => ['title' => 'About our floors', 'route' => 'pages_aboutOurFloors_overview', 'params' => []],
'07' => ['title' => 'Acoustic comfort backing', 'route' => 'pages_aboutOurFloors_detail', 'params' => ['sectionSlug' => 'section_acoustic']],
'08' => ['title' => 'Sustainability', 'route' => 'pages_aboutOurFloors_detail', 'params' => ['sectionSlug' => 'section_sustainability']],
'09' => ['title' => 'Support', 'route' => 'pages_support_index', 'params' => []],
'10' => ['title' => 'Contact', 'route' => 'pages_contact_index', 'params' => []],
// '11' => ['title' => 'Custom made luxury rugs', 'route' => 'pages_LE_project_index', 'params' => []],
];
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var LocaleService
*/
protected $localeService;
/**
* @var DocumentFileUploader
*/
private $documentFileUploader;
/**
* @var ApiController
*/
private $apiController;
/**
* @var FilePathAndUrlService
*/
private $filePathAndUrl;
private $apiPrefix;
public function __construct(EntityManagerInterface $entityManager, LocaleService $localeService,
ApiController $apiController, FilePathAndUrlService $filePathAndUrl,
DocumentFileUploader $documentFileUploader, $apiPrefix)
{
$this->em = $entityManager;
$this->localeService = $localeService;
$this->apiController = $apiController;
$this->filePathAndUrl = $filePathAndUrl;
$this->apiPrefix = $apiPrefix;
$this->documentFileUploader = $documentFileUploader;
}
public static function getSubscribedEvents(): array
{
return [
ContentEvent::PRE_RENDER => 'addContent',
KernelEvents::CONTROLLER => 'generateParametersParseContent',
KernelEvents::REQUEST => 'redirectionFromOldwebSite'
];
}
public function redirectionFromOldwebSite(RequestEvent $event)
{
$redirection = $this->em->getRepository(CustomRedirection::class)->findOneBy(['oldUrl' => $event->getRequest()->getRequestUri()]);
if($redirection)
$event->setResponse(new RedirectResponse($redirection->getNewUrl(), 301));
}
public function addContent(ContentEvent $event)
{
if (isset($event->getParameters()['locale'])) {
$locale = $event->getParameters()['locale'];
$parameters = $event->getParameters();
$parameters['menus'] = self::MENUS;
$parameters['colors_global'] = $this->localeService->getAllEntitiesTranslated(Color::class, $locale);
$parameters['kindsProject_global'] = $this->localeService->getAllEntitiesTranslated(KindProject::class, $locale);
$parameters['shapes_global'] = $this->localeService->getAllEntitiesTranslated(Shape::class, $locale);
usort($parameters['shapes_global'], function ($a, $b) {
return strcmp($a->getId(), $b->getId());
});
$aboutOurFloorSeoPage = $this->em->getRepository(SeoPage::class)->findOneBy(['name' => "About our floors"]);
$parameters['aboutSections_global'] = $this->localeService->getEntitiesTranslated(Section::class,
array_map(function ($section) {
return $section->getId();
}, $aboutOurFloorSeoPage->getSections()->getValues()), $locale);
$sectionsAboutOurFloor = $this->localeService->getEntitiesTranslated(Section::class,
array_map(function ($section) {
return $section->getId();
}, $aboutOurFloorSeoPage->getSections()->getValues()), 'en');
foreach ($sectionsAboutOurFloor as $section) {
if ($section->getDesignation() === 'History of 2tec2') {
$parameters['section_history'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === 'Sustainability') {
$parameters['section_sustainability'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === 'Flooring for any function') {
$parameters['section_flooring'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === 'Cleanability') {
$parameters['section_cleanability'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === 'Acoustic Comfort Backing') {
$parameters['section_acoustic'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === 'Worldwide Distribution') {
$parameters['section_worldwide'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
} elseif ($section->getDesignation() === '2tec2 Play') {
$parameters['section_play'] = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
}
}
if (isset($parameters['seoPage'])) {
$parameters['metaTitle'] = $parameters['seoPage']->getMetaTitle();
$parameters['metaDescription'] = $parameters['seoPage']->getMetaDescription();
}
$parameters['apiUrl'] = $this->apiPrefix;
$event->setParameters($parameters);
}
}
public function generateParametersParseContent(ControllerEvent $event)
{
$controllers = $event->getController();
$locale = $event->getRequest()->getLocale();
$sectionSlug = $event->getRequest()->get('sectionSlug');
$currentSection = null;
if (is_array($controllers) && isset($controllers[0]) && get_parent_class($controllers[0]) === ContentController::class && get_class($controllers[0]) !== "App\Controller\Security\SecurityController") {
$controller = $controllers[0];
$pageSlug = $controller->getPageSlug();
$seoPages = $this->em->getRepository(SeoPage::class)->getPagesBySlug($pageSlug);
$parameters = [];
if (count($seoPages) === 0) {
if ($sectionSlug === null) {
$resultSeoPages = array();
$seoPages = $this->localeService->getAllEntitiesTranslated(SeoPage::class, $locale);
foreach ($seoPages as $seoPage) {
$slugSeoPage = str_replace(['-', '_', '2', '3'], ['', '', 'two', 'three'], $seoPage->getSlug());
if ($slugSeoPage == $pageSlug)
$resultSeoPages[] = $seoPage;
}
$seoPages = $resultSeoPages;
} else {
$sections = $this->apiController->getAllSectionsOrderByID($locale, true);
foreach ($sections as $section) {
if ($section->getSlug() === $sectionSlug) {
$currentSection = $this->localeService->getEntityTranslated(Section::class, $section->getId(), $locale);
}
}
if ($currentSection)
$seoPages[] = $currentSection->getSeoPage();
}
foreach ($seoPages as $i => $seoPage) {
$seoPageKey = str_replace(['-', '2', '3'], ['_', 'two', 'three'], $seoPage->getSlug() . '_seopage');
if (!isset($parameters['metaTitle']) || $parameters['metaTitle'] === null)
$parameters['metaTitle'] = $seoPage->getMetaTitle() !== null ? $seoPage->getMetaTitle() : null;
if (!isset($parameters['metaDescription']) || $parameters['metaDescription'] === null)
$parameters['metaDescription'] = $seoPage->getMetaDescription() !== null ? $seoPage->getMetaDescription() : null;
foreach ($seoPage->getSections()->getValues() as $section) {
$this->localeService->setEntityTranslated($section, $event->getRequest()->getLocale());
$contentParsed = ContentParser::getParseContent($section->getContent() == !null ? $section->getContent() : '');
$sectionKey = strtolower(str_replace([' '], ['_'], $section->getName()));
$seoPageKey = 'seopage_' . ($i + 1);
$parameters[$seoPageKey][$sectionKey] = $contentParsed;
$parameters[$seoPageKey][$sectionKey]['slug'] = $section->getSlug();
$parameters[$seoPageKey][$sectionKey]['urlVideo'] = $section->getUrlVideo();
if (count($section->getAttachments()) == 0)
$this->initDefaultImg($parameters, $seoPageKey, $sectionKey);
foreach ($section->getAttachments()->getValues() as $key => $attachment) {
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['alt'] = $this->localeService->getFieldTranslatedFromObject('caption', $locale, get_class($attachment), $attachment->getId());
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['url']['webp'] = $this->filePathAndUrl->getAttachmentFullUrl($attachment);
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['url']['jpg'] = $this->filePathAndUrl->getAttachmentFullUrlJPG($attachment);
}
}
}
}
foreach ($seoPages as $seoPage) {
$seoPageKey = str_replace(['-', '2', '3'], ['_', 'two', 'three'], $seoPage->getSlug() . '_seopage');
if (!isset($parameters['metaTitle']) || $parameters['metaTitle'] === null)
$parameters['metaTitle'] = $seoPage->getMetaTitle() !== null ? $seoPage->getMetaTitle() : null;
if (!isset($parameters['metaDescription']) || $parameters['metaDescription'] === null)
$parameters['metaDescription'] = $seoPage->getMetaDescription() !== null ? $seoPage->getMetaDescription() : null;
foreach ($seoPage->getSections()->getValues() as $section) {
$this->localeService->setEntityTranslated($section, $event->getRequest()->getLocale());
$contentParsed = ContentParser::getParseContent($section->getContent() == !null ? $section->getContent() : '');
$sectionKey = strtolower(str_replace([' '], ['_'], $section->getName()));
$parameters[$seoPageKey][$sectionKey] = $contentParsed;
$parameters[$seoPageKey][$sectionKey]['slug'] = $section->getSlug();
$parameters[$seoPageKey][$sectionKey]['urlVideo'] = $section->getUrlVideo();
if (count($section->getAttachments()) == 0)
$this->initDefaultImg($parameters, $seoPageKey, $sectionKey);
foreach ($section->getAttachments()->getValues() as $key => $attachment) {
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['alt'] = $this->localeService->getFieldTranslatedFromObject('caption', $locale, get_class($attachment), $attachment->getId());
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['url']['webp'] = $this->filePathAndUrl->getAttachmentFullUrl($attachment);
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . ($key + 1)]['url']['jpg'] = $this->filePathAndUrl->getAttachmentFullUrlJPG($attachment);
}
}
}
$parameters['locale'] = $locale;
$parameters['url_technical_sheet'] = $this->documentFileUploader->getTechnicalPDF($event->getRequest())[0];
$event->getRequest()->request->add($parameters);
}
}
public function initDefaultImg(&$parameters, string $seoPageKey, string $sectionKey)
{
for ($i = 1; $i < 11; $i++) {
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . $i]['alt'] = "Il n'y a pas encore enregistré d'image pour cette section";
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . $i]['url']['webp'] = "/uploads/default" . ((($i - 1) % 7) + 1) . ".webp";
$parameters[$seoPageKey][$sectionKey]['pictures']['image_' . $i]['url']['jpg'] = "/uploads/default" . ((($i - 1) % 7) + 1) . ".jpg";
}
}
}