Skip to content

Commit

Permalink
Event list: add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Apr 19, 2023
1 parent 5089a6e commit d690e41
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
32 changes: 31 additions & 1 deletion app/Resources/views/admin/event/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% endblock %}

{% block content %}
<h4>Liste des événements ({{ events | length }})</h4>
<h4>Liste des événements ({{ result_count }})</h4>

{# Filter form --------- #}
<ul class="collapsible">
Expand All @@ -36,10 +36,40 @@

{% include "admin/event/_partial/table.html.twig" with { events: events } %}

<ul class="pagination">
<li class="{% if(current_page==1) %}disabled{% else %}waves-effect{% endif %}">
<a href="{% if(current_page==1) %}#!{% else %}{{ path("event_list", {'page':current_page-1}) }}{% endif %}" data-page="{{ current_page-1 }}">
<i class="material-icons">chevron_left</i>
</a>
</li>
{% for i in range(1, page_count) %}
<li class="{% if(current_page==i) %}active{% else %}waves-effect{% endif %}">
<a href="#" data-page="{{ i }}">{{ i }}</a>
</li>
{% endfor %}
<li class="{% if(current_page==page_count) %}disabled{% else %}waves-effect{% endif %}">
<a href="{% if(current_page==page_count) %}#!{% else %}{{ path("event_list", {'page':current_page+1}) }}{% endif %}" data-page="{{ current_page+1 }}">
<i class="material-icons">chevron_right</i>
</a>
</li>
</ul>

{% if is_granted("ROLE_ADMIN") %}
<br />
<a href="{{ path("proxies_list") }}" class="btn"><i class="material-icons left">list</i>Toutes les procurations</a>
<br />
<a href="{{ path('event_new') }}" class="btn"><i class="material-icons left">add</i>Ajouter un événement</a>
{% endif %}
{% endblock %}

{% block javascripts %}
<script>
jQuery(function() {
$('.pagination li:not(.disabled) a').click(function(e) {
e.preventDefault();
$('#form_page').val($(this).data('page'));
$('form[name=form]').submit();
});
});
</script>
{% endblock %}
32 changes: 27 additions & 5 deletions src/AppBundle/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Doctrine\ORM\Tools\Pagination\Paginator;


/**
Expand All @@ -33,6 +35,7 @@ private function filterFormFactory(Request $request): array
// default values
$res = [
"kind" => null,
'page' => 1,
];

// filter creation ----------------------
Expand All @@ -49,6 +52,9 @@ private function filterFormFactory(Request $request): array
->orderBy('ek.name', 'ASC');
},
))
->add('page', HiddenType::class, [
'data' => '1'
])
->add('submit', SubmitType::class, array(
'label' => 'Filtrer',
'attr' => array('class' => 'btn', 'value' => 'filtrer')
Expand All @@ -59,6 +65,7 @@ private function filterFormFactory(Request $request): array

if ($res["form"]->isSubmitted() && $res["form"]->isValid()) {
$res["kind"] = $res["form"]->get("kind")->getData();
$res["page"] = $res["form"]->get("page")->getData();
}

return $res;
Expand Down Expand Up @@ -94,20 +101,35 @@ public function listAction(Request $request)
$em = $this->getDoctrine()->getManager();

$filter = $this->filterFormFactory($request);
$findByFilter = array();
$sort = 'date';
$order = 'DESC';

$qb = $em->getRepository('AppBundle:Event')->createQueryBuilder('e')
->orderBy('e.' . $sort, $order);

if ($filter['kind']) {
$findByFilter['kind'] = $filter['kind'];
$qb = $qb->andWhere('e.kind = :kind')
->setParameter('kind', $filter['kind']);
}

$events = $em->getRepository('AppBundle:Event')
->findBy($findByFilter, array($sort => $order));
$limitPerPage = 25;
$paginator = new Paginator($qb);
$resultCount = count($paginator);
$pageCount = ($resultCount == 0) ? 1 : ceil($resultCount / $limitPerPage);
$currentPage = $filter['page'];
$currentPage = ($currentPage > $pageCount) ? $pageCount : $currentPage;

$paginator
->getQuery()
->setFirstResult($limitPerPage * ($currentPage-1)) // set the offset
->setMaxResults($limitPerPage); // set the limit

return $this->render('admin/event/list.html.twig', array(
'events' => $events,
'events' => $paginator,
'filter_form' => $filter['form']->createView(),
'result_count' => $resultCount,
'current_page' => $currentPage,
'page_count' => $pageCount,
));
}

Expand Down

0 comments on commit d690e41

Please sign in to comment.