src/App/Menu/AdminMenuListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Menu;
  3. use App\Entity\Core\AdminUser;
  4. use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
  5. use Knp\Menu\ItemInterface;
  6. use Sylius\Component\Core\Model\AdminUserInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. final class AdminMenuListener
  9. {
  10.     private $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     public function addAdminMenuItems(MenuBuilderEvent $event): void
  16.     {
  17.         $menu $event->getMenu();
  18.         $this->clearAllChildren($menu);
  19.         $this->showMenusForUser($menu);
  20.     }
  21.     private function addContentSubMenu(ItemInterface $menu): void
  22.     {
  23.         $contentSubmenu $menu
  24.             ->addChild('content')
  25.             ->setLabel('admin.menu.content.label');
  26.         $contentSubmenu
  27.             ->addChild('new', ['route' => 'app_admin_section_index'])
  28.             ->setLabel('admin.menu.content.section.label')
  29.             ->setLabelAttribute('icon''image')
  30.         ;
  31.         $contentSubmenu
  32.             ->addChild('offer', ['route' => 'app_admin_offer_index'])
  33.             ->setLabel('admin.menu.catalog.offer.label')
  34.             ->setLabelAttribute('icon''rocket')
  35.         ;
  36.     }
  37.     public function clearAllChildren(ItemInterface $menu)
  38.     {
  39.         $menu
  40.             ->removeChild('catalog')
  41.             ->removeChild('configuration')
  42.             ->removeChild('sales')
  43.             ->removeChild('marketing')
  44.             ->removeChild('customers')
  45.         ;
  46.     }
  47.     private function loadAdminMenus(ItemInterface $menu): void
  48.     {
  49.         $this->addContentSubMenu($menu);
  50.         $this->addAdminCatalogSubMenu($menu);
  51.         $this->addAdminCustomersSubMenu($menu);
  52.         // $this->addAdminMarketingSubMenu($menu);
  53.         $this->addAdminSalesSubMenu($menu);
  54.         $this->addAdminConfigurationSubMenu($menu);
  55.         $this->reorderMenu($menu);
  56.     }
  57.     private function loadDesignerMenus(ItemInterface $menu): void
  58.     {
  59.         $this->addContentSubMenu($menu);
  60.         $this->addAdminCatalogSubMenu($menu);
  61.     }
  62.     private function loadEstimationMenus(ItemInterface $menu): void
  63.     {
  64.         $this->addEstimationSalesSubMenu($menu);
  65.     }
  66.     private function loadOrderMenus(ItemInterface $menu): void
  67.     {
  68.         $this->addOrderSalesSubMenu($menu);
  69.     }
  70.     public function showMenusForUser(ItemInterface $menu): void {
  71.         $user $this->security->getUser();
  72.         if ($user->hasRole(AdminUserInterface::DEFAULT_ADMIN_ROLE)) {
  73.             $this->loadAdminMenus($menu);
  74.         }
  75.         if ($user->hasRole(AdminUser::DESIGNER)) {
  76.             $this->loadDesignerMenus($menu);
  77.         }
  78.         if ($user->hasRole(AdminUser::ESTIMATION)) {
  79.             $this->loadEstimationMenus($menu);
  80.         }
  81.         if ($user->hasRole(AdminUser::ORDER)) {
  82.             $this->loadOrderMenus($menu);
  83.         }
  84.     }
  85.     private function addAdminCatalogSubMenu(ItemInterface $menu): void
  86.     {
  87.         $catalog $menu
  88.             ->addChild('catalog')
  89.             ->setLabel('sylius.menu.admin.main.catalog.header');
  90.         $catalog
  91.             ->addChild('taxons', ['route' => 'sylius_admin_taxon_create'])
  92.             ->setLabel('sylius.menu.admin.main.catalog.taxons')
  93.             ->setLabelAttribute('icon''folder');
  94.         $catalog
  95.             ->addChild('products', ['route' => 'sylius_admin_product_index'])
  96.             ->setLabel('sylius.menu.admin.main.catalog.products')
  97.             ->setLabelAttribute('icon''cube');
  98.         $catalog
  99.             ->addChild('related-product', ['route' => 'app_admin_related_product_index'])
  100.             ->setLabel('admin.menu.catalog.related_product.label')
  101.             ->setLabelAttribute('icon''star');
  102.         $catalog
  103.             ->addChild('tag', ['route' => 'app_admin_tag_index'])
  104.             ->setLabel('admin.menu.catalog.tag.label')
  105.             ->setLabelAttribute('icon''tag')
  106.         ;
  107.         // $catalog
  108.         //     ->addChild('inventory', ['route' => 'sylius_admin_inventory_index'])
  109.         //     ->setLabel('sylius.menu.admin.main.catalog.inventory')
  110.         //     ->setLabelAttribute('icon', 'history');
  111.     }
  112.     private function addAdminCustomersSubMenu(ItemInterface $menu): void
  113.     {
  114.         $customers $menu
  115.             ->addChild('customers')
  116.             ->setLabel('sylius.menu.admin.main.customers.header');
  117.         $customers
  118.             ->addChild('customers', ['route' => 'sylius_admin_customer_index'])
  119.             ->setLabel('sylius.menu.admin.main.customers.customers')
  120.             ->setLabelAttribute('icon''users');
  121.     }
  122.     private function addAdminMarketingSubMenu(ItemInterface $menu): void
  123.     {
  124.         $marketing $menu
  125.             ->addChild('marketing')
  126.             ->setLabel('sylius.menu.admin.main.marketing.header');
  127.         $marketing
  128.             ->addChild('promotions', ['route' => 'sylius_admin_promotion_index'])
  129.             ->setLabel('sylius.menu.admin.main.marketing.promotions')
  130.             ->setLabelAttribute('icon''in cart');
  131.     }
  132.     private function addAdminSalesSubMenu(ItemInterface $menu): void
  133.     {
  134.         $sales $menu
  135.             ->addChild('sales')
  136.             ->setLabel('sylius.menu.admin.main.sales.header');
  137.         $sales
  138.             ->addChild('orders', ['route' => 'sylius_admin_order_index'])
  139.             ->setLabel('sylius.menu.admin.main.sales.orders')
  140.             ->setLabelAttribute('icon''cart');
  141.         $sales
  142.             ->addChild('estimations', ['route' => 'sylius_admin_order_item_index'])
  143.             ->setLabel('sylius.menu.admin.main.sales.estimations')
  144.             ->setLabelAttribute('icon''cart');
  145.         // $sales
  146.         //     ->addChild('shipemnts', ['route' => 'sylius_admin_shipment_index'])
  147.         //     ->setLabel('sylius.ui.shipments')
  148.         //     ->setLabelAttribute('icon', 'truck');
  149.     }
  150.     private function addAdminConfigurationSubMenu(ItemInterface $menu): void
  151.     {
  152.         $configuration $menu
  153.             ->addChild('configuration')
  154.             ->setLabel('sylius.menu.admin.main.configuration.header');
  155.         $configuration
  156.             ->addChild('payment_methods', ['route' => 'sylius_admin_payment_method_index'])
  157.             ->setLabel('sylius.menu.admin.main.configuration.payment_methods')
  158.             ->setLabelAttribute('icon''payment');
  159.         $configuration
  160.             ->addChild('shipping_methods', ['route' => 'sylius_admin_shipping_method_index'])
  161.             ->setLabel('sylius.menu.admin.main.configuration.shipping_methods')
  162.             ->setLabelAttribute('icon''shipping');
  163.         $configuration
  164.             ->addChild('shipping_categories', ['route' => 'sylius_admin_shipping_category_index'])
  165.             ->setLabel('sylius.menu.admin.main.configuration.shipping_categories')
  166.             ->setLabelAttribute('icon''shipping');
  167.         $configuration
  168.             ->addChild('tax_rates', ['route' => 'sylius_admin_tax_rate_index'])
  169.             ->setLabel('sylius.menu.admin.main.configuration.tax_rates')
  170.             ->setLabelAttribute('icon''money');
  171.         $configuration
  172.             ->addChild('admin_users', ['route' => 'sylius_admin_admin_user_index'])
  173.             ->setLabel('sylius.menu.admin.main.configuration.admin_users')
  174.             ->setLabelAttribute('icon''lock');
  175.     }
  176.     private function addOrderSalesSubMenu(ItemInterface $menu): void
  177.     {
  178.         $sales $menu
  179.             ->addChild('sales')
  180.             ->setLabel('sylius.menu.admin.main.sales.header');
  181.         $sales
  182.             ->addChild('orders', ['route' => 'sylius_admin_order_index'])
  183.             ->setLabel('sylius.menu.admin.main.sales.orders')
  184.             ->setLabelAttribute('icon''cart');
  185.     }
  186.     private function addEstimationSalesSubMenu(ItemInterface $menu): void
  187.     {
  188.         $sales $menu
  189.             ->addChild('sales')
  190.             ->setLabel('sylius.menu.admin.main.sales.header');
  191.         $sales
  192.             ->addChild('estimations', ['route' => 'sylius_admin_order_item_index'])
  193.             ->setLabel('sylius.menu.admin.main.sales.estimations')
  194.             ->setLabelAttribute('icon''cart');
  195.     }
  196.     private function reorderMenu(ItemInterface $menu): void
  197.     {
  198.         $order = [
  199.             'configuration',
  200.             'content',
  201.             'catalog',
  202.             'sales',
  203.             'customers',
  204.             // 'marketing'
  205.         ];
  206.         $menu->reorderChildren($order);
  207.     }
  208. }