<?php
/**
* @author Léo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\TicketsBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @var RouterInterface
*/
private RouterInterface $router;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
* @param RouterInterface $router
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security,
RouterInterface $router
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
$this->router = $router;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__TICKETS__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$requestUri = $this->requestStack->getCurrentRequest()->getRequestUri();
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$parentName = $this->tr->trans('Tickets', [], 'TicketsBundle');
$menuKey = 'TicketsMenu';
$menu->addChild($menuKey)
->setLabel($parentName)
->setExtra('icon', 'fas fa-exclamation-circle');
$menu[$menuKey]->addChild(
$this->tr->trans('Tickets', [], 'TicketsBundle'),
['route' => 'tickets_bundle__ticket_list']
);
$name = $this->tr->trans('Tickets', [], 'TicketsBundle');
$nameN0 = $this->tr->trans('Tickets N0', [], 'TicketsBundle');
$routeN0 = $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '0']]]);
$nameN1 = $this->tr->trans('Tickets N1', [], 'TicketsBundle');
$routeN1 = $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '1']]]);
$nameN2 = $this->tr->trans('Tickets N2', [], 'TicketsBundle');
$routeN2 = $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '2']]]);
$menu[$menuKey]->addChild($nameN0, ['uri' => $routeN0]);
$menu[$menuKey]->addChild($nameN1, ['uri' => $routeN1]);
$menu[$menuKey]->addChild($nameN2, ['uri' => $routeN2]);
if ($requestUri == $routeN0) {
$menu->getChild('TicketsMenu')->getChild($nameN0)->setCurrent(true);
} elseif ($requestUri == $routeN1) {
$menu->getChild('TicketsMenu')->getChild($nameN1)->setCurrent(true);
} elseif ($requestUri == $routeN2) {
$menu->getChild('TicketsMenu')->getChild($nameN2)->setCurrent(true);
} elseif (
$currentRoute == 'tickets_bundle__ticket_list'
|| strpos($currentPath, 'bundles/tickets-bundle/ticket/view') !== false
) {
$menu->getChild('TicketsMenu')->getChild($name)->setCurrent(true);
}
}
}