src/EventSubscriber/MenuBuilderSubscriber.php line 37

  1. <?php
  2. /*
  3.  * This file is part of the Tabler-Bundle demo.
  4.  * Copyright 2021 Kevin Papst - www.kevinpapst.de
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace App\EventSubscriber;
  10. use KevinPapst\TablerBundle\Event\MenuEvent;
  11. use KevinPapst\TablerBundle\Model\MenuItemInterface;
  12. use KevinPapst\TablerBundle\Model\MenuItemModel;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. class MenuBuilderSubscriber implements EventSubscriberInterface
  16. {
  17.     private $security;
  18.     private $environment;
  19.     public function __construct(AuthorizationCheckerInterface $securitystring $environment)
  20.     {
  21.         $this->security $security;
  22.         $this->environment $environment;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             MenuEvent::class => ['onSetupNavbar'100],
  28.         ];
  29.     }
  30.     public function onSetupNavbar(MenuEvent $event): void
  31.     {
  32.         $event->addItem(
  33.             new MenuItemModel('home''Home''home', [], 'fas fa-home')
  34.         );
  35.         
  36.         $forms = new MenuItemModel('users''Users'null, [], 'fas fa-users');
  37.         $forms->addChild(
  38.             new MenuItemModel('user''List''user.index', [], 'fas fa-user-friends')
  39.         );
  40.         $forms->addChild(
  41.             new MenuItemModel('user_add''Add''user.new', [], 'fas fa-user-plus')
  42.         );
  43.         $event->addItem($forms);
  44.         $forms = new MenuItemModel('surveys''Surveys'null, [], 'fas fa-laptop');
  45.         $forms->addChild(
  46.              new MenuItemModel('survey''List''survey.index', [], 'fas fa-list')
  47.         );
  48.         $event->addItem($forms);
  49.         $forms = new MenuItemModel('categories''Categories'null, [], 'fas fa-laptop');
  50.         $forms->addChild(
  51.              new MenuItemModel('category''List''category.index', [], 'fas fa-list')
  52.         );
  53.         $forms->addChild(
  54.             new MenuItemModel('category_add''Add''category.new', [], 'fas fa-user-plus')
  55.         );
  56.         // $forms->addChild(
  57.         //     new MenuItemModel('modelsurvey', 'Add', 'modelsurvey.new', [], 'fas fa-plus-square')
  58.         // );
  59.         $event->addItem($forms);
  60.         $report = new MenuItemModel('report''Reports''report.index', [], 'fas fa-tachometer-alt');
  61.         $report->setBadge('New');
  62.         $report->setBadgeColor('green');
  63.         $event->addItem($report);
  64.         if ($this->security->isGranted('IS_AUTHENTICATED')) {
  65.             $event->addItem(
  66.                 new MenuItemModel('logout''Logout''security_logout', [], 'fas fa-sign-out-alt')
  67.             );
  68.         }
  69.         $this->activateByRoute(
  70.             $event->getRequest()->get('_route'),
  71.             $event->getItems()
  72.         );
  73.     }
  74.     /**
  75.      * @param string $route
  76.      * @param MenuItemInterface[] $items
  77.      */
  78.     protected function activateByRoute(string $route, array $items): void
  79.     {
  80.         foreach ($items as $item) {
  81.             if ($item->hasChildren()) {
  82.                 $this->activateByRoute($route$item->getChildren());
  83.             } elseif ($item->getRoute() == $route) {
  84.                 $item->setIsActive(true);
  85.             }
  86.         }
  87.     }
  88. }