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 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); } 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

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; + } + ?> + + + + + + + + + » +