Initialize a CRUD for entities

This commit is contained in:
2019-11-19 09:32:33 +01:00
parent dc1bac05ee
commit 4575812a3b
11 changed files with 533 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
<?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\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\QueryBuilder;
use Chill\MainBundle\Pagination\PaginatorFactory;
/**
*
*
*/
abstract class CRUDController extends Controller
{
/**
*
* @var PaginatorFactory
*/
protected $paginatorFactory;
abstract protected function getEntity(): string;
abstract protected function orderingOptions(): array;
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);
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'))
);
}
}

110
CRUD/Resolver/Resolver.php Normal file
View File

@@ -0,0 +1,110 @@
<?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\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
/**
*
*
*/
class Resolver
{
/**
*
* @var EntityManagerInterface
*/
protected $em;
/**
*
* @var \Symfony\Component\PropertyAccess\PropertyAccessor
*/
protected $propertyAccess;
function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->buildPropertyAccess();
}
private function buildPropertyAccess()
{
$this->propertyAccess = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
}
/**
* Return the data at given path.
*
* Path are given to
*
* @param object $entity
* @param string $path
*/
public function getData($entity, $path)
{
return $this->propertyAccess->getValue($entity, $path);
}
public function getTwigTemplate($entity, $path): string
{
list($focusEntity, $subPath) = $this->getFocusedEntity($entity, $path);
$classMetadata = $this->em->getClassMetadata(\get_class($focusEntity));
$type = $classMetadata->getTypeOfField($subPath);
dump($type);
switch ($type) {
default:
return '@ChillMain/CRUD/_inc/default.html.twig';
}
}
/**
* Get the object on which the path apply
*
* This methods recursively parse the path and entity and return the entity
* which will deliver the info, and the last path.
*
* @param object $entity
* @param string $path
* @return array [$focusedEntity, $lastPath]
*/
private function getFocusedEntity($entity, $path)
{
if (\strpos($path, '.') === FALSE) {
return [$entity, $path];
}
$exploded = \explode('.', $path);
$subEntity = $this->propertyAccess
->getValue($entity, $exploded[0]);
return $this->getFocusedEntity($subEntity,
\implode('.', \array_slice($exploded, 1)));
}
}

View File

@@ -0,0 +1,87 @@
<?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\Routing;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
*
*
*/
class CRUDRoutesLoader
{
protected $config = [];
public function __construct($config)
{
$this->config = $config;
}
protected function addDummyConfig()
{
$this->config[] = [
'name' => 'country',
'actions' => ['index'],//, 'new', 'edit', 'delete'],
'base_path' => '/admin/country',
'controller' => \Chill\MainBundle\Controller\AdminCountryCRUDController::class
];
}
public function load()
{
$collection = new RouteCollection();
foreach ($this->config as $config) {
$collection->addCollection($this->loadConfig($config));
}
return $collection;
}
protected function loadConfig($config): RouteCollection
{
$collection = new RouteCollection();
foreach ($config['actions'] as $action) {
$defaults = [
'_controller' => $config['controller'].'::'.$action
];
if ($action === 'index') {
$path = "{_locale}".$config['base_path'];
$route = new Route($path, $defaults);
} else {
$path = "{_locale}".$config['base_path'].'/{id}/'.$action;
$requirements = [
'{id}' => '\d+'
];
$route = new Route($path, $defaults, $requirements);
}
$collection->add('chill_crud_'.$config['name'].'_'.$action, $route);
}
return $collection;
}
}

View File

@@ -0,0 +1,61 @@
<?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\Templating;
use Chill\MainBundle\CRUD\Resolver\Resolver;
use Twig\TwigFilter;
use Twig\Extension\AbstractExtension;
use Twig\Environment;
/**
* Twig filters to display data in crud template
*
*/
class TwigCRUDResolver extends AbstractExtension
{
/**
*
* @var Resolver
*/
protected $resolver;
function __construct(Resolver $resolver)
{
$this->resolver = $resolver;
}
public function getFilters()
{
return [
new TwigFilter('chill_crud_display', [$this, 'display'],
['needs_environment' => true, 'is_safe' => ['html']])
];
}
public function display(Environment $env, $entity, $path): string
{
$data = $this->resolver->getData($entity, $path);
$template = $this->resolver->getTwigTemplate($entity, $path);
return $env->render($template, ['data' => $data, 'entity' => $entity, ]);
}
}