From 72ddc4671278f8bfd6762d6a6ba91a965bc9a56f Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Mon, 17 Sep 2018 13:09:04 +0200 Subject: [PATCH] Prepared structure for forms --- .../src/Controller/SourceController.php | 59 ++++++++++++++----- .../{ => Source}/SourceTemplateFactory.php | 11 ++-- .../Source/SourceTemplateFormFactory.php | 18 ++++++ application/src/Entity/AbstractSource.php | 2 + application/src/Entity/NameSource.php | 3 +- .../templates/source/content/user.html.twig | 1 - application/templates/source/name.html.twig | 5 -- application/templates/source/user.html.twig | 5 -- .../source/{ => view}/content/name.html.twig | 0 .../source/view/content/user.html.twig | 1 + .../templates/source/view/name.html.twig | 5 ++ .../source/{ => view}/source.html.twig | 0 .../templates/source/view/user.html.twig | 5 ++ 13 files changed, 83 insertions(+), 32 deletions(-) rename application/src/Creator/Factory/Template/{ => Source}/SourceTemplateFactory.php (72%) create mode 100644 application/src/Creator/Factory/Template/Source/SourceTemplateFormFactory.php delete mode 100644 application/templates/source/content/user.html.twig delete mode 100644 application/templates/source/name.html.twig delete mode 100644 application/templates/source/user.html.twig rename application/templates/source/{ => view}/content/name.html.twig (100%) create mode 100644 application/templates/source/view/content/user.html.twig create mode 100644 application/templates/source/view/name.html.twig rename application/templates/source/{ => view}/source.html.twig (100%) create mode 100644 application/templates/source/view/user.html.twig diff --git a/application/src/Controller/SourceController.php b/application/src/Controller/SourceController.php index 1299eb0..04a2614 100644 --- a/application/src/Controller/SourceController.php +++ b/application/src/Controller/SourceController.php @@ -1,41 +1,72 @@ loadSource($request, $id); + $view = $this->view($source, 200) + ->setTemplate((new SourceTemplateFactory($source, $request))->getTemplatePath()) + ->setTemplateVar('source'); + return $this->handleView($view); + } + + /** + * + * @Route("/source/{id}.{_format}/edit", defaults={"_format"="html"}) + */ + public function edit(Request $request, int $id): Response + { + $source = $this->loadSource($request, $id); + $form = $this->createForm(NameSourceType::class, $source); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $source = $form->getData(); + $this->saveSource($source); + } + return $this->render((new SourceTemplateFormFactory($source, $request))->getTemplatePath(), array( + 'form' => $form->createView(), + )); + } + + private function loadSource(Request $request, int $id): SourceInterface { $source = $this->getDoctrine() ->getRepository(AbstractSource::class) ->find($id); - if (!$source) { - throw $this->createNotFoundException('No source found for id '.$id); + if (! $source) { + throw $this->createNotFoundException('No source found for id ' . $id); } - $view = $this->view($source, 200) - ->setTemplate((new SourceTemplateFactory($source, $request))->getTemplatePath()) - ->setTemplateVar('source'); - return $this->handleView($view); + return $source; + } + + private function saveSource(SourceInterface $source): void + { + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($source); + $entityManager->flush(); } } diff --git a/application/src/Creator/Factory/Template/SourceTemplateFactory.php b/application/src/Creator/Factory/Template/Source/SourceTemplateFactory.php similarity index 72% rename from application/src/Creator/Factory/Template/SourceTemplateFactory.php rename to application/src/Creator/Factory/Template/Source/SourceTemplateFactory.php index 6d01ee4..f05730a 100644 --- a/application/src/Creator/Factory/Template/SourceTemplateFactory.php +++ b/application/src/Creator/Factory/Template/Source/SourceTemplateFactory.php @@ -1,20 +1,19 @@ generateName().'.'.$this->request->getRequestFormat().'.twig'; + return self::SOURCE_TEMPLATE_ROOT.'/'.self::VIEW_FOLDER.'/'.$this->generateName().'.'.$this->request->getRequestFormat().'.twig'; } - private function generateName(): string + protected function generateName(): string { $reflection = new \ReflectionClass($this->source); $shortName = $reflection->getShortName(); diff --git a/application/src/Creator/Factory/Template/Source/SourceTemplateFormFactory.php b/application/src/Creator/Factory/Template/Source/SourceTemplateFormFactory.php new file mode 100644 index 0000000..bb2cd68 --- /dev/null +++ b/application/src/Creator/Factory/Template/Source/SourceTemplateFormFactory.php @@ -0,0 +1,18 @@ +generateName().'.'.$this->request->getRequestFormat().'.twig'; + } +} + diff --git a/application/src/Entity/AbstractSource.php b/application/src/Entity/AbstractSource.php index 6d310c2..80d1d71 100644 --- a/application/src/Entity/AbstractSource.php +++ b/application/src/Entity/AbstractSource.php @@ -4,6 +4,7 @@ namespace App\Entity; use App\Entity\Attribut\NodeAttribut; use Doctrine\ORM\Mapping as ORM; +use JMS\Serializer\Annotation\Exclude; /** * @author kevinfrantz @@ -22,6 +23,7 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface * @var NodeInterface * @ORM\OneToOne(targetEntity="Node",cascade={"persist", "remove"}) * @ORM\JoinColumn(name="node_id", referencedColumnName="id") + * @Exclude */ protected $node; diff --git a/application/src/Entity/NameSource.php b/application/src/Entity/NameSource.php index bc23e2b..69e1928 100644 --- a/application/src/Entity/NameSource.php +++ b/application/src/Entity/NameSource.php @@ -4,6 +4,7 @@ namespace App\Entity; use App\Entity\Attribut\NameAttribut; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Validator\Constraints as Assert; /** * @author kevinfrantz @@ -16,7 +17,7 @@ class NameSource extends AbstractSource implements NameSourceInterface /** * @ORM\Column(type="string",length=255) - * + * @Assert\NotBlank() * @var string */ protected $name; diff --git a/application/templates/source/content/user.html.twig b/application/templates/source/content/user.html.twig deleted file mode 100644 index 4dcc15d..0000000 --- a/application/templates/source/content/user.html.twig +++ /dev/null @@ -1 +0,0 @@ -{% include "source/content/name." ~ app.request.requestFormat ~ ".twig" with {'source':source.userSource}%} \ No newline at end of file diff --git a/application/templates/source/name.html.twig b/application/templates/source/name.html.twig deleted file mode 100644 index 775f636..0000000 --- a/application/templates/source/name.html.twig +++ /dev/null @@ -1,5 +0,0 @@ -{% extends "source/source.html.twig" %} -{% block content %} -

{% trans %} Name {% endtrans %}

-{% include "source/content/name." ~ app.request.requestFormat ~ ".twig" %} -{% endblock %} diff --git a/application/templates/source/user.html.twig b/application/templates/source/user.html.twig deleted file mode 100644 index 2998bab..0000000 --- a/application/templates/source/user.html.twig +++ /dev/null @@ -1,5 +0,0 @@ -{% extends "source/source.html.twig" %} -{% block content %} -

{% trans %} User {% endtrans %}

-{% include "source/content/user." ~ app.request.requestFormat ~ ".twig" %} -{% endblock %} \ No newline at end of file diff --git a/application/templates/source/content/name.html.twig b/application/templates/source/view/content/name.html.twig similarity index 100% rename from application/templates/source/content/name.html.twig rename to application/templates/source/view/content/name.html.twig diff --git a/application/templates/source/view/content/user.html.twig b/application/templates/source/view/content/user.html.twig new file mode 100644 index 0000000..9fe2559 --- /dev/null +++ b/application/templates/source/view/content/user.html.twig @@ -0,0 +1 @@ +{% include "source/view/content/name." ~ app.request.requestFormat ~ ".twig" with {'source':source.userSource}%} \ No newline at end of file diff --git a/application/templates/source/view/name.html.twig b/application/templates/source/view/name.html.twig new file mode 100644 index 0000000..b9f7b6b --- /dev/null +++ b/application/templates/source/view/name.html.twig @@ -0,0 +1,5 @@ +{% extends "source/view/source.html.twig" %} +{% block content %} +

{% trans %} Name {% endtrans %}

+{% include "source/view/content/name." ~ app.request.requestFormat ~ ".twig" %} +{% endblock %} diff --git a/application/templates/source/source.html.twig b/application/templates/source/view/source.html.twig similarity index 100% rename from application/templates/source/source.html.twig rename to application/templates/source/view/source.html.twig diff --git a/application/templates/source/view/user.html.twig b/application/templates/source/view/user.html.twig new file mode 100644 index 0000000..5ae2f67 --- /dev/null +++ b/application/templates/source/view/user.html.twig @@ -0,0 +1,5 @@ +{% extends "source/view/source.html.twig" %} +{% block content %} +

{% trans %} User {% endtrans %}

+{% include "source/view/content/user." ~ app.request.requestFormat ~ ".twig" %} +{% endblock %} \ No newline at end of file