<?php
namespace App\Component\AdminMenu\EventSubscriber;
use App\Component\AdminMenu\Event\AbstractBuildMenuEvent;
use App\Component\AdminMenu\MenuBuilder;
use App\Component\UserRole\Permission\UserRolePermission;
use App\Form\Setting\AbstractSettingType;
use App\Type\TypeCollection;
use Flagception\Manager\FeatureManagerInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class SettingMenuSubscriber extends AbstractMenuBuildEventSubscriber
{
protected static int $priority = -200;
protected TypeCollection $typeCollection;
protected TranslatorInterface $translator;
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
FeatureManagerInterface $featureManager,
TypeCollection $typeCollection,
TranslatorInterface $translator
) {
parent::__construct($authorizationChecker, $featureManager);
$this->typeCollection = $typeCollection;
$this->translator = $translator;
}
public function buildMenu(AbstractBuildMenuEvent $event)
{
if (!$this->authorizationChecker->isGranted('ROLE_ADMIN_USER')
|| !$this->authorizationChecker->isGranted(
UserRolePermission::MASK_VIEW,
AbstractSettingType::class)) {
return;
}
$trans = [$this->translator, 'trans'];
$menuBuilder = $this->getMenuBuilder($event->getMenu())
->addMainItem('Settings', 'fal fa-cog')
->addOverlay();
$generalSettings = $this->typeCollection->getTypesByGroup(TypeCollection::GROUP_GENERAL);
$generalSettingRoutes = $this->mapRoutes($generalSettings);
$generalSettingRoutes[$trans('Appearance', [], 'admin')] = ['route' => 'admin_appearance_index'];
$generalSettingRoutes[$trans('User roles', [], 'admin')] = ['route' => 'admin_user_role_index'];
$generalSettingRoutes[$trans('forms.overview', [], 'admin')] = ['route' => 'admin_forms'];
$menuBuilder->addSection('General settings');
$this->renderColumns($menuBuilder, $generalSettingRoutes, 'General', 'fal fa-cog');
$menuBuilder->end();
$menuBuilder->addSection('Vacancy settings');
$this->renderColumns(
$menuBuilder,
$this->mapRoutes($this->typeCollection->getTypesByGroup(TypeCollection::GROUP_VACANCY)),
'Vacancy',
'fal fa-suitcase'
);
$this->renderColumns(
$menuBuilder,
$this->mapRoutes($this->typeCollection->getTypesByGroup(TypeCollection::GROUP_ATS)),
'ATS',
'fal fa-users-cog'
);
$menuBuilder->end();
$moduleRoutes = $this->mapRoutes($this->typeCollection->getTypesByGroup(TypeCollection::GROUP_MODULES));
if ($this->featureManager->isActive('feature_salary_scale')) {
$salaryScaleName = $this->translator->trans(id: 'salary_scale', domain: 'admin');
$moduleRoutes[$salaryScaleName] = ['route' => 'admin_feature_salary_scale_index'];
}
$menuBuilder->addSection('Module settings');
$this->renderColumns(
$menuBuilder,
$moduleRoutes,
'Modules',
'fal fa-cubes'
);
$this->renderColumns(
$menuBuilder,
$this->mapRoutes($this->typeCollection->getTypesByGroup(TypeCollection::GROUP_EXTERNAL_PLUGINGS)),
'External plugins',
'fal fa-plug'
);
$this->renderColumns(
$menuBuilder,
[
$this->translator->trans('swatches.title', [], 'admin') => ['route' => 'admin_swatch_index'],
],
$this->translator->trans('theming.title', [], 'admin'),
'fal fa-palette'
);
$menuBuilder->end();
}
private function mapRoutes(array $settings): array
{
$routes = [];
foreach ($settings as $settingName => $setting) {
$data = [
'route' => 'admin_settings',
'params' => ['type' => $settingName],
'acl' => '',
];
if ($setting instanceof AbstractSettingType) {
$data['acl'] = $setting::getAclName();
}
$routes[$this->translator->trans($settingName, [], 'admin')] = $data;
}
return $routes;
}
private function renderColumns(
MenuBuilder $menuBuilder,
array $routes,
string $name,
string $icon
) {
ksort($routes);
$columns = array_chunk(
array_filter($routes, function (array $item) {
if (\array_key_exists('acl', $item)) {
return $this->authorizationChecker->isGranted(UserRolePermission::MASK_VIEW, $item['acl']);
}
return true;
}),
6,
true
);
$first = true;
foreach ($columns as $column) {
if ($first) {
$menuBuilder->addColumn($name, $icon);
} else {
$menuBuilder->addColumn();
}
foreach ($column as $settingName => $item) {
$menuBuilder->addItem($settingName, $item['route'], $item['params'] ?? []);
}
$menuBuilder->end();
$first = false;
}
}
}