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 $security, string $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('dashboard', 'dashboard', 'app_dashboard', [], 'fas fa-tachometer-alt')
  34.         );
  35.         if (!$this->security->isGranted('IS_AUTHENTICATED')) {
  36.             $event->addItem(
  37.                 new MenuItemModel('login', 'login', 'app_login', [], 'fas fa-sign-in-alt')
  38.             );
  39.         }
  40.         $Client = new MenuItemModel('Client', 'Clients', null, [],'fa fa-fw fa-address-book');
  41.         $Client->addChild(new MenuItemModel('Client', 'Clients Manager', 'app_client_index', [], 'far fa-users'));
  42.         $event->addItem($Client);
  43.         $Cases = new MenuItemModel('case', 'Registre.Cases', null, [],'fa fa-fw fa-balance-scale');
  44.         $Cases->addChild(new MenuItemModel('buttons', 'Cases', 'app_case_index', [], 'far fa-folder'));
  45.         $Cases->addChild(new MenuItemModel('buttons', 'MyCases','app_case_index_with_locked',['lockedbyme'=>'true'],'far fa-folder-open'));
  46.         $vertical = new MenuItemModel('navbar-vertical', '', null, []);
  47.         $vertical->setDivider(true);
  48.         $Cases->addChild($vertical);
  49.         $Cases->setDivider(true);
  50.         $Cases->addChild(new MenuItemModel('buttons', 'CreateNew','app_case_new',[],'far fa-plus-square'));
  51.         $event->addItem($Cases);
  52.         $Financial = new MenuItemModel('financial', 'Registre.Financial', null, [],'fa fa-fw fa-bank');
  53.         $Financial->addChild(new MenuItemModel('buttons', 'incomes/expenses', 'app_income_expense_index', [], 'fa fa-fw fa-columns'));
  54.         $event->addItem($Financial);
  55.         $Deposit = new MenuItemModel('Deposit', 'Registre.Deposits', null, [],'fa fa-fw fa-money');
  56.         $Deposit->addChild(new MenuItemModel('buttons', 'Deposits', 'app_deposit_index', [], 'fa fa-fw fa-usd'));
  57.         $event->addItem($Deposit);
  58.         if ($this->security->isGranted('ROLE_ADMIN')) {
  59.             $users = new MenuItemModel('user', 'Users', null, [], 'fa fa-fw fa-users');
  60.             $users->addChild(new MenuItemModel('buttons', 'Users', 'app_user_index', [], 'fa fa-fw fa-users'));
  61.             $event->addItem($users);
  62.         }
  63.         $Report = new MenuItemModel('report', 'Report', null, [], 'fa fa-fw fa-report');
  64.         $event->addItem($Report);
  65.         $this->activateByRoute(
  66.             $event->getRequest()->get('_route'),
  67.             $event->getItems()
  68.         );
  69.     }
  70.     /**
  71.      * @param string $route
  72.      * @param MenuItemInterface[] $items
  73.      */
  74.     protected function activateByRoute(string $route, array $items): void
  75.     {
  76.         foreach ($items as $item) {
  77.             if ($item->hasChildren()) {
  78.                 $this->activateByRoute($route, $item->getChildren());
  79.             } elseif ($item->getRoute() == $route) {
  80.                 $item->setIsActive(true);
  81.             }
  82.         }
  83.     }
  84. }