Skip to content

Commit

Permalink
#50 - Fix canoncial url
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed Mar 1, 2019
1 parent 47b5411 commit 2c1b709
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions code/site/components/com_pages/dispatcher/router/resolver/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,11 @@ public function resolve(ComPagesDispatcherRouterInterface $router)

if($page = $this->getObject('page.registry')->getPage($path))
{
$query = array();
if($collection = $page->isCollection())
{
//Set collection states
if(isset($collection['state']))
{
//Handle pagination
if (isset($collection['state']['limit']) && isset($request->query['page']))
{
$limit = $collection['state']['limit'];
$page = $request->query['page'] - 1;

$query['offset'] = $page * $limit;

unset($query['page']);
}
}

//Set the params in the query overwriting existing values
foreach($query as $key => $value) {
$request->query->set($key, $value);
if(isset($collection['state']) && isset($collection['state']['limit'])) {
$this->_resolvePagination($request, $collection['state']['limit']);
}
}
}
Expand All @@ -58,7 +42,15 @@ public function resolve(ComPagesDispatcherRouterInterface $router)
if($routes = $this->getObject('page.registry')->getRoutes($route->getPath()))
{
//Build the route
$url = $this->buildRoute($routes[0], $route->getQuery(true));
$url = $this->buildRoute($routes[0], $request->query->toArray());

if($collection = $page->isCollection())
{
//Handle pagination
if(isset($collection['state']) && isset($collection['state']['limit'])) {
$this->_generatePagination($url, $collection['state']['limit']);
}
}

//Qualify the url
$url = $router->qualifyUrl($url);
Expand Down Expand Up @@ -86,20 +78,36 @@ public function generate($page, array $query, ComPagesDispatcherRouterInterface
$url->query = array_diff_key($url->query, $collection['state']);

//Handle pagination
if(isset($collection['state']['limit']) && isset($url->query['offset']))
{
if($offset = $url->query['offset'])
{
$limit = $collection['state']['limit'];
$url->query['page'] = $offset/$limit + 1;
}

unset($url->query['offset']);
if(isset($collection['state']['limit'])) {
$this->_generatePagination($url, $collection['state']['limit']);
}
}
}
}

return $url;
}

protected function _resolvePagination(KDispatcherRequestInterface $request, $limit)
{
if(isset($request->query['page']))
{
$page = $request->query['page'] - 1;
$request->query['offset'] = $page * $limit;

unset($request->query['page']);
}
}

protected function _generatePagination(KHttpUrlInterface $url, $limit)
{
if(isset($url->query['offset']))
{
if($offset = $url->query['offset']) {
$url->query['page'] = $offset/$limit + 1;
}

unset($url->query['offset']);
}
}
}

0 comments on commit 2c1b709

Please sign in to comment.