mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Refactored menues
This commit is contained in:
		@@ -16,10 +16,6 @@ final class MenuEventType extends AbstractEnumType
 | 
			
		||||
{
 | 
			
		||||
    public const USER = 'app.menu.topbar.user';
 | 
			
		||||
 | 
			
		||||
    public const SOURCE = 'app.menu.subbar.source';
 | 
			
		||||
 | 
			
		||||
    public const NODE = 'app.menu.subbar.node';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * May this will be used in the future.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -11,12 +11,24 @@ use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
 | 
			
		||||
 */
 | 
			
		||||
final class RESTResponseType extends AbstractEnumType
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    public const JSON = 'json';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    public const HTML = 'html';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    public const XML = 'xml';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var array
 | 
			
		||||
     */
 | 
			
		||||
    protected static $choices = [
 | 
			
		||||
        self::JSON => 'json',
 | 
			
		||||
        self::HTML => 'html',
 | 
			
		||||
 
 | 
			
		||||
@@ -31,26 +31,6 @@ class Menu
 | 
			
		||||
        $this->factory = $factory;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param RequestStack $request
 | 
			
		||||
     *
 | 
			
		||||
     * @return ItemInterface
 | 
			
		||||
     */
 | 
			
		||||
    public function sourceNavbar(RequestStack $request): ItemInterface
 | 
			
		||||
    {
 | 
			
		||||
        return $this->createMenu(MenuEventType::SOURCE, $request);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param RequestStack $request
 | 
			
		||||
     *
 | 
			
		||||
     * @return ItemInterface
 | 
			
		||||
     */
 | 
			
		||||
    public function nodeSubbar(RequestStack $request): ItemInterface
 | 
			
		||||
    {
 | 
			
		||||
        return $this->createMenu(MenuEventType::NODE, $request);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param RequestStack $request
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 | 
			
		||||
use Knp\Menu\ItemInterface;
 | 
			
		||||
use Symfony\Component\EventDispatcher\Event;
 | 
			
		||||
use Symfony\Component\Translation\TranslatorInterface;
 | 
			
		||||
use Infinito\DBAL\Types\RESTResponseType;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
use FOS\RestBundle\Request\ParameterBag;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
@@ -15,19 +18,21 @@ abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
    /**
 | 
			
		||||
     * @var TranslatorInterface
 | 
			
		||||
     */
 | 
			
		||||
    private $translator;
 | 
			
		||||
 | 
			
		||||
    const FORMAT_TYPES = [
 | 
			
		||||
        'html',
 | 
			
		||||
        'json',
 | 
			
		||||
        'xml',
 | 
			
		||||
    ];
 | 
			
		||||
    protected $translator;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param TranslatorInterface $translator
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(TranslatorInterface $translator)
 | 
			
		||||
    {
 | 
			
		||||
        $this->translator = $translator;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param ItemInterface $menu
 | 
			
		||||
     * @param Event         $event
 | 
			
		||||
     * @param string        $route
 | 
			
		||||
     */
 | 
			
		||||
    protected function generateShowDropdown(ItemInterface $menu, Event $event, string $route): void
 | 
			
		||||
    {
 | 
			
		||||
        $dropdown = $menu->addChild($this->trans('show'), [
 | 
			
		||||
@@ -36,13 +41,10 @@ abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
                'dropdown' => 'true',
 | 
			
		||||
            ],
 | 
			
		||||
        ]);
 | 
			
		||||
        foreach (self::FORMAT_TYPES as $format) {
 | 
			
		||||
        foreach (RESTResponseType::getValues() as $format) {
 | 
			
		||||
            $dropdown->addChild($format, [
 | 
			
		||||
                'route' => $route,
 | 
			
		||||
                'routeParameters' => [
 | 
			
		||||
                    'id' => $this->getRequestId($event),
 | 
			
		||||
                    '_format' => $format,
 | 
			
		||||
                ],
 | 
			
		||||
                'routeParameters' => $this->getRequestAttributsSubstitutedFormat($event, $format),
 | 
			
		||||
                'attributes' => [
 | 
			
		||||
                    'icon' => 'fas fa-sign-out-alt',
 | 
			
		||||
                    'divider_append' => true,
 | 
			
		||||
@@ -51,22 +53,81 @@ abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
        }
 | 
			
		||||
        $dropdown->addChild($this->trans('standard'), [
 | 
			
		||||
            'route' => $route,
 | 
			
		||||
            'routeParameters' => [
 | 
			
		||||
                'id' => $this->getRequestId($event),
 | 
			
		||||
            ],
 | 
			
		||||
            'routeParameters' => $this->getRequestAttributs($event),
 | 
			
		||||
            'attributes' => [
 | 
			
		||||
                'icon' => 'fas fa-sign-out-alt',
 | 
			
		||||
            ],
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string $id
 | 
			
		||||
     * @param array  $parameter
 | 
			
		||||
     *
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    protected function trans(string $id, array $parameter = []): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->translator->trans($id, $parameter);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function getRequestId(Event $event): int
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Event $event
 | 
			
		||||
     *
 | 
			
		||||
     * @return int|string
 | 
			
		||||
     */
 | 
			
		||||
    protected function getRequestIdentity(Event $event)
 | 
			
		||||
    {
 | 
			
		||||
        return $event->getRequest()->getCurrentRequest()->attributes->get('id');
 | 
			
		||||
        return $this->getRequestAttributs($event)->get('identity');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Event $event
 | 
			
		||||
     *
 | 
			
		||||
     * @return Request
 | 
			
		||||
     */
 | 
			
		||||
    private function getCurrentRequest(Event $event): Request
 | 
			
		||||
    {
 | 
			
		||||
        return $event->getRequest()->getCurrentRequest();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Event $event
 | 
			
		||||
     *
 | 
			
		||||
     * @return ParameterBag
 | 
			
		||||
     */
 | 
			
		||||
    private function getRequestAttributs(Event $event): array
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getCurrentRequest($event)->attributes->get('_route_params') ?? [];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Event  $event
 | 
			
		||||
     * @param string $format
 | 
			
		||||
     *
 | 
			
		||||
     * @return number|string
 | 
			
		||||
     */
 | 
			
		||||
    private function getRequestAttributsSubstitutedFormat(Event $event, string $format): array
 | 
			
		||||
    {
 | 
			
		||||
        $attributs = $this->getRequestAttributs($event);
 | 
			
		||||
        $attributs['_format'] = $format;
 | 
			
		||||
 | 
			
		||||
        return $attributs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Event $event
 | 
			
		||||
     *
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    protected function shouldShowFormatSelection(Event $event): bool
 | 
			
		||||
    {
 | 
			
		||||
        foreach (['identity', 'layer'] as $attribut) {
 | 
			
		||||
            if (!key_exists($attribut, $this->getRequestAttributs($event))) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,52 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Infinito\Subscriber;
 | 
			
		||||
 | 
			
		||||
use Infinito\DBAL\Types\MenuEventType;
 | 
			
		||||
use Infinito\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',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,40 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Infinito\Subscriber;
 | 
			
		||||
 | 
			
		||||
use Infinito\Event\Menu\MenuEvent;
 | 
			
		||||
use Infinito\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',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,22 +9,23 @@ use Knp\Menu\ItemInterface;
 | 
			
		||||
use Infinito\Event\Menu\MenuEvent;
 | 
			
		||||
use Infinito\DBAL\Types\MenuEventType;
 | 
			
		||||
use Infinito\Domain\FixtureManagement\FixtureSource\ImpressumFixtureSource;
 | 
			
		||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
class UserMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
class UserMenuSubscriber extends AbstractEntityMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    const LAYER_GET_ROUTE = 'infinito_api_rest_layer_read';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var TokenStorageInterface
 | 
			
		||||
     */
 | 
			
		||||
    private $tokenStorage;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var TranslatorInterface
 | 
			
		||||
     */
 | 
			
		||||
    private $translator;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param TokenStorageInterface $tokenStorage
 | 
			
		||||
     * @param TranslatorInterface   $translator
 | 
			
		||||
@@ -32,7 +33,7 @@ class UserMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
    public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
 | 
			
		||||
    {
 | 
			
		||||
        $this->tokenStorage = $tokenStorage;
 | 
			
		||||
        $this->translator = $translator;
 | 
			
		||||
        parent::__construct($translator);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -54,22 +55,52 @@ class UserMenuSubscriber implements EventSubscriberInterface
 | 
			
		||||
                'icon' => 'fas fa-address-card',
 | 
			
		||||
            ],
 | 
			
		||||
        ]);
 | 
			
		||||
        if ($this->shouldShowFormatSelection($event)) {
 | 
			
		||||
            $this->generateShowDropdown($menu, $event, self::LAYER_GET_ROUTE);
 | 
			
		||||
        }
 | 
			
		||||
        $this->generateUserDropdown($menu);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return TokenInterface|null
 | 
			
		||||
     */
 | 
			
		||||
    private function getToken(): ?TokenInterface
 | 
			
		||||
    {
 | 
			
		||||
        return $this->tokenStorage->getToken();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return string
 | 
			
		||||
     */
 | 
			
		||||
    private function getUsername(): string
 | 
			
		||||
    {
 | 
			
		||||
        $token = $this->getToken();
 | 
			
		||||
 | 
			
		||||
        return ($token) ? $token->getUsername() : 'user';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return array|null
 | 
			
		||||
     */
 | 
			
		||||
    private function getRoles(): ?array
 | 
			
		||||
    {
 | 
			
		||||
        $token = $this->getToken();
 | 
			
		||||
 | 
			
		||||
        return ($token) ? $token->getRoles() : null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param ItemInterface $menu
 | 
			
		||||
     */
 | 
			
		||||
    private function generateUserDropdown(ItemInterface $menu): void
 | 
			
		||||
    {
 | 
			
		||||
        $dropdown = $menu->addChild($this->tokenStorage->getToken()
 | 
			
		||||
            ->getUsername() ?? 'user', [
 | 
			
		||||
        $dropdown = $menu->addChild($this->getUsername(), [
 | 
			
		||||
            'attributes' => [
 | 
			
		||||
                'dropdown' => true,
 | 
			
		||||
                'icon' => 'fas fa-user',
 | 
			
		||||
            ],
 | 
			
		||||
        ]);
 | 
			
		||||
        if ($this->tokenStorage->getToken()->getRoles()) {
 | 
			
		||||
        if ($this->getRoles()) {
 | 
			
		||||
            $dropdown->addChild($this->translator->trans('logout'), [
 | 
			
		||||
                'route' => 'logout',
 | 
			
		||||
                'attributes' => [
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user