diff --git a/CRUD/Controller/CRUDController.php b/CRUD/Controller/CRUDController.php
index 70f6e9a75..7a9c1e76b 100644
--- a/CRUD/Controller/CRUDController.php
+++ b/CRUD/Controller/CRUDController.php
@@ -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)) {
diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml
index 93c2c303d..bcf6ab106 100644
--- a/Resources/translations/messages.fr.yml
+++ b/Resources/translations/messages.fr.yml
@@ -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
\ No newline at end of file
+ link_duplicate: Dupliquer
diff --git a/Resources/views/CRUD/_index.html.twig b/Resources/views/CRUD/_index.html.twig
new file mode 100644
index 000000000..4e553d962
--- /dev/null
+++ b/Resources/views/CRUD/_index.html.twig
@@ -0,0 +1,48 @@
+
+
+{% block index_header %}
+
{{ ('crud.' ~ crud_name ~ '.index.title')|trans({'%crud_name%': crud_name}) }}
+{% endblock index_header %}
+
+{% if entities|length == 0 %}
+ {% block no_existing_entities %}
+
{{ no_existing_entities_sentences|default('No entities')|trans }}
+ {% endblock %}
+{% else %}
+ {% block table_entities %}
+
+
+
+ {% block table_entities_thead_tr %}
+ id |
+ {% endblock %}
+
+
+
+ {% block table_entities_tbody %}
+ {% for entity in entities %}
+
+ {{ entity.id }} |
+
+ {% endfor %}
+ {% endblock %}
+
+
+ {% endblock %}
+
+{% endif %}
+
+
+
+{% block list_actions %}
+
+{% endblock list_actions %}
+
diff --git a/Resources/views/CRUD/_new_content.html.twig b/Resources/views/CRUD/_new_content.html.twig
index 3da85d3ad..93609d8d1 100644
--- a/Resources/views/CRUD/_new_content.html.twig
+++ b/Resources/views/CRUD/_new_content.html.twig
@@ -1,6 +1,6 @@
{% block crud_content_header %}
-
{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}
+
{{ ('crud.' ~ crud_name ~ '.title_new')|trans({'%crud_name%' : crud_name }) }}
{% endblock crud_content_header %}
{% block crud_content_form %}
diff --git a/Resources/views/CRUD/_new_title.html.twig b/Resources/views/CRUD/_new_title.html.twig
index 60c581122..ebd2121a6 100644
--- a/Resources/views/CRUD/_new_title.html.twig
+++ b/Resources/views/CRUD/_new_title.html.twig
@@ -1 +1 @@
-{{ 'crud.%crud_name%.title_new'|trans({'%crud_name%' : crud_name }) }}
\ No newline at end of file
+{{ ('crud.' ~ crud_name ~ '.title_new')|trans({'%crud_name%' : crud_name }) }}
\ No newline at end of file
diff --git a/Resources/views/CRUD/index.html.twig b/Resources/views/CRUD/index.html.twig
index 83c8683c4..00608131d 100644
--- a/Resources/views/CRUD/index.html.twig
+++ b/Resources/views/CRUD/index.html.twig
@@ -1,39 +1,8 @@
{% extends '@ChillMain/layout.html.twig' %}
+{% block title %}{{ ('crud.' ~ crud_name ~ '.index.title')|trans({'%crud_name%': crud_name}) }}{% endblock %}
+
{% block content %}
-
{{ title|default('List of %class%')|trans({'%class%': class}) }}
-
- {% if entities|length == 0 %}
-
{{ no_existing_entities_sentences|default('No entities')|trans }}
- {% else %}
-
-
-
- {% for c in columns %}
- {{ c|trans }} |
- {% endfor %}
- |
-
-
-
- {% for entity in entities %}
-
- {% for c in columns %}
- {{ entity|chill_crud_display(c) }} |
- {% endfor %}
-
-
-
- {% for action in actions %}
- - {{ action }}
- {% endfor %}
-
- |
- {% endfor %}
-
-
-
- {% endif %}
-
-
+ {% embed '@ChillMain/CRUD/_index.html.twig' %}
+ {% endembed %}
{% endblock content %}
\ No newline at end of file