infinito/application/src/Controller/SourceController.php

83 lines
2.5 KiB
PHP
Raw Normal View History

2018-09-05 15:46:14 +02:00
<?php
2018-10-29 19:01:00 +01:00
2018-09-05 15:46:14 +02:00
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
2018-09-14 16:05:47 +02:00
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
2018-09-17 13:09:04 +02:00
use App\Creator\Factory\Template\Source\SourceTemplateFactory;
2018-10-03 16:14:15 +02:00
use App\Entity\Source\SourceInterface;
2018-09-17 13:09:04 +02:00
use App\Creator\Factory\Template\Source\SourceTemplateFormFactory;
2018-09-17 13:38:38 +02:00
use App\Creator\Factory\Form\Source\SourceFormFactory;
2018-09-20 16:03:00 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2018-10-03 17:10:15 +02:00
use App\Entity\Source\AbstractSource;
2018-10-28 15:46:55 +01:00
use App\Entity\Meta\RelationInterface;
2018-09-05 15:46:14 +02:00
/**
2018-10-04 20:48:12 +02:00
* @todo IMPLEMENT SECURITY!
2018-10-29 19:01:00 +01:00
*
2018-09-05 15:46:14 +02:00
* @author kevinfrantz
*/
2018-10-03 18:55:33 +02:00
class SourceController extends AbstractEntityController
2018-09-05 15:46:14 +02:00
{
2018-09-14 16:05:47 +02:00
/**
* @Route("/source/{id}.{_format}", defaults={"_format"="html"})
*/
public function show(Request $request, int $id): Response
2018-09-17 13:09:04 +02:00
{
2018-10-03 18:55:33 +02:00
$source = $this->loadEntityById($id);
2018-10-04 20:48:12 +02:00
// $assembler = $this->get(SourceDTOAssember::class);
// $dto = $assembler->build($source, $this->getUser());
2018-09-17 13:09:04 +02:00
$view = $this->view($source, 200)
->setTemplate((new SourceTemplateFactory($source, $request))->getTemplatePath())
->setTemplateVar('source');
2018-10-29 19:01:00 +01:00
2018-09-17 13:09:04 +02:00
return $this->handleView($view);
}
/**
2018-09-17 14:04:58 +02:00
* @Route("/source/{id}/edit.{_format}", defaults={"_format"="html"})
2018-09-17 13:09:04 +02:00
*/
public function edit(Request $request, int $id): Response
{
2018-10-03 18:55:33 +02:00
$source = $this->loadEntityById($id);
2018-09-17 13:38:38 +02:00
$form = $this->createForm((new SourceFormFactory($source))->getNamespace(), $source);
2018-09-17 13:09:04 +02:00
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$source = $form->getData();
$this->saveSource($source);
}
2018-10-29 19:01:00 +01:00
2018-09-20 14:26:28 +02:00
return $this->render((new SourceTemplateFormFactory($source, $request))->getTemplatePath(), [
2018-10-29 19:01:00 +01:00
'form' => $form->createView(),
2018-09-20 14:26:28 +02:00
]);
2018-09-17 13:09:04 +02:00
}
2018-09-20 16:03:00 +02:00
/**
* @Route("/source/{id}/node.{_format}", defaults={"_format"="html"})
*/
2018-10-04 20:48:12 +02:00
public function node(int $id): RedirectResponse
2018-09-20 16:03:00 +02:00
{
2018-10-04 20:48:12 +02:00
$nodeId = $this->loadNodeById($id)->getId();
2018-10-29 19:01:00 +01:00
return $this->redirectToRouteById('app_node_show', $nodeId);
2018-10-04 20:48:12 +02:00
}
2018-10-29 19:01:00 +01:00
private function loadNodeById(int $id): RelationInterface
{
2018-10-04 20:48:12 +02:00
return $this->loadEntityById($id)->getNode();
2018-09-20 16:03:00 +02:00
}
2018-09-17 13:09:04 +02:00
private function saveSource(SourceInterface $source): void
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($source);
$entityManager->flush();
2018-09-12 23:25:22 +03:00
}
2018-10-04 20:48:12 +02:00
2018-10-03 18:55:33 +02:00
protected function setEntityName(): void
{
$this->entityName = AbstractSource::class;
}
2018-09-05 15:46:14 +02:00
}