mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 14:07:25 +01:00
Prepared structure for forms
This commit is contained in:
parent
54a2a6a19c
commit
72ddc46712
@ -1,41 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use App\Entity\AbstractSource;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Source\Generator\StringGenerator;
|
||||
use FOS\RestBundle\FOSRestBundle;
|
||||
use App\Creator\Factory\Template\SourceTemplateFactory;
|
||||
use App\Creator\Factory\Template\Source\SourceTemplateFactory;
|
||||
use FOS\RestBundle\Controller\FOSRestController;
|
||||
use FOS\RestBundle\Controller\Annotations\View;
|
||||
use App\Form\NameSourceType;
|
||||
use App\Entity\SourceInterface;
|
||||
use App\Creator\Factory\Template\Source\SourceTemplateFormFactory;
|
||||
|
||||
/**
|
||||
* @todo IMPLEMENT SECURITY!
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class SourceController extends FOSRestController
|
||||
{
|
||||
|
||||
public function modify(int $id): Response
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
/**
|
||||
*
|
||||
* @Route("/source/{id}.{_format}", defaults={"_format"="html"})
|
||||
*/
|
||||
public function show(Request $request, int $id): Response
|
||||
{
|
||||
$source = $this->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();
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Creator\Factory\Template;
|
||||
namespace App\Creator\Factory\Template\Source;
|
||||
|
||||
use App\Entity\SourceInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Didn't know where to structure this file and how to name it.
|
||||
* Feel free to move it to a better place.
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class SourceTemplateFactory
|
||||
{
|
||||
const SOURCE_TEMPLATE_ROOT = 'source';
|
||||
|
||||
const VIEW_FOLDER = 'view';
|
||||
|
||||
/**
|
||||
* @var SourceInterface
|
||||
*/
|
||||
@ -36,10 +35,10 @@ class SourceTemplateFactory
|
||||
|
||||
public function getTemplatePath(): string
|
||||
{
|
||||
return self::SOURCE_TEMPLATE_ROOT.'/'.$this->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();
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace App\Creator\Factory\Template\Source;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*
|
||||
*/
|
||||
class SourceTemplateFormFactory extends SourceTemplateFactory
|
||||
{
|
||||
const FORM_FOLDER = 'form';
|
||||
|
||||
public function getTemplatePath(): string
|
||||
{
|
||||
return parent::SOURCE_TEMPLATE_ROOT.'/'.self::FORM_FOLDER.'/'.$this->generateName().'.'.$this->request->getRequestFormat().'.twig';
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -1 +0,0 @@
|
||||
{% include "source/content/name." ~ app.request.requestFormat ~ ".twig" with {'source':source.userSource}%}
|
@ -1,5 +0,0 @@
|
||||
{% extends "source/source.html.twig" %}
|
||||
{% block content %}
|
||||
<h1>{% trans %} Name {% endtrans %}</h1>
|
||||
{% include "source/content/name." ~ app.request.requestFormat ~ ".twig" %}
|
||||
{% endblock %}
|
@ -1,5 +0,0 @@
|
||||
{% extends "source/source.html.twig" %}
|
||||
{% block content %}
|
||||
<h1>{% trans %} User {% endtrans %}</h1>
|
||||
{% include "source/content/user." ~ app.request.requestFormat ~ ".twig" %}
|
||||
{% endblock %}
|
1
application/templates/source/view/content/user.html.twig
Normal file
1
application/templates/source/view/content/user.html.twig
Normal file
@ -0,0 +1 @@
|
||||
{% include "source/view/content/name." ~ app.request.requestFormat ~ ".twig" with {'source':source.userSource}%}
|
5
application/templates/source/view/name.html.twig
Normal file
5
application/templates/source/view/name.html.twig
Normal file
@ -0,0 +1,5 @@
|
||||
{% extends "source/view/source.html.twig" %}
|
||||
{% block content %}
|
||||
<h1>{% trans %} Name {% endtrans %}</h1>
|
||||
{% include "source/view/content/name." ~ app.request.requestFormat ~ ".twig" %}
|
||||
{% endblock %}
|
5
application/templates/source/view/user.html.twig
Normal file
5
application/templates/source/view/user.html.twig
Normal file
@ -0,0 +1,5 @@
|
||||
{% extends "source/view/source.html.twig" %}
|
||||
{% block content %}
|
||||
<h1>{% trans %} User {% endtrans %}</h1>
|
||||
{% include "source/view/content/user." ~ app.request.requestFormat ~ ".twig" %}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user