<?php
namespace App\EventSubscriber;
use App\Service\LocationIP\LocationIpInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Twig\Environment;
use App\Manager\InfoManager;
use App\Manager\InfoErrorManager;
use App\Manager\PropertyManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Form\OptionsTypeTrait;
class TwigEventSubscriber implements EventSubscriberInterface
{
use OptionsTypeTrait;
private $twig;
private $imgr;
private $emgr;
private $pmgr;
private $param;
private RequestStack $requestStack;
public function __construct(
Environment $twig,
InfoManager $imgr,
InfoErrorManager $emgr,
PropertyManager $pmgr,
ParameterBagInterface $param,
RequestStack $requestStack
) {
$this->twig = $twig;
$this->imgr = $imgr;
$this->emgr = $emgr;
$this->pmgr = $pmgr;
$this->param = $param;
$this->requestStack = $requestStack;
}
public function onControllerEvent(ControllerEvent $event)
{
$this->twig->addGlobal('menu_active', '');
$this->twig->addGlobal('info', $this->imgr->find(1));
$this->twig->addGlobal('info_error', $this->emgr->find(1));
$this->twig->addGlobal('filter_types', $this->pmgr->listTypesFilter());
$this->twig->addGlobal('filter_locations', $this->pmgr->listLocationsFilter());
$this->twig->addGlobal('list_autocomplete', $this->pmgr->listAutocomplete());
$this->twig->addGlobal('sd_external_paths', $this->external_paths);
$list_count = $this->pmgr->listCountGroupLocation();
$list_count = array_filter($list_count, function ($e) {
return $e['total'] < 2;
});
$list_count = array_column($list_count, 'sdurl', 'sdkey');
$this->twig->addGlobal('sdlistcount', $list_count);
$validations = [
'dni' => [
'endpoint' => $this->param->get('valid_dni_endpoint'),
'token' => $this->param->get('valid_dni_token'),
],
'email' => [
'endpoint' => $this->param->get('valid_email_endpoint'),
'apikey' => $this->param->get('valid_email_key'),
]
];
$this->twig->addGlobal('validations', $validations);
$this->twig->addGlobal('show_regionalization_script', $this->showRegionalizationScript());
}
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'onControllerEvent',
];
}
protected function showRegionalizationScript(): bool
{
$region = $this->requestStack->getSession()->get(LocationIpInterface::REGION);
$clear = (bool) $this->requestStack->getSession()->get(LocationIpInterface::CLEAR, false);
$invalid = (bool) $this->requestStack->getSession()->get(LocationIpInterface::INVALID, false);
if ($clear or $invalid) {
return false;
}
if (!is_null($region)) {
return false;
}
return true;
}
}