Skip to content

Commit

Permalink
🐛 Fix label & method countResults to display good things with simple …
Browse files Browse the repository at this point in the history
…pager
  • Loading branch information
elyanory committed May 15, 2024
1 parent c1fccde commit 4346a40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
27 changes: 22 additions & 5 deletions src/Datagrid/SimplePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Sonata\AdminBundle\Util\TraversableToCollection;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;

/**
* @author Lukas Kahwe Smith <[email protected]>
Expand Down Expand Up @@ -44,16 +45,14 @@ final class SimplePager extends Pager
* If set to 3 the pager will generate links to the next three pages
* etc.
*/
public function __construct(
int $maxPerPage = 10,
private int $threshold = 1
) {
public function __construct(int $maxPerPage = 10, private int $threshold = 1, private int $total = 0)
{
parent::__construct($maxPerPage);
}

public function countResults(): int
{
return ($this->getPage() - 1) * $this->getMaxPerPage() + ($this->thresholdCount ?? 0);
return $this->total;
}

public function getCurrentPageResults(): iterable
Expand All @@ -79,6 +78,23 @@ public function getCurrentPageResults(): iterable
return $this->results;
}

public function getTotalResults(): int
{
/** @var ProxyQuery $query */
$query = $this->getQuery();

Check failure on line 84 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @var for variable $query contains unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 84 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @var with type Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery is not subtype of native type Sonata\AdminBundle\Datagrid\ProxyQueryInterface|null.

Check failure on line 84 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:84:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)

if (null === $query) {
throw new \LogicException('Uninitialized query.');
}

$query->select(sprintf('COUNT(%s.id)', $query->getRootAlias()));

Check failure on line 90 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method getRootAlias() on an unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 90 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method select() on an unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 90 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:90:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)

Check failure on line 90 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:90:48: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)
$query->setFirstResult(0);

Check failure on line 91 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method setFirstResult() on an unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 91 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:91:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)
$query->setMaxResults(1);

Check failure on line 92 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method setMaxResults() on an unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 92 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:92:9: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)


return $query->getDoctrineQuery()->getSingleScalarResult();

Check failure on line 95 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to method getDoctrineQuery() on an unknown class Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery.

Check failure on line 95 in src/Datagrid/SimplePager.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedDocblockClass

src/Datagrid/SimplePager.php:95:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery does not exist (see https://psalm.dev/200)
}

/**
* @throws \LogicException the query is uninitialized
*/
Expand All @@ -103,6 +119,7 @@ public function init(): void
$query->setMaxResults($maxOffset);

$this->results = $this->getCurrentPageResults();
$this->total = $this->getTotalResults();

$t = (int) ceil(($this->thresholdCount ?? 0) / $this->getMaxPerPage()) + $this->getPage() - 1;
$this->setLastPage(max(1, $t));
Expand Down
9 changes: 1 addition & 8 deletions src/Resources/views/Pager/simple_pager_results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ file that was distributed with this source code.
{% extends '@SonataAdmin/Pager/base_results.html.twig' %}

{% block num_results %}
{% if admin.datagrid.pager.lastPage != admin.datagrid.pager.page %}
{{ 'list_results_count_prefix'|trans({}, 'SonataAdminBundle') }}
{% endif %}
{% trans with {'%count%': admin.datagrid.pager.countResults()} from 'SonataAdminBundle' %}list_results_count{% endtrans %}
&nbsp;-&nbsp;
{% endblock %}

{% block num_pages %}
{{ admin.datagrid.pager.page }}
/
{% if admin.datagrid.pager.lastPage != admin.datagrid.pager.page %}
?
{% else %}
{{ admin.datagrid.pager.lastpage }}
{% endif %}
{{ admin.datagrid.pager.lastPage }}
&nbsp;-&nbsp;
{% endblock %}

0 comments on commit 4346a40

Please sign in to comment.