From aafa87ed41a3e2dd99e967c526423b50a163aef0 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 2 May 2021 20:17:39 +0200 Subject: [PATCH] add settings panel Signed-off-by: Simon Vieille --- .../Setting/SettingAdminController.php | 98 +++++++++++++++++++ core/Entity/Setting.php | 92 +++++++++++++++++ .../EntityManager/EntityManagerEvent.php | 2 +- core/Event/Setting/SettingEvent.php | 28 ++++++ .../EventSuscriber/SettingEventSubscriber.php | 32 ++++++ core/Factory/SettingFactory.php | 22 +++++ core/Repository/SettingRepository.php | 21 ++++ core/Repository/SettingRepositoryQuery.php | 18 ++++ core/Resources/translations/messages.fr.yaml | 3 + .../views/admin/module/menu.html.twig | 10 ++ .../setting/setting_admin/_form.html.twig | 5 + .../setting/setting_admin/edit.html.twig | 20 ++++ .../setting/setting_admin/filters.html.twig | 21 ++++ .../setting/setting_admin/index.html.twig | 67 +++++++++++++ core/Setting/SettingManager.php | 73 ++++++++++++++ src/Event/.gitkeep | 0 src/EventSuscriber/SettingEventSubscriber.php | 58 +++++++++++ 17 files changed, 569 insertions(+), 1 deletion(-) create mode 100644 core/Controller/Setting/SettingAdminController.php create mode 100644 core/Entity/Setting.php create mode 100644 core/Event/Setting/SettingEvent.php create mode 100644 core/EventSuscriber/SettingEventSubscriber.php create mode 100644 core/Factory/SettingFactory.php create mode 100644 core/Repository/SettingRepository.php create mode 100644 core/Repository/SettingRepositoryQuery.php create mode 100644 core/Resources/views/setting/setting_admin/_form.html.twig create mode 100644 core/Resources/views/setting/setting_admin/edit.html.twig create mode 100644 core/Resources/views/setting/setting_admin/filters.html.twig create mode 100644 core/Resources/views/setting/setting_admin/index.html.twig create mode 100644 core/Setting/SettingManager.php create mode 100644 src/Event/.gitkeep create mode 100644 src/EventSuscriber/SettingEventSubscriber.php diff --git a/core/Controller/Setting/SettingAdminController.php b/core/Controller/Setting/SettingAdminController.php new file mode 100644 index 0000000..e466193 --- /dev/null +++ b/core/Controller/Setting/SettingAdminController.php @@ -0,0 +1,98 @@ +dispatch(new SettingEvent(), SettingEvent::INIT_EVENT); + + $pager = $query + ->orderBy('.section, .label') + ->paginate($page) + ; + + return $this->render('@Core/setting/setting_admin/index.html.twig', [ + 'pager' => $pager, + ]); + } + + /** + * @Route("/edit/{entity}", name="admin_setting_edit") + */ + public function edit( + Entity $entity, + EntityManager $entityManager, + EventDispatcherInterface $eventDispatcher, + Request $request + ): Response + { + $builder = $this->createFormBuilder($entity); + + $eventDispatcher->dispatch(new SettingEvent([ + 'builder' => $builder, + 'entity' => $entity, + ]), SettingEvent::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_setting_index'); + } + + $this->addFlash('warning', 'The form is not valid.'); + } + + return $this->render('@Core/setting/setting_admin/edit.html.twig', [ + 'form' => $form->createView(), + 'entity' => $entity, + ]); + } + + /** + * @Route("/delete/{entity}", name="admin_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_setting_index'); + } + + public function getSection(): string + { + return 'setting'; + } +} diff --git a/core/Entity/Setting.php b/core/Entity/Setting.php new file mode 100644 index 0000000..92f9cd3 --- /dev/null +++ b/core/Entity/Setting.php @@ -0,0 +1,92 @@ +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; + } +} diff --git a/core/Event/EntityManager/EntityManagerEvent.php b/core/Event/EntityManager/EntityManagerEvent.php index ee69320..7c5c666 100644 --- a/core/Event/EntityManager/EntityManagerEvent.php +++ b/core/Event/EntityManager/EntityManagerEvent.php @@ -6,7 +6,7 @@ use App\Core\Entity\EntityInterface; use Symfony\Contracts\EventDispatcher\Event; /** - * class EntityEvent. + * class EntityManagerEvent. * * @author Simon Vieille */ diff --git a/core/Event/Setting/SettingEvent.php b/core/Event/Setting/SettingEvent.php new file mode 100644 index 0000000..065b426 --- /dev/null +++ b/core/Event/Setting/SettingEvent.php @@ -0,0 +1,28 @@ + + */ +class SettingEvent extends Event +{ + const INIT_EVENT = 'setting_event.init'; + const FORM_INIT_EVENT = '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/SettingEventSubscriber.php b/core/EventSuscriber/SettingEventSubscriber.php new file mode 100644 index 0000000..d65e783 --- /dev/null +++ b/core/EventSuscriber/SettingEventSubscriber.php @@ -0,0 +1,32 @@ + + */ +class SettingEventSubscriber implements EventSubscriberInterface +{ + protected static int $priority = 0; + + public static function getSubscribedEvents() + { + return [ + SettingEvent::INIT_EVENT => ['onInit', self::$priority], + SettingEvent::FORM_INIT_EVENT => ['onFormInit', self::$priority], + ]; + } + + public function onInit(SettingEvent $event) + { + } + + public function onFormInit(SettingEvent $event) + { + } +} diff --git a/core/Factory/SettingFactory.php b/core/Factory/SettingFactory.php new file mode 100644 index 0000000..ceeb0dd --- /dev/null +++ b/core/Factory/SettingFactory.php @@ -0,0 +1,22 @@ + + */ +class SettingFactory +{ + public function create(string $code): Setting + { + $entity = new Setting(); + + $entity->setCode($code); + + return $entity; + } +} diff --git a/core/Repository/SettingRepository.php b/core/Repository/SettingRepository.php new file mode 100644 index 0000000..6976ae2 --- /dev/null +++ b/core/Repository/SettingRepository.php @@ -0,0 +1,21 @@ + + */ +class SettingRepositoryQuery extends RepositoryQuery +{ + public function __construct(SettingRepository $repository, PaginatorInterface $paginator) + { + parent::__construct($repository, 's', $paginator); + } +} diff --git a/core/Resources/translations/messages.fr.yaml b/core/Resources/translations/messages.fr.yaml index e2d552b..23f95f1 100644 --- a/core/Resources/translations/messages.fr.yaml +++ b/core/Resources/translations/messages.fr.yaml @@ -129,3 +129,6 @@ "Yes": "Oui" "No": "Non" "Locale": "Langue" +"Settings": "Paramètres" +"Setting": "Paramètre" +"Section": "Section" diff --git a/core/Resources/views/admin/module/menu.html.twig b/core/Resources/views/admin/module/menu.html.twig index a94cc19..e684a6b 100644 --- a/core/Resources/views/admin/module/menu.html.twig +++ b/core/Resources/views/admin/module/menu.html.twig @@ -67,5 +67,15 @@ + + {% endif %} diff --git a/core/Resources/views/setting/setting_admin/_form.html.twig b/core/Resources/views/setting/setting_admin/_form.html.twig new file mode 100644 index 0000000..f73d2c4 --- /dev/null +++ b/core/Resources/views/setting/setting_admin/_form.html.twig @@ -0,0 +1,5 @@ +
+
+ {{ form_widget(form) }} +
+
diff --git a/core/Resources/views/setting/setting_admin/edit.html.twig b/core/Resources/views/setting/setting_admin/edit.html.twig new file mode 100644 index 0000000..49c84e6 --- /dev/null +++ b/core/Resources/views/setting/setting_admin/edit.html.twig @@ -0,0 +1,20 @@ + + diff --git a/core/Resources/views/setting/setting_admin/filters.html.twig b/core/Resources/views/setting/setting_admin/filters.html.twig new file mode 100644 index 0000000..5cb33d8 --- /dev/null +++ b/core/Resources/views/setting/setting_admin/filters.html.twig @@ -0,0 +1,21 @@ + diff --git a/core/Resources/views/setting/setting_admin/index.html.twig b/core/Resources/views/setting/setting_admin/index.html.twig new file mode 100644 index 0000000..31065ba --- /dev/null +++ b/core/Resources/views/setting/setting_admin/index.html.twig @@ -0,0 +1,67 @@ +{% extends '@Core/admin/layout.html.twig' %} + +{% block title %}{{ 'Settings'|trans }} - {{ parent() }}{% endblock %} + +{% block body %} +
+
+
+

{{ 'Settings'|trans }}

+
+
+ + {{ knp_pagination_render(pager) }} +
+ +
+ + + + + + + + + + {% for item in pager %} + {% set edit = path('admin_setting_edit', {entity: item.id}) %} + + + + + + + {% else %} + + + + {% endfor %} + +
{{ 'Label'|trans }}{{ 'Section'|trans }}{{ 'Actions'|trans }}
+ + {{ item.label }} + + + {{ item.section }} + + + + + + +
+ + +
+
+
+ +
+
+ {{ 'No result'|trans }} +
+
+
+{% endblock %} diff --git a/core/Setting/SettingManager.php b/core/Setting/SettingManager.php new file mode 100644 index 0000000..f6018ec --- /dev/null +++ b/core/Setting/SettingManager.php @@ -0,0 +1,73 @@ + + */ +class SettingManager +{ + protected EntityManager $entityManager; + protected SettingRepositoryQuery $query; + protected SettingFactory $factory; + + public function __construct(EntityManager $entityManager, SettingRepositoryQuery $query, SettingFactory $factory) + { + $this->entityManager = $entityManager; + $this->query = $query; + $this->factory = $factory; + } + + public function init(string $code, string $section, string $label, $value = null) + { + $entity = $this->get($code); + $isNew = null === $entity; + + if ($isNew) { + $entity = $this->factory->create($code); + $entity->setValue($value); + } + + $entity + ->setCode($code) + ->setSection($section) + ->setLabel($label) + ; + + if ($isNew) { + $this->entityManager->create($entity); + } else { + $this->entityManager->update($entity); + } + } + + public function get(string $code): ?Setting + { + return $this->query->create() + ->where('.code = :code') + ->setParameter(':code', $code) + ->findOne() + ; + } + + public function set(string $code, $value): bool + { + $entity = $this->get($code); + + if (!$entity) { + return false; + } + + $entity->setValue($value); + $this->entityManager->update($entity); + + return true; + } +} diff --git a/src/Event/.gitkeep b/src/Event/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/EventSuscriber/SettingEventSubscriber.php b/src/EventSuscriber/SettingEventSubscriber.php new file mode 100644 index 0000000..45f313e --- /dev/null +++ b/src/EventSuscriber/SettingEventSubscriber.php @@ -0,0 +1,58 @@ + + */ +class SettingEventSubscriber extends EventSubscriber +{ + protected SettingManager $manager; + + public function __construct(SettingManager $manager) + { + $this->manager = $manager; + } + + public function onInit(SettingEvent $event) + { + // $this->manager->init('myapp_foo', 'My app', 'Foo', 'Default value'); + // $this->manager->init('myapp_bar', 'My app', 'Bar', true); + } + + public function onFormInit(SettingEvent $event) + { + $data = $event->getData(); + $builder = $data['builder']; + $entity = $data['entity']; + + // if ('myapp_foo' === $entity->getCode()) { + // $builder->add( + // 'value', + // TextType::class, + // [ + // 'label' => $entity->getLabel(), + // ] + // ); + // } + // + // if ('myapp_bar' === $entity->getCode()) { + // $builder->add( + // 'value', + // CheckboxType::class, + // [ + // 'label' => $entity->getLabel(), + // 'required' => false, + // ] + // ); + // } + } +}