This commit is contained in:
Loïc Guibert
2022-09-30 20:02:02 +01:00
commit 66dafc36c3
2561 changed files with 454489 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
<?php
namespace Grav\Plugin\Pagination;
use Grav\Common\Grav;
use Grav\Common\Iterator;
use Grav\Common\Page\Collection;
use Grav\Common\Uri;
class PaginationHelper extends Iterator
{
protected $current;
protected $items_per_page;
protected $page_count;
protected $url_params;
/**
* Create and initialize pagination.
*
* @param Collection $collection
*/
public function __construct(Collection $collection)
{
parent::__construct();
$grav = Grav::instance();
/** @var Uri $uri */
$uri = $grav['uri'];
$config = $grav['config'];
$this->current = $uri->currentPage();
// get params
$url_params = explode('/', ltrim($uri->params(), '/'));
$params = $collection->params();
foreach ($url_params as $key => $value) {
if (strpos($value, 'page' . $config->get('system.param_sep')) !== false) {
unset($url_params[$key]);
}
if (isset($params['ignore_url_params'])) {
foreach ((array)$params['ignore_params'] as $ignore_param) {
if (strpos($value, $ignore_param . $config->get('system.param_sep')) !== false) {
unset($url_params[$key]);
}
}
}
}
$this->url_params = '/'.implode('/', $url_params);
// check for empty params
if ($this->url_params === '/') {
$this->url_params = '';
}
$this->items_per_page = $params['limit'];
$this->page_count = ceil($collection->count() / $this->items_per_page);
for ($x=1; $x <= $this->page_count; $x++) {
if ($x === 1) {
$this->items[$x] = new PaginationPage($x, '');
} else {
$this->items[$x] = new PaginationPage($x, '/page' . $config->get('system.param_sep') . $x);
}
}
}
/**
* Returns true if current item has previous sibling.
*
* @return bool
*/
public function hasPrev()
{
if (array_key_exists($this->current -1, $this->items)) {
return true;
}
return false;
}
/**
* Returns true if current item has next sibling.
*
* @return bool
*/
public function hasNext()
{
if (array_key_exists($this->current +1, $this->items)) {
return true;
}
return false;
}
/**
* Return previous url.
*
* @return string|null
*/
public function prevUrl()
{
if (array_key_exists($this->current -1, $this->items)) {
return $this->items[$this->current -1]->url;
}
return null;
}
/**
* Return next url.
*
* @return string|null
*/
public function nextUrl()
{
if (array_key_exists($this->current +1, $this->items)) {
return $this->items[$this->current +1]->url;
}
return null;
}
public function params()
{
return $this->url_params;
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Grav\Plugin\Pagination;
use Grav\Common\Grav;
class PaginationPage
{
/**
* @var Grav
*/
protected $grav;
/**
* @var int
*/
public $number;
/**
* @var string
*/
public $url;
/**
* @var int
*/
protected $delta=0;
/**
* Constructor
*
* @param int $number
* @param string $url
*/
public function __construct($number, $url)
{
$this->grav = Grav::instance();
$this->number = $number;
$this->url = $url;
$this->delta = $this->grav['config']->get('plugins.pagination.delta');
}
/**
* Returns true if the page is the current one.
*
* @return bool
*/
public function isCurrent()
{
if ($this->grav['uri']->currentPage() == $this->number) {
return true;
}
return false;
}
/**
* Returns true if the page is within a configurable delta of the current one
*
* @return bool
*/
public function isInDelta()
{
if (!$this->delta) {
return true;
}
return abs($this->grav['uri']->currentPage() - $this->number) < $this->delta;
}
/**
* Returns true is this page is the last/first one at the border of the delta range
* (Used to display a "gap" li element ...)
*
* @return bool
*/
public function isDeltaBorder()
{
if (!$this->delta) {
return false;
}
return abs($this->grav['uri']->currentPage() - $this->number) == $this->delta;
}
}