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 %} -