<?php
namespace App\EventSubscriber\System;
use App\Entity\Channel\Channel;
use App\Entity\System\CampaignStatus;
use App\Entity\System\ProductStatus;
use App\Service\SystemService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use App\Entity\System\TemplateStatus;
class TwigGlobalSubscriber implements EventSubscriberInterface {
/**
* @var \Twig\Environment
*/
private $twig;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/** @var RouterInterface */
private $router;
/** @var Request|null */
private $request;
public function __construct(Environment $twig, EntityManagerInterface $entityManager, RouterInterface $router, RequestStack $requestStack) {
$this->twig = $twig;
$this->entityManager = $entityManager;
$this->router = $router;
$this->request = $requestStack->getCurrentRequest();
}
public function injectGlobalVariables(ControllerEvent $event) {
$lastProductStatus = $this->entityManager->getRepository(ProductStatus::class)->findLastRecord();
$lastProductStatusArray = $this->entityManager->getRepository(ProductStatus::class)->findLastRecordAsArray();
$lastTemplateStatus = $this->entityManager->getRepository(TemplateStatus::class)->findLastRecord();
$lastTemplateStatusArray = $this->entityManager->getRepository(TemplateStatus::class)->findLastRecordAsArray();
$lastCampaignStatus = $this->entityManager->getRepository(CampaignStatus::class)->findLastRecord();
$lastCampaignStatusArray = $this->entityManager->getRepository(CampaignStatus::class)->findLastRecordAsArray();
$request = $event->getRequest();
$isCurrentPathRoVignette = $request && $request->getPathInfo() == $this->router->generate('app_shop_vignette');
$isCurrentPathHuVignette = $request && $request->getPathInfo() == $this->router->generate('app_shop_hu_vignette');
$isCurrentPathRca = $request && $request->getPathInfo() == $this->router->generate('app_shop_rca_details');
$this->twig->addGlobal('lastProductStatus', $lastProductStatus);
$this->twig->addGlobal('lastProductStatusArray', $lastProductStatusArray);
$this->twig->addGlobal('lastTemplateStatus', $lastTemplateStatus);
$this->twig->addGlobal('lastTemplateStatusArray', $lastTemplateStatusArray);
$this->twig->addGlobal('lastCampaignStatus', $lastCampaignStatus);
$this->twig->addGlobal('lastCampaignStatusArray', $lastCampaignStatusArray);
$this->twig->addGlobal('isCurrentPathRoVignette', $isCurrentPathRoVignette);
$this->twig->addGlobal('isCurrentPathHuVignette', $isCurrentPathHuVignette);
$this->twig->addGlobal('isCurrentPathRca', $isCurrentPathRca);
$this->twig->addGlobal('rcaSiteUrl', $_ENV['RCA_SITE_URL']);
$this->twig->addGlobal('roVignetteSiteUrl', $_ENV['ROVINIETE_SITE_URL']);
$this->twig->addGlobal('isTicketing', $_ENV['IS_TICKETING']);
}
public static function getSubscribedEvents() {
return [KernelEvents::CONTROLLER => 'injectGlobalVariables'];
}
}