vendor/bluue/tickets-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\TicketsBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ConfigureMenuSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TranslatorInterface
  19.      */
  20.     private TranslatorInterface $tr;
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     protected RequestStack $requestStack;
  25.     /**
  26.      * @var Security
  27.      */
  28.     private Security $security;
  29.     /**
  30.      * @var RouterInterface
  31.      */
  32.     private RouterInterface $router;
  33.     /**
  34.      * @param RequestStack $requestStack
  35.      * @param TranslatorInterface $tr
  36.      * @param Security $security
  37.      * @param RouterInterface $router
  38.      */
  39.     public function __construct(
  40.         RequestStack $requestStack,
  41.         TranslatorInterface $tr,
  42.         Security $security,
  43.         RouterInterface $router
  44.     ) {
  45.         $this->requestStack $requestStack;
  46.         $this->tr $tr;
  47.         $this->security $security;
  48.         $this->router $router;
  49.     }
  50.     /**
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             ConfigureMenuEvent::NAME => 'onLoad'
  57.         ];
  58.     }
  59.     /**
  60.      * @param ConfigureMenuEvent $event
  61.      * @return void
  62.      */
  63.     public function onLoad(ConfigureMenuEvent $event): void
  64.     {
  65.         if (!$this->security->isGranted('ROLE__TICKETS__READ_ONLY')) {
  66.             return;
  67.         }
  68.         $menu $event->getMenu();
  69.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  70.         $requestUri $this->requestStack->getCurrentRequest()->getRequestUri();
  71.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  72.         $parentName $this->tr->trans('Tickets', [], 'TicketsBundle');
  73.         $menuKey 'TicketsMenu';
  74.         $menu->addChild($menuKey)
  75.             ->setLabel($parentName)
  76.             ->setExtra('icon''fas fa-exclamation-circle');
  77.         $menu[$menuKey]->addChild(
  78.             $this->tr->trans('Tickets', [], 'TicketsBundle'),
  79.             ['route' => 'tickets_bundle__ticket_list']
  80.         );
  81.         $name $this->tr->trans('Tickets', [], 'TicketsBundle');
  82.         $nameN0 $this->tr->trans('Tickets N0', [], 'TicketsBundle');
  83.         $routeN0 $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '0']]]);
  84.         $nameN1 $this->tr->trans('Tickets N1', [], 'TicketsBundle');
  85.         $routeN1 $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '1']]]);
  86.         $nameN2 $this->tr->trans('Tickets N2', [], 'TicketsBundle');
  87.         $routeN2 $this->router->generate('tickets_bundle__ticket_list', ['filter' => ['u' => ['level' => '2']]]);
  88.         $menu[$menuKey]->addChild($nameN0, ['uri' => $routeN0]);
  89.         $menu[$menuKey]->addChild($nameN1, ['uri' => $routeN1]);
  90.         $menu[$menuKey]->addChild($nameN2, ['uri' => $routeN2]);
  91.         if ($requestUri == $routeN0) {
  92.             $menu->getChild('TicketsMenu')->getChild($nameN0)->setCurrent(true);
  93.         } elseif ($requestUri == $routeN1) {
  94.             $menu->getChild('TicketsMenu')->getChild($nameN1)->setCurrent(true);
  95.         } elseif ($requestUri == $routeN2) {
  96.             $menu->getChild('TicketsMenu')->getChild($nameN2)->setCurrent(true);
  97.         } elseif (
  98.             $currentRoute == 'tickets_bundle__ticket_list'
  99.             || strpos($currentPath'bundles/tickets-bundle/ticket/view') !== false
  100.         ) {
  101.             $menu->getChild('TicketsMenu')->getChild($name)->setCurrent(true);
  102.         }
  103.     }
  104. }