src/Diplix/KMGBundle/Controller/Admin/BasicCRUDController.php line 132

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Controller\Admin;
  3. use Diplix\KMGBundle\Controller\BaseController;
  4. use Diplix\KMGBundle\Form\DefaultDeleteForm;
  5. use Diplix\KMGBundle\Repository\BasicRepository;
  6. use Symfony\Component\Form\FormError;
  7. use Symfony\Component\HttpFoundation\Request;
  8. /**
  9.  * A basic controller providing functions to list/edit/delete a configured entity
  10.  * Usage: inherit and provide the getDefaults()-method to populate the class members
  11.  *
  12.  */
  13. abstract class BasicCRUDController extends BaseController
  14. {
  15.     protected static $LIST "list.html.twig";
  16.     protected static $EDIT "edit.html.twig";
  17.     protected static $DEL  "delete.html.twig";
  18.     protected $routes = array(  'list'=>"",
  19.                                 'edit'=>"",
  20.                                 'delete'=>""
  21.                                 /*optional: 'export'*/);
  22.     protected $entityClassName;
  23.     protected $editFormClassName;
  24.     protected $entityName;
  25.     /**
  26.      * The default template scope for calling the default templates
  27.      * Override with your own template-path if required
  28.      * @return string
  29.      */
  30.     protected function getTemplateScope()
  31.     {
  32.         return "@DiplixKMG/Admin/CRUD/";
  33.     }
  34.     /**
  35.      * Get the repository for the edited entity
  36.      * Override if required
  37.      * @return BasicRepository
  38.      */
  39.     protected function getRepository()
  40.     {
  41.         return $this->getDoctrine()->getManager()->getRepository($this->entityClassName);
  42.     }
  43.     /**
  44.      * Override if required
  45.      * @return string
  46.      */
  47.     protected function getListTemplate()
  48.     {
  49.         return $this->getTemplateScope().self::$LIST;
  50.     }
  51.     /**
  52.      * Override if required
  53.      * @return string
  54.      */
  55.     protected function getEditTemplate()
  56.     {
  57.         return $this->getTemplateScope().self::$EDIT;
  58.     }
  59.     /**
  60.      * Override if required
  61.      * @return string
  62.      */
  63.     protected function getDeleteTemplate()
  64.     {
  65.         return $this->getTemplateScope().self::$DEL;
  66.     }
  67.     /**
  68.      * Override if required
  69.      * @return mixed
  70.      */
  71.     protected function getNewEntity()
  72.     {
  73.         return new $this->entityClassName;
  74.     }
  75.     /**
  76.      * Override to perform additional form input checks
  77.      * If there is an error , attach it directly to the form and return false
  78.      * else return true
  79.      * @param $form
  80.      * @param $em
  81.      * @return
  82.      */
  83.     protected function validateInput($form$em)
  84.     {
  85.         return true;
  86.     }
  87.     /**
  88.      *
  89.      * @return array(entityClassName=>x, editFormClassName=>y, routes=>array() )
  90.      */
  91.     abstract protected function getDefaults();
  92.     protected function init()
  93.     {
  94.         // get configuration
  95.         $def $this->getDefaults();
  96.         $this->entityClassName $def['entityClassName'];
  97.         $this->editFormClassName $def['editFormClassName'];
  98.         $this->entityName $def['entityName'];
  99.         $check array_diffarray_keys($this->routes)  , array_keys($def['routes']));
  100.         if (count($check)>0) throw new \Exception("Route configuration is incomplete !");
  101.         $this->routes $def['routes'];
  102.     }
  103.     public function indexAction(Request $request)
  104.     {
  105.         $this->init();
  106.         $repo $this->getRepository();
  107.         $list $repo->findAll();
  108.         $lastRecord null;
  109.         $lastRecordId $request->query->get('lastRecordId',0);
  110.         if ($lastRecordId 0$lastRecord $repo->findOneBy(array("id"=>$lastRecordId));
  111.         return $this->render($this->getListTemplate(), array(
  112.             "dataList"=>$list,
  113.             "lastRecord"=>$lastRecord,
  114.             "routes"=>$this->routes,
  115.             "entityName"=>$this->entityName));
  116.     }
  117.     public function editAction(Request $request)
  118.     {
  119.         $this->init();
  120.         $repo $this->getRepository();
  121.         $id $request->query->get('id');
  122.         if ($id>0)
  123.         {
  124.             $row $repo->findOneBy(array("id"=>$id));
  125.             if ($row===null)
  126.             {
  127.                 $this->addFlash('warning','message.invalid-id');
  128.                 return  $this->redirect($this->generateUrl($this->routes['list']));
  129.             }
  130.         }
  131.         else
  132.         {
  133.             if ($request->query->get('copyFrom',0)>0)
  134.             {
  135.                 $original $repo->findOneBy(['id'=>$request->query->get('copyFrom',0)]);
  136.                 $row = clone $original;
  137.             }
  138.             else
  139.             {
  140.                 $row $this->getNewEntity();
  141.             }
  142.         }
  143.         // render form
  144.         $form $this->createForm($this->editFormClassName,$row);
  145.         $failed false;
  146.         try
  147.         {
  148.             $form->handleRequest($request);
  149.         }
  150.         catch (\Throwable $ex)
  151.         {
  152.             $this->addFlash('danger',$ex->getMessage());
  153.         }
  154.         if ($failed || $form->isSubmitted())
  155.         {
  156.             $valid $form->isValid();
  157.             $data $form->getData();
  158.             if ($valid)
  159.             {
  160.                 $valid $this->validateInput($form,$this->getDoctrine()->getManager());
  161.             }
  162.             if (!$valid)
  163.             {
  164.                 $form->addError(new FormError("message.please-check-input"));
  165.                 return $this->render($this->getEditTemplate(),array (
  166.                     "form" => $form->createView(),
  167.                     "row"=> $row,
  168.                     "routes"=>$this->routes,
  169.                     "entityName"=>$this->entityName
  170.                 ));
  171.             }
  172.             $repo->persistFlush($data);
  173.             $this->addFlash('success','message.saved');
  174.             return  $this->redirect($this->generateUrl$this->routes['list'], array(
  175.                                 "lastRecordId"=>$data->getId(),
  176.                     )));
  177.         }
  178.         else
  179.         {
  180.             return $this->render($this->getEditTemplate(),array (
  181.                 "form" => $form->createView(),
  182.                 "row"=> $row,
  183.                 "routes"=>$this->routes,
  184.                 "entityName"=>$this->entityName
  185.             ));
  186.         }
  187.     }
  188.     public function deleteAction(Request $request)
  189.     {
  190.         $this->init();
  191.         $repo $this->getRepository();
  192.         $id $request->get('id'); // _GET or _POST
  193.         $row $repo->findOneBy(array("id"=>$id));
  194.         if ($row===null)
  195.         {
  196.             $this->addFlash('warning','message.invalid-id');
  197.             return  $this->redirect($this->generateUrl($this->routes['list']));
  198.         }
  199.         $form $this->createForm(DefaultDeleteForm::class,array("id"=>$id));
  200.         $form->handleRequest($request);
  201.         if ($form->isSubmitted())
  202.         {
  203.             if ($form->get('commit')->getData() == 1)
  204.             {
  205.                 $repo->delete($row);
  206.                 $this->addFlash('success','message.record-deleted');
  207.             }
  208.             return  $this->redirect($this->generateUrl($this->routes['list']));
  209.         }
  210.         else
  211.         {
  212.             return $this->render($this->getDeleteTemplate(),array (
  213.                 "row"=> $row,
  214.                 "form"=>$form->createView(),
  215.                 "routes"=>$this->routes,
  216.                 "entityName"=>$this->entityName
  217.                 ));
  218.         }
  219.     }
  220. }