diff --git a/core/Controller/Setting/NavigationSettingAdminController.php b/core/Controller/Setting/NavigationSettingAdminController.php new file mode 100644 index 0000000..9ef22e3 --- /dev/null +++ b/core/Controller/Setting/NavigationSettingAdminController.php @@ -0,0 +1,78 @@ +createFormBuilder($entity); + + $eventDispatcher->dispatch(new NavigationSettingEvent([ + 'builder' => $builder, + 'entity' => $entity, + ]), NavigationSettingEvent::FORM_INIT_EVENT); + + $form = $builder->getForm(); + + if ($request->isMethod('POST')) { + $form->handleRequest($request); + + if ($form->isValid()) { + $entityManager->update($entity); + $this->addFlash('success', 'The data has been saved.'); + + return $this->redirectToRoute('admin_site_navigation_show', [ + 'entity' => $entity->getNavigation()->getId(), + ]); + } + + $this->addFlash('warning', 'The form is not valid.'); + } + + return $this->render('@Core/setting/navigation_setting_admin/edit.html.twig', [ + 'form' => $form->createView(), + 'entity' => $entity, + ]); + } + + /** + * @Route("/delete/{entity}", name="admin_navigation_setting_delete", methods={"DELETE"}) + */ + public function delete(Entity $entity, EntityManager $entityManager, Request $request): Response + { + if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) { + $entityManager->delete($entity); + + $this->addFlash('success', 'The data has been removed.'); + } + + return $this->redirectToRoute('admin_site_navigation_show', [ + 'entity' => $entity->getNavigation()->getId(), + ]); + } + + public function getSection(): string + { + return ''; + } +} diff --git a/core/Controller/Site/NavigationAdminController.php b/core/Controller/Site/NavigationAdminController.php index 63c5994..ce1bf54 100644 --- a/core/Controller/Site/NavigationAdminController.php +++ b/core/Controller/Site/NavigationAdminController.php @@ -6,10 +6,13 @@ use App\Core\Controller\Admin\Crud\CrudController; use App\Core\Crud\CrudConfiguration; use App\Core\Crud\Field; use App\Core\Entity\Site\Navigation as Entity; +use App\Core\Event\Setting\NavigationSettingEvent; use App\Core\Factory\Site\NavigationFactory as Factory; use App\Core\Form\Site\NavigationType as Type; use App\Core\Manager\EntityManager; +use App\Core\Repository\NavigationSettingRepositoryQuery; use App\Core\Repository\Site\NavigationRepositoryQuery as RepositoryQuery; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; @@ -36,8 +39,24 @@ class NavigationAdminController extends CrudController /** * @Route("/admin/site/navigation/show/{entity}", name="admin_site_navigation_show", methods={"GET"}) */ - public function show(Entity $entity): Response - { + public function show( + Entity $entity, + EventDispatcherInterface $eventDispatcher, + NavigationSettingRepositoryQuery $settingQuery + ): Response { + $eventDispatcher->dispatch(new NavigationSettingEvent([ + 'navigation' => $entity, + ]), NavigationSettingEvent::INIT_EVENT); + + $settings = $settingQuery + ->where('.navigation = :navigation') + ->orderBy('.section, .label') + ->setParameter(':navigation', $entity->getId()) + ->paginate(1, 1000) + ; + + $this->getConfiguration()->addViewData('show', 'settings', $settings); + return $this->doShow($entity); } diff --git a/core/Entity/NavigationSetting.php b/core/Entity/NavigationSetting.php new file mode 100644 index 0000000..cee3540 --- /dev/null +++ b/core/Entity/NavigationSetting.php @@ -0,0 +1,111 @@ +id; + } + + public function getSection(): ?string + { + return $this->section; + } + + public function setSection(string $section): self + { + $this->section = $section; + + return $this; + } + + public function getLabel(): ?string + { + return $this->label; + } + + public function setLabel(string $label): self + { + $this->label = $label; + + return $this; + } + + public function getCode(): ?string + { + return $this->code; + } + + public function setCode(string $code): self + { + $this->code = $code; + + return $this; + } + + public function getValue() + { + return json_decode($this->value, true); + } + + public function setValue($value): self + { + $this->value = json_encode($value); + + return $this; + } + + public function getNavigation(): ?Navigation + { + return $this->navigation; + } + + public function setNavigation(?Navigation $navigation): self + { + $this->navigation = $navigation; + + return $this; + } +} diff --git a/core/Entity/Site/Navigation.php b/core/Entity/Site/Navigation.php index 86800fc..22f5a88 100644 --- a/core/Entity/Site/Navigation.php +++ b/core/Entity/Site/Navigation.php @@ -4,6 +4,7 @@ namespace App\Core\Entity\Site; use App\Core\Doctrine\Timestampable; use App\Core\Entity\EntityInterface; +use App\Core\Entity\NavigationSetting; use App\Core\Repository\Site\NavigationRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -64,9 +65,15 @@ class Navigation implements EntityInterface */ private $sortOrder; + /** + * @ORM\OneToMany(targetEntity=NavigationSetting::class, mappedBy="navigation", orphanRemoval=true) + */ + private $navigationSettings; + public function __construct() { $this->menus = new ArrayCollection(); + $this->navigationSettings = new ArrayCollection(); } public function getId(): ?int @@ -203,4 +210,34 @@ class Navigation implements EntityInterface return $this; } + + /** + * @return Collection|NavigationSetting[] + */ + public function getNavigationSettings(): Collection + { + return $this->navigationSettings; + } + + public function addNavigationSetting(NavigationSetting $navigationSetting): self + { + if (!$this->navigationSettings->contains($navigationSetting)) { + $this->navigationSettings[] = $navigationSetting; + $navigationSetting->setNavigation($this); + } + + return $this; + } + + public function removeNavigationSetting(NavigationSetting $navigationSetting): self + { + if ($this->navigationSettings->removeElement($navigationSetting)) { + // set the owning side to null (unless already changed) + if ($navigationSetting->getNavigation() === $this) { + $navigationSetting->setNavigation(null); + } + } + + return $this; + } } diff --git a/core/Event/Setting/NavigationSettingEvent.php b/core/Event/Setting/NavigationSettingEvent.php new file mode 100644 index 0000000..1611fcc --- /dev/null +++ b/core/Event/Setting/NavigationSettingEvent.php @@ -0,0 +1,28 @@ + + */ +class NavigationSettingEvent extends Event +{ + const INIT_EVENT = 'navigation_setting_event.init'; + const FORM_INIT_EVENT = 'navigation_setting_event.form_init'; + + protected $data; + + public function __construct($data = null) + { + $this->data = $data; + } + + public function getData() + { + return $this->data; + } +} diff --git a/core/EventSuscriber/NavigationSettingEventSubscriber.php b/core/EventSuscriber/NavigationSettingEventSubscriber.php new file mode 100644 index 0000000..d9ce5a6 --- /dev/null +++ b/core/EventSuscriber/NavigationSettingEventSubscriber.php @@ -0,0 +1,32 @@ + + */ +abstract class NavigationSettingEventSubscriber implements EventSubscriberInterface +{ + protected static int $priority = 0; + + public static function getSubscribedEvents() + { + return [ + NavigationSettingEvent::INIT_EVENT => ['onInit', self::$priority], + NavigationSettingEvent::FORM_INIT_EVENT => ['onFormInit', self::$priority], + ]; + } + + public function onInit(NavigationSettingEvent $event) + { + } + + public function onFormInit(NavigationSettingEvent $event) + { + } +} diff --git a/core/Factory/NavigationSettingFactory.php b/core/Factory/NavigationSettingFactory.php new file mode 100644 index 0000000..9b4f883 --- /dev/null +++ b/core/Factory/NavigationSettingFactory.php @@ -0,0 +1,25 @@ + + */ +class NavigationSettingFactory implements FactoryInterface +{ + public function create(Navigation $navigation, string $code): NavigationSetting + { + $entity = new NavigationSetting(); + + $entity + ->setNavigation($navigation) + ->setCode($code); + + return $entity; + } +} diff --git a/core/Repository/NavigationSettingRepository.php b/core/Repository/NavigationSettingRepository.php new file mode 100644 index 0000000..32a8382 --- /dev/null +++ b/core/Repository/NavigationSettingRepository.php @@ -0,0 +1,21 @@ + + */ +class NavigationSettingRepositoryQuery extends RepositoryQuery +{ + public function __construct(NavigationSettingRepository $repository, PaginatorInterface $paginator) + { + parent::__construct($repository, 'ns', $paginator); + } +} diff --git a/core/Resources/views/setting/navigation_setting_admin/_form.html.twig b/core/Resources/views/setting/navigation_setting_admin/_form.html.twig new file mode 100644 index 0000000..f73d2c4 --- /dev/null +++ b/core/Resources/views/setting/navigation_setting_admin/_form.html.twig @@ -0,0 +1,5 @@ +
{{ 'Label'|trans }} | +{{ 'Section'|trans }} | +{{ 'Actions'|trans }} | +
---|---|---|
+ + {{ item.label|trans }} + + | ++ {{ item.section|trans }} + | ++ + + + + + + | +
+
+
+
+
+ {{ 'No result'|trans }}
+
+ |
+