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.                 'customers_invalid_subscriptions',
  97.                 $this->translatorService->trans('menu.item.customers_invalid_subscriptions'),
  98.                 'customer_admin_invalid_subscriptions',
  99.                 []
  100.             )
  101.         );
  102.         $accounting->addChild(
  103.             new MenuItemModel(
  104.                 'orders',
  105.                 $this->translatorService->trans('menu.item.orders'),
  106.                 'order_admin_index',
  107.                 []
  108.             )
  109.         );
  110.         $accounting->addChild(
  111.             new MenuItemModel(
  112.                 'orders_cancellation',
  113.                 'Solicitudes de cancelación de pedido',
  114.                 'order_cancellation_request_admin_index',
  115.                 []
  116.             )
  117.         );
  118.         $accounting->addChild(
  119.             new MenuItemModel(
  120.                 'carts',
  121.                 $this->translatorService->trans('menu.item.carts'),
  122.                 'cart_admin_index',
  123.                 []
  124.             )
  125.         );
  126.         $accounting->addChild(
  127.             new MenuItemModel(
  128.                 'vies',
  129.                 $this->translatorService->trans('menu.item.vies'),
  130.                 'vies_admin_index',
  131.                 []
  132.             )
  133.         );
  134.         $accounting->addChild(
  135.             new MenuItemModel(
  136.                 'wishlist',
  137.                 $this->translatorService->trans('menu.item.wishlist'),
  138.                 'wishlist_admin_index',
  139.                 []
  140.             )
  141.         );
  142.         $accounting->addChild(
  143.             new MenuItemModel(
  144.                 'ftp',
  145.                 $this->translatorService->trans('menu.item.ftp'),
  146.                 'ftp_admin_index',
  147.                 []
  148.             )
  149.         );
  150.         $accounting->addChild(
  151.             new MenuItemModel(
  152.                 'access control',
  153.                 $this->translatorService->trans('menu.item.access_control'),
  154.                 'customer_authentication_access_control_admin_index',
  155.                 []
  156.             )
  157.         );
  158.         $accounting->addChild(
  159.             new MenuItemModel(
  160.                 'access control log',
  161.                 $this->translatorService->trans('menu.item.access_control_log'),
  162.                 'customer_authentication_access_control_log_admin_index',
  163.                 []
  164.             )
  165.         );
  166.         $event->addItem($accounting);
  167.         $packs = new MenuItemModel(
  168.             'packs',
  169.             $this->translatorService->trans('menu.item.packs'),
  170.             '',
  171.             [],
  172.             'fal fa-box',
  173.         );
  174.         $packs->addChild(
  175.             new MenuItemModel(
  176.                 'purchasedPacks',
  177.                 $this->translatorService->trans('menu.item.purchased_packs'),
  178.                 'subscription_customer_admin_index',
  179.                 []
  180.             )
  181.         );
  182.         $event->addItem($packs);
  183.         $stripe = new MenuItemModel(
  184.             'stripe',
  185.             $this->translatorService->trans('menu.item.stripe'),
  186.             '',
  187.             [],
  188.             'fal fa-box',
  189.         );
  190.         $stripe->addChild(
  191.             new MenuItemModel(
  192.                 'stripeSubscriptions',
  193.                 $this->translatorService->trans('menu.item.stripe_subscriptions'),
  194.                 'stripe_subscription_admin_index',
  195.                 []
  196.             )
  197.         );
  198.         $stripe->addChild(
  199.             new MenuItemModel(
  200.                 'stripeNotifications',
  201.                 $this->translatorService->trans('menu.item.stripe_notifications'),
  202.                 'stripe_notification_admin_index',
  203.                 []
  204.             )
  205.         );
  206.         $event->addItem($stripe);
  207.         $returns = new MenuItemModel(
  208.             'returns',
  209.             $this->translatorService->trans('menu.item.returns'),
  210.             'return_admin_index',
  211.             [],
  212.             'fa-sharp fa-light fa-person-walking-arrow-loop-left',
  213.         );
  214.         $event->addItem($returns);
  215.         $catalog = new MenuItemModel(
  216.             'catalog',
  217.             $this->translatorService->trans('menu.item.catalog'),
  218.             '',
  219.             [],
  220.             'fa-sharp fa-light fa-boxes-stacked',
  221.         );
  222.         $catalog->addChild(
  223.             new MenuItemModel(
  224.                 'product',
  225.                 $this->translatorService->trans('menu.product'),
  226.                 'product_admin_index',
  227.                 [],
  228.                 'fa-sharp fa-light fa-box',
  229.             )
  230.         );
  231.         $catalog->addChild(
  232.             new MenuItemModel(
  233.                 'product_stock',
  234.                 $this->translatorService->trans('menu.product_stock'),
  235.                 'product_stock_admin_index',
  236.                 [],
  237.                 'fa-sharp fa-light fa-box',
  238.             )
  239.         );
  240.         $catalog->addChild(
  241.             new MenuItemModel(
  242.                 'product_attribute',
  243.                 $this->translatorService->trans('menu.product_attribute'),
  244.                 'product_attribute_admin_index',
  245.                 [],
  246.                 'fa-sharp fa-light fa-box',
  247.             )
  248.         );
  249.         $catalog->addChild(
  250.             new MenuItemModel(
  251.                 'taxonomy',
  252.                 $this->translatorService->trans('menu.taxonomy'),
  253.                 'taxonomy_admin_index',
  254.                 [],
  255.                 'fa-sharp fa-light fa-box',
  256.             )
  257.         );
  258.         $catalog->addChild(
  259.             new MenuItemModel(
  260.                 'catalog',
  261.                 $this->translatorService->trans('menu.catalog'),
  262.                 'catalog_admin_index',
  263.                 [],
  264.                 'fa-sharp fa-light fa-box',
  265.             )
  266.         );
  267.         $catalog->addChild(
  268.             new MenuItemModel(
  269.                 'gpsr',
  270.                 $this->translatorService->trans('menu.item.gpsr'),
  271.                 'gpsr_admin_index',
  272.                 [],
  273.                 'fa-sharp fa-light fa-box',
  274.             )
  275.         );
  276.         $event->addItem($catalog);
  277.         $report = new MenuItemModel(
  278.             'report',
  279.             $this->translatorService->trans('menu.item.report'),
  280.             '',
  281.             [],
  282.             'fal fa-list'
  283.         );
  284.         $report->addChild(
  285.             new MenuItemModel(
  286.                 'order_report',
  287.                 $this->translatorService->trans('menu.item.customer_order_report'),
  288.                 'customer_order_info_report_admin_index',
  289.                 []
  290.             )
  291.         );
  292.         $report->addChild(
  293.             new MenuItemModel(
  294.                 'paypal_payer_detail',
  295.                 $this->translatorService->trans('menu.item.paypal_payer_detail'),
  296.                 'paypal_payer_detail_admin_index',
  297.                 []
  298.             )
  299.         );
  300.         $event->addItem($report);
  301.         $configuration = new MenuItemModel(
  302.             'configuration',
  303.             $this->translatorService->trans('menu.item.configuration'),
  304.             '',
  305.             [],
  306.             'fal fa-gear'
  307.         );
  308.         $configuration->addChild(
  309.             new MenuItemModel(
  310.                 'paymentMethodConfiguration',
  311.                 $this->translatorService->trans('menu.item.payment_method_configuration'),
  312.                 'payment_method_configuration_admin_index',
  313.                 []
  314.             )
  315.         );
  316.         $configuration->addChild(
  317.             new MenuItemModel(
  318.                 'discountCode',
  319.                 $this->translatorService->trans('menu.item.discount_code_configuration'),
  320.                 'code_discount_admin_index',
  321.                 []
  322.             )
  323.         );
  324.         $configuration->addChild(
  325.             new MenuItemModel(
  326.                 'discountCodeRule',
  327.                 'Discount code rules',
  328.                 // $this->translatorService->trans('menu.item.discount_code_rule_configuration'),
  329.                 'code_discount_rule_admin_index',
  330.                 []
  331.             )
  332.         );
  333.         $configuration->addChild(
  334.             new MenuItemModel(
  335.                 'language',
  336.                 $this->translatorService->trans('menu.item.language'),
  337.                 'language_admin_index',
  338.                 []
  339.             )
  340.         );
  341.         $configuration->addChild(
  342.             new MenuItemModel(
  343.                 'country',
  344.                 $this->translatorService->trans('menu.item.country'),
  345.                 'country_admin_index',
  346.                 []
  347.             )
  348.         );
  349.         $configuration->addChild(
  350.             new MenuItemModel(
  351.                 'state',
  352.                 $this->translatorService->trans('menu.item.state'),
  353.                 'state_admin_index',
  354.                 []
  355.             )
  356.         );
  357.         $configuration->addChild(
  358.             new MenuItemModel(
  359.                 'employee',
  360.                 $this->translatorService->trans('menu.item.employees'),
  361.                 'employee_admin_index',
  362.                 []
  363.             )
  364.         );
  365.         $configuration->addChild(
  366.             new MenuItemModel(
  367.                 'acl',
  368.                 $this->translatorService->trans('menu.item.routes'),
  369.                 'acl_admin_index',
  370.                 []
  371.             )
  372.         );
  373.         $event->addItem($configuration);
  374.         $services = new MenuItemModel(
  375.             'services',
  376.             $this->translatorService->trans('menu.item.services'),
  377.             '',
  378.             [],
  379.             'fal fa-cart-shopping'
  380.         );
  381.         $services->addChild(
  382.             new MenuItemModel(
  383.                 'service_list',
  384.                 $this->translatorService->trans('menu.item.services_list'),
  385.                 'service_admin_index',
  386.                 []
  387.             )
  388.         );
  389.         $event->addItem($services);
  390.         $tapfiliate = new MenuItemModel(
  391.             'tapfiliate',
  392.             $this->translatorService->trans('menu.item.tapfiliate'),
  393.             '',
  394.             [],
  395.             'fal fa-cart-shopping',
  396.         );
  397.         $tapfiliate->addChild(
  398.             new MenuItemModel(
  399.                 'tapfiliate_customers',
  400.                 $this->translatorService->trans('menu.item.tapfiliate_customers'),
  401.                 'admin_tapfiliate_customers',
  402.                 []
  403.             )
  404.         );
  405.         $tapfiliate->addChild(
  406.             new MenuItemModel(
  407.                 'tapfiliate_conversion',
  408.                 $this->translatorService->trans('menu.item.tapfiliate_conversion'),
  409.                 'admin_tapfiliate_conversion',
  410.                 []
  411.             )
  412.         );
  413.         $event->addItem($tapfiliate);
  414.         $shops = new MenuItemModel(
  415.             'shops',
  416.             $this->translatorService->trans('menu.item.shops'),
  417.             '',
  418.             [],
  419.             'fa-sharp fa-light fa-boxes-stacked',
  420.         );
  421.         $shops->addChild(
  422.             new MenuItemModel(
  423.                 'shops',
  424.                 $this->translatorService->trans('menu.shops'),
  425.                 'shops_admin_index',
  426.                 [],
  427.                 'fa-sharp fa-light fa-box',
  428.             )
  429.         );
  430.         $shops->addChild(
  431.             new MenuItemModel(
  432.                 'shops_active',
  433.                 $this->translatorService->trans('menu.shops.active'),
  434.                 'shops_admin_index',
  435.                 ['filter' => 'active'],
  436.                 'fa-sharp fa-light fa-box',
  437.             )
  438.         );
  439.         $shops->addChild(
  440.             new MenuItemModel(
  441.                 'shops_prestashop',
  442.                 $this->translatorService->trans('menu.shops.prestashop'),
  443.                 'shops_admin_index',
  444.                 ['filter' => 'prestashop'],
  445.                 'fa-sharp fa-light fa-box',
  446.             )
  447.         );
  448.         $shops->addChild(
  449.             new MenuItemModel(
  450.                 'shops_shopify',
  451.                 $this->translatorService->trans('menu.shops.shopify'),
  452.                 'shops_admin_index',
  453.                 ['filter' => 'shopify'],
  454.                 'fa-sharp fa-light fa-box',
  455.             )
  456.         );
  457.         $event->addItem($shops);
  458.         $event->addItem(
  459.             new MenuItemModel(
  460.                 'alert_management_admin_index',
  461.                 $this->translatorService->trans('menu.item.alert_management'),
  462.                 'alert_management_admin_index',
  463.                 [],
  464.                 'fa fa-triangle-exclamation'
  465.             )
  466.         );
  467.         $event->addItem(
  468.             new MenuItemModel(
  469.                 'old_admin',
  470.                 'Old Admin Panel',
  471.                 'old_admin',
  472.                 [],
  473.                 'fa fa-home'
  474.             )
  475.         );
  476.         $request $event->getRequest();
  477.         $this->accessControlEvent($event);
  478.         /** @var MenuItemModel[] $items */
  479.         $items $event->getItems();
  480.         $this->activateByRoute(
  481.             $request,
  482.             $event->getRequest()->get('_route'),
  483.             $items
  484.         );
  485.     }
  486.     /**
  487.      * @param MenuItemModel[] $items
  488.      */
  489.     protected function activateByRoute(
  490.         Request $request,
  491.         ?string $route,
  492.         array $items
  493.     ): void {
  494.         if (empty($route)) {
  495.             return;
  496.         }
  497.         foreach ($items as $item) {
  498.             if ($item->hasChildren()) {
  499.                 $this->activateByRoute($request$route$item->getChildren());
  500.             } elseif ($item->getRoute() === $route) {
  501.                 $item->setActive(true);
  502.                 return;
  503.             }
  504.         }
  505.     }
  506.     protected function accessControlEvent(
  507.         SidebarMenuEvent $event
  508.     ): void {
  509.         foreach ($event->getItems() as $item) {
  510.             if (!empty($item->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($item->getRoute())) {
  511.                 $event->removeItem($item);
  512.                 continue;
  513.             }
  514.             if ($item->hasChildren()) {
  515.                 $this->accessControlItem($item);
  516.                 if (!$item->hasChildren() && $item->getRoute() === '') {
  517.                     $event->removeItem($item);
  518.                 }
  519.             } elseif ($item->getRoute() === '') {
  520.                 $event->removeItem($item);
  521.             }
  522.         }
  523.     }
  524.     protected function accessControlItem(
  525.         MenuItemInterface $item
  526.     ): void {
  527.         $a 0;
  528.         /** @var MenuItemModel $child */
  529.         foreach ($item->getChildren() as $child) {
  530.             if (!empty($child->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($child->getRoute())) {
  531.                 $item->removeChild($child);
  532.                 continue;
  533.             }
  534.             if ($child->hasChildren()) {
  535.                 $this->accessControlItem($child);
  536.                 if (!$child->hasChildren() && $child->getRoute() === '') {
  537.                     $item->removeChild($child);
  538.                 }
  539.             } elseif ($child->getRoute() === '') {
  540.                 $item->removeChild($child);
  541.             }
  542.         }
  543.     }
  544. }