Skip to content

Commit

Permalink
Fixed pagination when the pager has a lot of pages (#137)
Browse files Browse the repository at this point in the history
* Fixed pagination when the pager has a lot of pages + templates refactoring
* Small refactoring
  • Loading branch information
DamienHarper authored Dec 3, 2019
1 parent 1bb56ee commit 16d58e8
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 52 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.2.2] - Not yet released

### Changes
* Fixed pagination when the pager has a lot of pages
* Fixed documentation related to `@Security` annotation (thanks to @jean-gui)


## [3.2.1] - 2019-12-02

### Changes
Expand Down
12 changes: 3 additions & 9 deletions src/DoctrineAuditBundle/Reader/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,12 @@ public function getAuditsByTransactionHash(string $transactionHash): array
*/
public function getAuditsPager(string $entity, $id = null, int $page = 1, int $pageSize = self::PAGE_SIZE): array
{
$queryBuilder = $this->getAuditsQueryBuilder($entity, $id);

$currentPage = $page < 1 ? 1 : $page;
$firstResult = ($currentPage - 1) * $pageSize;

$queryBuilder
->setFirstResult($firstResult)
->setMaxResults($pageSize)
;
$queryBuilder = $this->getAuditsQueryBuilder($entity, $id, $page, $pageSize);

$paginator = new Paginator($queryBuilder);
$numResults = $paginator->count();

$currentPage = $page < 1 ? 1 : $page;
$hasPreviousPage = $currentPage > 1;
$hasNextPage = ($currentPage * $pageSize) < $numResults;

Expand Down
20 changes: 10 additions & 10 deletions src/DoctrineAuditBundle/Reader/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ class Paginator implements Countable, IteratorAggregate
/**
* @var QueryBuilder
*/
private $query;
private $queryBuilder;

/**
* @var int
*/
private $count;

public function __construct($query)
public function __construct($queryBuilder)
{
$this->query = $query;
$this->queryBuilder = $queryBuilder;
}

public function count()
{
$queryBuilder = $this->cloneQuery($this->query);
$queryBuilder = $this->cloneQuery($this->queryBuilder);

$result = $queryBuilder
->resetQueryPart('select')
Expand All @@ -45,10 +45,10 @@ public function count()

public function getIterator()
{
$offset = $this->query->getFirstResult();
$length = $this->query->getMaxResults();
$offset = $this->queryBuilder->getFirstResult();
$length = $this->queryBuilder->getMaxResults();

$result = $this->cloneQuery($this->query)
$result = $this->cloneQuery($this->queryBuilder)
->setMaxResults($length)
->setFirstResult($offset)
->execute()
Expand All @@ -58,11 +58,11 @@ public function getIterator()
return new ArrayIterator($result);
}

private function cloneQuery(QueryBuilder $query): QueryBuilder
private function cloneQuery(QueryBuilder $queryBuilder): QueryBuilder
{
/** @var QueryBuilder $cloneQuery */
$cloneQuery = clone $query;
$cloneQuery->setParameters($query->getParameters());
$cloneQuery = clone $queryBuilder;
$cloneQuery->setParameters($queryBuilder->getParameters());

return $cloneQuery;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "@DHDoctrineAudit/layout.html.twig" %}

{% import '@DHDoctrineAudit/Audit/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helper.html.twig' as helper %}
{% import '@DHDoctrineAudit/Audit/helpers/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helpers/helper.html.twig' as helper %}

{% block dh_doctrine_audit_content %}
<div class="card border-0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{% extends "@DHDoctrineAudit/layout.html.twig" %}

{% import '@DHDoctrineAudit/Audit/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helper.html.twig' as helper %}
{% import '@DHDoctrineAudit/Audit/helpers/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helpers/helper.html.twig' as helper %}
{% import '@DHDoctrineAudit/Audit/helpers/pager.html.twig' as pager %}

{% block dh_doctrine_audit_content %}
<div class="card border-0">
Expand All @@ -25,31 +26,7 @@
{% endfor %}
</div>

{% if paginator.haveToPaginate %}
<nav class="float-right">
<ul class="pagination">
{% if paginator.hasPreviousPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: paginator.previousPage}) }}" class="page-link" rel="previous"><i class="fa fw fa-arrow-left"></i> Previous</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link"><i class="fa fw fa-arrow-left"></i> Previous</span></li>
{% endif %}

{% for i in 1..paginator.numPages %}
{% if i == paginator.currentPage %}
<li class="page-item active"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: i}) }}" class="page-link">{{ i }}</a></li>
{% endif %}
{% endfor %}

{% if paginator.hasNextPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: paginator.nextPage}) }}" class="page-link" rel="next">Next <i class="fa fw fa-arrow-right"></i></a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">Next <i class="fa fw fa-arrow-right"></i></span></li>
{% endif %}
</ul>
</nav>
{% endif %}
{{ pager.render(entity, paginator) }}
</div>
</div>
{% endblock dh_doctrine_audit_content %}
4 changes: 2 additions & 2 deletions src/DoctrineAuditBundle/Resources/views/Audit/entry.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% import '@DHDoctrineAudit/Audit/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helper.html.twig' as helper %}
{% import '@DHDoctrineAudit/Audit/helpers/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helpers/helper.html.twig' as helper %}

<article class="timeline-entry">
<div class="timeline-entry-inner">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{% macro render(entity, paginator, proximity = 3, displayedPages = 10) %}
{% if paginator.haveToPaginate %}
{% set start = paginator.currentPage - proximity %}
{% set end = paginator.currentPage + proximity %}
{% if start < 1 %}
{% set end = min(end + (1 - start), paginator.numPages) %}
{% set start = 1 %}
{% endif %}
{% if end > paginator.numPages %}
{% set start = max(start - (end - paginator.numPages), 1) %}
{% set end = paginator.numPages %}
{% endif %}

<nav class="float-right">
<ul class="pagination">
{{ _self.first(entity, paginator) }}
{{ _self.previous(entity, paginator) }}
{{ _self.dotsIfStartIsOver3(entity, paginator, start) }}
{{ _self.pages(entity, paginator, start, end) }}
{{ _self.dotsIfEndIsUnder3ToLast(entity, paginator, end) }}
{{ _self.next(entity, paginator) }}
{{ _self.last(entity, paginator) }}
</ul>
</nav>
{% endif %}
{% endmacro render %}

{% macro first(entity, paginator) %}
{% if paginator.hasPreviousPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: 1}) }}" class="page-link" rel="previous"><i class="fa fw fa-angle-double-left"></i> First</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link"><i class="fa fw fa-angle-double-left"></i> First</span></li>
{% endif %}
{% endmacro first %}

{% macro previous(entity, paginator) %}
{% if paginator.hasPreviousPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: paginator.previousPage}) }}" class="page-link" rel="previous"><i class="fa fw fa-angle-left"></i> Previous</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link"><i class="fa fw fa-angle-left"></i> Previous</span></li>
{% endif %}
{% endmacro previous %}

{% macro dotsIfStartIsOver3(entity, paginator, start) %}
{% if start > 1 %}
<li class="page-item disabled"><span class="page-link">&hellip;</span></li>
{% endif %}
{% endmacro dotsIfStartIsOver3 %}

{% macro pages(entity, paginator, start, end) %}
{% for i in start..end %}
{{ _self.page(entity, paginator, i) }}
{% endfor %}
{% endmacro pages %}

{% macro dotsIfEndIsUnder3ToLast(entity, paginator, end) %}
{% if end < paginator.numPages %}
<li class="page-item disabled"><span class="page-link">&hellip;</span></li>
{% endif %}
{% endmacro dotsIfEndIsUnder3ToLast %}

{% macro next(entity, paginator) %}
{% if paginator.hasNextPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: paginator.nextPage}) }}" class="page-link" rel="next">Next <i class="fa fw fa-angle-right"></i></a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">Next <i class="fa fw fa-angle-right"></i></span></li>
{% endif %}
{% endmacro next %}

{% macro last(entity, paginator) %}
{% if paginator.hasNextPage %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: paginator.numPages}) }}" class="page-link" rel="previous">Last <i class="fa fw fa-angle-double-right"></i></a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">Last <i class="fa fw fa-angle-double-right"></i></span></li>
{% endif %}
{% endmacro last %}

{% macro page(entity, paginator, page) %}
{% if page == paginator.currentPage %}
<li class="page-item active"><span class="page-link">{{ page }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {entity: entity, page: page}) }}" class="page-link">{{ page }}</a></li>
{% endif %}
{% endmacro page %}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "@DHDoctrineAudit/layout.html.twig" %}

{% import '@DHDoctrineAudit/Audit/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helper.html.twig' as helper %}
{% import '@DHDoctrineAudit/Audit/helpers/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helpers/helper.html.twig' as helper %}

{% block dh_doctrine_audit_content %}
<div class="card border-0">
Expand Down

0 comments on commit 16d58e8

Please sign in to comment.