vendor/bluue/accounting-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\AccountingBundle\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 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__ACCOUNTING__READ_ONLY')) {
  59.             return;
  60.         }
  61.         $menu $event->getMenu();
  62.         $menuAccountingChilds = [
  63.             [
  64.                 'key' => 'accountingExport',
  65.                 'label' => $this->tr->trans('Accounting export', [], 'AccountingBundle'),
  66.                 'route' => 'accounting_bundle__export',
  67.             ]
  68.         ];
  69.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  70.         $accountingTranslate $this->tr->trans('Accounting', [], 'AccountingBundle');
  71.         $menu->addChild('AccountingMenu')
  72.             ->setLabel($accountingTranslate)
  73.             ->setExtra('icon''fas fa-calculator');
  74.         foreach ($menuAccountingChilds as $menuAccountingChild) {
  75.             $current $menuAccountingChild['route'] == $currentRoute;
  76.             $menu['AccountingMenu']->addChild(
  77.                 $menuAccountingChild['key'],
  78.                 [
  79.                     'route' => $menuAccountingChild['route'],
  80.                     'label' => $menuAccountingChild['label'],
  81.                     'current' => $current
  82.                 ]
  83.             );
  84.         }
  85.         if (CheckBundleInstall::exist('sales-bundle')) {
  86.             $menu['AccountingMenu']->addChild(
  87.                 $this->tr->trans('Payment expectations', [], 'AccountingBundle'),
  88.                 [
  89.                     'route' => 'sales_bundle__payment_expectation_list',
  90.                     'current' => 'sales_bundle__payment_expectation_list' == $currentRoute
  91.                 ]
  92.             );
  93.             $menu['AccountingMenu']->addChild(
  94.                 $this->tr->trans('Intra-community sales', [], 'AccountingBundle'),
  95.                 [
  96.                     'route' => 'sales_bundle__intracommunity_sales_list',
  97.                     'current' => 'sales_bundle__intracommunity_sales_list' == $currentRoute
  98.                 ]
  99.             );
  100.             if ($this->security->isGranted('ROLE__SALES__READ_ONLY')) {
  101.                 $menu['AccountingMenu']->addChild(
  102.                     $this->tr->trans('Payments export', [], 'AccountingBundle'),
  103.                     [
  104.                         'route' => 'sales_bundle__payment_export',
  105.                         'current' => 'sales_bundle__payment_export' == $currentRoute
  106.                     ]
  107.                 );
  108.             }
  109.         }
  110.     }
  111. }