mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
397 lines
12 KiB
PHP
397 lines
12 KiB
PHP
<?php
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* Copyright (C) 2019, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\CRUD\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;
|
|
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
class CRUDController extends AbstractController
|
|
{
|
|
/**
|
|
*
|
|
* @var PaginatorFactory
|
|
*/
|
|
protected $paginatorFactory;
|
|
|
|
/**
|
|
* 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 processTemplateParameters($action): array
|
|
{
|
|
$configured = $this->getTemplateParameters($action);
|
|
|
|
switch($action) {
|
|
case 'index':
|
|
$default = [
|
|
'columns' => $this->getDoctrine()->getManager()
|
|
->getClassMetadata($this->getEntity())
|
|
->getIdentifierFieldNames(),
|
|
'actions' => ['edit', 'delete']
|
|
];
|
|
break;
|
|
default:
|
|
throw new \LogicException("this action is not supported: $action");
|
|
}
|
|
|
|
$result = \array_merge($default, $configured);
|
|
|
|
// add constants
|
|
$result['class'] = $this->getEntity();
|
|
|
|
return $result;
|
|
}
|
|
|
|
protected function orderQuery(QueryBuilder $query, Request $request): QueryBuilder
|
|
{
|
|
$defaultOrdering = $this->getDefaultOrdering();
|
|
|
|
foreach ($defaultOrdering as $sort => $order) {
|
|
$query->addOrderBy('e.'.$sort, $order);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$totalItems = $this->getDoctrine()->getManager()
|
|
->createQuery("SELECT COUNT(e) FROM ".$this->getEntity()." e")
|
|
->getSingleScalarResult()
|
|
;
|
|
|
|
$query = $this->getDoctrine()->getManager()
|
|
->createQueryBuilder()
|
|
->select('e')
|
|
->from($this->getEntity(), 'e');
|
|
|
|
$this->orderQuery($query, $request);
|
|
|
|
$paginator = $this->paginatorFactory->create($totalItems);
|
|
|
|
$query->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())
|
|
->setMaxResults($paginator->getItemsPerPage())
|
|
;
|
|
|
|
$entities = $query->getQuery()->getResult();
|
|
|
|
return $this->render($this->getTemplate('index'), \array_merge([
|
|
'entities' => $entities,
|
|
], $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,
|
|
]
|
|
);
|
|
}
|
|
}
|