<?php
/**
* @author Léo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\StocksBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Services\CheckBundleInstall;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigureMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $tr;
/**
* @var RequestStack
*/
protected RequestStack $requestStack;
/**
* @var Security
*/
private Security $security;
/**
* @param RequestStack $requestStack
* @param TranslatorInterface $tr
* @param Security $security
*/
public function __construct(
RequestStack $requestStack,
TranslatorInterface $tr,
Security $security
) {
$this->requestStack = $requestStack;
$this->tr = $tr;
$this->security = $security;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__STOCKS__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$stockMenuChilds = [
[
'key' => 'stocks',
'label' => $this->tr->trans('Stocks', [], 'StocksBundle'),
'route' => 'stocks_bundle__stock_product_list',
'matchPath' => [
'bundles/stocks-bundle/stock-product/stock-location/list/',
'bundles/stocks-bundle/stock-product/stock-location/stock-quantity/list'
]
],
[
'key' => 'stock_movements',
'label' => $this->tr->trans('Stock Movements', [], 'StocksBundle'),
'route' => 'stocks_bundle__stock_movement_list',
'matchPath' => [
'bundles/stocks-bundle/stock-movement'
]
],
[
'key' => 'locations',
'label' => $this->tr->trans('Locations', [], 'StocksBundle'),
'route' => 'stocks_bundle__location_list',
'matchPath' => [
'bundles/stocks-bundle/location'
]
],
[
'key' => 'sub_locations',
'label' => $this->tr->trans('Sub-locations', [], 'StocksBundle'),
'route' => 'stocks_bundle__sub_location_list',
'matchPath' => [
'bundles/stocks-bundle/sub-location'
]
],
[
'key' => 'stock_limit_date',
'label' => $this->tr->trans('Stock limit date', [], 'StocksBundle'),
'route' => 'stocks_bundle__stock_location_limit_date_list'
],
[
'key' => 'stock_preparation',
'label' => $this->tr->trans('Stock preparation', [], 'StocksBundle'),
'route' => 'stocks_bundle__stock_preparation_list'
],
[
'key' => 'mass_stock_entries',
'label' => $this->tr->trans('Mass entries', [], 'StocksBundle'),
'route' => 'stocks_bundle__mass_stock_entry_list',
'matchPath' => [
'bundles/stocks-bundle/mass-stock-entry'
]
],
[
'key' => 'mass_stock_removal',
'label' => $this->tr->trans('Mass stocks removal', [], 'StocksBundle'),
'route' => 'stocks_bundle__mass_stock_removal_list',
'matchPath' => [
'bundles/stocks-bundle/mass-stock-removal'
]
],
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$parentName = $this->tr->trans('Stocks', [], 'StocksBundle');
$menuKey = 'StocksMenu';
$menu->addChild($menuKey)
->setLabel($parentName)
->setExtra('icon', 'fas fa-solid fa-box-open');
foreach ($stockMenuChilds as $stockMenuChild) {
$current = false;
if ($stockMenuChild['route'] == $currentRoute) {
$current = true;
} else {
if (isset($stockMenuChild['matchPath'])) {
foreach ($stockMenuChild['matchPath'] as $matchPath) {
if (strpos($currentPath, $matchPath) !== false) {
$current = true;
}
}
}
}
$menu['StocksMenu']->addChild(
$stockMenuChild['key'],
[
'route' => $stockMenuChild['route'],
'label' => $stockMenuChild['label'],
'current' => $current
]
);
}
if (CheckBundleInstall::exist('suppliers-orders-bundle')) {
$menu['StocksMenu']->addChild(
'product_restock',
[
'label' => $this->tr->trans('Product restock', [], 'StocksBundle'),
'route' => 'stocks_bundle__product_restock_list',
'current' => in_array($currentRoute, [
'stocks_bundle__product_restock_list',
'stocks_bundle__product_restock_holding_list',
'stocks_bundle__product_quantity_to_hold_list'
])
]
);
}
}
}