From c0bfa5b334b61d9ee341b4b6a81019437ef36c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 18:32:03 +0200 Subject: [PATCH 1/4] RVRNEXT-22 add pagination template --- views/templates/pagination.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 views/templates/pagination.php diff --git a/views/templates/pagination.php b/views/templates/pagination.php new file mode 100644 index 0000000..8c85631 --- /dev/null +++ b/views/templates/pagination.php @@ -0,0 +1,31 @@ +

+ « + + 1 ? + ($currentPage - $maxAdditionalPagesHalf < $pages - $maxAdditionalPages ? + $currentPage - $maxAdditionalPagesHalf : + $pages - $maxAdditionalPages) : + 1; + $end = $start + $maxAdditionalPages < $pages ? + $start + $maxAdditionalPages : + $pages; + } + ?> + + + + + + + + + » +

From bf415a2a463b87ced432bc0d7f2c38d17de18438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 18:32:19 +0200 Subject: [PATCH 2/4] RVRNEXT-22 use pagination template --- views/communities/transactions.php | 33 +++++++----------------------- views/events/events.php | 33 +++++++----------------------- 2 files changed, 14 insertions(+), 52 deletions(-) diff --git a/views/communities/transactions.php b/views/communities/transactions.php index 612b12a..23e40c5 100644 --- a/views/communities/transactions.php +++ b/views/communities/transactions.php @@ -13,20 +13,13 @@

New transaction

0): ?> + $community->getSlug()]; + ?> + 1): ?> -

- « - - - - - - - - - - » -

+ @include(templates/pagination) @@ -51,19 +44,7 @@ 1): ?> -

- « - - - - - - - - - - » -

+ @include(templates/pagination)

New transaction

diff --git a/views/events/events.php b/views/events/events.php index a3cf74b..45c70e1 100644 --- a/views/events/events.php +++ b/views/events/events.php @@ -9,20 +9,13 @@

New event

0): ?> + $community->getSlug()]; + ?> + 1): ?> -

- « - - - - - - - - - - » -

+ @include(templates/pagination) @@ -36,19 +29,7 @@ 1): ?> -

- « - - - - - - - - - - » -

+ @include(templates/pagination)

New event

From 9929e00a3f331037c01c62f2b12735d25bad1a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 20:33:20 +0200 Subject: [PATCH 3/4] RVRNEXT-22 use Select::paginate in repositories --- src/Repository/EventRepository.php | 4 ++-- src/Repository/TransactionRepository.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index 9e568e4..3767c67 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -52,11 +52,11 @@ class EventRepository yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); } - public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator { $select = $this->selectAllByCommunity($community); $select->orderBy('start', 'DESC'); - $select->limit($limit, $start); + $select->paginate($page, $itemsPerPage); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); } diff --git a/src/Repository/TransactionRepository.php b/src/Repository/TransactionRepository.php index 8d2c5b0..66057dd 100644 --- a/src/Repository/TransactionRepository.php +++ b/src/Repository/TransactionRepository.php @@ -66,20 +66,20 @@ class TransactionRepository return $select->count() > 0; } - public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator { $select = $this->selectAllByCommunity($community); $select->orderBy('time', 'DESC'); - $select->limit($limit, $start); + $select->paginate($page, $itemsPerPage); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); } - public function getPagedByEvent(Event $event, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + public function getPagedByEvent(Event $event, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator { $select = $this->selectAllByEvent($event); $select->orderBy('time', 'DESC'); - $select->limit($limit, $start); + $select->paginate($page, $itemsPerPage); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); } From d1c926541aec0f8035e29b7bdefe9010ecf0247d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 20:33:44 +0200 Subject: [PATCH 4/4] RVRNEXT-22 use the new repository methods in controllers --- src/Controller/EventController.php | 7 +++---- src/Controller/TransactionController.php | 9 ++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Controller/EventController.php b/src/Controller/EventController.php index be72ca1..ab2aa5f 100644 --- a/src/Controller/EventController.php +++ b/src/Controller/EventController.php @@ -68,17 +68,16 @@ class EventController implements IAuthenticationRequired, ISecured { $itemsPerPage = 10; $numberOfEvents = $this->eventRepository->countAllByCommunity($this->community); - $pages = ceil($numberOfEvents / $itemsPerPage); - $currentPage = Container::$request->query('page') ?: 0; + $currentPage = Container::$request->query('page') ?: 1; $events = $this->eventRepository->getPagedByCommunity( $this->community, - $currentPage * $itemsPerPage, + $currentPage, $itemsPerPage ); return new HtmlContent('events/events', [ 'community' => $this->community, - 'pages' => $pages, + 'pages' => ceil($numberOfEvents / $itemsPerPage), 'currentPage' => $currentPage, 'numberOfEvents' => $numberOfEvents, 'events' => $events diff --git a/src/Controller/TransactionController.php b/src/Controller/TransactionController.php index 2a08ff5..0bbb91c 100644 --- a/src/Controller/TransactionController.php +++ b/src/Controller/TransactionController.php @@ -84,19 +84,18 @@ class TransactionController implements IAuthenticationRequired, ISecured $numberOfTransactions = $event ? $this->transactionRepository->countAllByEvent($event) : $this->transactionRepository->countAllByCommunity($this->community); - $pages = ceil($numberOfTransactions / $itemsPerPage); - $currentPage = Container::$request->query('page') ?: 0; + $currentPage = Container::$request->query('page') ?: 1; $transactions = $event ? $this->transactionRepository->getPagedByEvent( $event, - $currentPage * $itemsPerPage, + $currentPage, $itemsPerPage, true, ['currency', 'payer_user', 'payee_user'] ) : $this->transactionRepository->getPagedByCommunity( $this->community, - $currentPage * $itemsPerPage, + $currentPage, $itemsPerPage, true, ['event', 'currency', 'payer_user', 'payee_user'] @@ -106,7 +105,7 @@ class TransactionController implements IAuthenticationRequired, ISecured 'community' => $this->community, 'event' => $event, 'exchangeRateCalculator' => $exchangeRateCalculator, - 'pages' => $pages, + 'pages' => ceil($numberOfTransactions / $itemsPerPage), 'currentPage' => $currentPage, 'numberOfTransactions' => $numberOfTransactions, 'transactions' => $transactions