<?php
namespace Diplix\KMGBundle\Controller\Admin;
use Diplix\KMGBundle\Controller\BaseController;
use Diplix\KMGBundle\Form\DefaultDeleteForm;
use Diplix\KMGBundle\Repository\BasicRepository;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
/**
* A basic controller providing functions to list/edit/delete a configured entity
* Usage: inherit and provide the getDefaults()-method to populate the class members
*
*/
abstract class BasicCRUDController extends BaseController
{
protected static $LIST = "list.html.twig";
protected static $EDIT = "edit.html.twig";
protected static $DEL = "delete.html.twig";
protected $routes = array( 'list'=>"",
'edit'=>"",
'delete'=>""
/*optional: 'export'*/);
protected $entityClassName;
protected $editFormClassName;
protected $entityName;
/**
* The default template scope for calling the default templates
* Override with your own template-path if required
* @return string
*/
protected function getTemplateScope()
{
return "@DiplixKMG/Admin/CRUD/";
}
/**
* Get the repository for the edited entity
* Override if required
* @return BasicRepository
*/
protected function getRepository()
{
return $this->getDoctrine()->getManager()->getRepository($this->entityClassName);
}
/**
* Override if required
* @return string
*/
protected function getListTemplate()
{
return $this->getTemplateScope().self::$LIST;
}
/**
* Override if required
* @return string
*/
protected function getEditTemplate()
{
return $this->getTemplateScope().self::$EDIT;
}
/**
* Override if required
* @return string
*/
protected function getDeleteTemplate()
{
return $this->getTemplateScope().self::$DEL;
}
/**
* Override if required
* @return mixed
*/
protected function getNewEntity()
{
return new $this->entityClassName;
}
/**
* Override to perform additional form input checks
* If there is an error , attach it directly to the form and return false
* else return true
* @param $form
* @param $em
* @return
*/
protected function validateInput($form, $em)
{
return true;
}
/**
*
* @return array(entityClassName=>x, editFormClassName=>y, routes=>array() )
*/
abstract protected function getDefaults();
protected function init()
{
// get configuration
$def = $this->getDefaults();
$this->entityClassName = $def['entityClassName'];
$this->editFormClassName = $def['editFormClassName'];
$this->entityName = $def['entityName'];
$check = array_diff( array_keys($this->routes) , array_keys($def['routes']));
if (count($check)>0) throw new \Exception("Route configuration is incomplete !");
$this->routes = $def['routes'];
}
public function indexAction(Request $request)
{
$this->init();
$repo = $this->getRepository();
$list = $repo->findAll();
$lastRecord = null;
$lastRecordId = $request->query->get('lastRecordId',0);
if ($lastRecordId > 0) $lastRecord = $repo->findOneBy(array("id"=>$lastRecordId));
return $this->render($this->getListTemplate(), array(
"dataList"=>$list,
"lastRecord"=>$lastRecord,
"routes"=>$this->routes,
"entityName"=>$this->entityName));
}
public function editAction(Request $request)
{
$this->init();
$repo = $this->getRepository();
$id = $request->query->get('id');
if ($id>0)
{
$row = $repo->findOneBy(array("id"=>$id));
if ($row===null)
{
$this->addFlash('warning','message.invalid-id');
return $this->redirect($this->generateUrl($this->routes['list']));
}
}
else
{
if ($request->query->get('copyFrom',0)>0)
{
$original = $repo->findOneBy(['id'=>$request->query->get('copyFrom',0)]);
$row = clone $original;
}
else
{
$row = $this->getNewEntity();
}
}
// render form
$form = $this->createForm($this->editFormClassName,$row);
$failed = false;
try
{
$form->handleRequest($request);
}
catch (\Throwable $ex)
{
$this->addFlash('danger',$ex->getMessage());
}
if ($failed || $form->isSubmitted())
{
$valid = $form->isValid();
$data = $form->getData();
if ($valid)
{
$valid = $this->validateInput($form,$this->getDoctrine()->getManager());
}
if (!$valid)
{
$form->addError(new FormError("message.please-check-input"));
return $this->render($this->getEditTemplate(),array (
"form" => $form->createView(),
"row"=> $row,
"routes"=>$this->routes,
"entityName"=>$this->entityName
));
}
$repo->persistFlush($data);
$this->addFlash('success','message.saved');
return $this->redirect($this->generateUrl( $this->routes['list'], array(
"lastRecordId"=>$data->getId(),
)));
}
else
{
return $this->render($this->getEditTemplate(),array (
"form" => $form->createView(),
"row"=> $row,
"routes"=>$this->routes,
"entityName"=>$this->entityName
));
}
}
public function deleteAction(Request $request)
{
$this->init();
$repo = $this->getRepository();
$id = $request->get('id'); // _GET or _POST
$row = $repo->findOneBy(array("id"=>$id));
if ($row===null)
{
$this->addFlash('warning','message.invalid-id');
return $this->redirect($this->generateUrl($this->routes['list']));
}
$form = $this->createForm(DefaultDeleteForm::class,array("id"=>$id));
$form->handleRequest($request);
if ($form->isSubmitted())
{
if ($form->get('commit')->getData() == 1)
{
$repo->delete($row);
$this->addFlash('success','message.record-deleted');
}
return $this->redirect($this->generateUrl($this->routes['list']));
}
else
{
return $this->render($this->getDeleteTemplate(),array (
"row"=> $row,
"form"=>$form->createView(),
"routes"=>$this->routes,
"entityName"=>$this->entityName
));
}
}
}