<?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\InventoriesBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Services\CheckBundleInstall;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
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
*/
protected 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'
];
}
public function onLoad(ConfigureMenuEvent $event)
{
if (!$this->security->isGranted('ROLE__INVENTORIES__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$path = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$inventoriesMenuChilds = [
[
'key' => 'inventories',
'name' => $this->tr->trans('Inventories', [], 'InventoriesBundle'),
'route' => 'inventories_bundle__inventory_list',
'matchPath' => [
'bundles/inventories-bundle/inventory'
]
]
];
foreach ($inventoriesMenuChilds as $inventoriesMenuChild) {
$current = false;
if ($inventoriesMenuChild['route'] == $currentRoute) {
$current = true;
} else {
foreach ($inventoriesMenuChild['matchPath'] as $matchPath) {
if (strpos($path, $matchPath) !== false) {
$current = true;
}
}
}
$menu['StocksMenu']->addChild(
$inventoriesMenuChild['key'],
[
'label' => $inventoriesMenuChild['name'],
'route' => $inventoriesMenuChild['route'],
'current' => $current
]
);
}
}
}