Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI segment as page number in Pagination #1213

Merged
merged 11 commits into from
Oct 29, 2018
43 changes: 43 additions & 0 deletions system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,33 @@ public function getSegment(int $number): string
return $this->segments[$number] ?? '';
}


/**
* Set the value of a specific segment of the URI path.
* Allows to set only existing segments or add new one.
*
* @param int $number
* @param mixed $value (string or int)
*
* @return $this
*/
public function setSegment(int $number, $value)
{
// The segment should treat the array as 1-based for the user
// but we still have to deal with a zero-based array.
$number -= 1;

if ($number > count($this->segments) + 1)
{
throw HTTPException::forURISegmentOutOfRange($number);
}

$this->segments[$number] = $value;
$this->refreshPath();

return $this;
}

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -668,6 +695,22 @@ public function setPath(string $path)
return $this;
}

/**
* Sets the path portion of the URI based on segments.
*
* @param string $path
*
* @return $this
*/
public function refreshPath()
{
$this->path = $this->filterPath(implode('/', $this->segments));

$this->segments = explode('/', $this->path);

return $this;
}

//--------------------------------------------------------------------

/**
Expand Down
58 changes: 52 additions & 6 deletions system/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class Pager implements PagerInterface
*/
protected $groups = [];

/**
* URI segment for groups if provided.
*
* @var array
*/
protected $segment = [];

/**
* Our configuration instance.
*
Expand Down Expand Up @@ -132,14 +139,15 @@ public function simpleLinks(string $group = 'default', string $template = 'defau
* @param int $perPage
* @param int $total
* @param string $template The output template alias to render.
* @param int $segment (if page number is provided by URI segment)
*
* @return string
*/
public function makeLinks(int $page, int $perPage, int $total, string $template = 'default_full'): string
public function makeLinks(int $page, int $perPage, int $total, string $template = 'default_full', int $segment = 0): string
{
$name = time();

$this->store($name, $page, $perPage, $total);
$this->store($name, $page, $perPage, $total, $segment);

return $this->displayLinks($name, $template);
}
Expand Down Expand Up @@ -178,11 +186,14 @@ protected function displayLinks(string $group, string $template)
* @param int $page
* @param int $perPage
* @param int $total
* @param int $segment
*
* @return mixed
*/
public function store(string $group, int $page, int $perPage, int $total)
public function store(string $group, int $page, int $perPage, int $total, int $segment = 0)
{
$this->segment[$group] = $segment;

$this->ensureGroup($group);

$this->groups[$group]['currentPage'] = $page;
Expand Down Expand Up @@ -315,17 +326,36 @@ public function getPageURI(int $page = null, string $group = 'default', $returnO

$uri = $this->groups[$group]['uri'];


$segment = $this->segment[$group]??0;

if ($this->only)
{

$query = array_intersect_key($_GET, array_flip($this->only));

$query['page'] = $page;
if($segment > 0)
{
$uri->setSegment($segment, $page);
}
else
{
$query['page'] = $page;
}

$uri->setQueryArray($query);
}
else
{
$uri->addQuery('page', $page);
if($segment > 0)
{
$uri->setSegment($segment, $page);
}
else
{
$uri->addQuery('page', $page);
}

}

return $returnObject === true ? $uri : (string) $uri;
Expand Down Expand Up @@ -432,6 +462,7 @@ public function getDetails(string $group = 'default'): array

$newGroup['next'] = $this->getNextPageURI($group);
$newGroup['previous'] = $this->getPreviousPageURI($group);
$newGroup['segment'] = $this->segment[$group]??0;

return $newGroup;
}
Expand Down Expand Up @@ -470,11 +501,26 @@ protected function ensureGroup(string $group)
'uri' => clone Services::request()->uri,
'hasMore' => false,
'total' => null,
'currentPage' => $_GET['page_' . $group] ?? $_GET['page'] ?? 1,
'perPage' => $this->config->perPage,
'pageCount' => 1,
];

if(array_key_exists($group, $this->segment))
{
try
{
$this->groups[$group]['currentPage'] = $this->groups[$group]['uri']->getSegment($this->segment[$group]);
}
catch (\CodeIgniter\HTTP\Exceptions\HTTPException $e)
{
$this->groups[$group]['currentPage'] = 1;
}
}
else
{
$this->groups[$group]['currentPage'] = $_GET['page_' . $group] ?? $_GET['page'] ?? 1;
}

if ($_GET)
{
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
Expand Down
40 changes: 35 additions & 5 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PagerRenderer
protected $total;
protected $pageCount;
protected $uri;
protected $segment;

//--------------------------------------------------------------------

Expand All @@ -65,6 +66,7 @@ public function __construct(array $details)
$this->total = $details['total'];
$this->uri = $details['uri'];
$this->pageCount = $details['pageCount'];
$this->segment = $details['segment'] ?? 0;
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -117,7 +119,14 @@ public function getPrevious()

$uri = clone $this->uri;

$uri->addQuery('page', $this->first - 1);
if($this->segment == 0)
{
$uri->addQuery('page', $this->first - 1);
}
else
{
$uri->setSegment($this->segment, $this->first -1);
}

return (string) $uri;
}
Expand Down Expand Up @@ -154,7 +163,14 @@ public function getNext()

$uri = clone $this->uri;

$uri->addQuery('page', $this->last + 1);
if($this->segment == 0)
{
$uri->addQuery('page', $this->last + 1);
}
else
{
$uri->setSegment($this->segment, $this->last + 1);
}

return (string) $uri;
}
Expand All @@ -170,7 +186,14 @@ public function getFirst(): string
{
$uri = clone $this->uri;

$uri->addQuery('page', 1);
if($this->segment == 0)
{
$uri->addQuery('page', 1);
}
else
{
$uri->setSegment($this->segment, 1);
}

return (string) $uri;
}
Expand All @@ -186,7 +209,14 @@ public function getLast(): string
{
$uri = clone $this->uri;

$uri->addQuery('page', $this->pageCount);
if($this->segment == 0)
{
$uri->addQuery('page', $this->pageCount);
}
else
{
$uri->setSegment($this->segment, $this->pageCount);
}

return (string) $uri;
}
Expand All @@ -210,7 +240,7 @@ public function links(): array
for ($i = $this->first; $i <= $this->last; $i ++ )
{
$links[] = [
'uri' => (string) $uri->addQuery('page', $i),
'uri' => (string) ($this->segment == 0 ? $uri->addQuery('page', $i) : $uri->setSegment($this->segment, $i)),
'title' => (int) $i,
'active' => ($i == $this->current)
];
Expand Down
Loading