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

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\SalesBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use App\Services\CheckBundleInstall;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Security;
  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.     protected 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 string[]
  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__SALES__READ_ONLY')) {
  59.             return;
  60.         }
  61.         $menu $event->getMenu();
  62.         $menuSalesChilds = [
  63.             [
  64.                 'key' => 'quotations',
  65.                 'label' => $this->tr->trans('Quotations', [], 'SalesBundle'),
  66.                 'route' => 'sales_bundle__quotation_list',
  67.                 'matchPath' => 'bundles/sales-bundle/quotation/'
  68.             ],
  69.             [
  70.                 'key' => 'orders',
  71.                 'label' => $this->tr->trans('Orders', [], 'SalesBundle'),
  72.                 'route' => 'sales_bundle__order_list',
  73.                 'matchPath' => 'bundles/sales-bundle/order/'
  74.             ],
  75.             [
  76.                 'key' => 'delivery_notes',
  77.                 'label' => $this->tr->trans('Delivery notes', [], 'SalesBundle'),
  78.                 'route' => 'sales_bundle__delivery_note_list',
  79.                 'matchPath' => 'bundles/sales-bundle/delivery-note/'
  80.             ],
  81.             [
  82.                 'key' => 'invoices',
  83.                 'label' => $this->tr->trans('Invoices', [], 'SalesBundle'),
  84.                 'route' => 'sales_bundle__invoice_list',
  85.                 'matchPath' => 'bundles/sales-bundle/invoice/'
  86.             ],
  87.             [
  88.                 'key' => 'credit_notes',
  89.                 'label' => $this->tr->trans('Credit notes', [], 'SalesBundle'),
  90.                 'route' => 'sales_bundle__credit_note_list',
  91.                 'matchPath' => 'bundles/sales-bundle/credit-note/'
  92.             ],
  93.             [
  94.                 'key' => 'payments',
  95.                 'label' => $this->tr->trans('Payments', [], 'SalesBundle'),
  96.                 'route' => 'sales_bundle__payment_list'
  97.             ]
  98.         ];
  99.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  100.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  101.         $salesTranslate $this->tr->trans('Sales', [], 'SalesBundle');
  102.         $menu->addChild('SalesMenu')
  103.             ->setLabel($salesTranslate)
  104.             ->setExtra('icon''fas fa-money-bill-alt');
  105.         foreach ($menuSalesChilds as $menuSalesChild) {
  106.             $current $menuSalesChild['route'] == $currentRoute
  107.                 || (
  108.                     !empty($menuSalesChild['matchPath'])
  109.                     && strpos($currentPath$menuSalesChild['matchPath']) !== false
  110.                 )
  111.             ;
  112.             $menu['SalesMenu']->addChild(
  113.                 $menuSalesChild['key'],
  114.                 [
  115.                     'route' => $menuSalesChild['route'],
  116.                     'label' => $menuSalesChild['label'],
  117.                     'current' => $current
  118.                 ]
  119.             );
  120.         }
  121.         $matchPath = [
  122.             'bundles/sales-bundle/customer'
  123.         ];
  124.         foreach ($matchPath as $match) {
  125.             if (strpos($currentPath$match) !== false) {
  126.                 $menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
  127.             }
  128.         }
  129.     }
  130. }