Skip to content

Commit

Permalink
Opportunity to change parameter whith we get current page
Browse files Browse the repository at this point in the history
  • Loading branch information
MashinaMashina committed Jan 14, 2020
1 parent 88e3d78 commit df56424
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
2 changes: 1 addition & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ public function chunk(int $size, Closure $userFunc)
*/
public function paginate(int $perPage = 20, string $group = 'default', int $page = 0)
{
$pager = \Config\Services::pager();
$pager = \Config\Services::pager(null, null, false);
$page = $page >= 1 ? $page : $pager->getCurrentPage($group);

$total = $this->countAllResults(false);
Expand Down
65 changes: 50 additions & 15 deletions system/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ public function setPath(string $path, string $group = 'default')
return $this;
}

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

/**
* Sets the $_GET parameter from which we take the page number
*
* @param string $pageSelector
* @param string $group
*
* @return mixed
*/
public function setPageSelector(string $pageSelector, string $group = 'default')
{
$this->ensureGroup($group);

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

$this->calculateCurrentPage($group);

return $this;
}

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

/**
Expand Down Expand Up @@ -348,7 +369,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re
}
else
{
$uri->addQuery('page', $page);
$uri->addQuery($this->groups[$group]['pageSelector'], $page);
}

if ($this->only)
Expand All @@ -357,7 +378,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re

if (! $segment)
{
$query['page'] = $page;
$query[$this->groups[$group]['pageSelector']] = $page;
}

$uri->setQueryArray($query);
Expand Down Expand Up @@ -503,13 +524,31 @@ protected function ensureGroup(string $group)
}

$this->groups[$group] = [
'uri' => clone Services::request()->uri,
'hasMore' => false,
'total' => null,
'perPage' => $this->config->perPage,
'pageCount' => 1,
'uri' => clone Services::request()->uri,
'hasMore' => false,
'total' => null,
'perPage' => $this->config->perPage,
'pageCount' => 1,
'pageSelector' => $group === 'default' ? 'page' : 'page_' . $group,
];

$this->calculateCurrentPage($group);

if ($_GET)
{
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
}
}

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

/**
* Calculating the current page
*
* @param string $group
*/
protected function calculateCurrentPage(string $group)
{
if (array_key_exists($group, $this->segment))
{
try
Expand All @@ -523,15 +562,11 @@ protected function ensureGroup(string $group)
}
else
{
$page = $_GET['page_' . $group] ?? $_GET['page'] ?? 1;
$page = intval($page);

$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
}
$pageSelector = $this->groups[$group]['pageSelector'];

if ($_GET)
{
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
$page = (int) $_GET[$pageSelector] ?? 1;

$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
}
}

Expand Down
35 changes: 21 additions & 14 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ class PagerRenderer
* @var integer
*/
protected $segment;

/**
* Name of $_GET parameter
*
* @var integer
*/
protected $pageSelector;

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

/**
Expand All @@ -103,13 +109,14 @@ class PagerRenderer
*/
public function __construct(array $details)
{
$this->first = 1;
$this->last = $details['pageCount'];
$this->current = $details['currentPage'];
$this->total = $details['total'];
$this->uri = $details['uri'];
$this->pageCount = $details['pageCount'];
$this->segment = $details['segment'] ?? 0;
$this->first = 1;
$this->last = $details['pageCount'];
$this->current = $details['currentPage'];
$this->total = $details['total'];
$this->uri = $details['uri'];
$this->pageCount = $details['pageCount'];
$this->segment = $details['segment'] ?? 0;
$this->pageSelector = $details['pageSelector'] ?? 'page';
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -164,7 +171,7 @@ public function getPrevious()

if ($this->segment === 0)
{
$uri->addQuery('page', $this->first - 1);
$uri->addQuery($this->pageSelector, $this->first - 1);
}
else
{
Expand Down Expand Up @@ -208,7 +215,7 @@ public function getNext()

if ($this->segment === 0)
{
$uri->addQuery('page', $this->last + 1);
$uri->addQuery($this->pageSelector, $this->last + 1);
}
else
{
Expand All @@ -231,7 +238,7 @@ public function getFirst(): string

if ($this->segment === 0)
{
$uri->addQuery('page', 1);
$uri->addQuery($this->pageSelector, 1);
}
else
{
Expand All @@ -254,7 +261,7 @@ public function getLast(): string

if ($this->segment === 0)
{
$uri->addQuery('page', $this->pageCount);
$uri->addQuery($this->pageSelector, $this->pageCount);
}
else
{
Expand All @@ -277,7 +284,7 @@ public function getCurrent(): string

if ($this->segment === 0)
{
$uri->addQuery('page', $this->current);
$uri->addQuery($this->pageSelector, $this->current);
}
else
{
Expand Down Expand Up @@ -306,7 +313,7 @@ public function links(): array
for ($i = $this->first; $i <= $this->last; $i ++)
{
$links[] = [
'uri' => (string) ($this->segment === 0 ? $uri->addQuery('page', $i) : $uri->setSegment($this->segment, $i)),
'uri' => (string) ($this->segment === 0 ? $uri->addQuery($this->pageSelector, $i) : $uri->setSegment($this->segment, $i)),
'title' => (int) $i,
'active' => ($i === $this->current),
];
Expand Down

0 comments on commit df56424

Please sign in to comment.