2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* PageIndex.php
|
|
|
|
* Contains key class PageIndex.
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class PageIndex
|
|
|
|
{
|
2021-02-17 16:41:58 +01:00
|
|
|
protected $base_url;
|
2017-11-05 16:27:12 +01:00
|
|
|
protected $current_page = 1;
|
2021-02-17 16:41:58 +01:00
|
|
|
protected $index_class = 'pagination';
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $items_per_page = 0;
|
2021-02-17 20:06:33 +01:00
|
|
|
private $linkBuilder;
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $needsPageIndex = false;
|
2017-11-05 16:27:12 +01:00
|
|
|
protected $num_pages = 1;
|
2021-02-17 16:41:58 +01:00
|
|
|
protected $page_index = [];
|
|
|
|
protected $page_slug = '%AMP%page=%PAGE%';
|
2016-09-01 23:13:23 +02:00
|
|
|
protected $recordCount = 0;
|
|
|
|
protected $sort_direction = null;
|
2021-02-17 16:41:58 +01:00
|
|
|
protected $sort_order = null;
|
|
|
|
protected $start = 0;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
public function __construct($options)
|
|
|
|
{
|
2021-02-17 16:41:58 +01:00
|
|
|
static $neededKeys = ['base_url', 'items_per_page', 'recordCount'];
|
|
|
|
foreach ($neededKeys as $key)
|
|
|
|
{
|
|
|
|
if (!isset($options[$key]))
|
|
|
|
throw new Exception('PageIndex: argument ' . $key . ' missing in options');
|
|
|
|
|
|
|
|
$this->$key = $options[$key];
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:06:33 +01:00
|
|
|
static $optionalKeys = ['index_class', 'linkBuilder', 'page_slug', 'sort_direction', 'sort_order', 'start'];
|
2021-02-17 16:41:58 +01:00
|
|
|
foreach ($optionalKeys as $key)
|
|
|
|
if (isset($options[$key]))
|
|
|
|
$this->$key = $options[$key];
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
$this->generatePageIndex();
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:06:33 +01:00
|
|
|
private function buildLink($start = null, $order = null, $dir = null)
|
|
|
|
{
|
|
|
|
if (isset($this->linkBuilder))
|
|
|
|
return call_user_func($this->linkBuilder, $start, $order, $dir);
|
|
|
|
else
|
|
|
|
return $this->getLink($start, $order, $dir);
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
protected function generatePageIndex()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Example 1:
|
|
|
|
[1] [2] [3] [...] [c-2] [c-1] [c] [c+1] [c+2] [...] [n-2] [n-1] [n]
|
|
|
|
\---------/ \-------------------------/ \-------------/
|
|
|
|
lower current/contiguous pages upper
|
|
|
|
|
|
|
|
Example 2:
|
|
|
|
[1] [2] [3] [4] [5] [c] [6] [7] [...] [n/2] [...] [n-2] [n-1] [n]
|
|
|
|
\---------/ \-----------------/ \---/ \-------------/
|
|
|
|
lower current/cont. pgs. center upper
|
|
|
|
*/
|
|
|
|
|
|
|
|
$this->num_pages = ceil($this->recordCount / $this->items_per_page);
|
|
|
|
$this->current_page = min(ceil($this->start / $this->items_per_page) + 1, $this->num_pages);
|
|
|
|
if ($this->num_pages == 0)
|
|
|
|
{
|
|
|
|
$this->needsPageIndex = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lowerLower = 1;
|
|
|
|
$lowerUpper = min($this->num_pages, 3);
|
|
|
|
|
|
|
|
$contigLower = max($lowerUpper + 1, max($this->current_page - 2, 1));
|
|
|
|
$contigUpper = min($this->current_page + 2, $this->num_pages);
|
|
|
|
|
|
|
|
$center = floor($this->num_pages / 2);
|
|
|
|
|
|
|
|
$upperLower = max($contigUpper + 1, max(0, $this->num_pages - 2));
|
|
|
|
$upperUpper = $this->num_pages;
|
|
|
|
|
|
|
|
$this->page_index = [];
|
|
|
|
|
|
|
|
// Lower pages
|
|
|
|
for ($p = $lowerLower; $p <= $lowerUpper; $p++)
|
|
|
|
$this->page_index[$p] = [
|
|
|
|
'index' => $p,
|
|
|
|
'is_selected' => $this->current_page == $p,
|
2021-02-17 20:06:33 +01:00
|
|
|
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// The center of the page index.
|
|
|
|
if ($center > $lowerUpper && $center < $contigLower)
|
|
|
|
{
|
|
|
|
// Gap?
|
|
|
|
if ($lowerUpper != $center)
|
|
|
|
$this->page_index[] = '...';
|
|
|
|
|
|
|
|
$this->page_index[$center] = [
|
|
|
|
'index' => $center,
|
|
|
|
'is_selected' => $this->current_page == $center,
|
2021-02-17 20:06:33 +01:00
|
|
|
'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gap?
|
|
|
|
if ($contigLower != $p)
|
|
|
|
$this->page_index[] = '...';
|
|
|
|
|
|
|
|
// contig pages
|
|
|
|
for ($p = $contigLower; $p <= $contigUpper; $p++)
|
|
|
|
$this->page_index[$p] = [
|
|
|
|
'index' => $p,
|
|
|
|
'is_selected' => $this->current_page == $p,
|
2021-02-17 20:06:33 +01:00
|
|
|
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// The center of the page index.
|
|
|
|
if ($center > $contigUpper && $center < $upperLower)
|
|
|
|
{
|
|
|
|
// Gap?
|
|
|
|
if ($contigUpper != $center)
|
|
|
|
$this->page_index[] = '...';
|
|
|
|
|
|
|
|
$this->page_index[$center] = [
|
|
|
|
'index' => $center,
|
|
|
|
'is_selected' => $this->current_page == $center,
|
2021-02-17 20:06:33 +01:00
|
|
|
'href'=> $this->buildLink(($center - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gap?
|
|
|
|
if ($upperLower != $p)
|
|
|
|
$this->page_index[] = '...';
|
|
|
|
|
|
|
|
// Upper pages
|
|
|
|
for ($p = $upperLower; $p <= $upperUpper; $p++)
|
|
|
|
$this->page_index[$p] = [
|
|
|
|
'index' => $p,
|
|
|
|
'is_selected' => $this->current_page == $p,
|
2021-02-17 20:06:33 +01:00
|
|
|
'href'=> $this->buildLink(($p - 1) * $this->items_per_page, $this->sort_order, $this->sort_direction),
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// Previous page?
|
|
|
|
if ($this->current_page > 1)
|
|
|
|
$this->page_index['previous'] = $this->page_index[$this->current_page - 1];
|
|
|
|
|
|
|
|
// Next page?
|
|
|
|
if ($this->current_page < $this->num_pages)
|
|
|
|
$this->page_index['next'] = $this->page_index[$this->current_page + 1];
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:19:47 +02:00
|
|
|
public function getLink($start = null, $order = null, $dir = null)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
$url = $this->base_url;
|
|
|
|
$amp = strpos($this->base_url, '?') ? '&' : '?';
|
|
|
|
|
|
|
|
if (!empty($start))
|
|
|
|
{
|
2016-09-02 14:19:47 +02:00
|
|
|
$page = $start !== '%d' ? ($start / $this->items_per_page) + 1 : $start;
|
2016-09-01 23:13:23 +02:00
|
|
|
$url .= strtr($this->page_slug, ['%PAGE%' => $page, '%AMP%' => $amp]);
|
|
|
|
$amp = '&';
|
|
|
|
}
|
|
|
|
if (!empty($order))
|
|
|
|
{
|
|
|
|
$url .= $amp . 'order=' . $order;
|
|
|
|
$amp = '&';
|
|
|
|
}
|
|
|
|
if (!empty($dir))
|
|
|
|
{
|
|
|
|
$url .= $amp . 'dir=' . $dir;
|
|
|
|
$amp = '&';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPageIndex()
|
|
|
|
{
|
|
|
|
return $this->page_index;
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:19:47 +02:00
|
|
|
public function getItemsPerPage()
|
|
|
|
{
|
|
|
|
return $this->items_per_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSortOrder()
|
|
|
|
{
|
|
|
|
return $this->sort_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSortDirection()
|
|
|
|
{
|
|
|
|
return $this->sort_direction;
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function getPageIndexClass()
|
|
|
|
{
|
|
|
|
return $this->index_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCurrentPage()
|
|
|
|
{
|
|
|
|
return $this->current_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNumberOfPages()
|
|
|
|
{
|
|
|
|
return $this->num_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecordCount()
|
|
|
|
{
|
|
|
|
return $this->recordCount;
|
|
|
|
}
|
|
|
|
}
|