add filters in crud
This commit is contained in:
parent
a191db23e1
commit
cf6bb84bfb
|
@ -6,10 +6,10 @@ use App\Core\Controller\Admin\AdminController;
|
||||||
use App\Core\Crud\CrudConfiguration;
|
use App\Core\Crud\CrudConfiguration;
|
||||||
use App\Core\Entity\EntityInterface;
|
use App\Core\Entity\EntityInterface;
|
||||||
use App\Core\Manager\EntityManager;
|
use App\Core\Manager\EntityManager;
|
||||||
|
use App\Core\Repository\RepositoryQuery;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\Session\Session;
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
use App\Core\Repository\RepositoryQuery;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class CrudController.
|
* class CrudController.
|
||||||
|
@ -20,25 +20,26 @@ abstract class CrudController extends AdminController
|
||||||
{
|
{
|
||||||
abstract protected function getConfiguration(): CrudConfiguration;
|
abstract protected function getConfiguration(): CrudConfiguration;
|
||||||
|
|
||||||
|
protected array $filters = [];
|
||||||
|
|
||||||
protected function doIndex(int $page = 1, RepositoryQuery $query, Request $request, Session $session): Response
|
protected function doIndex(int $page = 1, RepositoryQuery $query, Request $request, Session $session): Response
|
||||||
{
|
{
|
||||||
/*$this->updateFilters($options['request'], $options['session']);*/
|
$configuration = $this->getConfiguration();
|
||||||
|
|
||||||
|
$this->updateFilters($request, $session);
|
||||||
|
|
||||||
$pager = $query
|
$pager = $query
|
||||||
//->useFilters($this->filters)
|
->useFilters($this->filters)
|
||||||
->paginate($page)
|
->paginate($page, $configuration->getMaxPerPage('index'))
|
||||||
;
|
;
|
||||||
|
|
||||||
/*$viewOptions = array_merge([
|
|
||||||
'pager' => $pager,
|
|
||||||
'hasFilters' => !empty($this->filters),
|
|
||||||
], $options['viewOptions']);*/
|
|
||||||
|
|
||||||
$configuration = $this->getConfiguration();
|
|
||||||
|
|
||||||
return $this->render($this->getConfiguration()->getView('index'), [
|
return $this->render($this->getConfiguration()->getView('index'), [
|
||||||
'configuration' => $configuration,
|
'configuration' => $configuration,
|
||||||
'pager' => $pager,
|
'pager' => $pager,
|
||||||
|
'filters' => [
|
||||||
|
'show' => $configuration->getForm('filter') !== null,
|
||||||
|
'isEmpty' => empty($this->filters),
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,41 +120,53 @@ abstract class CrudController extends AdminController
|
||||||
|
|
||||||
protected function doFilter(Session $session): Response
|
protected function doFilter(Session $session): Response
|
||||||
{
|
{
|
||||||
$form = $this->createForm($this->forms['filters']);
|
$configuration = $this->getConfiguration();
|
||||||
$form->submit($session->get($this->filterRequestId, []));
|
$type = $configuration->getForm('filter');
|
||||||
|
|
||||||
return $this->render($this->getView('filters'), [
|
if (null === $type) {
|
||||||
|
throw $this->createNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$form = $this->createForm($type);
|
||||||
|
$form->submit($session->get($form->getName(), []));
|
||||||
|
|
||||||
|
return $this->render($configuration->getView('filter'), [
|
||||||
'form' => $form->createView(),
|
'form' => $form->createView(),
|
||||||
|
'configuration' => $configuration,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function updateFilters(Request $request, Session $session)
|
protected function updateFilters(Request $request, Session $session)
|
||||||
{
|
{
|
||||||
if ($request->query->has($this->filterRequestId)) {
|
$configuration = $this->getConfiguration();
|
||||||
$filters = $request->query->get($this->filterRequestId);
|
$type = $configuration->getForm('filter');
|
||||||
|
|
||||||
|
if (null === $type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$form = $this->createForm($type);
|
||||||
|
|
||||||
|
if ($request->query->has($form->getName())) {
|
||||||
|
$filters = $request->query->get($form->getName());
|
||||||
|
|
||||||
if ('0' === $filters) {
|
if ('0' === $filters) {
|
||||||
$filters = [];
|
$filters = [];
|
||||||
}
|
}
|
||||||
} elseif ($session->has($this->filterRequestId)) {
|
} elseif ($session->has($form->getName())) {
|
||||||
$filters = $session->get($this->filterRequestId);
|
$filters = $session->get($form->getName());
|
||||||
} else {
|
} else {
|
||||||
$filters = [];
|
$filters = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->forms['filters'])) {
|
$form->submit($filters);
|
||||||
$form = $this->createForm($this->forms['filters']);
|
|
||||||
$form->submit($filters);
|
|
||||||
} else {
|
|
||||||
$form = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($filters)) {
|
if (empty($filters)) {
|
||||||
$this->filters = $filters;
|
$this->filters = $filters;
|
||||||
$session->set($this->filterRequestId, $filters);
|
$session->set($form->getName(), $filters);
|
||||||
} elseif (null !== $form && $form->isValid()) {
|
} elseif ($form->isValid()) {
|
||||||
$this->filters = $form->getData();
|
$this->filters = $form->getData();
|
||||||
$session->set($this->filterRequestId, $filters);
|
$session->set($form->getName(), $filters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ class CrudConfiguration
|
||||||
protected array $formOptions = [];
|
protected array $formOptions = [];
|
||||||
protected array $views = [];
|
protected array $views = [];
|
||||||
protected array $fields = [];
|
protected array $fields = [];
|
||||||
|
protected array $maxPerPage = [];
|
||||||
|
|
||||||
/* -- */
|
/* -- */
|
||||||
|
|
||||||
|
@ -55,9 +56,9 @@ class CrudConfiguration
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getForm(string $context): string
|
public function getForm(string $context): ?string
|
||||||
{
|
{
|
||||||
return $this->forms[$context];
|
return $this->forms[$context] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setFormOptions(string $context, array $options = []): self
|
public function setFormOptions(string $context, array $options = []): self
|
||||||
|
@ -146,4 +147,18 @@ class CrudConfiguration
|
||||||
{
|
{
|
||||||
return $this->fields[$context] ?? [];
|
return $this->fields[$context] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -- */
|
||||||
|
|
||||||
|
public function setMaxPerPage(string $page, int $max)
|
||||||
|
{
|
||||||
|
$this->maxPerPage[$page] = $max;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMaxPerPage(string $page, int $default = 20)
|
||||||
|
{
|
||||||
|
return $this->maxPerPage[$page] ?? $default;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,9 +127,13 @@
|
||||||
"Name": "Nom"
|
"Name": "Nom"
|
||||||
"Authentication": "Authentification"
|
"Authentication": "Authentification"
|
||||||
"Anyway": "Peu importe"
|
"Anyway": "Peu importe"
|
||||||
|
"Reset": "Réinitialiser"
|
||||||
"Yes": "Oui"
|
"Yes": "Oui"
|
||||||
"No": "Non"
|
"No": "Non"
|
||||||
|
"yes": "oui"
|
||||||
|
"no": "non"
|
||||||
"Locale": "Langue"
|
"Locale": "Langue"
|
||||||
"Settings": "Paramètres"
|
"Settings": "Paramètres"
|
||||||
"Setting": "Paramètre"
|
"Setting": "Paramètre"
|
||||||
"Section": "Section"
|
"Section": "Section"
|
||||||
|
"Filter": "Filtrer"
|
||||||
|
|
21
core/Resources/views/admin/crud/filter.html.twig
Normal file
21
core/Resources/views/admin/crud/filter.html.twig
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title">
|
||||||
|
{{ 'Filter'|trans }}
|
||||||
|
</h5>
|
||||||
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||||
|
<span aria-hidden="true">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<form action="{{ path(configuration.pageRoute('index')) }}" id="form-filters" method="GET">
|
||||||
|
{{ form_widget(form) }}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<a href="{{ path(configuration.pageRoute('index'), {(form.vars.name): 0}) }}" class="btn btn-secondary">{{ 'Reset'|trans }}</a>
|
||||||
|
<button type="submit" form="form-filters" class="btn btn-primary">{{ 'Filter'|trans }}</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
{% block header %}
|
{% block header %}
|
||||||
<div class="bg-light pl-5 pr-4 pt-5 {% if pager.paginationData.pageCount < 2 %}pb-5{% endif %}">
|
<div class="bg-light pl-5 pr-4 pt-5 {% if pager.paginationData.pageCount < 2 %}{% if filters.show %}pb-3{% else %}pb-5{% endif %}{% endif %}">
|
||||||
<div class="crud-header">
|
<div class="crud-header">
|
||||||
{% block header_title %}
|
{% block header_title %}
|
||||||
<h1 class="crud-header-title">{{ configuration.pageTitle('index')|trans }}</h1>
|
<h1 class="crud-header-title">{{ configuration.pageTitle('index')|trans }}</h1>
|
||||||
|
@ -24,7 +24,20 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{ knp_pagination_render(pager) }}
|
{% if filters.show %}
|
||||||
|
<div class="row pb-3">
|
||||||
|
<div class="col-auto ml-auto {% if pager.getPaginationData.pageCount > 1 %}mr-3{% endif %}">
|
||||||
|
<button data-modal="{{ path(configuration.pageRoute('filter')) }}" class="btn btn-sm btn-secondary">
|
||||||
|
{{ 'Filter'|trans }} {% if not filters.isEmpty %}({{ 'yes'|trans }}){% endif %}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="col-auto">
|
||||||
|
{{ knp_pagination_render(pager) }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
{{ knp_pagination_render(pager) }}
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue