infinito/application/src/Subscriber/UserMenuSubscriber.php

97 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2018-09-20 14:26:28 +02:00
namespace App\Subscriber;
2018-09-13 00:24:49 +02:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
2018-09-17 17:43:26 +02:00
use Knp\Menu\ItemInterface;
2018-10-04 22:05:31 +02:00
use App\Event\Menu\MenuEvent;
use App\DBAL\Types\MenuEventType;
class UserMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var TranslatorInterface
*/
private $translator;
2018-09-20 14:26:28 +02:00
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}
2018-10-04 22:05:31 +02:00
public function onUserMenuConfigure(MenuEvent $event): void
{
$menu = $event->getItem();
2018-09-20 14:26:28 +02:00
$menu->addChild($this->translator->trans('start'), [
2018-09-13 00:24:49 +02:00
'route' => 'homepage',
'attributes' => [
2018-09-20 14:26:28 +02:00
'icon' => 'fab fa-font-awesome-flag',
],
2018-09-17 17:43:26 +02:00
]);
2018-09-20 14:26:28 +02:00
$menu->addChild($this->translator->trans('imprint'), [
2018-09-17 17:43:26 +02:00
'route' => 'imprint',
'attributes' => [
2018-09-20 14:26:28 +02:00
'icon' => 'fas fa-address-card',
],
2018-09-17 17:43:26 +02:00
]);
$this->generateUserDropdown($menu);
}
2018-09-17 17:43:26 +02:00
private function generateUserDropdown(ItemInterface $menu): void
{
2018-09-13 02:47:57 +02:00
$dropdown = $menu->addChild($this->tokenStorage->getToken()
->getUsername() ?? 'user', [
2018-09-13 00:24:49 +02:00
'attributes' => [
'dropdown' => true,
2018-09-20 14:26:28 +02:00
'icon' => 'fas fa-user',
],
2018-09-13 00:24:49 +02:00
]);
if ($this->tokenStorage->getToken()->getRoles()) {
2018-09-20 14:26:28 +02:00
$dropdown->addChild($this->translator->trans('logout'), [
2018-09-12 19:59:55 +02:00
'route' => 'logout',
'attributes' => [
2018-09-06 11:26:10 +02:00
'icon' => 'fas fa-sign-out-alt',
2018-09-20 14:26:28 +02:00
'divider_append' => true,
],
2018-09-13 02:47:57 +02:00
]);
2018-09-20 14:26:28 +02:00
$dropdown->addChild($this->translator->trans('edit profile'), [
2018-09-13 02:47:57 +02:00
'route' => 'fos_user_profile_edit',
'attributes' => [
'icon' => 'fas fa-user-edit',
2018-09-20 14:26:28 +02:00
'divider_append' => true,
],
2018-09-13 00:24:49 +02:00
]);
} else {
2018-09-20 14:26:28 +02:00
$dropdown->addChild($this->translator->trans('login'), [
2018-09-13 02:24:25 +02:00
'route' => 'fos_user_security_login',
'attributes' => [
2018-09-13 00:24:49 +02:00
'divider_append' => true,
2018-09-20 14:26:28 +02:00
'icon' => 'fas fa-sign-in-alt',
],
2018-09-13 00:24:49 +02:00
]);
}
$dropdown->addChild('register', [
2018-09-13 02:24:25 +02:00
'route' => 'fos_user_registration_register',
2018-09-13 00:24:49 +02:00
'attributes' => [
2018-09-20 14:26:28 +02:00
'icon' => 'fas fa-file-signature',
],
2018-09-13 00:24:49 +02:00
]);
}
public static function getSubscribedEvents(): array
{
return [
2018-10-29 19:01:00 +01:00
MenuEventType::USER => 'onUserMenuConfigure',
];
}
}