work on basic CRUDController and improve configuration

This commit is contained in:
Julien Fastré 2019-11-22 13:45:30 +01:00
parent 4575812a3b
commit e6bf77530b
8 changed files with 355 additions and 47 deletions

View File

@ -20,48 +20,46 @@
namespace Chill\MainBundle\CRUD\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\QueryBuilder;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
*
*
*/
abstract class CRUDController extends Controller
class CRUDController extends AbstractController
{
/**
*
* @var PaginatorFactory
*/
protected $paginatorFactory;
abstract protected function getEntity(): string;
abstract protected function orderingOptions(): array;
/**
* The crud configuration
*
* This configuration si defined by `chill_main['crud']`.
*
* @var array
*/
protected $crudConfig;
public function setCrudConfig(array $config)
{
$this->crudConfig = $config;
}
protected function getDefaultOrdering(): array
{
return $this->orderingOptions();
}
protected function getTemplate($action): string
{
switch($action) {
case 'index':
return '@ChillMain\CRUD\index.html.twig';
default:
throw new LogicException("action not supported: $action");
}
}
protected function getTemplateParameters($action): array
{
return [];
}
protected function processTemplateParameters($action): array
{
$configured = $this->getTemplateParameters($action);
@ -125,4 +123,274 @@ abstract class CRUDController extends Controller
], $this->processTemplateParameters('index'))
);
}
public function edit(Request $request, $id): Response
{
return $this->formEditAction('edit', $request, $id);
}
protected function formEditAction($action, Request $request, $id, $formClass = null): Response
{
$entity = $this->getEntity($id, $request);
if (NULL === $entity) {
throw $this->createNotFoundException(sprintf("The %s with id %s "
. "is not found"), $this->getCrudName(), $id);
}
$this->checkACL($action, $entity);
$form = $this->createFormFor($action, $entity, $formClass);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
$this->onPostFlush($action, $entity, $form, $request);
$this->addFlash('succes', $this->generateFormSuccessMessage($action, $entity));
$result = $this->onBeforeRedirect($action, $entity, $form, $request);
if ($result instanceof Response) {
return $result;
}
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index', [
'id' => $entity->getId()
]);
} elseif ($form->isSubmitted()) {
$this->addFlash('error', $this->generateFormErrorMessage($form));
}
$defaultTemplateParameters = [
'form' => $form->createView(),
'entity' => $entity,
'crud_name' => $this->getCrudName()
];
return $this->render(
$this->getTemplateFor($action, $entity, $request),
$this->generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters)
);
}
protected function formCreateAction($action, Request $request, $formClass = null): Response
{
$entity = $this->createEntity($request);
$this->checkACL($action, $entity);
$form = $this->createFormFor($action, $entity, $formClass);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$this->onPrePersist($action, $entity, $form, $request);
$em->persist($entity);
$this->onPostPersist($action, $entity, $form, $request);
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
$this->onPostFlush($action, $entity, $form, $request);
$this->getPaginatorFactory();
$this->addFlash('succes', $this->generateFormSuccessMessage($action, $entity));
$result = $this->onBeforRedirect($action, $entity, $form, $request);
if ($result instanceof Response) {
return $result;
}
return $this->redirectToRoute('chill_crud_'.$this->get, ['id' => $entity->getId()]);
} elseif ($form->isSubmitted()) {
$this->addFlash('error', $this->generateFormErrorMessage($form));
}
$defaultTemplateParameters = [
'form' => $form->createView(),
'entity' => $entity
];
return $this->render(
$this->getTemplateFor($action),
$this->generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters)
);
}
/**
* get the instance of the entity with the given id
*
* @param string $id
* @return object
*/
protected function getEntity($id, Request $request): ?object
{
return $this->getDoctrine()
->getRepository($this->getEntityClass())
->find($id);
}
protected function getEntityClass(): string
{
return $this->crudConfig['class'];
}
protected function getCrudName(): string
{
return $this->crudConfig['name'];
}
protected function checkACL($entity, $action)
{
$this->denyAccessUnlessGranted($this->getRoleFor($action), $entity);
}
protected function getRoleFor($action)
{
return $this->buildDefaultRole($action);
}
protected function buildDefaultRole($action)
{
if (empty($this->crudConfig['base_role'])) {
throw new \LogicException(sprintf("the base role is not defined. You must define "
. "on or override %s or %s methods", __METHOD__, "getRoleFor"));
}
return \strtoupper(
$this->crudConfig['base_role'].
'_'.
$action);
}
protected function getFormClassFor($action)
{
return $this->crudConfig[$action]['form_class']
?? $this->crudConfig['form_class'];
}
protected function createFormFor($action, $entity, $formClass = null)
{
$formClass = $formClass ?? $this->getFormClassFor($action);
$form = $this->createForm($formClass, $entity);
$form->add('submit', SubmitType::class, [
'label' => $action
]);
return $form;
}
protected function generateFormErrorMessage($action, FormInterface $form): string
{
$msg = 'This form contains errors';
return $this->getTranslator()->trans($msg);
}
protected function generateFormSuccessMessage($action, $entity): string
{
switch ($action) {
case 'edit':
$msg = "The data have been successfully updated";
break;
case 'new':
$msg = "The date have been successfully created";
break;
default:
$msg = "Your request has been successfully executed";
}
return $this->getTranslator()->trans($msg);
}
protected function generateTemplateParameter(
$action,
$entity,
Request $request,
array $defaultTemplateParameters = []
) {
return $defaultTemplateParameters;
}
protected function createEntity(Request $request): object
{
$type = $this->getEntityClass();
return new $type;
}
protected function getTemplateFor($action, $entity, Request $request)
{
if (!empty($this->crudConfig[$action]['template'])) {
return $this->crudConfig[$action]['template'];
}
switch ($action) {
case 'new':
return '@ChillMain/CRUD/new.html.twig';
case 'edit':
return '@ChillMain/CRUD/edit.html.twig';
case 'index':
return '@ChillMain/CRUD/index.html.twig';
default:
throw new \LogicException("the view for action $action is not "
. "defined. You should override ".__METHOD__." to add this "
. "action");
}
}
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onFormValid(object $entity, FormInterface $form, Request $request)
{
}
protected function onBeforeRedirect(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function getPaginatorFactory(): PaginatorFactory
{
return $this->get(PaginatorFactory::class);
}
protected function getTranslator(): TranslatorInterface
{
return $this->container->get('translator');
}
public static function getSubscribedServices()
{
return \array_merge(
parent::getSubscribedServices(),
[
PaginatorFactory::class => PaginatorFactory::class,
'translator' => TranslatorInterface::class,
]
);
}
}

View File

@ -25,7 +25,7 @@ use Symfony\Component\Routing\RouteCollection;
/**
*
* Load the route for CRUD
*
*/
class CRUDRoutesLoader

View File

@ -17,27 +17,4 @@ class AdminCountryCRUDController extends CRUDController
{
$this->paginatorFactory = $paginator;
}
protected function getEntity(): string
{
return Country::class;
}
protected function orderingOptions(): array
{
return [
'countryCode' => 'ASC'
];
}
protected function getTemplateParameters($action): array
{
switch ($action) {
case 'index':
return [
'columns' => [ 'countryCode', 'name' ],
'title' => 'Liste des pays'
];
}
}
}

View File

@ -221,5 +221,28 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
;
$container->setDefinition('chill_main_crud_route_loader', $definition);
$alreadyExistingNames = [];
foreach ($config as $crudEntry) {
$controller = $crudEntry['controller'];
$name = $crudEntry['name'];
// check for existing crud names
if (\in_array($name, $alreadyExistingNames)) {
throw new LogicException(sprintf("the name %s is defined twice in CRUD", $name));
}
if (!$container->has($controller)) {
$controllerDefinition = new Definition($controller);
$controllerDefinition->addTag('controller.service_arguments');
$controllerDefinition->setAutoconfigured(true);
$container->setDefinition($controller, $controllerDefinition);
}
$container->setParameter('chill_main_crud_config_'.$name, $crudEntry);
$container->getDefinition($controller)
->addMethodCall('setCrudConfig', ['%chill_main_crud_config_'.$name.'%']);
}
}
}

View File

@ -124,10 +124,11 @@ class Configuration implements ConfigurationInterface
->scalarNode('controller')->cannotBeEmpty()->isRequired()->end()
->scalarNode('name')->cannotBeEmpty()->isRequired()->end()
->scalarNode('base_path')->cannotBeEmpty()->isRequired()->end()
->scalarNode('base_role')->defaultNull()->end()
->scalarNode('form_class')->defaultNull()->end()
->arrayNode('actions')
->scalarPrototype()->end()
//->defaultValue(['index', 'new', 'edit', 'show', 'delete'])
//->ifEmpty()->thenInvalid()
->defaultValue(['index', 'new', 'edit', 'show', 'delete'])
->end()
->end()
->end()

View File

@ -0,0 +1,28 @@
<div class="{% block crud_content_main_div_class %}grid-10 centered{% endblock %}">
{% block crud_content_header %}
<h1>{{ 'crud.title.edit_of_%crud_name%'|trans({'%crud_name%' : crud_name }) }}</h1>
{% endblock crud_content_header %}
{% block crud_content_form %}
{{ form_start(form) }}
{% for f in form if f.vars.name != 'submit' %}
{{ form_row(f) }}
{% endfor %}
{% block crud_content_form_actions %}
<div class="{% block crud_content_form_actions_class %}grid-12 centered sticky-form-buttons{% endblock %}">
<ul class="record_actions">
{% block content_form_actions_back %}
<li class="cancel">
<a class="sc-button bt-cancel" href="{{ chill_return_path_or('chill_crud_'~crud_name~'_index') }}">
{{ 'Cancel'|trans }}
</a>
{% endblock %}
<li>{{ form_widget(form.submit, { 'attr': { 'class': 'sc-button bt-edit'} } ) }}</li>
</ul>
</div>
{% endblock %}
{{ form_end(form) }}
{% endblock %}
</div>

View File

@ -0,0 +1 @@
{{ 'crud.title.edit_of_%crud_name%'|trans({'%crud_name%' : crud_name }) }}

View File

@ -0,0 +1,10 @@
{% extends '@ChillMain/layout.html.twig' %}
{% block title %}
{% include('@ChillMain/CRUD/_edit_title.html.twig') %}
{% endblock %}
{% block content %}
{% embed '@ChillMain/CRUD/_edit_content.html.twig' %}
{% endembed %}
{% endblock %}