<?php
// src/Controller/EntityController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\Entity\AbstractEntity;
use App\Repository\EntityRepository;
/**
* @Route("/entity", name="entity_")
*/
abstract class EntityController extends BaseController
{
const REF = '';
const NAME = '';
protected $entityClassName = '';
protected $repositoryClassName = '';
protected ?EntityRepository $repository;
public function __construct(ContainerInterface $container, EntityManagerInterface $em)
{
parent::__construct($container, $em);
//if(empty(static::$entity))
$this->entityClassName = 'App\\Entity\\'.static::NAME;
$this->repositoryClassName = 'App\\Repository\\'.static::NAME.'Repository';
$this->repository = $this->em->getRepository($this->entityClassName);
}
protected function getEntity(int $id) :?AbstractEntity
{
$object = $this->repository->find($id);
//var_dump($object);
return $object;
}
/* PARAMS */
public function params()
{
$this->params['page']['head']['title'] = $this->repository::LABEL;
$this->params['page']['title'] = $this->repository::LABEL;
$this->params['entity'] = [
'ref' => static::REF,
'name' => static::NAME,
'label' => $this->repository::LABEL,
];
$this->params['route'] = [
'update' => static::REF.'_update',
'delete' => static::REF.'_delete',
];
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_index',
'label' => $this->repository::LABEL,
];
$entity_classname = 'App\\Entity\\'.static::NAME;
$this->params['fields'] = $entity_classname::FIELDS;
}
/* ACTIONS */
/**
* @Route("", name="index")
*/
public function indexAction(Request $request): Response
{
$this->params();
$this->params['page']['subtitle'] = 'Accueil';
//return $this->render('entity/index.html.twig', $this->params);
return $this->redirectToRoute(static::REF.'_list');
}
/**
* @Route("/list", name="list")
*/
public function listAction(Request $request): Response
{
$this->params();
$this->params['page']['head']['title'] = 'Liste '.$this->repository::LABEL;
$this->params['page']['subtitle'] = 'Liste';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_list',
'label' => 'Liste',
];
// Pagination
$page_limit = $request->query->get('limit');
// Test page size
if (empty($page_limit) || !is_numeric($page_limit) || $page_limit != (int)$page_limit || $page_limit<$this->repository::SEARCH_LIMIT_MIN || $page_limit>$this->repository::SEARCH_LIMIT_MAX)
$page_limit = $this->repository::SEARCH_LIMIT;
$elems = count($this->repository->findAll());
$page_max = ceil($elems / $page_limit);
$page_nb = $request->query->get('page');
if (empty($page_nb) || !is_numeric($page_nb) || $page_nb != (int)$page_nb || $page_nb<1)
$page_nb = 1;
elseif($page_nb>$page_max)
$page_nb = $page_max;
$this->params['page_limit'] = $page_limit;
$this->params['page_max'] = $page_max;
$this->params['page_nb'] = $page_nb;
$this->params['elem_nb'] = $elems;
// Tri
$order = $request->query->get('order');
$list = $this->repository->findBy([], !empty($order) ?$order :[], $page_limit, ($page_limit * ($page_nb - 1)));
$this->params['list'] = $list;
// Twig template
$tplfile = static::REF.'/list.html.twig';
if (!file_exists('../templates/'.$tplfile))
$tplfile = 'entity/list.html.twig';
return $this->render($tplfile, $this->params);
}
/**
* @Route("/search", name="search")
*/
public function searchAction(Request $request): Response
{
// Mots-clés
$q = $request->query->get('q');
// Tri
$order = $request->query->get('order');
$this->params();
$this->params['page']['head']['title'] = 'Recherche '.$this->repository::LABEL;
$this->params['page']['subtitle'] = 'Recherche';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_search',
'label' => 'Recherche',
];
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_search',
'label' => $q,
'params' => ['q'=>$q],
];
$this->params['search_q'] = $q;
// Pagination
$page_limit = $request->query->get('limit');
// Test page size
if (empty($page_limit) || !is_numeric($page_limit) || $page_limit != (int)$page_limit || $page_limit<$this->repository::SEARCH_LIMIT_MIN || $page_limit>$this->repository::SEARCH_LIMIT_MAX)
$page_limit = $this->repository::SEARCH_LIMIT;
$elems = $this->repository->countByQ($q);
//var_dump($elems); die();
$page_max = ceil($elems / $page_limit);
$page_nb = $request->query->get('page');
if (empty($page_nb) || !is_numeric($page_nb) || $page_nb != (int)$page_nb || $page_nb<1)
$page_nb = 1;
elseif($page_nb>$page_max)
$page_nb = $page_max;
$this->params['page_limit'] = $page_limit;
$this->params['page_max'] = $page_max;
$this->params['page_nb'] = $page_nb;
$this->params['elem_nb'] = $elems;
$list = $this->repository->findByQ($q, !empty($order) ?$order :[], $page_limit, ($page_limit * ($page_nb - 1)));
$this->params['list'] = $list;
// Twig template
$tplfile = static::REF.'/list.html.twig';
if (!file_exists('../templates/'.$tplfile))
$tplfile = 'entity/list.html.twig';
return $this->render($tplfile, $this->params);
}
/**
* @Route("/create", name="create")
*/
public function createAction(Request $request, ValidatorInterface $validator): Response
{
$object = $this->repository->createEntity();
$this->params();
$this->params['page']['head']['title'] = 'Création '.$this->repository::LABEL;
$this->params['page']['subtitle'] = 'Création';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_create',
'label' => 'Ajouter',
];
if (class_exists('App\\Form\\'.static::NAME.'Type')) {
$form_classname = 'App\\Form\\'.static::NAME.'Type';
$object->setUtime(new \DateTime());
$form = $this->createForm($form_classname, $object);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$date = new \DateTime();
$object->setCtime($date);
//var_dump($object); die();
if ($form->isValid()) {
//var_dump($object); die();
$errors = $validator->validate($object);
//var_dump($errors);
if (count($errors) > 0) {
return new Response((string) $errors, 400);
}
// @todo user entityrepository
$this->em->persist($object);
$this->em->flush();
return $this->redirectToRoute(static::REF.'_read', ['id'=>$object->getId()]);
}
else {
$errors = $validator->validate($object);
var_dump($errors);
if (count($errors) > 0) {
return new Response((string) $errors, 400);
}
}
}
$this->params['form'] = $form->createView();
}
return $this->render('entity/create.html.twig', $this->params);
}
/**
* @Route("/ajax/create", name="ajax_create")
*/
public function createAjaxAction(Request $request, ValidatorInterface $validator): Response
{
$this->repository->createEntity();
$this->params();
$this->params['page']['subtitle'] = 'Création';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_create',
'label' => 'Ajouter',
];
if (class_exists('App\\Form\\'.static::NAME.'Type')) {
$form_classname = 'App\\Form\\'.static::NAME.'Type';
$object->setUtime(new \DateTime());
$form = $this->createForm($form_classname, $object);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$date = new \DateTime();
$object->setCtime($date);
//var_dump($object);
if ($form->isValid()) {
//var_dump($object); die();
$errors = $validator->validate($object);
//var_dump($errors);
if (count($errors) > 0) {
return new JsonResponse((string) $errors, 400);
}
// @todo user entityrepository
$this->em->persist($object);
$this->em->flush();
return new JsonResponse($object);
}
else {
$errors = $validator->validate($object);
var_dump($errors);
if (count($errors) > 0) {
return new JsonResponse((string) $errors, 400);
}
}
}
$this->params['form'] = $form->createView();
}
return new JsonResponse($this->params);
}
/**
* @Route("/{id}", name="read")
*/
public function readAction(Request $request, int $id): Response
{
if (!($object = $this->getEntity($id)))
return $this->redirectToRoute(static::REF.'_list');
$this->params();
$this->params['object'] = $object;
$this->params['object_class'] = get_class($object);
$this->params['page']['head']['title'] = $object.' - '.$this->repository::LABEL;
$this->params['page']['subtitle'] = 'Affichage';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_read',
'label' => $object,
'params' => ['id'=>$id],
];
if (class_exists('App\\Form\\'.static::NAME.'Type')) {
$form_classname = 'App\\Form\\'.static::NAME.'Type';
$form = $this->createForm($form_classname, $object, ['disabled' => true]);
$this->params['form'] = $form->createView();
}
// Twig template
$tplfile = static::REF.'/view.html.twig';
if (!file_exists('../templates/'.$tplfile))
$tplfile = 'entity/view.html.twig';
return $this->render($tplfile, $this->params);
}
/**
* @Route("/ajax/{id}", name="ajax_read")
*/
public function readAjaxAction(Request $request, int $id): Response
{
if (!($object = $this->getEntity($id)))
die('404');
return new JsonResponse($object->__toJson());
}
/**
* @Route("/ajax/{id}/update", name="ajax_update")
*/
public function updateAjaxAction(Request $request, int $id, EntityManagerInterface $em): Response
{
if (!($object = $this->getEntity($id)))
die('404');
$data = $request->request->get('data');
//var_dump($data); die();
if (is_array($data)) foreach($data as $name=>$value) {
$object->{"set$name"}($value);
}
$p = $em->persist($object);
$f = $em->flush();
$r = ['data'=>$data];
return new JsonResponse($r);
}
/**
* @Route("/{id}/update", name="update")
*/
public function updateAction(Request $request, ValidatorInterface $validator, int $id): Response
{
if (!($object = $this->getEntity($id)))
return $this->redirectToRoute(static::REF.'_list');
$this->params();
$this->params['object'] = $object;
$this->params['page']['head']['title'] = 'Modifier '.$object.' - '.$this->repository::LABEL;
$this->params['page']['subtitle'] = 'Modifier';
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_read',
'label' => $object,
'params' => ['id'=>$id],
];
$this->params['page']['breadcrumbs'][] = [
'route' => static::REF.'_update',
'label' => 'Modifier',
'params' => ['id'=>$id],
];
if (class_exists('App\\Form\\'.static::NAME.'Type')) {
$form_classname = 'App\\Form\\'.static::NAME.'Type';
$form = $this->createForm($form_classname, $object);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$errors = $validator->validate($object);
//var_dump($errors);
if (count($errors) > 0) {
return new Response((string) $errors, 400);
}
$this->em->persist($object);
$this->em->flush();
return $this->redirectToRoute(static::REF.'_read', ['id'=>$object->getId()]);
}
$this->params['form'] = $form->createView();
}
return $this->render('entity/update.html.twig', $this->params);
}
/**
* @Route("/{id}/delete", name="delete")
*/
public function removeAction(Request $request, int $id): Response
{
if (($object = $this->getEntity($id))) {
$this->em->remove($object);
$this->em->flush();
}
return $this->redirectToRoute(static::REF.'_list');
}
/**
* @Route("/ajax/{id}/delete", name="ajax_delete")
*/
public function removeAjaxAction(Request $request, int $id): Response
{
if (!($object = $this->getEntity($id))) {
}
$this->params();
$this->params['object'] = $object;
$this->params['page']['subtitle'] = 'Suppression';
return $this->render('entity/delete.html.twig', $this->params);
}
}