vendor/bluue/planning-bundle/src/EventSubscriber/ConfigurationBundleSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (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\PlanningBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Event\MenuConfigurationPageEvent;
  11. use App\Event\BundleRolesEvent;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ConfigurationBundleSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var TranslatorInterface
  20.      */
  21.     private TranslatorInterface $tr;
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     protected RequestStack $requestStack;
  26.     /**
  27.      * @var Security
  28.      */
  29.     private Security $security;
  30.     /**
  31.      * @param RequestStack $requestStack
  32.      * @param TranslatorInterface $tr
  33.      * @param Security $security
  34.      */
  35.     public function __construct(
  36.         RequestStack $requestStack,
  37.         TranslatorInterface $tr,
  38.         Security $security
  39.     ) {
  40.         $this->requestStack $requestStack;
  41.         $this->tr $tr;
  42.         $this->security $security;
  43.     }
  44.     /**
  45.      * @return string[]
  46.      */
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             ConfigureMenuEvent::NAME => 'mainMenu',
  51.             MenuConfigurationPageEvent::NAME => 'configurationMenu',
  52.             BundleRolesEvent::NAME => 'bundleRoles'
  53.         ];
  54.     }
  55.     /**
  56.      * @param ConfigureMenuEvent $event
  57.      * @return void
  58.      */
  59.     public function mainMenu(ConfigureMenuEvent $event): void
  60.     {
  61.         if (!$this->security->isGranted('ROLE__PLANNING__READ_ONLY')) {
  62.             return;
  63.         }
  64.         $menu $event->getMenu();
  65.         if (!$menu->getChild('PlanningMenu')) {
  66.             $menu->addChild('PlanningMenu')
  67.                 ->setLabel($this->tr->trans('Planning', [], 'PlanningBundle'))
  68.                 ->setExtra('icon''far fa-calendar-alt');
  69.         }
  70.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  71.         $planningMenu = [
  72.             [
  73.                 'name' => $this->tr->trans('Planning', [], 'PlanningBundle'),
  74.                 'route' => 'planning_bundle__slot_list'
  75.             ]
  76.         ];
  77.         foreach ($planningMenu as $planningMenuChild) {
  78.             $menu['PlanningMenu']->addChild(
  79.                 $planningMenuChild['name'],
  80.                 ['route' => $planningMenuChild['route']]
  81.             );
  82.             if ($planningMenuChild['route'] == $currentRoute) {
  83.                 $menu->getChild('PlanningMenu')->getChild($planningMenuChild['name'])->setCurrent(true);
  84.             }
  85.         }
  86.     }
  87.     /**
  88.      * @param MenuConfigurationPageEvent $event
  89.      * @return void
  90.      */
  91.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  92.     {
  93.         if ($this->security->isGranted('ROLE__PLANNING__ADMIN')) {
  94.             $pages $event->getPages();
  95.             $pages[] = [
  96.                 'id' => 'planning_bundle__configuration_settings',
  97.                 'name' => $this->tr->trans('Planning Module', [], 'PlanningBundle'),
  98.                 'config' => [],
  99.                 'subpages' => [
  100.                     ['id' => 'planning_bundle__configuration_settings'],
  101.                     ['id' => 'planning_bundle__configuration_slot_type_list']
  102.                 ]
  103.             ];
  104.             $event->setPages($pages);
  105.         }
  106.     }
  107.     /**
  108.      * @param BundleRolesEvent $event
  109.      * @return void
  110.      */
  111.     public function bundleRoles(BundleRolesEvent $event): void
  112.     {
  113.         $bundles $event->getBundles();
  114.         $bundles[] = [
  115.             'select_name' => 'planning',
  116.             'name' => $this->tr->trans('Planning', [], 'PlanningBundle')
  117.         ];
  118.         $event->setBundles($bundles);
  119.     }
  120. }