<?php
namespace App\Controller;
use App\Entity\Alergeno;
use App\Entity\Feed;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @Route("", name="index")
*/
public function index(Request $request, EntityManagerInterface $em): Response
{
$fb = new Feed();
$form = $this->createCrearForm($fb);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($fb);
$em->flush();
$this->addFlash('success', '¡Feed enviado correctamente! Gracias por su comentario.');
return $this->render('home/index.html.twig', [
'form' => $form->createView(),
]);
}
return $this->render('home/index.html.twig', [
'form' => $form->createView(),
]);
}
/**
* @Route("prueba", name="prueba")
*/
public function prueba(Request $request, EntityManagerInterface $em)
{
$serializer = $this->get('serializer');
$this->logger->info('Testing prueba');
$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
// it works for ODM also
$article = $em->find(Alergeno::class, 6 /*article id*/);
return new Response($serializer->serialize(
$article,
"json",
['groups' => ['alergeno']]
), Response::HTTP_OK);
}
/**
* @Route("lang", name="lang")
*/
public function lang(Request $request, EntityManagerInterface $em)
{
$request->setLocale($request->get('_locale'));
$request->getSession()->set('_locale', $request->get('_locale'));
$referer = $request->headers->get('referer');
return $this->redirect($referer);
}
/**
* Funcion auxiliar que crea el form para crear una pregunta
*/
private function createCrearForm(Feed $entity)
{
$builder = $this->createFormBuilder($entity);
$builder->setAction($this->generateUrl('index'));
$builder->setMethod('POST');
$builder->add('feedback', TextareaType::class, array('required' => true));
$builder->add('email', EmailType::class, array('required' => true));
$builder->add('nombre', TextType::class, array('required' => true));
$builder->add('_token', HiddenType::class, array('mapped' => false));
return $builder->getForm();
}
}