Compare commits

...

4 Commits

Author SHA1 Message Date
d1c926541a
RVRNEXT-22 use the new repository methods in controllers
All checks were successful
rvr-nextgen/pipeline/pr-master This commit looks good
2023-05-28 20:34:07 +02:00
9929e00a3f
RVRNEXT-22 use Select::paginate in repositories 2023-05-28 20:34:07 +02:00
bf415a2a46
RVRNEXT-22 use pagination template 2023-05-28 20:34:00 +02:00
c0bfa5b334
RVRNEXT-22 add pagination template 2023-05-28 20:33:59 +02:00
7 changed files with 58 additions and 67 deletions

View File

@ -68,17 +68,16 @@ class EventController implements IAuthenticationRequired, ISecured
{ {
$itemsPerPage = 10; $itemsPerPage = 10;
$numberOfEvents = $this->eventRepository->countAllByCommunity($this->community); $numberOfEvents = $this->eventRepository->countAllByCommunity($this->community);
$pages = ceil($numberOfEvents / $itemsPerPage); $currentPage = Container::$request->query('page') ?: 1;
$currentPage = Container::$request->query('page') ?: 0;
$events = $this->eventRepository->getPagedByCommunity( $events = $this->eventRepository->getPagedByCommunity(
$this->community, $this->community,
$currentPage * $itemsPerPage, $currentPage,
$itemsPerPage $itemsPerPage
); );
return new HtmlContent('events/events', [ return new HtmlContent('events/events', [
'community' => $this->community, 'community' => $this->community,
'pages' => $pages, 'pages' => ceil($numberOfEvents / $itemsPerPage),
'currentPage' => $currentPage, 'currentPage' => $currentPage,
'numberOfEvents' => $numberOfEvents, 'numberOfEvents' => $numberOfEvents,
'events' => $events 'events' => $events

View File

@ -84,19 +84,18 @@ class TransactionController implements IAuthenticationRequired, ISecured
$numberOfTransactions = $event ? $numberOfTransactions = $event ?
$this->transactionRepository->countAllByEvent($event) : $this->transactionRepository->countAllByEvent($event) :
$this->transactionRepository->countAllByCommunity($this->community); $this->transactionRepository->countAllByCommunity($this->community);
$pages = ceil($numberOfTransactions / $itemsPerPage); $currentPage = Container::$request->query('page') ?: 1;
$currentPage = Container::$request->query('page') ?: 0;
$transactions = $event ? $transactions = $event ?
$this->transactionRepository->getPagedByEvent( $this->transactionRepository->getPagedByEvent(
$event, $event,
$currentPage * $itemsPerPage, $currentPage,
$itemsPerPage, $itemsPerPage,
true, true,
['currency', 'payer_user', 'payee_user'] ['currency', 'payer_user', 'payee_user']
) : ) :
$this->transactionRepository->getPagedByCommunity( $this->transactionRepository->getPagedByCommunity(
$this->community, $this->community,
$currentPage * $itemsPerPage, $currentPage,
$itemsPerPage, $itemsPerPage,
true, true,
['event', 'currency', 'payer_user', 'payee_user'] ['event', 'currency', 'payer_user', 'payee_user']
@ -106,7 +105,7 @@ class TransactionController implements IAuthenticationRequired, ISecured
'community' => $this->community, 'community' => $this->community,
'event' => $event, 'event' => $event,
'exchangeRateCalculator' => $exchangeRateCalculator, 'exchangeRateCalculator' => $exchangeRateCalculator,
'pages' => $pages, 'pages' => ceil($numberOfTransactions / $itemsPerPage),
'currentPage' => $currentPage, 'currentPage' => $currentPage,
'numberOfTransactions' => $numberOfTransactions, 'numberOfTransactions' => $numberOfTransactions,
'transactions' => $transactions 'transactions' => $transactions

View File

@ -52,11 +52,11 @@ class EventRepository
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); 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 = $this->selectAllByCommunity($community);
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->limit($limit, $start); $select->paginate($page, $itemsPerPage);
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
} }

View File

@ -66,20 +66,20 @@ class TransactionRepository
return $select->count() > 0; 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 = $this->selectAllByCommunity($community);
$select->orderBy('time', 'DESC'); $select->orderBy('time', 'DESC');
$select->limit($limit, $start); $select->paginate($page, $itemsPerPage);
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); 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 = $this->selectAllByEvent($event);
$select->orderBy('time', 'DESC'); $select->orderBy('time', 'DESC');
$select->limit($limit, $start); $select->paginate($page, $itemsPerPage);
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations);
} }

View File

@ -13,20 +13,13 @@
<p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p> <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p>
<?php if ($numberOfTransactions > 0): ?> <?php if ($numberOfTransactions > 0): ?>
<?php
$paginationRouteId = 'community.transactions';
$paginationRouteParams = ['communitySlug' => $community->getSlug()];
?>
<?php if ($pages > 1): ?> <?php if ($pages > 1): ?>
<p class="paginateContainer marginTop"> @include(templates/pagination)
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => 0]) ?>">«</a>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => max(0, $currentPage - 1)]) ?>"></a>
<?php for ($i = 0; $i < $pages; $i++): ?>
<?php if ($currentPage == $i): ?>
<span class="selected"><?= $i + 1 ?></span>
<?php else: ?>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $i]) ?>"><?= $i + 1 ?></a>
<?php endif; ?>
<?php endfor; ?>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => min($pages - 1, $currentPage + 1)]) ?>"></a>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $pages - 1]) ?>">»</a>
</p>
<?php endif; ?> <?php endif; ?>
<?php foreach ($transactions as $transaction): ?> <?php foreach ($transactions as $transaction): ?>
@ -51,19 +44,7 @@
<?php endforeach; ?> <?php endforeach; ?>
<?php if ($pages > 1): ?> <?php if ($pages > 1): ?>
<p class="paginateContainer marginTop"> @include(templates/pagination)
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => 0]) ?>">«</a>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => max(0, $currentPage - 1)]) ?>"></a>
<?php for ($i = 0; $i < $pages; $i++): ?>
<?php if ($currentPage == $i): ?>
<span class="selected"><?= $i + 1 ?></span>
<?php else: ?>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $i]) ?>"><?= $i + 1 ?></a>
<?php endif; ?>
<?php endfor; ?>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => min($pages - 1, $currentPage + 1)]) ?>"></a>
<a href="<?= Container::$routeCollection->getRoute('community.transactions')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $pages - 1]) ?>">»</a>
</p>
<?php endif; ?> <?php endif; ?>
<p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p> <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.transactions.new')->generateLink(['communitySlug' => $community->getSlug(), 'event' => isset($event) ? $event->getSlug() : null]) ?>">New transaction</a></p>

View File

@ -9,20 +9,13 @@
<p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.events.new')->generateLink(['communitySlug' => $community->getSlug()]) ?>">New event</a></p> <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.events.new')->generateLink(['communitySlug' => $community->getSlug()]) ?>">New event</a></p>
<?php if ($numberOfEvents > 0): ?> <?php if ($numberOfEvents > 0): ?>
<?php
$paginationRouteId = 'community.events';
$paginationRouteParams = ['communitySlug' => $community->getSlug()];
?>
<?php if ($pages > 1): ?> <?php if ($pages > 1): ?>
<p class="paginateContainer marginTop"> @include(templates/pagination)
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => 0]) ?>">«</a>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => max(0, $currentPage - 1)]) ?>"></a>
<?php for ($i = 0; $i < $pages; $i++): ?>
<?php if ($currentPage == $i): ?>
<span class="selected"><?= $i + 1 ?></span>
<?php else: ?>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $i]) ?>"><?= $i + 1 ?></a>
<?php endif; ?>
<?php endfor; ?>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => min($pages - 1, $currentPage + 1)]) ?>"></a>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $pages - 1]) ?>">»</a>
</p>
<?php endif; ?> <?php endif; ?>
<?php foreach ($events as $event): ?> <?php foreach ($events as $event): ?>
@ -36,19 +29,7 @@
<?php endforeach; ?> <?php endforeach; ?>
<?php if ($pages > 1): ?> <?php if ($pages > 1): ?>
<p class="paginateContainer marginTop"> @include(templates/pagination)
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => 0]) ?>">«</a>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => max(0, $currentPage - 1)]) ?>"></a>
<?php for ($i = 0; $i < $pages; $i++): ?>
<?php if ($currentPage == $i): ?>
<span class="selected"><?= $i + 1 ?></span>
<?php else: ?>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $i]) ?>"><?= $i + 1 ?></a>
<?php endif; ?>
<?php endfor; ?>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => min($pages - 1, $currentPage + 1)]) ?>"></a>
<a href="<?= Container::$routeCollection->getRoute('community.events')->generateLink(['communitySlug' => $community->getSlug(), 'page' => $pages - 1]) ?>">»</a>
</p>
<?php endif; ?> <?php endif; ?>
<p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.events.new')->generateLink(['communitySlug' => $community->getSlug()]) ?>">New event</a></p> <p class="marginTop"><a href="<?= Container::$routeCollection->getRoute('community.events.new')->generateLink(['communitySlug' => $community->getSlug()]) ?>">New event</a></p>

View File

@ -0,0 +1,31 @@
<p class="paginateContainer marginTop">
<a href="<?= Container::$routeCollection->getRoute($paginationRouteId)->generateLink(array_merge($paginationRouteParams, ['page' => 1])) ?>">«</a>
<a href="<?= Container::$routeCollection->getRoute($paginationRouteId)->generateLink(array_merge($paginationRouteParams, ['page' => max(1, $currentPage - 1)])) ?>"></a>
<?php
$maxPages = 7;
if ($pages <= $maxPages) {
$start = 1;
$end = $pages;
} else {
$maxAdditionalPages = $maxPages - 1;
$maxAdditionalPagesHalf = ceil($maxAdditionalPages / 2);
$start = $currentPage - $maxAdditionalPagesHalf > 1 ?
($currentPage - $maxAdditionalPagesHalf < $pages - $maxAdditionalPages ?
$currentPage - $maxAdditionalPagesHalf :
$pages - $maxAdditionalPages) :
1;
$end = $start + $maxAdditionalPages < $pages ?
$start + $maxAdditionalPages :
$pages;
}
?>
<?php for ($i = $start; $i <= $end; $i++): ?>
<?php if ($currentPage == $i): ?>
<span class="selected"><?= $i ?></span>
<?php else: ?>
<a href="<?= Container::$routeCollection->getRoute($paginationRouteId)->generateLink(array_merge($paginationRouteParams, ['page' => $i])) ?>"><?= $i ?></a>
<?php endif; ?>
<?php endfor; ?>
<a href="<?= Container::$routeCollection->getRoute($paginationRouteId)->generateLink(array_merge($paginationRouteParams, ['page' => min($pages, $currentPage + 1)])) ?>"></a>
<a href="<?= Container::$routeCollection->getRoute($paginationRouteId)->generateLink(array_merge($paginationRouteParams, ['page' => $pages])) ?>">»</a>
</p>