vendor/bluue/stocks-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 66

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\StocksBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Services\CheckBundleInstall;
  11. use Symfony\Component\Security\Core\Security;
  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.      * @param RequestStack $requestStack
  31.      * @param TranslatorInterface $tr
  32.      * @param Security $security
  33.      */
  34.     public function __construct(
  35.         RequestStack $requestStack,
  36.         TranslatorInterface $tr,
  37.         Security $security
  38.     ) {
  39.         $this->requestStack $requestStack;
  40.         $this->tr $tr;
  41.         $this->security $security;
  42.     }
  43.     /**
  44.      * @return array
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             ConfigureMenuEvent::NAME => 'onLoad'
  50.         ];
  51.     }
  52.     /**
  53.      * @param ConfigureMenuEvent $event
  54.      * @return void
  55.      */
  56.     public function onLoad(ConfigureMenuEvent $event): void
  57.     {
  58.         if (!$this->security->isGranted('ROLE__STOCKS__READ_ONLY')) {
  59.             return;
  60.         }
  61.         $menu $event->getMenu();
  62.         $stockMenuChilds = [
  63.             [
  64.                 'key' => 'stocks',
  65.                 'label' => $this->tr->trans('Stocks', [], 'StocksBundle'),
  66.                 'route' => 'stocks_bundle__stock_product_list',
  67.                 'matchPath' => [
  68.                     'bundles/stocks-bundle/stock-product/stock-location/list/',
  69.                     'bundles/stocks-bundle/stock-product/stock-location/stock-quantity/list'
  70.                 ]
  71.             ],
  72.             [
  73.                 'key' => 'stock_movements',
  74.                 'label' => $this->tr->trans('Stock Movements', [], 'StocksBundle'),
  75.                 'route' => 'stocks_bundle__stock_movement_list',
  76.                 'matchPath' => [
  77.                     'bundles/stocks-bundle/stock-movement'
  78.                 ]
  79.             ],
  80.             [
  81.                 'key' => 'locations',
  82.                 'label' => $this->tr->trans('Locations', [], 'StocksBundle'),
  83.                 'route' => 'stocks_bundle__location_list',
  84.                 'matchPath' => [
  85.                     'bundles/stocks-bundle/location'
  86.                 ]
  87.             ],
  88.             [
  89.                 'key' => 'sub_locations',
  90.                 'label' => $this->tr->trans('Sub-locations', [], 'StocksBundle'),
  91.                 'route' => 'stocks_bundle__sub_location_list',
  92.                 'matchPath' => [
  93.                     'bundles/stocks-bundle/sub-location'
  94.                 ]
  95.             ],
  96.             [
  97.                 'key' => 'stock_limit_date',
  98.                 'label' => $this->tr->trans('Stock limit date', [], 'StocksBundle'),
  99.                 'route' => 'stocks_bundle__stock_location_limit_date_list'
  100.             ],
  101.             [
  102.                 'key' => 'stock_preparation',
  103.                 'label' => $this->tr->trans('Stock preparation', [], 'StocksBundle'),
  104.                 'route' => 'stocks_bundle__stock_preparation_list'
  105.             ],
  106.             [
  107.                 'key' => 'mass_stock_entries',
  108.                 'label' => $this->tr->trans('Mass entries', [], 'StocksBundle'),
  109.                 'route' => 'stocks_bundle__mass_stock_entry_list',
  110.                 'matchPath' => [
  111.                     'bundles/stocks-bundle/mass-stock-entry'
  112.                 ]
  113.             ],
  114.             [
  115.                 'key' => 'mass_stock_removal',
  116.                 'label' => $this->tr->trans('Mass stocks removal', [], 'StocksBundle'),
  117.                 'route' => 'stocks_bundle__mass_stock_removal_list',
  118.                 'matchPath' => [
  119.                     'bundles/stocks-bundle/mass-stock-removal'
  120.                 ]
  121.             ],
  122.         ];
  123.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  124.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  125.         $parentName $this->tr->trans('Stocks', [], 'StocksBundle');
  126.         $menuKey 'StocksMenu';
  127.         $menu->addChild($menuKey)
  128.             ->setLabel($parentName)
  129.             ->setExtra('icon''fas fa-solid fa-box-open');
  130.         foreach ($stockMenuChilds as $stockMenuChild) {
  131.             $current false;
  132.             if ($stockMenuChild['route'] == $currentRoute) {
  133.                 $current true;
  134.             } else {
  135.                 if (isset($stockMenuChild['matchPath'])) {
  136.                     foreach ($stockMenuChild['matchPath'] as $matchPath) {
  137.                         if (strpos($currentPath$matchPath) !== false) {
  138.                             $current true;
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.             $menu['StocksMenu']->addChild(
  144.                 $stockMenuChild['key'],
  145.                 [
  146.                     'route' => $stockMenuChild['route'],
  147.                     'label' => $stockMenuChild['label'],
  148.                     'current' => $current
  149.                 ]
  150.             );
  151.         }
  152.         if (CheckBundleInstall::exist('suppliers-orders-bundle')) {
  153.             $menu['StocksMenu']->addChild(
  154.                 'product_restock',
  155.                 [
  156.                     'label' => $this->tr->trans('Product restock', [], 'StocksBundle'),
  157.                     'route' => 'stocks_bundle__product_restock_list',
  158.                     'current' => in_array($currentRoute, [
  159.                         'stocks_bundle__product_restock_list',
  160.                         'stocks_bundle__product_restock_holding_list',
  161.                         'stocks_bundle__product_quantity_to_hold_list'
  162.                     ])
  163.                 ]
  164.             );
  165.         }
  166.     }
  167. }