vendor/bluue/forecasted-stock-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 70

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\ForecastedStockBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Event\MenuConfigurationPageEvent;
  11. use App\Services\CheckBundleInstall;
  12. use Bluue\StocksBundle\Event\MenuStocksConfigurationEvent;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ConfigureMenuSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var TranslatorInterface
  21.      */
  22.     private TranslatorInterface $tr;
  23.     /**
  24.      * @var RequestStack
  25.      */
  26.     protected RequestStack $requestStack;
  27.     /**
  28.      * @var Security
  29.      */
  30.     private Security $security;
  31.     /**
  32.      * @param RequestStack $requestStack
  33.      * @param TranslatorInterface $tr
  34.      * @param Security $security
  35.      */
  36.     public function __construct(
  37.         RequestStack $requestStack,
  38.         TranslatorInterface $tr,
  39.         Security $security
  40.     ) {
  41.         $this->requestStack $requestStack;
  42.         $this->tr $tr;
  43.         $this->security $security;
  44.     }
  45.     /**
  46.      * @return string[]
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             ConfigureMenuEvent::NAME => 'onLoad',
  52.             MenuStocksConfigurationEvent::NAME => 'configurationStocksMenu',
  53.             MenuConfigurationPageEvent::NAME => 'configurationMenu'
  54.         ];
  55.     }
  56.     /**
  57.      * @param ConfigureMenuEvent $event
  58.      * @return void
  59.      */
  60.     public function onLoad(ConfigureMenuEvent $event): void
  61.     {
  62.         if (!$this->security->isGranted('ROLE__STOCKS__READ_ONLY')) {
  63.             return;
  64.         }
  65.         $menu $event->getMenu();
  66.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  67.         $forecastsMenuChilds = [
  68.             [
  69.                 'key' => 'forecasts',
  70.                 'name' => $this->tr->trans('Forecasts', [], 'ForecastedStockBundle'),
  71.                 'route' => 'forecasted_stock_bundle__list'
  72.             ]
  73.         ];
  74.         if (CheckBundleInstall::exist('sales-bundle')) {
  75.             $forecastsMenuChilds[] = [
  76.                 'key' => 'forecasts_from_sales',
  77.                 'name' => $this->tr->trans('Forecasts (from sales)', [], 'ForecastedStockBundle'),
  78.                 'route' => 'forecasted_stock_bundle__list_from_sales'
  79.             ];
  80.         }
  81.         foreach ($forecastsMenuChilds as $forecastsMenuChild) {
  82.             $current false;
  83.             if ($forecastsMenuChild['route'] == $currentRoute) {
  84.                 $current true;
  85.             }
  86.             $menu['StocksMenu']->addChild(
  87.                 $forecastsMenuChild['key'],
  88.                 [
  89.                     'label' => $forecastsMenuChild['name'],
  90.                     'route' => $forecastsMenuChild['route'],
  91.                     'current' => $current
  92.                 ]
  93.             );
  94.         }
  95.     }
  96.     /**
  97.      * @param MenuStocksConfigurationEvent $event
  98.      * @return void
  99.      */
  100.     public function configurationStocksMenu(MenuStocksConfigurationEvent $event)
  101.     {
  102.         $pages $event->getPages();
  103.         $pages[] = [
  104.             'id' => 'forecasted_stock_bundle__configuration_settings',
  105.             'name' => $this->tr->trans('Forecasted stock settings', [], 'ForecastedStockBundle')
  106.         ];
  107.         $event->setPages($pages);
  108.     }
  109.     /**
  110.      * @param MenuConfigurationPageEvent $event
  111.      * @return void
  112.      */
  113.     public function configurationMenu(MenuConfigurationPageEvent $event): void
  114.     {
  115.         $pages $event->getPages();
  116.         $stocksMenu array_search('stocks_bundle__configuration_settings'array_column($pages'id'));
  117.         if (is_numeric($stocksMenu)) {
  118.             $pages[$stocksMenu]['subpages'][] = ['id' => 'forecasted_stock_bundle__configuration_settings'];
  119.             $event->setPages($pages);
  120.         }
  121.     }
  122. }