<?php
namespace App\Menu;
use App\Entity\Core\AdminUser;
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent;
use Knp\Menu\ItemInterface;
use Sylius\Component\Core\Model\AdminUserInterface;
use Symfony\Component\Security\Core\Security;
final class AdminMenuListener
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function addAdminMenuItems(MenuBuilderEvent $event): void
{
$menu = $event->getMenu();
$this->clearAllChildren($menu);
$this->showMenusForUser($menu);
}
private function addContentSubMenu(ItemInterface $menu): void
{
$contentSubmenu = $menu
->addChild('content')
->setLabel('admin.menu.content.label');
$contentSubmenu
->addChild('new', ['route' => 'app_admin_section_index'])
->setLabel('admin.menu.content.section.label')
->setLabelAttribute('icon', 'image')
;
$contentSubmenu
->addChild('offer', ['route' => 'app_admin_offer_index'])
->setLabel('admin.menu.catalog.offer.label')
->setLabelAttribute('icon', 'rocket')
;
}
public function clearAllChildren(ItemInterface $menu)
{
$menu
->removeChild('catalog')
->removeChild('configuration')
->removeChild('sales')
->removeChild('marketing')
->removeChild('customers')
;
}
private function loadAdminMenus(ItemInterface $menu): void
{
$this->addContentSubMenu($menu);
$this->addAdminCatalogSubMenu($menu);
$this->addAdminCustomersSubMenu($menu);
// $this->addAdminMarketingSubMenu($menu);
$this->addAdminSalesSubMenu($menu);
$this->addAdminConfigurationSubMenu($menu);
$this->reorderMenu($menu);
}
private function loadDesignerMenus(ItemInterface $menu): void
{
$this->addContentSubMenu($menu);
$this->addAdminCatalogSubMenu($menu);
}
private function loadEstimationMenus(ItemInterface $menu): void
{
$this->addEstimationSalesSubMenu($menu);
}
private function loadOrderMenus(ItemInterface $menu): void
{
$this->addOrderSalesSubMenu($menu);
}
public function showMenusForUser(ItemInterface $menu): void {
$user = $this->security->getUser();
if ($user->hasRole(AdminUserInterface::DEFAULT_ADMIN_ROLE)) {
$this->loadAdminMenus($menu);
}
if ($user->hasRole(AdminUser::DESIGNER)) {
$this->loadDesignerMenus($menu);
}
if ($user->hasRole(AdminUser::ESTIMATION)) {
$this->loadEstimationMenus($menu);
}
if ($user->hasRole(AdminUser::ORDER)) {
$this->loadOrderMenus($menu);
}
}
private function addAdminCatalogSubMenu(ItemInterface $menu): void
{
$catalog = $menu
->addChild('catalog')
->setLabel('sylius.menu.admin.main.catalog.header');
$catalog
->addChild('taxons', ['route' => 'sylius_admin_taxon_create'])
->setLabel('sylius.menu.admin.main.catalog.taxons')
->setLabelAttribute('icon', 'folder');
$catalog
->addChild('products', ['route' => 'sylius_admin_product_index'])
->setLabel('sylius.menu.admin.main.catalog.products')
->setLabelAttribute('icon', 'cube');
$catalog
->addChild('related-product', ['route' => 'app_admin_related_product_index'])
->setLabel('admin.menu.catalog.related_product.label')
->setLabelAttribute('icon', 'star');
$catalog
->addChild('tag', ['route' => 'app_admin_tag_index'])
->setLabel('admin.menu.catalog.tag.label')
->setLabelAttribute('icon', 'tag')
;
// $catalog
// ->addChild('inventory', ['route' => 'sylius_admin_inventory_index'])
// ->setLabel('sylius.menu.admin.main.catalog.inventory')
// ->setLabelAttribute('icon', 'history');
}
private function addAdminCustomersSubMenu(ItemInterface $menu): void
{
$customers = $menu
->addChild('customers')
->setLabel('sylius.menu.admin.main.customers.header');
$customers
->addChild('customers', ['route' => 'sylius_admin_customer_index'])
->setLabel('sylius.menu.admin.main.customers.customers')
->setLabelAttribute('icon', 'users');
}
private function addAdminMarketingSubMenu(ItemInterface $menu): void
{
$marketing = $menu
->addChild('marketing')
->setLabel('sylius.menu.admin.main.marketing.header');
$marketing
->addChild('promotions', ['route' => 'sylius_admin_promotion_index'])
->setLabel('sylius.menu.admin.main.marketing.promotions')
->setLabelAttribute('icon', 'in cart');
}
private function addAdminSalesSubMenu(ItemInterface $menu): void
{
$sales = $menu
->addChild('sales')
->setLabel('sylius.menu.admin.main.sales.header');
$sales
->addChild('orders', ['route' => 'sylius_admin_order_index'])
->setLabel('sylius.menu.admin.main.sales.orders')
->setLabelAttribute('icon', 'cart');
$sales
->addChild('estimations', ['route' => 'sylius_admin_order_item_index'])
->setLabel('sylius.menu.admin.main.sales.estimations')
->setLabelAttribute('icon', 'cart');
// $sales
// ->addChild('shipemnts', ['route' => 'sylius_admin_shipment_index'])
// ->setLabel('sylius.ui.shipments')
// ->setLabelAttribute('icon', 'truck');
}
private function addAdminConfigurationSubMenu(ItemInterface $menu): void
{
$configuration = $menu
->addChild('configuration')
->setLabel('sylius.menu.admin.main.configuration.header');
$configuration
->addChild('payment_methods', ['route' => 'sylius_admin_payment_method_index'])
->setLabel('sylius.menu.admin.main.configuration.payment_methods')
->setLabelAttribute('icon', 'payment');
$configuration
->addChild('shipping_methods', ['route' => 'sylius_admin_shipping_method_index'])
->setLabel('sylius.menu.admin.main.configuration.shipping_methods')
->setLabelAttribute('icon', 'shipping');
$configuration
->addChild('shipping_categories', ['route' => 'sylius_admin_shipping_category_index'])
->setLabel('sylius.menu.admin.main.configuration.shipping_categories')
->setLabelAttribute('icon', 'shipping');
$configuration
->addChild('tax_rates', ['route' => 'sylius_admin_tax_rate_index'])
->setLabel('sylius.menu.admin.main.configuration.tax_rates')
->setLabelAttribute('icon', 'money');
$configuration
->addChild('admin_users', ['route' => 'sylius_admin_admin_user_index'])
->setLabel('sylius.menu.admin.main.configuration.admin_users')
->setLabelAttribute('icon', 'lock');
}
private function addOrderSalesSubMenu(ItemInterface $menu): void
{
$sales = $menu
->addChild('sales')
->setLabel('sylius.menu.admin.main.sales.header');
$sales
->addChild('orders', ['route' => 'sylius_admin_order_index'])
->setLabel('sylius.menu.admin.main.sales.orders')
->setLabelAttribute('icon', 'cart');
}
private function addEstimationSalesSubMenu(ItemInterface $menu): void
{
$sales = $menu
->addChild('sales')
->setLabel('sylius.menu.admin.main.sales.header');
$sales
->addChild('estimations', ['route' => 'sylius_admin_order_item_index'])
->setLabel('sylius.menu.admin.main.sales.estimations')
->setLabelAttribute('icon', 'cart');
}
private function reorderMenu(ItemInterface $menu): void
{
$order = [
'configuration',
'content',
'catalog',
'sales',
'customers',
// 'marketing'
];
$menu->reorderChildren($order);
}
}