mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
create index action for CRUD
This commit is contained in:
parent
7a952d6f88
commit
ba742dd7be
@ -32,6 +32,7 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Chill\MainBundle\CRUD\Resolver\Resolver;
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -39,12 +40,6 @@ use Chill\MainBundle\CRUD\Resolver\Resolver;
|
||||
*/
|
||||
class CRUDController extends AbstractController
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var PaginatorFactory
|
||||
*/
|
||||
protected $paginatorFactory;
|
||||
|
||||
/**
|
||||
* The crud configuration
|
||||
*
|
||||
@ -59,13 +54,10 @@ class CRUDController extends AbstractController
|
||||
$this->crudConfig = $config;
|
||||
}
|
||||
|
||||
protected function getDefaultOrdering(): array
|
||||
{
|
||||
return $this->orderingOptions();
|
||||
}
|
||||
|
||||
protected function processTemplateParameters($action): array
|
||||
{
|
||||
throw new Exception('is this method used ?');
|
||||
|
||||
$configured = $this->getTemplateParameters($action);
|
||||
|
||||
switch($action) {
|
||||
@ -89,43 +81,57 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function orderQuery(QueryBuilder $query, Request $request): QueryBuilder
|
||||
protected function orderQuery($action, QueryBuilder $query, Request $request, PaginatorInterface $paginator): 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())
|
||||
;
|
||||
return $this->indexEntityAction('index', $request);
|
||||
}
|
||||
|
||||
protected function indexEntityAction($action, Request $request)
|
||||
{
|
||||
$totalItems = $this->countEntities($action, $request);
|
||||
$paginator = $this->getPaginatorFactory()->create($totalItems);
|
||||
$query = $this->queryEntities($action, $request, $paginator);
|
||||
|
||||
$entities = $query->getQuery()->getResult();
|
||||
|
||||
return $this->render($this->getTemplate('index'), \array_merge([
|
||||
'entities' => $entities,
|
||||
], $this->processTemplateParameters('index'))
|
||||
);
|
||||
$defaultTemplateParameters = [
|
||||
'entities' => $entities,
|
||||
'crud_name' => $this->getCrudName(),
|
||||
'paginator' => $paginator
|
||||
];
|
||||
|
||||
return $this->render(
|
||||
$this->getTemplateFor($action, $entities, $request),
|
||||
$this->generateTemplateParameter($action, $entities, $request, $defaultTemplateParameters)
|
||||
);
|
||||
}
|
||||
|
||||
protected function queryEntities($action, Request $request, PaginatorInterface $paginator)
|
||||
{
|
||||
$query = $this->getDoctrine()->getManager()
|
||||
->createQueryBuilder()
|
||||
->select('e')
|
||||
->from($this->getEntityClass(), 'e')
|
||||
->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())
|
||||
->setMaxResults($paginator->getItemsPerPage())
|
||||
;
|
||||
|
||||
$this->orderQuery($action, $query, $request, $paginator);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function countEntities($action, Request $request)
|
||||
{
|
||||
return $this->getDoctrine()->getManager()
|
||||
->createQuery("SELECT COUNT(e) FROM ".$this->getEntityClass()." e")
|
||||
->getSingleScalarResult()
|
||||
;
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id): Response
|
||||
@ -393,6 +399,14 @@ class CRUDController extends AbstractController
|
||||
return new $type;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $action
|
||||
* @param mixed $entity the entity for the current request, or an array of entities
|
||||
* @param Request $request
|
||||
* @return string
|
||||
* @throws \LogicException
|
||||
*/
|
||||
protected function getTemplateFor($action, $entity, Request $request)
|
||||
{
|
||||
if ($this->hasCustomTemplate($action, $entity, $request)) {
|
||||
|
@ -232,6 +232,7 @@ Impersonate: Mode fantôme
|
||||
Impersonate mode: Mode fantôme
|
||||
|
||||
crud:
|
||||
# general items
|
||||
new:
|
||||
button_action_form: Créer
|
||||
link_edit: Modifier
|
||||
@ -248,4 +249,4 @@ crud:
|
||||
default:
|
||||
success: Les données ont été enregistrées
|
||||
view:
|
||||
link_duplicate: Dupliquer
|
||||
link_duplicate: Dupliquer
|
||||
|
48
Resources/views/CRUD/_index.html.twig
Normal file
48
Resources/views/CRUD/_index.html.twig
Normal file
@ -0,0 +1,48 @@
|
||||
<div class="grid-10 centered">
|
||||
|
||||
{% block index_header %}
|
||||
<h1>{{ ('crud.' ~ crud_name ~ '.index.title')|trans({'%crud_name%': crud_name}) }}</h1>
|
||||
{% endblock index_header %}
|
||||
|
||||
{% if entities|length == 0 %}
|
||||
{% block no_existing_entities %}
|
||||
<p>{{ no_existing_entities_sentences|default('No entities')|trans }}</p>
|
||||
{% endblock %}
|
||||
{% else %}
|
||||
{% block table_entities %}
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
{% block table_entities_thead_tr %}
|
||||
<th>id</th>
|
||||
{% endblock %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% block table_entities_tbody %}
|
||||
{% for entity in entities %}
|
||||
<tr>
|
||||
<td>{{ entity.id }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
||||
|
||||
{% endif %}
|
||||
|
||||
<div class="crud_index__pagination">
|
||||
{{ chill_pagination(paginator) }}
|
||||
</div>
|
||||
|
||||
{% block list_actions %}
|
||||
<ul class="record_actions sticky-form-buttons">
|
||||
{% block add_new %}
|
||||
<li>
|
||||
<a href="{{ chill_path_add_return_path('chill_crud_' ~ crud_name ~ '_new') }}" class="sc-button bt-new">{{ ('crud.'~crud_name~'.index.add_new')|trans( {'%crud_name%': crud_name} ) }}</a>
|
||||
</li>
|
||||
{% endblock %}
|
||||
</ul>
|
||||
{% endblock list_actions %}
|
||||
</div>
|
@ -1,6 +1,6 @@
|
||||
<div class="{% block crud_content_main_div_class %}grid-10 centered{% endblock %}">
|
||||
{% block crud_content_header %}
|
||||
<h1>{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}</h1>
|
||||
<h1>{{ ('crud.' ~ crud_name ~ '.title_new')|trans({'%crud_name%' : crud_name }) }}</h1>
|
||||
{% endblock crud_content_header %}
|
||||
|
||||
{% block crud_content_form %}
|
||||
|
@ -1 +1 @@
|
||||
{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}
|
||||
{{ ('crud.' ~ crud_name ~ '.title_new')|trans({'%crud_name%' : crud_name }) }}
|
@ -1,39 +1,8 @@
|
||||
{% extends '@ChillMain/layout.html.twig' %}
|
||||
|
||||
{% block title %}{{ ('crud.' ~ crud_name ~ '.index.title')|trans({'%crud_name%': crud_name}) }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{{ title|default('List of %class%')|trans({'%class%': class}) }}</h1>
|
||||
|
||||
{% if entities|length == 0 %}
|
||||
<p>{{ no_existing_entities_sentences|default('No entities')|trans }}</p>
|
||||
{% else %}
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
{% for c in columns %}
|
||||
<th>{{ c|trans }}</th>
|
||||
{% endfor %}
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for entity in entities %}
|
||||
<tr>
|
||||
{% for c in columns %}
|
||||
<td>{{ entity|chill_crud_display(c) }}</td>
|
||||
{% endfor %}
|
||||
|
||||
<td>
|
||||
<ul class="record-actions">
|
||||
{% for action in actions %}
|
||||
<li>{{ action }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% embed '@ChillMain/CRUD/_index.html.twig' %}
|
||||
{% endembed %}
|
||||
{% endblock content %}
|
Loading…
x
Reference in New Issue
Block a user