src/Controller/EntityController.php line 34

Open in your IDE?
  1. <?php
  2. // src/Controller/EntityController.php
  3. namespace App\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Validator\Validator\ValidatorInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use App\Entity\AbstractEntity;
  14. use App\Repository\EntityRepository;
  15. /**
  16.  * @Route("/entity", name="entity_")
  17.  */
  18. abstract class EntityController extends BaseController
  19. {
  20.     const REF '';
  21.     const NAME '';
  22.     
  23.     protected $entityClassName '';
  24.     protected $repositoryClassName '';
  25.     protected ?EntityRepository $repository;
  26.     
  27.     public function __construct(ContainerInterface $containerEntityManagerInterface $em)
  28.     {
  29.         parent::__construct($container$em);
  30.         
  31.         //if(empty(static::$entity))
  32.         $this->entityClassName 'App\\Entity\\'.static::NAME;
  33.         $this->repositoryClassName 'App\\Repository\\'.static::NAME.'Repository';
  34.         $this->repository $this->em->getRepository($this->entityClassName);
  35.     }
  36.     protected function getEntity(int $id) :?AbstractEntity
  37.     {
  38.         $object $this->repository->find($id);
  39.         //var_dump($object);
  40.         return $object;
  41.     }
  42.     /* PARAMS */
  43.     
  44.     public function params()
  45.     {
  46.         $this->params['page']['head']['title'] = $this->repository::LABEL;
  47.         $this->params['page']['title'] = $this->repository::LABEL;
  48.         $this->params['entity'] = [
  49.             'ref' => static::REF,
  50.             'name' => static::NAME,
  51.             'label' => $this->repository::LABEL,
  52.         ];
  53.         $this->params['route'] = [
  54.             'update' => static::REF.'_update',
  55.             'delete' => static::REF.'_delete',
  56.         ];
  57.         $this->params['page']['breadcrumbs'][] = [
  58.             'route' => static::REF.'_index',
  59.             'label' => $this->repository::LABEL,
  60.         ];
  61.         $entity_classname 'App\\Entity\\'.static::NAME;
  62.         $this->params['fields'] = $entity_classname::FIELDS;
  63.     }
  64.     /* ACTIONS */
  65.     
  66.    /**
  67.     * @Route("", name="index")
  68.     */
  69.     public function indexAction(Request $request): Response
  70.     {
  71.         $this->params();
  72.         $this->params['page']['subtitle'] = 'Accueil';
  73.         //return $this->render('entity/index.html.twig', $this->params);
  74.         return $this->redirectToRoute(static::REF.'_list');
  75.     }
  76.     
  77.    /**
  78.     * @Route("/list", name="list")
  79.     */
  80.     public function listAction(Request $request): Response
  81.     {
  82.         $this->params();
  83.         $this->params['page']['head']['title'] = 'Liste '.$this->repository::LABEL;
  84.         $this->params['page']['subtitle'] = 'Liste';
  85.         $this->params['page']['breadcrumbs'][] = [
  86.             'route' => static::REF.'_list',
  87.             'label' => 'Liste',
  88.         ];
  89.         // Pagination
  90.         $page_limit $request->query->get('limit');
  91.         // Test page size
  92.         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)
  93.             $page_limit $this->repository::SEARCH_LIMIT;
  94.         
  95.         $elems count($this->repository->findAll());
  96.         $page_max ceil($elems $page_limit);
  97.         $page_nb $request->query->get('page');
  98.         if (empty($page_nb) || !is_numeric($page_nb) || $page_nb != (int)$page_nb || $page_nb<1)
  99.             $page_nb 1;
  100.         elseif($page_nb>$page_max)
  101.             $page_nb $page_max;
  102.         $this->params['page_limit'] = $page_limit;
  103.         $this->params['page_max'] = $page_max;
  104.         $this->params['page_nb'] = $page_nb;
  105.         $this->params['elem_nb'] = $elems;
  106.         // Tri
  107.         $order $request->query->get('order');
  108.         $list $this->repository->findBy([], !empty($order) ?$order :[], $page_limit, ($page_limit * ($page_nb 1)));
  109.         $this->params['list'] = $list;
  110.         
  111.         // Twig template
  112.         $tplfile = static::REF.'/list.html.twig';
  113.         if (!file_exists('../templates/'.$tplfile))
  114.             $tplfile 'entity/list.html.twig';
  115.         
  116.         return $this->render($tplfile$this->params);
  117.     }
  118.     
  119.    /**
  120.     * @Route("/search", name="search")
  121.     */
  122.     public function searchAction(Request $request): Response
  123.     {
  124.         // Mots-clés
  125.         $q $request->query->get('q');
  126.         // Tri
  127.         $order $request->query->get('order');
  128.         $this->params();
  129.         $this->params['page']['head']['title'] = 'Recherche '.$this->repository::LABEL;
  130.         $this->params['page']['subtitle'] = 'Recherche';
  131.         $this->params['page']['breadcrumbs'][] = [
  132.             'route' => static::REF.'_search',
  133.             'label' => 'Recherche',
  134.         ];
  135.         $this->params['page']['breadcrumbs'][] = [
  136.             'route' => static::REF.'_search',
  137.             'label' => $q,
  138.             'params' => ['q'=>$q],
  139.         ];
  140.         $this->params['search_q'] = $q;
  141.         // Pagination
  142.         $page_limit $request->query->get('limit');
  143.         // Test page size
  144.         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)
  145.             $page_limit $this->repository::SEARCH_LIMIT;
  146.         
  147.         $elems $this->repository->countByQ($q);
  148.         //var_dump($elems); die();
  149.         $page_max ceil($elems $page_limit);
  150.         $page_nb $request->query->get('page');
  151.         if (empty($page_nb) || !is_numeric($page_nb) || $page_nb != (int)$page_nb || $page_nb<1)
  152.             $page_nb 1;
  153.         elseif($page_nb>$page_max)
  154.             $page_nb $page_max;
  155.         $this->params['page_limit'] = $page_limit;
  156.         $this->params['page_max'] = $page_max;
  157.         $this->params['page_nb'] = $page_nb;
  158.         $this->params['elem_nb'] = $elems;
  159.         $list $this->repository->findByQ($q, !empty($order) ?$order :[], $page_limit, ($page_limit * ($page_nb 1)));
  160.         $this->params['list'] = $list;
  161.         
  162.         // Twig template
  163.         $tplfile = static::REF.'/list.html.twig';
  164.         if (!file_exists('../templates/'.$tplfile))
  165.             $tplfile 'entity/list.html.twig';
  166.         
  167.         return $this->render($tplfile$this->params);
  168.     }
  169.     
  170.    /**
  171.     * @Route("/create", name="create")
  172.     */
  173.     public function createAction(Request $requestValidatorInterface $validator): Response
  174.     {
  175.         $object $this->repository->createEntity();
  176.         
  177.         $this->params();
  178.         $this->params['page']['head']['title'] = 'Création '.$this->repository::LABEL;
  179.         $this->params['page']['subtitle'] = 'Création';
  180.         $this->params['page']['breadcrumbs'][] = [
  181.             'route' => static::REF.'_create',
  182.             'label' => 'Ajouter',
  183.         ];
  184.         
  185.         if (class_exists('App\\Form\\'.static::NAME.'Type')) {
  186.             $form_classname 'App\\Form\\'.static::NAME.'Type';
  187.             $object->setUtime(new \DateTime());
  188.             $form $this->createForm($form_classname$object);
  189.             
  190.             $form->handleRequest($request);
  191.             
  192.             
  193.             if ($form->isSubmitted()) {
  194.                 $date = new \DateTime();
  195.                 $object->setCtime($date);
  196.                 //var_dump($object); die();
  197.                 if ($form->isValid()) {
  198.                     //var_dump($object); die();
  199.                     $errors $validator->validate($object);
  200.                     //var_dump($errors);
  201.                     if (count($errors) > 0) {
  202.                         return new Response((string) $errors400);
  203.                     }
  204.                 
  205.                     // @todo user entityrepository
  206.                     $this->em->persist($object);
  207.                     $this->em->flush();
  208.                 
  209.                     return $this->redirectToRoute(static::REF.'_read', ['id'=>$object->getId()]);
  210.                 }
  211.                 else {
  212.                     $errors $validator->validate($object);
  213.                     var_dump($errors);
  214.                     if (count($errors) > 0) {
  215.                         return new Response((string) $errors400);
  216.                     }
  217.                 }
  218.             }
  219.             $this->params['form'] = $form->createView();
  220.         }
  221.         
  222.         return $this->render('entity/create.html.twig'$this->params);
  223.     }
  224.     
  225.     /**
  226.      * @Route("/ajax/create", name="ajax_create")
  227.      */
  228.      public function createAjaxAction(Request $requestValidatorInterface $validator): Response
  229.      {
  230.         $this->repository->createEntity();
  231.          
  232.          $this->params();
  233.          $this->params['page']['subtitle'] = 'Création';
  234.          $this->params['page']['breadcrumbs'][] = [
  235.              'route' => static::REF.'_create',
  236.              'label' => 'Ajouter',
  237.          ];
  238.          
  239.          if (class_exists('App\\Form\\'.static::NAME.'Type')) {
  240.              $form_classname 'App\\Form\\'.static::NAME.'Type';
  241.              $object->setUtime(new \DateTime());
  242.              $form $this->createForm($form_classname$object);
  243.              
  244.              $form->handleRequest($request);
  245.              
  246.              
  247.              if ($form->isSubmitted()) {
  248.                  $date = new \DateTime();
  249.                  $object->setCtime($date);
  250.                  //var_dump($object);
  251.                  if ($form->isValid()) {
  252.                      //var_dump($object); die();
  253.                      $errors $validator->validate($object);
  254.                      //var_dump($errors);
  255.                      if (count($errors) > 0) {
  256.                          return new JsonResponse((string) $errors400);
  257.                      }
  258.                  
  259.                      // @todo user entityrepository
  260.                      $this->em->persist($object);
  261.                      $this->em->flush();
  262.                  
  263.                       return new JsonResponse($object);
  264.                  }
  265.                  else {
  266.                      $errors $validator->validate($object);
  267.                      var_dump($errors);
  268.                      if (count($errors) > 0) {
  269.                          return new JsonResponse((string) $errors400);
  270.                      }
  271.  
  272.                  }
  273.              }
  274.              $this->params['form'] = $form->createView();
  275.          }
  276.          
  277.          return new JsonResponse($this->params);
  278.      }
  279.     /**
  280.      * @Route("/{id}", name="read")
  281.      */
  282.     public function readAction(Request $requestint $id): Response
  283.     {
  284.         if (!($object $this->getEntity($id)))
  285.             return $this->redirectToRoute(static::REF.'_list');
  286.         
  287.         $this->params();
  288.         $this->params['object'] = $object;
  289.         $this->params['object_class'] = get_class($object);
  290.         $this->params['page']['head']['title'] = $object.' - '.$this->repository::LABEL;
  291.         $this->params['page']['subtitle'] = 'Affichage';
  292.         $this->params['page']['breadcrumbs'][] = [
  293.             'route' => static::REF.'_read',
  294.             'label' => $object,
  295.             'params' => ['id'=>$id],
  296.         ];
  297.         
  298.         if (class_exists('App\\Form\\'.static::NAME.'Type')) {
  299.             $form_classname 'App\\Form\\'.static::NAME.'Type';
  300.             $form $this->createForm($form_classname$object, ['disabled' => true]);
  301.             $this->params['form'] = $form->createView();
  302.         }
  303.         
  304.         // Twig template
  305.         $tplfile = static::REF.'/view.html.twig';
  306.         if (!file_exists('../templates/'.$tplfile))
  307.             $tplfile 'entity/view.html.twig';
  308.         
  309.         return $this->render($tplfile$this->params);
  310.     }
  311.     /**
  312.      * @Route("/ajax/{id}", name="ajax_read")
  313.      */
  314.     public function readAjaxAction(Request $requestint $id): Response
  315.     {
  316.         if (!($object $this->getEntity($id)))
  317.             die('404');
  318.         
  319.         return new JsonResponse($object->__toJson());
  320.     }
  321.     /**
  322.      * @Route("/ajax/{id}/update", name="ajax_update")
  323.      */
  324.     public function updateAjaxAction(Request $requestint $idEntityManagerInterface $em): Response
  325.     {
  326.         if (!($object $this->getEntity($id)))
  327.             die('404');
  328.         
  329.         $data $request->request->get('data');
  330.         //var_dump($data); die();
  331.         if (is_array($data)) foreach($data as $name=>$value) {
  332.             $object->{"set$name"}($value);
  333.         }
  334.         $p $em->persist($object);
  335.         $f $em->flush();
  336.         $r = ['data'=>$data];
  337.         return new JsonResponse($r);
  338.     }
  339.     /**
  340.      * @Route("/{id}/update", name="update")
  341.      */
  342.     public function updateAction(Request $requestValidatorInterface $validatorint $id): Response
  343.     {
  344.         if (!($object $this->getEntity($id)))
  345.             return $this->redirectToRoute(static::REF.'_list');
  346.         
  347.         $this->params();
  348.         $this->params['object'] = $object;
  349.         $this->params['page']['head']['title'] = 'Modifier '.$object.' - '.$this->repository::LABEL;
  350.         $this->params['page']['subtitle'] = 'Modifier';
  351.         $this->params['page']['breadcrumbs'][] = [
  352.             'route' => static::REF.'_read',
  353.             'label' => $object,
  354.             'params' => ['id'=>$id],
  355.         ];
  356.         $this->params['page']['breadcrumbs'][] = [
  357.             'route' => static::REF.'_update',
  358.             'label' => 'Modifier',
  359.             'params' => ['id'=>$id],
  360.         ];
  361.         if (class_exists('App\\Form\\'.static::NAME.'Type')) {
  362.             $form_classname 'App\\Form\\'.static::NAME.'Type';
  363.             $form $this->createForm($form_classname$object);
  364.             
  365.             $form->handleRequest($request);
  366.             if ($form->isSubmitted() && $form->isValid()) {
  367.                 $errors $validator->validate($object);
  368.                 //var_dump($errors);
  369.                 if (count($errors) > 0) {
  370.                     return new Response((string) $errors400);
  371.                 }
  372.                 
  373.                 $this->em->persist($object);
  374.                 $this->em->flush();
  375.                 return $this->redirectToRoute(static::REF.'_read', ['id'=>$object->getId()]);
  376.             }
  377.             
  378.             $this->params['form'] = $form->createView();
  379.         }
  380.         
  381.         return $this->render('entity/update.html.twig'$this->params);
  382.     }
  383.     /**
  384.      * @Route("/{id}/delete", name="delete")
  385.      */
  386.     public function removeAction(Request $requestint $id): Response
  387.     {
  388.         if (($object $this->getEntity($id))) {
  389.             $this->em->remove($object);
  390.             $this->em->flush();
  391.         }
  392.         
  393.         return $this->redirectToRoute(static::REF.'_list');
  394.     }
  395.     /**
  396.      * @Route("/ajax/{id}/delete", name="ajax_delete")
  397.      */
  398.     public function removeAjaxAction(Request $requestint $id): Response
  399.     {
  400.         if (!($object $this->getEntity($id))) {
  401.         }
  402.         
  403.         $this->params();
  404.         $this->params['object'] = $object;
  405.         $this->params['page']['subtitle'] = 'Suppression';
  406.         return $this->render('entity/delete.html.twig'$this->params);
  407.     }
  408. }