<?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\SuppliersOrdersBundle\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
*/
private Security $security;
/**
* ConfigureMenuSubscriber constructor.
* @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__SUPPLIERS__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$suppliersOrdersBundleChilds = [
[
'key' => 'orders',
'name' => $this->tr->trans('Orders', [], 'SuppliersOrdersBundle'),
'route' => 'suppliers_orders_bundle__supplier_order_list',
'matchPath' => 'bundles/suppliers-orders-bundle/supplier-order/'
],
[
'key' => 'waiting_products',
'name' => $this->tr->trans('Waiting products', [], 'SuppliersOrdersBundle'),
'route' => 'suppliers_orders_bundle__waiting_product_list'
]
];
foreach ($suppliersOrdersBundleChilds as $suppliersOrdersBundleChild) {
$current = $suppliersOrdersBundleChild['route'] == $currentRoute
|| (
!empty($suppliersOrdersBundleChild['matchPath'])
&& strpos($currentPath, $suppliersOrdersBundleChild['matchPath']) !== false
)
;
$menu['SuppliersMenu']->addChild(
$suppliersOrdersBundleChild['key'],
[
'label' => $suppliersOrdersBundleChild['name'],
'route' => $suppliersOrdersBundleChild['route'],
'current' => $current
]
);
}
if (CheckBundleInstall::exist('suppliers-bundle')) {
$matchPath = 'bundles/suppliers-orders-bundle/supplier-view';
if (strpos($currentPath, $matchPath) !== false) {
$menu->getChild('SuppliersMenu')->getChild('suppliers')->setCurrent(true);
}
}
}
}