mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-09-13 13:57:10 +02:00
Optimized for SPA
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Subscriber;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
const FORMAT_TYPES = [
|
||||
'html',
|
||||
'json',
|
||||
'xml',
|
||||
];
|
||||
|
||||
public function __construct(TranslatorInterface $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
protected function generateShowDropdown(ItemInterface $menu, Event $event, string $route): void
|
||||
{
|
||||
$dropdown = $menu->addChild($this->trans('show'), [
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-eye',
|
||||
'dropdown' => 'true',
|
||||
],
|
||||
]);
|
||||
foreach (self::FORMAT_TYPES as $format) {
|
||||
$dropdown->addChild($format, [
|
||||
'route' => $route,
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
'_format' => $format,
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-sign-out-alt',
|
||||
'divider_append' => true,
|
||||
],
|
||||
]);
|
||||
}
|
||||
$dropdown->addChild($this->trans('standard'), [
|
||||
'route' => $route,
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-sign-out-alt',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function trans(string $id, array $parameter = []): string
|
||||
{
|
||||
return $this->translator->trans($id, $parameter);
|
||||
}
|
||||
|
||||
protected function getRequestId(Event $event): int
|
||||
{
|
||||
return $event->getRequest()->getCurrentRequest()->attributes->get('id');
|
||||
}
|
||||
}
|
52
application/symfony/src/Subscriber/NodeMenuSubscriber.php
Normal file
52
application/symfony/src/Subscriber/NodeMenuSubscriber.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Subscriber;
|
||||
|
||||
use App\DBAL\Types\MenuEventType;
|
||||
use App\Event\Menu\MenuEvent;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class NodeMenuSubscriber extends AbstractEntityMenuSubscriber
|
||||
{
|
||||
public function onNodeMenuConfigure(MenuEvent $event): void
|
||||
{
|
||||
$menu = $event->getItem();
|
||||
$this->generateShowDropdown($menu, $event, 'app_source_show');
|
||||
$menu->addChild($this->trans('law'), [
|
||||
'route' => 'app_node_law',
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fa fa-gavel',
|
||||
],
|
||||
]);
|
||||
$menu->addChild($this->trans('parents'), [
|
||||
'route' => 'app_node_parents',
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fa fa-female',
|
||||
],
|
||||
]);
|
||||
$menu->addChild($this->trans('childs'), [
|
||||
'route' => 'app_node_childs',
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fa fa-child',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
MenuEventType::NODE => 'onNodeMenuConfigure',
|
||||
];
|
||||
}
|
||||
}
|
40
application/symfony/src/Subscriber/SourceMenuSubscriber.php
Normal file
40
application/symfony/src/Subscriber/SourceMenuSubscriber.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Subscriber;
|
||||
|
||||
use App\Event\Menu\MenuEvent;
|
||||
use App\DBAL\Types\MenuEventType;
|
||||
|
||||
class SourceMenuSubscriber extends AbstractEntityMenuSubscriber
|
||||
{
|
||||
public function onSourceMenuConfigure(MenuEvent $event): void
|
||||
{
|
||||
$menu = $event->getItem();
|
||||
$menu->addChild($this->trans('edit'), [
|
||||
'route' => 'app_source_edit',
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-edit',
|
||||
],
|
||||
]);
|
||||
$this->generateShowDropdown($menu, $event, 'app_source_show');
|
||||
$menu->addChild($this->trans('node'), [
|
||||
'route' => 'app_source_node',
|
||||
'routeParameters' => [
|
||||
'id' => $this->getRequestId($event),
|
||||
],
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-globe',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
MenuEventType::SOURCE => 'onSourceMenuConfigure',
|
||||
];
|
||||
}
|
||||
}
|
96
application/symfony/src/Subscriber/UserMenuSubscriber.php
Normal file
96
application/symfony/src/Subscriber/UserMenuSubscriber.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Subscriber;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use App\Event\Menu\MenuEvent;
|
||||
use App\DBAL\Types\MenuEventType;
|
||||
|
||||
class UserMenuSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var TokenStorageInterface
|
||||
*/
|
||||
private $tokenStorage;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
|
||||
{
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function onUserMenuConfigure(MenuEvent $event): void
|
||||
{
|
||||
$menu = $event->getItem();
|
||||
$menu->addChild($this->translator->trans('start'), [
|
||||
'route' => 'homepage',
|
||||
'attributes' => [
|
||||
'icon' => 'fab fa-font-awesome-flag',
|
||||
],
|
||||
]);
|
||||
|
||||
$menu->addChild($this->translator->trans('imprint'), [
|
||||
'route' => 'imprint',
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-address-card',
|
||||
],
|
||||
]);
|
||||
$this->generateUserDropdown($menu);
|
||||
}
|
||||
|
||||
private function generateUserDropdown(ItemInterface $menu): void
|
||||
{
|
||||
$dropdown = $menu->addChild($this->tokenStorage->getToken()
|
||||
->getUsername() ?? 'user', [
|
||||
'attributes' => [
|
||||
'dropdown' => true,
|
||||
'icon' => 'fas fa-user',
|
||||
],
|
||||
]);
|
||||
if ($this->tokenStorage->getToken()->getRoles()) {
|
||||
$dropdown->addChild($this->translator->trans('logout'), [
|
||||
'route' => 'logout',
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-sign-out-alt',
|
||||
'divider_append' => true,
|
||||
],
|
||||
]);
|
||||
$dropdown->addChild($this->translator->trans('edit profile'), [
|
||||
'route' => 'fos_user_profile_edit',
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-user-edit',
|
||||
'divider_append' => true,
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
$dropdown->addChild($this->translator->trans('login'), [
|
||||
'route' => 'fos_user_security_login',
|
||||
'attributes' => [
|
||||
'divider_append' => true,
|
||||
'icon' => 'fas fa-sign-in-alt',
|
||||
],
|
||||
]);
|
||||
}
|
||||
$dropdown->addChild('register', [
|
||||
'route' => 'fos_user_registration_register',
|
||||
'attributes' => [
|
||||
'icon' => 'fas fa-file-signature',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
MenuEventType::USER => 'onUserMenuConfigure',
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user