Added source menu

This commit is contained in:
Kevin Frantz 2018-09-20 14:26:28 +02:00
parent 9ae2290835
commit 3bf84d3ec8
14 changed files with 201 additions and 55 deletions

View File

@ -21,6 +21,11 @@ services:
factory: ['@app.menu_builder', 'userTopbar']
tags:
- { name: knp_menu.menu, alias: userTopbar }
app.menu.source:
class: Knp\Menu\MenuItem
factory: ['@app.menu_builder', 'sourceNavbar']
tags:
- { name: knp_menu.menu, alias: sourceNavbar }
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:

View File

@ -1,4 +1,5 @@
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
@ -7,19 +8,18 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Creator\Factory\Template\Source\SourceTemplateFactory;
use FOS\RestBundle\Controller\FOSRestController;
use App\Form\NameSourceType;
use App\Entity\SourceInterface;
use App\Creator\Factory\Template\Source\SourceTemplateFormFactory;
use App\Creator\Factory\Form\Source\SourceFormFactory;
/**
* @todo IMPLEMENT SECURITY!
*
* @author kevinfrantz
*/
class SourceController extends FOSRestController
{
/**
*
* @Route("/source/{id}.{_format}", defaults={"_format"="html"})
*/
public function show(Request $request, int $id): Response
@ -28,11 +28,11 @@ class SourceController extends FOSRestController
$view = $this->view($source, 200)
->setTemplate((new SourceTemplateFactory($source, $request))->getTemplatePath())
->setTemplateVar('source');
return $this->handleView($view);
}
/**
*
* @Route("/source/{id}/edit.{_format}", defaults={"_format"="html"})
*/
public function edit(Request $request, int $id): Response
@ -44,9 +44,10 @@ class SourceController extends FOSRestController
$source = $form->getData();
$this->saveSource($source);
}
return $this->render((new SourceTemplateFormFactory($source, $request))->getTemplatePath(), array(
return $this->render((new SourceTemplateFormFactory($source, $request))->getTemplatePath(), [
'form' => $form->createView(),
));
]);
}
private function loadSource(Request $request, int $id): SourceInterface
@ -57,6 +58,7 @@ class SourceController extends FOSRestController
if (!$source) {
throw $this->createNotFoundException('No source found for id '.$id);
}
return $source;
}

View File

@ -1,12 +1,11 @@
<?php
namespace App\Creator\Factory\Form\Source;
use App\Entity\SourceInterface;
/**
*
* @author kevinfrantz
*
*/
class SourceFormFactory
{
@ -17,18 +16,20 @@ class SourceFormFactory
*/
private $source;
public function __construct(SourceInterface $source){
public function __construct(SourceInterface $source)
{
$this->source = $source;
}
public function getNamespace():string{
public function getNamespace(): string
{
return self::FORM_NAMESPACE.$this->getName();
}
protected function getName(): string
{
$reflectionClass = new \ReflectionClass($this->source);
return $reflectionClass->getShortName().'Type';
}
}

View File

@ -1,10 +1,9 @@
<?php
namespace App\Creator\Factory\Template\Source;
/**
*
* @author kevinfrantz
*
*/
class SourceTemplateFormFactory extends SourceTemplateFactory
{
@ -15,4 +14,3 @@ class SourceTemplateFormFactory extends SourceTemplateFactory
return parent::SOURCE_TEMPLATE_ROOT.'/'.self::FORM_FOLDER.'/'.$this->generateName().'.'.$this->request->getRequestFormat().'.twig';
}
}

View File

@ -18,6 +18,7 @@ class NameSource extends AbstractSource implements NameSourceInterface
/**
* @ORM\Column(type="string",length=255)
* @Assert\NotBlank()
*
* @var string
*/
protected $name;

View File

@ -0,0 +1,59 @@
<?php
namespace App\Event\Menu\Topbar;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\RequestStack;
class SourceMenuEvent extends Event
{
public const EVENT = 'app.menu.source.user';
/**
* @var FactoryInterface
*/
private $factory;
/**
* @var ItemInterface
*/
private $item;
/**
* @var RequestStack
*/
private $request;
public function __construct(FactoryInterface $factory, ItemInterface $item, RequestStack $request)
{
$this->factory = $factory;
$this->item = $item;
$this->request = $request;
}
/**
* @return FactoryInterface
*/
public function getFactory(): FactoryInterface
{
return $this->factory;
}
/**
* @return ItemInterface
*/
public function getItem(): ItemInterface
{
return $this->item;
}
/**
* @return RequestStack
*/
public function getRequest(): RequestStack
{
return $this->request;
}
}

View File

@ -1,7 +1,5 @@
<?php
// src/Menu/Menu.php
namespace App\Menu;
use App\Event\Menu\Topbar\UserMenuEvent;
@ -9,6 +7,7 @@ use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Event\Menu\Topbar\SourceMenuEvent;
class Menu
{
@ -28,6 +27,19 @@ class Menu
$this->factory = $factory;
}
public function SourceNavbar(RequestStack $request): ItemInterface
{
$menu = $this->factory->createItem('root', [
'childrenAttributes' => [
'class' => 'navbar-nav mr-auto',
],
]);
$this->dispatcher->dispatch(SourceMenuEvent::EVENT, new SourceMenuEvent($this->factory, $menu, $request));
return $menu;
}
public function userTopbar(RequestStack $request): ItemInterface
{
$menu = $this->factory->createItem('root', [
@ -36,10 +48,7 @@ class Menu
],
]);
$this->dispatcher->dispatch(
UserMenuEvent::EVENT,
new UserMenuEvent($this->factory, $menu, $request)
);
$this->dispatcher->dispatch(UserMenuEvent::EVENT, new UserMenuEvent($this->factory, $menu, $request));
return $menu;
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
use App\Event\Menu\Topbar\SourceMenuEvent;
class SourceMenuSubscriber 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 onSourceMenuConfigure(SourceMenuEvent $event): void
{
$menu = $event->getItem();
$menu->addChild($this->translator->trans('edit'), [
'route' => 'app_source_edit',
'routeParameters' => ['id' => $event->getRequest()->getCurrentRequest()->attributes->get('id')],
'attributes' => [
'icon' => 'fas fa-edit',
],
]);
$menu->addChild($this->translator->trans('show'), [
'route' => 'app_source_show',
'routeParameters' => ['id' => $event->getRequest()->getCurrentRequest()->attributes->get('id')],
'attributes' => [
'icon' => 'fas fa-eye',
],
]);
}
public static function getSubscribedEvents(): array
{
return [
SourceMenuEvent::EVENT => 'onSourceMenuConfigure',
];
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace App\Subscriber;
use App\Event\Menu\Topbar\UserMenuEvent;
@ -9,15 +10,12 @@ use Knp\Menu\ItemInterface;
class UserMenuSubscriber implements EventSubscriberInterface
{
/**
*
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
*
* @var TranslatorInterface
*/
private $translator;
@ -31,18 +29,18 @@ class UserMenuSubscriber implements EventSubscriberInterface
public function onUserMenuConfigure(UserMenuEvent $event): void
{
$menu = $event->getItem();
$menu->addChild('start', [
$menu->addChild($this->translator->trans('start'), [
'route' => 'homepage',
'attributes' => [
'icon' => 'fab fa-font-awesome-flag'
]
'icon' => 'fab fa-font-awesome-flag',
],
]);
$menu->addChild('imprint', [
$menu->addChild($this->translator->trans('imprint'), [
'route' => 'imprint',
'attributes' => [
'icon' => 'fas fa-address-card'
]
'icon' => 'fas fa-address-card',
],
]);
$this->generateUserDropdown($menu);
}
@ -53,45 +51,45 @@ class UserMenuSubscriber implements EventSubscriberInterface
->getUsername() ?? 'user', [
'attributes' => [
'dropdown' => true,
'icon' => 'fas fa-user'
]
'icon' => 'fas fa-user',
],
]);
if ($this->tokenStorage->getToken()->getRoles()) {
$dropdown->addChild('logout', [
$dropdown->addChild($this->translator->trans('logout'), [
'route' => 'logout',
'attributes' => [
'icon' => 'fas fa-sign-out-alt',
'divider_append' => true
]
'divider_append' => true,
],
]);
$dropdown->addChild('edit profile', [
$dropdown->addChild($this->translator->trans('edit profile'), [
'route' => 'fos_user_profile_edit',
'attributes' => [
'icon' => 'fas fa-user-edit',
'divider_append' => true
]
'divider_append' => true,
],
]);
} else {
$dropdown->addChild('login', [
$dropdown->addChild($this->translator->trans('login'), [
'route' => 'fos_user_security_login',
'attributes' => [
'divider_append' => true,
'icon' => 'fas fa-sign-in-alt'
]
'icon' => 'fas fa-sign-in-alt',
],
]);
}
$dropdown->addChild('register', [
'route' => 'fos_user_registration_register',
'attributes' => [
'icon' => 'fas fa-file-signature'
]
'icon' => 'fas fa-file-signature',
],
]);
}
public static function getSubscribedEvents(): array
{
return [
UserMenuEvent::EVENT => 'onUserMenuConfigure'
UserMenuEvent::EVENT => 'onUserMenuConfigure',
];
}
}

View File

@ -0,0 +1,11 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
{{ knp_menu_render('sourceNavbar', {'currentClass': 'active', 'template': 'frames/structure/navbar/knp_menu.html.twig'}) }}
</div>
</nav>

View File

@ -1,6 +1,6 @@
{% extends "frames/default.html.twig" %}
{% extends "source/source.html.twig" %}
{% block title %}
{% trans %}
Source
Source edit
{% endtrans %}
{% endblock %}

View File

@ -0,0 +1,9 @@
{% extends "frames/default.html.twig" %}
{% block title %}
{% trans %}
Source
{% endtrans %}
{% endblock %}
{% block sub_navigation %}
{% include 'frames/structure/navbar/source_sub.html.twig'%}
{% endblock %}

View File

@ -1,4 +1,4 @@
{% extends "frames/default.html.twig" %}
{% extends "source/source.html.twig" %}
{% block title %}
{% trans %}
Source