<?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\PlanningBundle\EventSubscriber;
use App\Event\ConfigureMenuEvent;
use App\Event\MenuConfigurationPageEvent;
use App\Event\BundleRolesEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigurationBundleSubscriber 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 => 'mainMenu',
MenuConfigurationPageEvent::NAME => 'configurationMenu',
BundleRolesEvent::NAME => 'bundleRoles'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function mainMenu(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__PLANNING__READ_ONLY')) {
return;
}
$menu = $event->getMenu();
if (!$menu->getChild('PlanningMenu')) {
$menu->addChild('PlanningMenu')
->setLabel($this->tr->trans('Planning', [], 'PlanningBundle'))
->setExtra('icon', 'far fa-calendar-alt');
}
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$planningMenu = [
[
'name' => $this->tr->trans('Planning', [], 'PlanningBundle'),
'route' => 'planning_bundle__slot_list'
]
];
foreach ($planningMenu as $planningMenuChild) {
$menu['PlanningMenu']->addChild(
$planningMenuChild['name'],
['route' => $planningMenuChild['route']]
);
if ($planningMenuChild['route'] == $currentRoute) {
$menu->getChild('PlanningMenu')->getChild($planningMenuChild['name'])->setCurrent(true);
}
}
}
/**
* @param MenuConfigurationPageEvent $event
* @return void
*/
public function configurationMenu(MenuConfigurationPageEvent $event): void
{
if ($this->security->isGranted('ROLE__PLANNING__ADMIN')) {
$pages = $event->getPages();
$pages[] = [
'id' => 'planning_bundle__configuration_settings',
'name' => $this->tr->trans('Planning Module', [], 'PlanningBundle'),
'config' => [],
'subpages' => [
['id' => 'planning_bundle__configuration_settings'],
['id' => 'planning_bundle__configuration_slot_type_list']
]
];
$event->setPages($pages);
}
}
/**
* @param BundleRolesEvent $event
* @return void
*/
public function bundleRoles(BundleRolesEvent $event): void
{
$bundles = $event->getBundles();
$bundles[] = [
'select_name' => 'planning',
'name' => $this->tr->trans('Planning', [], 'PlanningBundle')
];
$event->setBundles($bundles);
}
}