From 4653feb123870e2555238d16b4186b01f6fedb50 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Mon, 12 May 2025 20:27:39 -0500 Subject: [PATCH] wip: pagination --- .../Repository/DownloadRepository.php | 18 +++--- src/Twig/Components/DownloadList.php | 10 +++- src/Util/Paginator.php | 59 +++++++++++++++++++ templates/components/DownloadList.html.twig | 7 ++- templates/components/MonitorList.html.twig | 6 +- templates/components/StatusBadge.html.twig | 2 +- templates/partial/paginator.html.twig | 48 +++++++++++++++ 7 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 src/Util/Paginator.php create mode 100644 templates/partial/paginator.html.twig diff --git a/src/Download/Framework/Repository/DownloadRepository.php b/src/Download/Framework/Repository/DownloadRepository.php index ba46138..b2b6a3f 100644 --- a/src/Download/Framework/Repository/DownloadRepository.php +++ b/src/Download/Framework/Repository/DownloadRepository.php @@ -4,10 +4,9 @@ namespace App\Download\Framework\Repository; use App\Download\Framework\Entity\Download; use App\User\Framework\Entity\User; +use App\Util\Paginator; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; -use Knp\Component\Pager\Paginator; -use Knp\Component\Pager\PaginatorInterface; use Symfony\Component\Security\Core\User\UserInterface; /** @@ -17,38 +16,35 @@ class DownloadRepository extends ServiceEntityRepository { private ManagerRegistry $managerRegistry; - public function __construct(ManagerRegistry $registry, ManagerRegistry $managerRegistry) + private Paginator $paginator; + + public function __construct(ManagerRegistry $registry, ManagerRegistry $managerRegistry, Paginator $paginator) { parent::__construct($registry, Download::class); $this->managerRegistry = $managerRegistry; + $this->paginator = $paginator; } public function getCompletePaginated(int $pageNumber = 1, int $perPage = 10) { - $firstResult = ($pageNumber - 1) * $perPage; $query = $this->createQueryBuilder('d') ->andWhere('d.status IN (:statuses)') ->orderBy('d.id', 'DESC') ->setParameter('statuses', ['Complete']) - ->setFirstResult($firstResult) - ->setMaxResults($perPage) ->getQuery(); - return new \Doctrine\ORM\Tools\Pagination\Paginator($query); + return $this->paginator->paginate($query, $pageNumber, $perPage); } public function getActivePaginated(int $pageNumber = 1, int $perPage = 5) { - $firstResult = ($pageNumber - 1) * $perPage; $query = $this->createQueryBuilder('d') ->andWhere('d.status IN (:statuses)') ->orderBy('d.id', 'ASC') ->setParameter('statuses', ['New', 'In Progress']) - ->setFirstResult($firstResult) - ->setMaxResults($perPage) ->getQuery(); - return new \Doctrine\ORM\Tools\Pagination\Paginator($query); + return $this->paginator->paginate($query, $pageNumber, $perPage); } public function insert( diff --git a/src/Twig/Components/DownloadList.php b/src/Twig/Components/DownloadList.php index 6b9a817..d16eacb 100644 --- a/src/Twig/Components/DownloadList.php +++ b/src/Twig/Components/DownloadList.php @@ -2,6 +2,8 @@ namespace App\Twig\Components; +use App\Download\Framework\Repository\DownloadRepository; +use App\Util\Paginator; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\DefaultActionTrait; @@ -13,12 +15,16 @@ final class DownloadList extends AbstractController public string $type; + public function __construct( + private readonly DownloadRepository $downloadRepository, + ) {} + public function getDownloads() { if ($this->type === "active") { - return $this->getUser()->queryDownloads('in-progress', 5); + return $this->downloadRepository->getActivePaginated(); } elseif ($this->type === "complete") { - return $this->getUser()->queryDownloads('complete', 5); + return $this->downloadRepository->getCompletePaginated(); } return []; diff --git a/src/Util/Paginator.php b/src/Util/Paginator.php new file mode 100644 index 0000000..ec39e0d --- /dev/null +++ b/src/Util/Paginator.php @@ -0,0 +1,59 @@ +getQuery() + ->setFirstResult($limit * ($page - 1)) + ->setMaxResults($limit); + + $this->total = $paginator->count(); + $this->lastPage = (int) ceil($paginator->count() / $paginator->getQuery()->getMaxResults()); + $this->items = $paginator; + + return $this; + } + + public function getTotal(): int + { + return $this->total; + } + + public function getLastPage(): int + { + return $this->lastPage; + } + + public function getItems() + { + return $this->items; + } +} \ No newline at end of file diff --git a/templates/components/DownloadList.html.twig b/templates/components/DownloadList.html.twig index 233d602..afaffbd 100644 --- a/templates/components/DownloadList.html.twig +++ b/templates/components/DownloadList.html.twig @@ -15,7 +15,7 @@ {% if this.downloads|length > 0 %} - {% for download in this.downloads %} + {% for download in this.downloads.items %} {{ download.title }} @@ -40,5 +40,10 @@ {% endif %} + + {% if this.downloads.items|length > 0 %} + {% set paginator = this.downloads %} + {% include 'partial/paginator.html.twig' %} + {% endif %} diff --git a/templates/components/MonitorList.html.twig b/templates/components/MonitorList.html.twig index aa3474b..89cc1ac 100644 --- a/templates/components/MonitorList.html.twig +++ b/templates/components/MonitorList.html.twig @@ -25,11 +25,11 @@ - + {% if this.userMonitors()|length > 0 %} {% for monitor in this.userMonitors() %} - + {{ monitor.title }} @@ -54,7 +54,7 @@ {% endfor %} {% else %} - + No monitors diff --git a/templates/components/StatusBadge.html.twig b/templates/components/StatusBadge.html.twig index 91f419b..fc945fd 100644 --- a/templates/components/StatusBadge.html.twig +++ b/templates/components/StatusBadge.html.twig @@ -1,3 +1,3 @@ - + {{ status }} \ No newline at end of file diff --git a/templates/partial/paginator.html.twig b/templates/partial/paginator.html.twig new file mode 100644 index 0000000..78d3f78 --- /dev/null +++ b/templates/partial/paginator.html.twig @@ -0,0 +1,48 @@ +{% set _currentPage = app.request.query.get('page') ?: 1 %} +{% set _currentRoute = app.request.attributes.get('_route') %} +{% set _lastPage = paginator.lastPage %} +{% set _currentParams = app.request.query.all|merge(app.request.attributes.get('_route_params')) %} + +{% if paginator.lastPage > 1 %} + +{% endif %}