<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\ForecastedStockBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Event\MenuConfigurationPageEvent;
use App\Services\CheckBundleInstall;
use Bluue\StocksBundle\Event\MenuStocksConfigurationEvent;
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;
/**
* @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',
MenuStocksConfigurationEvent::NAME => 'configurationStocksMenu',
MenuConfigurationPageEvent::NAME => 'configurationMenu'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function onLoad(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__STOCKS__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$forecastsMenuChilds = [
[
'key' => 'forecasts',
'name' => $this->tr->trans('Forecasts', [], 'ForecastedStockBundle'),
'route' => 'forecasted_stock_bundle__list'
]
];
if (CheckBundleInstall::exist('sales-bundle')) {
$forecastsMenuChilds[] = [
'key' => 'forecasts_from_sales',
'name' => $this->tr->trans('Forecasts (from sales)', [], 'ForecastedStockBundle'),
'route' => 'forecasted_stock_bundle__list_from_sales'
];
}
foreach ($forecastsMenuChilds as $forecastsMenuChild) {
$current = false;
if ($forecastsMenuChild['route'] == $currentRoute) {
$current = true;
}
$menu['StocksMenu']->addChild(
$forecastsMenuChild['key'],
[
'label' => $forecastsMenuChild['name'],
'route' => $forecastsMenuChild['route'],
'current' => $current
]
);
}
}
/**
* @param MenuStocksConfigurationEvent $event
* @return void
*/
public function configurationStocksMenu(MenuStocksConfigurationEvent $event)
{
$pages = $event->getPages();
$pages[] = [
'id' => 'forecasted_stock_bundle__configuration_settings',
'name' => $this->tr->trans('Forecasted stock settings', [], 'ForecastedStockBundle')
];
$event->setPages($pages);
}
/**
* @param MenuConfigurationPageEvent $event
* @return void
*/
public function configurationMenu(MenuConfigurationPageEvent $event): void
{
$pages = $event->getPages();
$stocksMenu = array_search('stocks_bundle__configuration_settings', array_column($pages, 'id'));
if (is_numeric($stocksMenu)) {
$pages[$stocksMenu]['subpages'][] = ['id' => 'forecasted_stock_bundle__configuration_settings'];
$event->setPages($pages);
}
}
}