From b8335d38827690b28486f51f50cccc6fca3095b1 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 18 Mar 2021 21:40:28 +0100 Subject: [PATCH] add base of page CRUD --- assets/css/admin.scss | 4 + config/packages/doctrine.yaml | 9 +- src/Controller/Site/NodeAdminController.php | 3 + src/Controller/Site/PageAdminController.php | 84 +++++++ src/Entity/Site/Page/Block.php | 79 +++++++ src/Entity/Site/Page/Page.php | 211 ++++++++++++++++++ src/Factory/Site/Page/PageFactory.php | 23 ++ src/Form/Site/NodeType.php | 1 + src/Form/Site/Page/PageType.php | 76 +++++++ src/Form/Site/Page/TextBlockType.php | 32 +++ src/Form/Site/Page/TextareaBlockType.php | 26 +++ src/Page/SimplePage.php | 70 ++++++ src/Repository/Site/Page/BlockRepository.php | 15 ++ .../Site/Page/BlockRepositoryQuery.php | 19 ++ src/Repository/Site/Page/PageRepository.php | 15 ++ .../Site/Page/PageRepositoryQuery.php | 28 +++ templates/site/page_admin/_form.html.twig | 40 ++++ templates/site/page_admin/edit.html.twig | 32 +++ 18 files changed, 765 insertions(+), 2 deletions(-) create mode 100644 src/Controller/Site/PageAdminController.php create mode 100644 src/Entity/Site/Page/Block.php create mode 100644 src/Entity/Site/Page/Page.php create mode 100644 src/Factory/Site/Page/PageFactory.php create mode 100644 src/Form/Site/Page/PageType.php create mode 100644 src/Form/Site/Page/TextBlockType.php create mode 100644 src/Form/Site/Page/TextareaBlockType.php create mode 100644 src/Page/SimplePage.php create mode 100644 src/Repository/Site/Page/BlockRepository.php create mode 100644 src/Repository/Site/Page/BlockRepositoryQuery.php create mode 100644 src/Repository/Site/Page/PageRepository.php create mode 100644 src/Repository/Site/Page/PageRepositoryQuery.php create mode 100644 templates/site/page_admin/_form.html.twig create mode 100644 templates/site/page_admin/edit.html.twig diff --git a/assets/css/admin.scss b/assets/css/admin.scss index f1350a4..42b932e 100644 --- a/assets/css/admin.scss +++ b/assets/css/admin.scss @@ -423,3 +423,7 @@ table.table-fixed, .table-fixed > table { } } } + +fieldset.form-group { + margin-bottom: 0; +} diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index c8c6e32..9781681 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -10,12 +10,17 @@ doctrine: naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware auto_mapping: true mappings: - App: + App\Entity: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' - alias: App + alias: App\Entity + App\Page: + is_bundle: false + type: annotation + dir: '%kernel.project_dir%/src/Page' + prefix: 'App\Page' gedmo_tree: type: annotation prefix: Gedmo\Tree\Entity diff --git a/src/Controller/Site/NodeAdminController.php b/src/Controller/Site/NodeAdminController.php index e06f3cb..999142c 100644 --- a/src/Controller/Site/NodeAdminController.php +++ b/src/Controller/Site/NodeAdminController.php @@ -16,6 +16,9 @@ use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\TaggedContainerInterface; /** * @Route("/admin/site/node") diff --git a/src/Controller/Site/PageAdminController.php b/src/Controller/Site/PageAdminController.php new file mode 100644 index 0000000..ccd656e --- /dev/null +++ b/src/Controller/Site/PageAdminController.php @@ -0,0 +1,84 @@ +create(FooPage::class); + $entity = $factory->create(SimplePage::class); + $entity->setName('Page de test '.mt_rand()); + + $entityManager->create($entity); + + $this->addFlash('success', 'Donnée enregistrée.'); + + return $this->redirectToRoute('admin_site_page_edit', [ + 'entity' => $entity->getId(), + ]); + } + + /** + * @Route("/edit/{entity}", name="admin_site_page_edit") + */ + public function edit( + int $entity, + EntityFactory $factory, + EntityManager $entityManager, + EntityRepositoryQuery $repositoryQuery, + Request $request + ): Response { + // $page = $factory->create(PageFoo::class); + // $page->setName('Page de test 2'); + // $entityManager->update($page); + // die; + + $entity = $repositoryQuery->filterById($entity)->findOne(); + $form = $this->createForm(EntityType::class, $entity); + + if ($request->isMethod('POST')) { + $form->handleRequest($request); + + if ($form->isValid()) { + $entityManager->update($entity); + + $this->addFlash('success', 'Donnée enregistrée.'); + + return $this->redirectToRoute('admin_site_page_edit', [ + 'entity' => $entity->getId(), + ]); + } + + $this->addFlash('warning', 'Le formulaire est invalide.'); + } + + return $this->render('site/page_admin/edit.html.twig', [ + 'form' => $form->createView(), + 'entity' => $entity, + ]); + } + + public function getSection(): string + { + return ''; + } +} diff --git a/src/Entity/Site/Page/Block.php b/src/Entity/Site/Page/Block.php new file mode 100644 index 0000000..4c374d7 --- /dev/null +++ b/src/Entity/Site/Page/Block.php @@ -0,0 +1,79 @@ +id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getValue(): ?string + { + return $this->value; + } + + public function setValue(string $value): self + { + $this->value = $value; + + return $this; + } + + public function getPage(): ?Page + { + return $this->page; + } + + public function setPage(?Page $page): self + { + $this->page = $page; + + return $this; + } +} diff --git a/src/Entity/Site/Page/Page.php b/src/Entity/Site/Page/Page.php new file mode 100644 index 0000000..180db61 --- /dev/null +++ b/src/Entity/Site/Page/Page.php @@ -0,0 +1,211 @@ +blocks = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getTemplate(): ?string + { + return $this->template; + } + + public function setTemplate(?string $template): self + { + $this->template = $template; + + return $this; + } + + /** + * @return Collection|Block[] + */ + public function getBlocks(): Collection + { + return $this->blocks; + } + + public function addBlock(Block $block): self + { + if (!$this->blocks->contains($block)) { + $this->blocks[] = $block; + $block->setPage($this); + } + + return $this; + } + + public function removeBlock(Block $block): self + { + if ($this->blocks->removeElement($block)) { + // set the owning side to null (unless already changed) + if ($block->getPage() === $this) { + $block->setPage(null); + } + } + + return $this; + } + + public function buildForm(FormBuilderInterface $builder) + { + } + + public function getBlock($name) + { + foreach ($this->getBlocks() as $block) { + if ($block->getName() === $name) { + return $block; + } + } + + $block = new Block(); + $block->setName($name); + $block->setPage($this); + + return $block; + } + + public function setBlock(Block $block): self + { + foreach ($this->blocks->toArray() as $key => $value) { + if ($value->getName() === $block->getName()) { + $this->blocks->remove($key); + $this->blocks->add($block); + + return $this; + } + } + + $this->blocks->add($block); + + return $this; + } + + public function getMetaTitle(): ?string + { + return $this->metaTitle; + } + + public function setMetaTitle(?string $metaTitle): self + { + $this->metaTitle = $metaTitle; + + return $this; + } + + public function getMetaDescrition(): ?string + { + return $this->metaDescrition; + } + + public function setMetaDescrition(?string $metaDescrition): self + { + $this->metaDescrition = $metaDescrition; + + return $this; + } + + public function getOgTitle(): ?string + { + return $this->ogTitle; + } + + public function setOgTitle(?string $ogTitle): self + { + $this->ogTitle = $ogTitle; + + return $this; + } + + public function getOgDescription(): ?string + { + return $this->ogDescription; + } + + public function setOgDescription(?string $ogDescription): self + { + $this->ogDescription = $ogDescription; + + return $this; + } +} diff --git a/src/Factory/Site/Page/PageFactory.php b/src/Factory/Site/Page/PageFactory.php new file mode 100644 index 0000000..a309342 --- /dev/null +++ b/src/Factory/Site/Page/PageFactory.php @@ -0,0 +1,23 @@ + + */ +class PageFactory +{ + public function create(string $className): Page + { + $entity = new $className(); + + return $entity; + } +} diff --git a/src/Form/Site/NodeType.php b/src/Form/Site/NodeType.php index 81480a6..1c99590 100644 --- a/src/Form/Site/NodeType.php +++ b/src/Form/Site/NodeType.php @@ -67,6 +67,7 @@ class NodeType extends AbstractType { $resolver->setDefaults([ 'data_class' => Node::class, + 'pages' => null, ]); } } diff --git a/src/Form/Site/Page/PageType.php b/src/Form/Site/Page/PageType.php new file mode 100644 index 0000000..3a06c28 --- /dev/null +++ b/src/Form/Site/Page/PageType.php @@ -0,0 +1,76 @@ +add( + 'metaTitle', + TextType::class, + [ + 'label' => 'Titre', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'metaDescrition', + TextType::class, + [ + 'label' => 'Description', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'ogTitle', + TextType::class, + [ + 'label' => 'Titre', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'ogDescription', + TextType::class, + [ + 'label' => 'Description', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->getData()->buildForm($builder); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Page::class, + ]); + } +} diff --git a/src/Form/Site/Page/TextBlockType.php b/src/Form/Site/Page/TextBlockType.php new file mode 100644 index 0000000..c6876a9 --- /dev/null +++ b/src/Form/Site/Page/TextBlockType.php @@ -0,0 +1,32 @@ +add( + 'value', + TextType::class, + array_merge([ + 'required' => false, + 'label' => false, + ], $options['options']), + ); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Block::class, + 'options' => [], + ]); + } +} diff --git a/src/Form/Site/Page/TextareaBlockType.php b/src/Form/Site/Page/TextareaBlockType.php new file mode 100644 index 0000000..a41a006 --- /dev/null +++ b/src/Form/Site/Page/TextareaBlockType.php @@ -0,0 +1,26 @@ +add( + 'value', + TextareaType::class, + array_merge([ + 'required' => false, + 'label' => false, + ], $options['options']), + ); + } +} diff --git a/src/Page/SimplePage.php b/src/Page/SimplePage.php new file mode 100644 index 0000000..3a1c3fb --- /dev/null +++ b/src/Page/SimplePage.php @@ -0,0 +1,70 @@ +add( + 'title', + TextBlockType::class, + [ + 'label' => 'Titre', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + + $builder->add( + 'content', + TextareaBlockType::class, + [ + 'label' => 'Content', + 'options' => [ + 'attr' => [ + 'data-tinymce' => '', + 'rows' => '18', + ], + 'constraints' => [ + ], + ] + ] + ); + } + + public function setTitle(Block $block) + { + return $this->setBlock($block); + } + + public function getTitle() + { + return $this->getBlock('title'); + } + + public function setContent(Block $block) + { + return $this->setBlock($block); + } + + public function getContent() + { + return $this->getBlock('content'); + } +} diff --git a/src/Repository/Site/Page/BlockRepository.php b/src/Repository/Site/Page/BlockRepository.php new file mode 100644 index 0000000..3cd4648 --- /dev/null +++ b/src/Repository/Site/Page/BlockRepository.php @@ -0,0 +1,15 @@ + + */ +class BlockRepositoryQuery extends RepositoryQuery +{ + public function __construct(BlockRepository $repository, PaginatorInterface $paginator) + { + parent::__construct($repository, 'b', $paginator); + } +} diff --git a/src/Repository/Site/Page/PageRepository.php b/src/Repository/Site/Page/PageRepository.php new file mode 100644 index 0000000..5fd82b7 --- /dev/null +++ b/src/Repository/Site/Page/PageRepository.php @@ -0,0 +1,15 @@ + + */ +class PageRepositoryQuery extends RepositoryQuery +{ + public function __construct(PageRepository $repository, PaginatorInterface $paginator) + { + parent::__construct($repository, 'p', $paginator); + } + + public function filterById($id) + { + $this + ->where('.id = :id') + ->setParameter(':id', $id); + + return $this; + } +} diff --git a/templates/site/page_admin/_form.html.twig b/templates/site/page_admin/_form.html.twig new file mode 100644 index 0000000..60709a6 --- /dev/null +++ b/templates/site/page_admin/_form.html.twig @@ -0,0 +1,40 @@ +{% set formMetas %} + {% for item in ['metaTitle', 'metaDescrition'] %} + {{ form_row(form[item]) }} + {% endfor %} +{% endset %} + +{% set formOpenGraph %} + {% for item in ['ogTitle', 'ogDescription'] %} + {{ form_row(form[item]) }} + {% endfor %} +{% endset %} + +{% set formSitemap %} +{% endset %} + +
+
+ {{ form_widget(form) }} +
+
+ + +
+
+ {{ formMetas|raw }} +
+
+ {{ formOpenGraph|raw }} +
+
+
+
+ diff --git a/templates/site/page_admin/edit.html.twig b/templates/site/page_admin/edit.html.twig new file mode 100644 index 0000000..11aa800 --- /dev/null +++ b/templates/site/page_admin/edit.html.twig @@ -0,0 +1,32 @@ +{% extends 'admin/layout.html.twig' %} + +{% block body %} +
+
+
+

{{ entity.name }}

+
+ +
+
+ +
+
+
+
+ +
+
+
+
+ {{ include('site/page_admin/_form.html.twig') }} +
+
+
+ + {{ form_rest(form) }} +
+{% endblock %}