mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-04 13:54:59 +00:00
Initialize a CRUD for entities
This commit is contained in:
128
CRUD/Controller/CRUDController.php
Normal file
128
CRUD/Controller/CRUDController.php
Normal 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'))
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user