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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\Security;
  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 Security $security;
  20.     private TranslatorInterface $translatorService;
  21.     private AdminRouteSecurityService $adminRouteSecurityService;
  22.     public function __construct(
  23.         Security $security,
  24.         TranslatorInterface $translatorService,
  25.         AdminRouteSecurityService $adminRouteSecurityService
  26.     ) {
  27.         $this->security $security;
  28.         $this->translatorService $translatorService;
  29.         $this->adminRouteSecurityService $adminRouteSecurityService;
  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->security->getUser();
  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.         $stripe = new MenuItemModel(
  176.             'stripe',
  177.             $this->translatorService->trans('menu.item.stripe'),
  178.             '',
  179.             [],
  180.             'fal fa-box',
  181.         );
  182.         $stripe->addChild(
  183.             new MenuItemModel(
  184.                 'stripeSubscriptions',
  185.                 $this->translatorService->trans('menu.item.stripe_subscriptions'),
  186.                 'stripe_subscription_admin_index',
  187.                 []
  188.             )
  189.         );
  190.         $stripe->addChild(
  191.             new MenuItemModel(
  192.                 'stripeNotifications',
  193.                 $this->translatorService->trans('menu.item.stripe_notifications'),
  194.                 'stripe_notification_admin_index',
  195.                 []
  196.             )
  197.         );
  198.         $event->addItem($stripe);
  199.         $returns = new MenuItemModel(
  200.             'returns',
  201.             $this->translatorService->trans('menu.item.returns'),
  202.             'return_admin_index',
  203.             [],
  204.             'fa-sharp fa-light fa-person-walking-arrow-loop-left',
  205.         );
  206.         $event->addItem($returns);
  207.         $catalog = new MenuItemModel(
  208.             'catalog',
  209.             $this->translatorService->trans('menu.item.catalog'),
  210.             '',
  211.             [],
  212.             'fa-sharp fa-light fa-boxes-stacked',
  213.         );
  214.         $catalog->addChild(
  215.             new MenuItemModel(
  216.                 'product',
  217.                 $this->translatorService->trans('menu.product'),
  218.                 'product_admin_index',
  219.                 [],
  220.                 'fa-sharp fa-light fa-box',
  221.             )
  222.         );
  223.         $catalog->addChild(
  224.             new MenuItemModel(
  225.                 'product_stock',
  226.                 $this->translatorService->trans('menu.product_stock'),
  227.                 'product_stock_admin_index',
  228.                 [],
  229.                 'fa-sharp fa-light fa-box',
  230.             )
  231.         );
  232.         $catalog->addChild(
  233.             new MenuItemModel(
  234.                 'product_attribute',
  235.                 $this->translatorService->trans('menu.product_attribute'),
  236.                 'product_attribute_admin_index',
  237.                 [],
  238.                 'fa-sharp fa-light fa-box',
  239.             )
  240.         );
  241.         $catalog->addChild(
  242.             new MenuItemModel(
  243.                 'taxonomy',
  244.                 $this->translatorService->trans('menu.taxonomy'),
  245.                 'taxonomy_admin_index',
  246.                 [],
  247.                 'fa-sharp fa-light fa-box',
  248.             )
  249.         );
  250.         $catalog->addChild(
  251.             new MenuItemModel(
  252.                 'catalog',
  253.                 $this->translatorService->trans('menu.catalog'),
  254.                 'catalog_admin_index',
  255.                 [],
  256.                 'fa-sharp fa-light fa-box',
  257.             )
  258.         );
  259.         $catalog->addChild(
  260.             new MenuItemModel(
  261.                 'gpsr',
  262.                 $this->translatorService->trans('menu.item.gpsr'),
  263.                 'gpsr_admin_index',
  264.                 [],
  265.                 'fa-sharp fa-light fa-box',
  266.             )
  267.         );
  268.         $event->addItem($catalog);
  269.         $report = new MenuItemModel(
  270.             'report',
  271.             $this->translatorService->trans('menu.item.report'),
  272.             '',
  273.             [],
  274.             'fal fa-list'
  275.         );
  276.         $report->addChild(
  277.             new MenuItemModel(
  278.                 'order_report',
  279.                 $this->translatorService->trans('menu.item.customer_order_report'),
  280.                 'customer_order_info_report_admin_index',
  281.                 []
  282.             )
  283.         );
  284.         $report->addChild(
  285.             new MenuItemModel(
  286.                 'paypal_payer_detail',
  287.                 $this->translatorService->trans('menu.item.paypal_payer_detail'),
  288.                 'paypal_payer_detail_admin_index',
  289.                 []
  290.             )
  291.         );
  292.         $event->addItem($report);
  293.         $configuration = new MenuItemModel(
  294.             'configuration',
  295.             $this->translatorService->trans('menu.item.configuration'),
  296.             '',
  297.             [],
  298.             'fal fa-gear'
  299.         );
  300.         $configuration->addChild(
  301.             new MenuItemModel(
  302.                 'paymentMethodConfiguration',
  303.                 $this->translatorService->trans('menu.item.payment_method_configuration'),
  304.                 'payment_method_configuration_admin_index',
  305.                 []
  306.             )
  307.         );
  308.         $configuration->addChild(
  309.             new MenuItemModel(
  310.                 'discountCode',
  311.                 $this->translatorService->trans('menu.item.discount_code_configuration'),
  312.                 'code_discount_admin_index',
  313.                 []
  314.             )
  315.         );
  316.         $configuration->addChild(
  317.             new MenuItemModel(
  318.                 'discountCodeRule',
  319.                 'Discount code rules',
  320.                 // $this->translatorService->trans('menu.item.discount_code_rule_configuration'),
  321.                 'code_discount_rule_admin_index',
  322.                 []
  323.             )
  324.         );
  325.         $configuration->addChild(
  326.             new MenuItemModel(
  327.                 'language',
  328.                 $this->translatorService->trans('menu.item.language'),
  329.                 'language_admin_index',
  330.                 []
  331.             )
  332.         );
  333.         $configuration->addChild(
  334.             new MenuItemModel(
  335.                 'country',
  336.                 $this->translatorService->trans('menu.item.country'),
  337.                 'country_admin_index',
  338.                 []
  339.             )
  340.         );
  341.         $configuration->addChild(
  342.             new MenuItemModel(
  343.                 'state',
  344.                 $this->translatorService->trans('menu.item.state'),
  345.                 'state_admin_index',
  346.                 []
  347.             )
  348.         );
  349.         $configuration->addChild(
  350.             new MenuItemModel(
  351.                 'employee',
  352.                 $this->translatorService->trans('menu.item.employees'),
  353.                 'employee_admin_index',
  354.                 []
  355.             )
  356.         );
  357.         $configuration->addChild(
  358.             new MenuItemModel(
  359.                 'acl',
  360.                 $this->translatorService->trans('menu.item.routes'),
  361.                 'acl_admin_index',
  362.                 []
  363.             )
  364.         );
  365.         $event->addItem($configuration);
  366.         $services = new MenuItemModel(
  367.             'services',
  368.             $this->translatorService->trans('menu.item.services'),
  369.             '',
  370.             [],
  371.             'fal fa-cart-shopping'
  372.         );
  373.         $services->addChild(
  374.             new MenuItemModel(
  375.                 'service_list',
  376.                 $this->translatorService->trans('menu.item.services_list'),
  377.                 'service_admin_index',
  378.                 []
  379.             )
  380.         );
  381.         $event->addItem($services);
  382.         $tapfiliate = new MenuItemModel(
  383.             'tapfiliate',
  384.             $this->translatorService->trans('menu.item.tapfiliate'),
  385.             '',
  386.             [],
  387.             'fal fa-cart-shopping',
  388.         );
  389.         $tapfiliate->addChild(
  390.             new MenuItemModel(
  391.                 'tapfiliate_customers',
  392.                 $this->translatorService->trans('menu.item.tapfiliate_customers'),
  393.                 'admin_tapfiliate_customers',
  394.                 []
  395.             )
  396.         );
  397.         $tapfiliate->addChild(
  398.             new MenuItemModel(
  399.                 'tapfiliate_conversion',
  400.                 $this->translatorService->trans('menu.item.tapfiliate_conversion'),
  401.                 'admin_tapfiliate_conversion',
  402.                 []
  403.             )
  404.         );
  405.         $event->addItem($tapfiliate);
  406.         $event->addItem(
  407.             new MenuItemModel(
  408.                 'old_admin',
  409.                 'Old Admin Panel',
  410.                 'old_admin',
  411.                 [],
  412.                 'fa fa-home'
  413.             )
  414.         );
  415.         $request $event->getRequest();
  416.         $this->accessControlEvent($event);
  417.         /** @var MenuItemModel[] $items */
  418.         $items $event->getItems();
  419.         $this->activateByRoute(
  420.             $request,
  421.             $event->getRequest()->get('_route'),
  422.             $items
  423.         );
  424.     }
  425.     /**
  426.      * @param MenuItemModel[] $items
  427.      */
  428.     protected function activateByRoute(
  429.         Request $request,
  430.         ?string $route,
  431.         array $items
  432.     ): void {
  433.         if (empty($route)) {
  434.             return;
  435.         }
  436.         foreach ($items as $item) {
  437.             if ($item->hasChildren()) {
  438.                 $this->activateByRoute($request$route$item->getChildren());
  439.             } elseif ($item->getRoute() === $route) {
  440.                 $item->setActive(true);
  441.                 return;
  442.             }
  443.         }
  444.     }
  445.     protected function accessControlEvent(
  446.         SidebarMenuEvent $event
  447.     ): void {
  448.         foreach ($event->getItems() as $item) {
  449.             if (!empty($item->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($item->getRoute())) {
  450.                 $event->removeItem($item);
  451.                 continue;
  452.             }
  453.             if ($item->hasChildren()) {
  454.                 $this->accessControlItem($item);
  455.                 if (!$item->hasChildren() && $item->getRoute() === '') {
  456.                     $event->removeItem($item);
  457.                 }
  458.             } elseif ($item->getRoute() === '') {
  459.                 $event->removeItem($item);
  460.             }
  461.         }
  462.     }
  463.     protected function accessControlItem(
  464.         MenuItemInterface $item
  465.     ): void {
  466.         $a 0;
  467.         /** @var MenuItemModel $child */
  468.         foreach ($item->getChildren() as $child) {
  469.             if (!empty($child->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($child->getRoute())) {
  470.                 $item->removeChild($child);
  471.                 continue;
  472.             }
  473.             if ($child->hasChildren()) {
  474.                 $this->accessControlItem($child);
  475.                 if (!$child->hasChildren() && $child->getRoute() === '') {
  476.                     $item->removeChild($child);
  477.                 }
  478.             } elseif ($child->getRoute() === '') {
  479.                 $item->removeChild($child);
  480.             }
  481.         }
  482.     }
  483. }