<?php
declare(strict_types=1);
namespace Bluue\Cx3Bundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConfigureMenuEventSubscriber 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 string[]
*/
public static function getSubscribedEvents(): array
{
return [
ConfigureMenuEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
$menu = $event->getMenu();
$menuSAVChilds = [
[
'key' => 'calls',
'label' => $this->tr->trans('Calls', [], 'Cx3Bundle'),
'route' => 'cx3_bundle__call_list',
]
];
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$parentName = $this->tr->trans('SAV', [], 'Cx3Bundle');
$menu->addChild('SAVMenu')
->setLabel($parentName)
->setExtra('icon', 'fas fa-users')
;
foreach ($menuSAVChilds as $menuSAVChild) {
$current = $menuSAVChild['route'] == $currentRoute
|| (
!empty($menuSAVChild['matchPath'])
&& strpos($currentPath, $menuSAVChild['matchPath']) !== false
)
;
$menu['SAVMenu']->addChild(
$menuSAVChild['key'],
[
'route' => $menuSAVChild['route'],
'label' => $menuSAVChild['label'],
'current' => $current
]
);
}
}
}