Skip to content

Commit

Permalink
Merge pull request #36 from lachieh/pagination-update
Browse files Browse the repository at this point in the history
Use framework paginator for better support of magic pagination
  • Loading branch information
edalzell authored Mar 22, 2020
2 parents a8a69cc + 62c8c54 commit 1e5eda0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Events/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public function all($from, $to)
return carbon($event->start_date)->setTimeFromTimeString($event->startTime());
})->values();
}

public function count()
{
return $this->events->count();
}
}
18 changes: 14 additions & 4 deletions Events/EventsTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Statamic\API\Request;
use Spatie\CalendarLinks\Link;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Statamic\Addons\Events\EventFactory;
use Statamic\Presenters\PaginationPresenter;
use Statamic\Addons\Collection\CollectionTags;
use Illuminate\Pagination\LengthAwarePaginator;

class EventsTags extends CollectionTags
{
Expand Down Expand Up @@ -119,8 +120,11 @@ protected function paginate()

$events = $this->events->upcoming($this->limit + 1, $this->offset);

$paginator = new Paginator(
$count = $this->events->count();

$paginator = new LengthAwarePaginator(
$events,
$count,
$this->limit,
$page
);
Expand All @@ -129,8 +133,14 @@ protected function paginate()
$paginator->appends(Request::all());

$this->paginationData = [
'prev_page' => $paginator->previousPageUrl(),
'next_page' => $paginator->nextPageUrl(),
'total_items' => $count,
'items_per_page' => $this->limit,
'total_pages' => $paginator->lastPage(),
'current_page' => $paginator->currentPage(),
'prev_page' => $paginator->previousPageUrl(),
'next_page' => $paginator->nextPageUrl(),
'auto_links' => $paginator->render(),
'links' => $paginator->render(new PaginationPresenter($paginator))
];

$this->dates = $events->slice(0, $this->limit);
Expand Down

0 comments on commit 1e5eda0

Please sign in to comment.