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-house'
  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-address-book',
  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.         $packs->addChild(
  183.             new MenuItemModel(
  184.                 'historyPacks',
  185.                 $this->translatorService->trans('menu.item.history_packs'),
  186.                 'admin_subscription_customer_history',
  187.                 []
  188.             )
  189.         );
  190.         $event->addItem($packs);
  191.         $shops = new MenuItemModel(
  192.             'shops',
  193.             $this->translatorService->trans('menu.item.shops'),
  194.             '',
  195.             [],
  196.             'fa-sharp fa-light fa-shop',
  197.         );
  198.         $shops->addChild(
  199.             new MenuItemModel(
  200.                 'shops',
  201.                 $this->translatorService->trans('menu.shops'),
  202.                 'shops_admin_index',
  203.                 [],
  204.             )
  205.         );
  206.         $shops->addChild(
  207.             new MenuItemModel(
  208.                 'shops_active',
  209.                 $this->translatorService->trans('menu.shops.active'),
  210.                 'shops_admin_index',
  211.                 ['filter' => 'active'],
  212.             )
  213.         );
  214.         $shops->addChild(
  215.             new MenuItemModel(
  216.                 'shops_prestashop',
  217.                 $this->translatorService->trans('menu.shops.prestashop'),
  218.                 'shops_admin_index',
  219.                 ['filter' => 'prestashop'],
  220.             )
  221.         );
  222.         $shops->addChild(
  223.             new MenuItemModel(
  224.                 'shops_shopify',
  225.                 $this->translatorService->trans('menu.shops.shopify'),
  226.                 'shops_admin_index',
  227.                 ['filter' => 'shopify'],
  228.             )
  229.         );
  230.         $event->addItem($shops);
  231.         $stripe = new MenuItemModel(
  232.             'stripe',
  233.             $this->translatorService->trans('menu.item.stripe'),
  234.             '',
  235.             [],
  236.             'fa-brands fa-stripe-s',
  237.         );
  238.         $stripe->addChild(
  239.             new MenuItemModel(
  240.                 'stripeSubscriptions',
  241.                 $this->translatorService->trans('menu.item.stripe_subscriptions'),
  242.                 'stripe_subscription_admin_index',
  243.                 []
  244.             )
  245.         );
  246.         $stripe->addChild(
  247.             new MenuItemModel(
  248.                 'stripeNotifications',
  249.                 $this->translatorService->trans('menu.item.stripe_notifications'),
  250.                 'stripe_notification_admin_index',
  251.                 []
  252.             )
  253.         );
  254.         $event->addItem($stripe);
  255.         $returns = new MenuItemModel(
  256.             'returns',
  257.             $this->translatorService->trans('menu.item.returns'),
  258.             '',
  259.             [],
  260.             'fal fa-right-left',
  261.         );
  262.         $returns->addChild(
  263.             new MenuItemModel(
  264.                 'return_admin_index',
  265.                 $this->translatorService->trans('menu.item.return_admin'),
  266.                 'return_admin_index',
  267.                 []
  268.             )
  269.         );
  270.         $returns->addChild(
  271.             new MenuItemModel(
  272.                 'return_admin_index',
  273.                 $this->translatorService->trans('menu.item.return_admin.sat'),
  274.                 'return_admin_index',
  275.                 ['filter' => 'sat'],
  276.             )
  277.         );
  278.         $event->addItem($returns);
  279.         $catalog = new MenuItemModel(
  280.             'catalog',
  281.             $this->translatorService->trans('menu.item.catalog'),
  282.             '',
  283.             [],
  284.             'fa-sharp fa-light fa-list-tree',
  285.         );
  286.         $catalog->addChild(
  287.             new MenuItemModel(
  288.                 'product',
  289.                 $this->translatorService->trans('menu.product'),
  290.                 'product_admin_index',
  291.                 [],
  292.             )
  293.         );
  294.         $catalog->addChild(
  295.             new MenuItemModel(
  296.                 'product_stock',
  297.                 $this->translatorService->trans('menu.product_stock'),
  298.                 'product_stock_admin_index',
  299.                 [],
  300.             )
  301.         );
  302.         $catalog->addChild(
  303.             new MenuItemModel(
  304.                 'product_attribute',
  305.                 $this->translatorService->trans('menu.product_attribute'),
  306.                 'product_attribute_admin_index',
  307.                 [],
  308.             )
  309.         );
  310.         $catalog->addChild(
  311.             new MenuItemModel(
  312.                 'taxonomy',
  313.                 $this->translatorService->trans('menu.taxonomy'),
  314.                 'taxonomy_admin_index',
  315.                 [],
  316.             )
  317.         );
  318.         $catalog->addChild(
  319.             new MenuItemModel(
  320.                 'catalog',
  321.                 $this->translatorService->trans('menu.catalog'),
  322.                 'catalog_admin_index',
  323.                 [],
  324.             )
  325.         );
  326.         $catalog->addChild(
  327.             new MenuItemModel(
  328.                 'gpsr',
  329.                 $this->translatorService->trans('menu.item.gpsr'),
  330.                 'gpsr_admin_index',
  331.                 [],
  332.             )
  333.         );
  334.         $event->addItem($catalog);
  335.         $report = new MenuItemModel(
  336.             'report',
  337.             $this->translatorService->trans('menu.item.report'),
  338.             '',
  339.             [],
  340.             'fal fa-cart-shopping'
  341.         );
  342.         $report->addChild(
  343.             new MenuItemModel(
  344.                 'paypal_payer_detail',
  345.                 $this->translatorService->trans('menu.item.paypal_payer_detail'),
  346.                 'paypal_payer_detail_admin_index',
  347.                 []
  348.             )
  349.         );
  350.         $report->addChild(
  351.             new MenuItemModel(
  352.                 'order_file_batches',
  353.                 $this->translatorService->trans('menu.item.order_file_batches'),
  354.                 'order_file_batch_admin_index',
  355.                 []
  356.             )
  357.         );
  358.         $event->addItem($report);
  359.         $configuration = new MenuItemModel(
  360.             'configuration',
  361.             $this->translatorService->trans('menu.item.configuration'),
  362.             '',
  363.             [],
  364.             'fal fa-gear'
  365.         );
  366.         $configuration->addChild(
  367.             new MenuItemModel(
  368.                 'paymentMethodConfiguration',
  369.                 $this->translatorService->trans('menu.item.payment_method_configuration'),
  370.                 'payment_method_configuration_admin_index',
  371.                 []
  372.             )
  373.         );
  374.         $configuration->addChild(
  375.             new MenuItemModel(
  376.                 'discountCode',
  377.                 $this->translatorService->trans('menu.item.discount_code_configuration'),
  378.                 'code_discount_admin_index',
  379.                 []
  380.             )
  381.         );
  382.         $configuration->addChild(
  383.             new MenuItemModel(
  384.                 'discountCodeRule',
  385.                 'Discount code rules',
  386.                 // $this->translatorService->trans('menu.item.discount_code_rule_configuration'),
  387.                 'code_discount_rule_admin_index',
  388.                 []
  389.             )
  390.         );
  391.         $configuration->addChild(
  392.             new MenuItemModel(
  393.                 'language',
  394.                 $this->translatorService->trans('menu.item.language'),
  395.                 'language_admin_index',
  396.                 []
  397.             )
  398.         );
  399.         $configuration->addChild(
  400.             new MenuItemModel(
  401.                 'country',
  402.                 $this->translatorService->trans('menu.item.country'),
  403.                 'country_admin_index',
  404.                 []
  405.             )
  406.         );
  407.         $configuration->addChild(
  408.             new MenuItemModel(
  409.                 'state',
  410.                 $this->translatorService->trans('menu.item.state'),
  411.                 'state_admin_index',
  412.                 []
  413.             )
  414.         );
  415.         $configuration->addChild(
  416.             new MenuItemModel(
  417.                 'employee',
  418.                 $this->translatorService->trans('menu.item.employees'),
  419.                 'employee_admin_index',
  420.                 []
  421.             )
  422.         );
  423.         $configuration->addChild(
  424.             new MenuItemModel(
  425.                 'acl',
  426.                 $this->translatorService->trans('menu.item.routes'),
  427.                 'acl_admin_index',
  428.                 []
  429.             )
  430.         );
  431.         $configuration->addChild(
  432.             new MenuItemModel(
  433.                 'topSales',
  434.                 'Top Ventas',
  435.                 'top_sales_admin_index',
  436.                 []
  437.             )
  438.         );
  439.         $event->addItem($configuration);
  440.         $services = new MenuItemModel(
  441.             'services',
  442.             $this->translatorService->trans('menu.item.services'),
  443.             '',
  444.             [],
  445.             'fal fa-network-wired'
  446.         );
  447.         $services->addChild(
  448.             new MenuItemModel(
  449.                 'service_list',
  450.                 $this->translatorService->trans('menu.item.services_list'),
  451.                 'service_admin_index',
  452.                 []
  453.             )
  454.         );
  455.         $event->addItem($services);
  456.         $tapfiliate = new MenuItemModel(
  457.             'tapfiliate',
  458.             $this->translatorService->trans('menu.item.tapfiliate'),
  459.             '',
  460.             [],
  461.             'fal fa-money-check',
  462.         );
  463.         $tapfiliate->addChild(
  464.             new MenuItemModel(
  465.                 'tapfiliate_customers',
  466.                 $this->translatorService->trans('menu.item.tapfiliate_customers'),
  467.                 'admin_tapfiliate_customers',
  468.                 []
  469.             )
  470.         );
  471.         $tapfiliate->addChild(
  472.             new MenuItemModel(
  473.                 'tapfiliate_conversion',
  474.                 $this->translatorService->trans('menu.item.tapfiliate_conversion'),
  475.                 'admin_tapfiliate_conversion',
  476.                 []
  477.             )
  478.         );
  479.         $event->addItem($tapfiliate);
  480.         $event->addItem(
  481.             new MenuItemModel(
  482.                 'alert_management_admin_index',
  483.                 $this->translatorService->trans('menu.item.alert_management'),
  484.                 'alert_management_admin_index',
  485.                 [],
  486.                 'fa fa-triangle-exclamation'
  487.             )
  488.         );
  489.         $request $event->getRequest();
  490.         $this->accessControlEvent($event);
  491.         /** @var MenuItemModel[] $items */
  492.         $items $event->getItems();
  493.         $this->activateByRoute(
  494.             $request,
  495.             $event->getRequest()->get('_route'),
  496.             $items
  497.         );
  498.     }
  499.     /**
  500.      * @param MenuItemModel[] $items
  501.      */
  502.     protected function activateByRoute(
  503.         Request $request,
  504.         ?string $route,
  505.         array $items
  506.     ): void {
  507.         if (empty($route)) {
  508.             return;
  509.         }
  510.         foreach ($items as $item) {
  511.             if ($item->hasChildren()) {
  512.                 $this->activateByRoute($request$route$item->getChildren());
  513.             } elseif ($item->getRoute() === $route) {
  514.                 $item->setActive(true);
  515.                 return;
  516.             }
  517.         }
  518.     }
  519.     protected function accessControlEvent(
  520.         SidebarMenuEvent $event
  521.     ): void {
  522.         foreach ($event->getItems() as $item) {
  523.             if (!empty($item->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($item->getRoute())) {
  524.                 $event->removeItem($item);
  525.                 continue;
  526.             }
  527.             if ($item->hasChildren()) {
  528.                 $this->accessControlItem($item);
  529.                 if (!$item->hasChildren() && $item->getRoute() === '') {
  530.                     $event->removeItem($item);
  531.                 }
  532.             } elseif ($item->getRoute() === '') {
  533.                 $event->removeItem($item);
  534.             }
  535.         }
  536.     }
  537.     protected function accessControlItem(
  538.         MenuItemInterface $item
  539.     ): void {
  540.         $a 0;
  541.         /** @var MenuItemModel $child */
  542.         foreach ($item->getChildren() as $child) {
  543.             if (!empty($child->getRoute()) && !$this->adminRouteSecurityService->accessToRoute($child->getRoute())) {
  544.                 $item->removeChild($child);
  545.                 continue;
  546.             }
  547.             if ($child->hasChildren()) {
  548.                 $this->accessControlItem($child);
  549.                 if (!$child->hasChildren() && $child->getRoute() === '') {
  550.                     $item->removeChild($child);
  551.                 }
  552.             } elseif ($child->getRoute() === '') {
  553.                 $item->removeChild($child);
  554.             }
  555.         }
  556.     }
  557. }