src/EventSubscriber/SidebarMenuBuilderSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use A4BGroup\A4bThemeBundle\Event\SidebarMenuEvent;
  5. use A4BGroup\A4bThemeBundle\Event\BreadcrumbMenuEvent;
  6. use A4BGroup\A4bThemeBundle\Model\MenuItemInterface;
  7. use A4BGroup\A4bThemeBundle\Model\MenuItemModel;
  8. use App\Entity\System\Employee;
  9. use App\Service\AdminRouteSecurityService;
  10. use App\Service\SecurityService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15.  * Class SidebarMenuBuilderSubscriber configures the main navigation.
  16.  */
  17. class SidebarMenuBuilderSubscriber implements EventSubscriberInterface
  18. {
  19.     private TranslatorInterface $translatorService;
  20.     private AdminRouteSecurityService $adminRouteSecurityService;
  21.     private SecurityService $securityService;
  22.     public function __construct(
  23.         TranslatorInterface $translatorService,
  24.         AdminRouteSecurityService $adminRouteSecurityService,
  25.         SecurityService $securityService
  26.     ) {
  27.         $this->translatorService $translatorService;
  28.         $this->adminRouteSecurityService $adminRouteSecurityService;
  29.         $this->securityService $securityService;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             SidebarMenuEvent::class => 'onSetup',
  38.             BreadcrumbMenuEvent::class => 'onSetup',
  39.         ];
  40.     }
  41.     /**
  42.      * Generate the main menu.
  43.      *
  44.      * @param SidebarMenuEvent $event
  45.      */
  46.     public function onSetup(SidebarMenuEvent $event): void
  47.     {
  48.         /** @var Employee $employee */
  49.         $employee $this->securityService->findSessionEmployee();
  50.         $this->translatorService->setLocale($employee->getLocale() ?? \Locale::getDefault());
  51.         $event->addItem(
  52.             new MenuItemModel(
  53.                 'admin_dashboard',
  54.                 'Dashboard',
  55.                 'admin_dashboard_index',
  56.                 [],
  57.                 'fa-solid fa-grid-horizontal'
  58.             )
  59.         );
  60.         $event->addItem(
  61.             new MenuItemModel(
  62.                 'admin_cache_index',
  63.                 'Cache',
  64.                 'admin_cache_index',
  65.                 [],
  66.                 'fa-solid fa-recycle'
  67.             )
  68.         );
  69.         $event->addItem(
  70.             new MenuItemModel(
  71.                 'logs',
  72.                 $this->translatorService->trans('menu.item.logs'),
  73.                 'logs_admin_index',
  74.                 [],
  75.                 'fal fa-tag'
  76.             )
  77.         );
  78.         // MENU ACCOUNTING START
  79.         $accounting = new MenuItemModel(
  80.             'accounting',
  81.             $this->translatorService->trans('menu.item.accounting'),
  82.             '',
  83.             [],
  84.             'fal fa-user',
  85.         );
  86.         $accounting->addChild(
  87.             new MenuItemModel(
  88.                 'customers',
  89.                 $this->translatorService->trans('menu.item.customers'),
  90.                 'customer_admin_index',
  91.                 []
  92.             )
  93.         );
  94.         $accounting->addChild(
  95.             new MenuItemModel(
  96.                 'orders',
  97.                 $this->translatorService->trans('menu.item.orders'),
  98.                 'order_admin_index',
  99.                 []
  100.             )
  101.         );
  102.         $accounting->addChild(
  103.             new MenuItemModel(
  104.                 'orders_cancellation',
  105.                 'Solicitudes de cancelación de pedido',
  106.                 'order_cancellation_request_admin_index',
  107.                 []
  108.             )
  109.         );
  110.         $accounting->addChild(
  111.             new MenuItemModel(
  112.                 'carts',
  113.                 $this->translatorService->trans('menu.item.carts'),
  114.                 'cart_admin_index',
  115.                 []
  116.             )
  117.         );
  118.         $accounting->addChild(
  119.             new MenuItemModel(
  120.                 'vies',
  121.                 $this->translatorService->trans('menu.item.vies'),
  122.                 'vies_admin_index',
  123.                 []
  124.             )
  125.         );
  126.         $accounting->addChild(
  127.             new MenuItemModel(
  128.                 'wishlist',
  129.                 $this->translatorService->trans('menu.item.wishlist'),
  130.                 'wishlist_admin_index',
  131.                 []
  132.             )
  133.         );
  134.         $accounting->addChild(
  135.             new MenuItemModel(
  136.                 'ftp',
  137.                 $this->translatorService->trans('menu.item.ftp'),
  138.                 'ftp_admin_index',
  139.                 []
  140.             )
  141.         );
  142.         $accounting->addChild(
  143.             new MenuItemModel(
  144.                 'access control',
  145.                 $this->translatorService->trans('menu.item.access_control'),
  146.                 'customer_authentication_access_control_admin_index',
  147.                 []
  148.             )
  149.         );
  150.         $accounting->addChild(
  151.             new MenuItemModel(
  152.                 'access control log',
  153.                 $this->translatorService->trans('menu.item.access_control_log'),
  154.                 'customer_authentication_access_control_log_admin_index',
  155.                 []
  156.             )
  157.         );
  158.         $event->addItem($accounting);
  159.         $packs = new MenuItemModel(
  160.             'packs',
  161.             $this->translatorService->trans('menu.item.packs'),
  162.             '',
  163.             [],
  164.             'fal fa-box',
  165.         );
  166.         $packs->addChild(
  167.             new MenuItemModel(
  168.                 'purchasedPacks',
  169.                 $this->translatorService->trans('menu.item.purchased_packs'),
  170.                 'subscription_customer_admin_index',
  171.                 []
  172.             )
  173.         );
  174.         $event->addItem($packs);
  175.         $returns = new MenuItemModel(
  176.             'returns',
  177.             $this->translatorService->trans('menu.item.returns'),
  178.             'return_admin_index',
  179.             [],
  180.             'fa-sharp fa-light fa-person-walking-arrow-loop-left',
  181.         );
  182.         $event->addItem($returns);
  183.         $catalog = new MenuItemModel(
  184.             'catalog',
  185.             $this->translatorService->trans('menu.item.catalog'),
  186.             '',
  187.             [],
  188.             'fa-sharp fa-light fa-boxes-stacked',
  189.         );
  190.         $catalog->addChild(
  191.             new MenuItemModel(
  192.                 'product',
  193.                 $this->translatorService->trans('menu.product'),
  194.                 'product_admin_index',
  195.                 [],
  196.                 'fa-sharp fa-light fa-box',
  197.             )
  198.         );
  199.         $catalog->addChild(
  200.             new MenuItemModel(
  201.                 'product_stock',
  202.                 $this->translatorService->trans('menu.product_stock'),
  203.                 'product_stock_admin_index',
  204.                 [],
  205.                 'fa-sharp fa-light fa-box',
  206.             )
  207.         );
  208.         $catalog->addChild(
  209.             new MenuItemModel(
  210.                 'product_attribute',
  211.                 $this->translatorService->trans('menu.product_attribute'),
  212.                 'product_attribute_admin_index',
  213.                 [],
  214.                 'fa-sharp fa-light fa-box',
  215.             )
  216.         );
  217.         $catalog->addChild(
  218.             new MenuItemModel(
  219.                 'taxonomy',
  220.                 $this->translatorService->trans('menu.taxonomy'),
  221.                 'taxonomy_admin_index',
  222.                 [],
  223.                 'fa-sharp fa-light fa-box',
  224.             )
  225.         );
  226.         $catalog->addChild(
  227.             new MenuItemModel(
  228.                 'catalog',
  229.                 $this->translatorService->trans('menu.catalog'),
  230.                 'catalog_admin_index',
  231.                 [],
  232.                 'fa-sharp fa-light fa-box',
  233.             )
  234.         );
  235.         $catalog->addChild(
  236.             new MenuItemModel(
  237.                 'gpsr',
  238.                 $this->translatorService->trans('menu.item.gpsr'),
  239.                 'gpsr_admin_index',
  240.                 [],
  241.                 'fa-sharp fa-light fa-box',
  242.             )
  243.         );
  244.         $event->addItem($catalog);
  245.         $report = new MenuItemModel(
  246.             'report',
  247.             $this->translatorService->trans('menu.item.report'),
  248.             '',
  249.             [],
  250.             'fal fa-list'
  251.         );
  252.         $report->addChild(
  253.             new MenuItemModel(
  254.                 'order_report',
  255.                 $this->translatorService->trans('menu.item.customer_order_report'),
  256.                 'customer_order_info_report_admin_index',
  257.                 []
  258.             )
  259.         );
  260.         $report->addChild(
  261.             new MenuItemModel(
  262.                 'paypal_payer_detail',
  263.                 $this->translatorService->trans('menu.item.paypal_payer_detail'),
  264.                 'paypal_payer_detail_admin_index',
  265.                 []
  266.             )
  267.         );
  268.         $event->addItem($report);
  269.         $configuration = new MenuItemModel(
  270.             'configuration',
  271.             $this->translatorService->trans('menu.item.configuration'),
  272.             '',
  273.             [],
  274.             'fal fa-gear'
  275.         );
  276.         $configuration->addChild(
  277.             new MenuItemModel(
  278.                 'paymentMethodConfiguration',
  279.                 $this->translatorService->trans('menu.item.payment_method_configuration'),
  280.                 'payment_method_configuration_admin_index',
  281.                 []
  282.             )
  283.         );
  284.         $configuration->addChild(
  285.             new MenuItemModel(
  286.                 'discountCode',
  287.                 $this->translatorService->trans('menu.item.discount_code_configuration'),
  288.                 'code_discount_admin_index',
  289.                 []
  290.             )
  291.         );
  292.         $configuration->addChild(
  293.             new MenuItemModel(
  294.                 'discountCodeRule',
  295.                 'Discount code rules',
  296.                 // $this->translatorService->trans('menu.item.discount_code_rule_configuration'),
  297.                 'code_discount_rule_admin_index',
  298.                 []
  299.             )
  300.         );
  301.         $configuration->addChild(
  302.             new MenuItemModel(
  303.                 'language',
  304.                 $this->translatorService->trans('menu.item.language'),
  305.                 'language_admin_index',
  306.                 []
  307.             )
  308.         );
  309.         $configuration->addChild(
  310.             new MenuItemModel(
  311.                 'country',
  312.                 $this->translatorService->trans('menu.item.country'),
  313.                 'country_admin_index',
  314.                 []
  315.             )
  316.         );
  317.         $configuration->addChild(
  318.             new MenuItemModel(
  319.                 'state',
  320.                 $this->translatorService->trans('menu.item.state'),
  321.                 'state_admin_index',
  322.                 []
  323.             )
  324.         );
  325.         $configuration->addChild(
  326.             new MenuItemModel(
  327.                 'employee',
  328.                 $this->translatorService->trans('menu.item.employees'),
  329.                 'employee_admin_index',
  330.                 []
  331.             )
  332.         );
  333.         $configuration->addChild(
  334.             new MenuItemModel(
  335.                 'acl',
  336.                 $this->translatorService->trans('menu.item.routes'),
  337.                 'acl_admin_index',
  338.                 []
  339.             )
  340.         );
  341.         $event->addItem($configuration);
  342.         $services = new MenuItemModel(
  343.             'services',
  344.             $this->translatorService->trans('menu.item.services'),
  345.             '',
  346.             [],
  347.             'fal fa-cart-shopping'
  348.         );
  349.         $services->addChild(
  350.             new MenuItemModel(
  351.                 'service_list',
  352.                 $this->translatorService->trans('menu.item.services_list'),
  353.                 'service_admin_index',
  354.                 []
  355.             )
  356.         );
  357.         $event->addItem($services);
  358.         $tapfiliate = new MenuItemModel(
  359.             'tapfiliate',
  360.             $this->translatorService->trans('menu.item.tapfiliate'),
  361.             '',
  362.             [],
  363.             'fal fa-cart-shopping',
  364.         );
  365.         $tapfiliate->addChild(
  366.             new MenuItemModel(
  367.                 'tapfiliate_customers',
  368.                 $this->translatorService->trans('menu.item.tapfiliate_customers'),
  369.                 'admin_tapfiliate_customers',
  370.                 []
  371.             )
  372.         );
  373.         $tapfiliate->addChild(
  374.             new MenuItemModel(
  375.                 'tapfiliate_conversion',
  376.                 $this->translatorService->trans('menu.item.tapfiliate_conversion'),
  377.                 'admin_tapfiliate_conversion',
  378.                 []
  379.             )
  380.         );
  381.         $event->addItem($tapfiliate);
  382.         $event->addItem(
  383.             new MenuItemModel(
  384.                 'old_admin',
  385.                 'Old Admin Panel',
  386.                 'old_admin',
  387.                 [],
  388.                 'fa fa-home'
  389.             )
  390.         );
  391.         $request $event->getRequest();
  392.         $this->accessControlEvent($event);
  393.         /** @var MenuItemModel[] $items */
  394.         $items $event->getItems();
  395.         $this->activateByRoute(
  396.             $request,
  397.             $event->getRequest()->get('_route'),
  398.             $items
  399.         );
  400.     }
  401.     /**
  402.      * @param MenuItemModel[] $items
  403.      */
  404.     protected function activateByRoute(
  405.         Request $request,
  406.         ?string $route,
  407.         array $items
  408.     ): void {
  409.         if (empty($route)) {
  410.             return;
  411.         }
  412.         foreach ($items as $item) {
  413.             if ($item->hasChildren()) {
  414.                 $this->activateByRoute($request$route$item->getChildren());
  415.             } elseif ($item->getRoute() === $route) {
  416.                 $item->setActive(true);
  417.                 return;
  418.             }
  419.         }
  420.     }
  421.     protected function accessControlEvent(
  422.         SidebarMenuEvent $event
  423.     ): void {
  424.         foreach ($event->getItems() as $item) {
  425.             if (!empty($item->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($item->getRoute())) {
  426.                 $event->removeItem($item);
  427.                 continue;
  428.             }
  429.             if ($item->hasChildren()) {
  430.                 $this->accessControlItem($item);
  431.                 if (!$item->hasChildren() && $item->getRoute() === '') {
  432.                     $event->removeItem($item);
  433.                 }
  434.             } elseif ($item->getRoute() === '') {
  435.                 $event->removeItem($item);
  436.             }
  437.         }
  438.     }
  439.     protected function accessControlItem(
  440.         MenuItemInterface $item
  441.     ): void {
  442.         $a 0;
  443.         /** @var MenuItemModel $child */
  444.         foreach ($item->getChildren() as $child) {
  445.             if (!empty($child->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($child->getRoute())) {
  446.                 $item->removeChild($child);
  447.                 continue;
  448.             }
  449.             if ($child->hasChildren()) {
  450.                 $this->accessControlItem($child);
  451.                 if (!$child->hasChildren() && $child->getRoute() === '') {
  452.                     $item->removeChild($child);
  453.                 }
  454.             } elseif ($child->getRoute() === '') {
  455.                 $item->removeChild($child);
  456.             }
  457.         }
  458.     }
  459. }