<?php
// src/EventSubscriber/KnpMenuBuilderSubscriber.php
namespace App\EventSubscriber;
use App\Entity\ClientsChantiers;
use Doctrine\ORM\EntityManagerInterface;
use KevinPapst\AdminLTEBundle\Event\KnpMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
class KnpMenuBuilderSubscriber implements EventSubscriberInterface
{
public $checker;
public $security;
public $em;
public function __construct(AuthorizationCheckerInterface $authorizationChecker,Security $security,EntityManagerInterface $em)
{
$this->checker = $authorizationChecker;
$this->security = $security;
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
KnpMenuEvent::class => ['onSetupMenu', 100],
];
}
public function onSetupMenu(KnpMenuEvent $event)
{
$user = $this->security->getUser();
$menu = $event->getMenu();
$menu->addChild(
'accueil',
['route' => 'publicPages','label' => 'Retour au site', 'childOptions' => $event->getChildOptions()]
)->setLabelAttribute('icon', 'fas fa-home');
// Mes outils
$menu->addChild(
'menu-label-01',
['label' => 'Mes outils', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'homepage',
['route' => 'admin', 'label' => 'Dashboard', 'childOptions' => $event->getChildOptions()]
)->setLabelAttribute('icon', 'fas fa-tachometer-alt');
// --------------------------------------------
// ----------------- MENU USER ---------------
// --------------------------------------------
if ($this->checker->isGranted('ROLE_USER')) {
if(!empty($user->getAcls())) {
// Mes chantiers
$menu->addChild(
'menu-admin-102',
['label' => 'Mes adresses', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$loop = 1;
foreach ($user->getAcls()->getListacces() as $actList) {
$EntityClientsChantiers = $this->em->getRepository(ClientsChantiers::class)->findOneBy(array('id'=>$actList));
if($EntityClientsChantiers) {
$menu->addChild(
'menu-admin-102-child0'.$loop,
[
'route' => 'admin_fiche_chantier',
'routeParameters' => array('id' => $EntityClientsChantiers->getId()),
'label' => $EntityClientsChantiers->getNom(),
'childOptions' => $event->getChildOptions(),
]
)->setLabelAttribute('icon', 'fas fa-angle-right');
$loop++;
}
}
}
}
// --------------------------------------------
// -------------- MENU ADMIN ------------
// --------------------------------------------
if ($this->checker->isGranted('ROLE_ADMIN')) {
// Clients
$menu->addChild(
'menu-admin-02',
['label' => 'Codial', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'menu-admin-02-child01',
[
'route' => 'admin_clients',
'label' => 'Liste DO',
'childOptions' => $event->getChildOptions(),
/*'extras' => [
'routes' => [
'route' => 'admin_clients_detail_chantier'
]
],*/
]
)->setLabelAttribute('icon', 'fas fa-angle-right');
$menu->addChild(
'menu-admin-02-child02',
[
'route' => 'admin_clients_chantiers',
'label' => 'Liste Chantiers',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'admin_clients_detail_chantier'
]
],
]
)->setLabelAttribute('icon', 'fas fa-angle-right');
$menu->addChild(
'menu-admin-02-child03',
[
'route' => 'admin_clients_interventions',
'label' => 'Liste Interventions',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'admin_clients_detail_intervention'
]
],
]
)->setLabelAttribute('icon', 'fas fa-angle-right');
}
// --------------------------------------------
// -------------- MENU SUPER ADMIN ------------
// --------------------------------------------
if ($this->checker->isGranted('ROLE_SUPER_ADMIN')) {
// Mes pages
$menu->addChild(
'menu-pages',
['label' => 'Front office', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'menu-pages-list',
[
'route' => 'listePages',
'label' => 'Liste des pages',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'addPages'
],
],
]
)->setLabelAttribute('icon', 'fas fa-sitemap');
$menu->addChild(
'menu-pages-list-sections',
[
'route' => 'listSectionAccueil',
'label' => 'Sections accueil',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'addSectionAccueil'
],
],
]
)->setLabelAttribute('icon', 'fas fa-ellipsis-h');
$menu->addChild(
'menu-pages-list-sectionsFooter',
[
'route' => 'listSectionFooter',
'label' => 'Section footer',
'childOptions' => $event->getChildOptions(),
/*'extras' => [
'routes' => [
'route' => 'addSectionAccueil'
],
],*/
]
)->setLabelAttribute('icon', 'fas fa-ellipsis-h');
$menu->addChild(
'menu-pages-nav-01',
[
'route' => 'menuList',
'label' => 'Menu header',
'childOptions' => $event->getChildOptions(),
'routeParameters' => ['type' => 'header'],
]
)->setLabelAttribute('icon', 'fas fa-ellipsis-h');
$menu->addChild(
'menu-pages-nav-02',
[
'route' => 'menuList',
'label' => 'Menu footer',
'childOptions' => $event->getChildOptions(),
'routeParameters' => ['type' => 'footer'],
]
)->setLabelAttribute('icon', 'fas fa-ellipsis-h');
// Users
$menu->addChild(
'menu-admin-01',
['label' => 'Utilisateurs', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'menu-admin-01-child',
[
'route' => 'adminUsersList',
'label' => 'Liste utilisateurs',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'adminProfilChange'
],
/*'badge' => [
'color' => 'green',
'value' => "ok",
],*/
],
]
)->setLabelAttribute('icon', 'fas fa-users');
// Parametres
$menu->addChild(
'menu-supAdmin-02',
['label' => 'Paramètre', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'menu-supAdmin-02-child01',
[
'route' => 'admin_parametres_statuts',
'label' => 'Statuts Interventions',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'admin_parametres_statuts_add'
]
],
]
)->setLabelAttribute('icon', 'fas fa-database');
$menu->addChild(
'menu-supAdmin-02-child02',
[
'route' => 'admin_parametres_mails',
'label' => 'Mails',
'childOptions' => $event->getChildOptions(),
'extras' => [
'routes' => [
'route' => 'admin_parametres_mails_add'
]
],
]
)->setLabelAttribute('icon', 'fa fa-envelope');
// Maintenance
$menu->addChild(
'menu-supAdmin-01',
['label' => 'Maintenance', 'childOptions' => $event->getChildOptions()]
)->setAttribute('class', 'header');
$menu->addChild(
'menu-supAdmin-01-child',
['route' => 'listDGSTools', 'label' => 'Liste outils', 'childOptions' => $event->getChildOptions()]
)->setLabelAttribute('icon', 'fas fa-cogs');
$menu->addChild(
'menu-supAdmin-02-child',
['route' => 'listDGSToolsLogsRead', 'label' => 'Logs', 'childOptions' => $event->getChildOptions()]
)->setLabelAttribute('icon', 'fas fa-cogs');
}
}
}