<?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\ProjectsBundle\EventSubscriber;
use App\Event\AdditionalJavascriptsEvent;
use App\Event\ConfigureMenuEvent;
use App\Event\MenuConfigurationPageEvent;
use App\Event\BundleRolesEvent;
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 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',
AdditionalJavascriptsEvent::NAME => 'additionalJs'
];
}
/**
* @param ConfigureMenuEvent $event
* @return void
*/
public function mainMenu(ConfigureMenuEvent $event): void
{
if (!$this->security->isGranted('ROLE__PROJECTS__READ_ONLY')) {
return;
}
$currentPath = $this->requestStack->getCurrentRequest()->getPathInfo();
$currentRoute = $this->requestStack->getCurrentRequest()->attributes->get('_route');
$menu = $event->getMenu();
$menu->addChild('ProjectsMenu')
->setLabel($this->tr->trans('Projects / Tasks', [], 'ProjectsBundle'))
->setExtra('icon', 'fas fa-project-diagram');
$projectsMenu = [
[
'name' => $this->tr->trans('Projects', [], 'ProjectsBundle'),
'route' => 'projects_bundle__project_list',
'matchPath' => 'bundles/projects-bundle/project/'
],
[
'name' => $this->tr->trans('Tasks', [], 'ProjectsBundle'),
'route' => 'projects_bundle__task_list',
'matchPath' => 'bundles/projects-bundle/task/'
]
];
if ($this->security->isGranted('ROLE__PROJECTS__SMALL_ADMIN')) {
$projectsMenu[] = [
'name' => $this->tr->trans('Mass tasks', [], 'ProjectsBundle'),
'route' => 'projects_bundle__mass_task',
'matchPath' => null
];
}
foreach ($projectsMenu as $projectsMenuChild) {
$current = $projectsMenuChild['route'] == $currentRoute
|| (
$projectsMenuChild['matchPath']
&& strpos($currentPath, $projectsMenuChild['matchPath']) !== false
)
;
$menu['ProjectsMenu']->addChild(
$projectsMenuChild['name'],
[
'route' => $projectsMenuChild['route'],
'current' => $current
]
);
}
if (CheckBundleInstall::exist('crm-bundle')) {
$matchPath = 'bundles/projects-bundle/opportunity';
if (strpos($currentPath, $matchPath) !== false) {
$menu->getChild('CrmMenu')->getChild('opportunities')->setCurrent(true);
}
}
$matchPath = [
'bundles/projects-bundle/customer'
];
foreach ($matchPath as $match) {
if (strpos($currentPath, $match) !== false) {
$menu->getChild('CustomersMenu')->getChild('customers')->setCurrent(true);
}
}
}
/**
* @param MenuConfigurationPageEvent $event
* @return void
*/
public function configurationMenu(MenuConfigurationPageEvent $event): void
{
if ($this->security->isGranted('ROLE__PROJECTS__ADMIN')) {
$pages = $event->getPages();
$pages[] = [
'id' => 'projects_bundle__configuration_settings',
'name' => $this->tr->trans('Projects Module', [], 'ProjectsBundle'),
'config' => [],
'subpages' => [
['id' => 'projects_bundle__configuration_settings'],
['id' => 'projects_bundle__configuration_project_step_list'],
['id' => 'projects_bundle__configuration_project_state_list'],
['id' => 'projects_bundle__configuration_project_type_list'],
['id' => 'projects_bundle__configuration_task_state_list'],
['id' => 'projects_bundle__configuration_task_priority_list'],
['id' => 'projects_bundle__task_models']
]
];
$event->setPages($pages);
}
}
/**
* @param BundleRolesEvent $event
* @return void
*/
public function bundleRoles(BundleRolesEvent $event): void
{
$bundles = $event->getBundles();
$bundles[] = [
'select_name' => 'projects',
'name' => $this->tr->trans('Projects', [], 'ProjectsBundle')
];
$event->setBundles($bundles);
}
/**
* @param AdditionalJavascriptsEvent $event
* @return void
*/
public function additionalJs(AdditionalJavascriptsEvent $event): void
{
$javascripts = [
[
'entry' => 'projects_bundle',
'controllers' => [
[
'name' => 'projects--task-model-form',
'routes' => [
'products_bundle__product_sheet'
]
]
]
]
];
$event->addJavascripts($javascripts);
}
}