Skip to content

Commit

Permalink
[4.4] Check pagination parameters from request, another try (#44023)
Browse files Browse the repository at this point in the history
* Check pagination parameters from request

* cs

* only for CMS router

* paramsFromRequest list

---------
  • Loading branch information
Fedik authored Sep 23, 2024
1 parent 4c9786f commit d4a1f41
Showing 1 changed file with 60 additions and 28 deletions.
88 changes: 60 additions & 28 deletions libraries/src/Pagination/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Router\Route;
use Joomla\Filter\InputFilter;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
Expand Down Expand Up @@ -96,6 +97,31 @@ class Pagination
*/
protected $additionalUrlParams = [];

/**
* List of parameters that will be added from request automatically.
* When exists they will be added to the $additionalUrlParams list, while pagination initialisation.
*
* In format key => filter
*
* @var string[]
*
* @since __DEPLOY_VERSION__
*/
protected $paramsFromRequest = [
'format' => 'CMD',
'option' => 'CMD',
'controller' => 'CMD',
'view' => 'CMD',
'layout' => 'STRING',
'task' => 'CMD',
'template' => 'CMD',
'templateStyle' => 'INT',
'tmpl' => 'CMD',
'tpl' => 'CMD',
'id' => 'STRING',
'Itemid' => 'INT',
];

/**
* @var CMSApplication The application object
* @since 3.4
Expand Down Expand Up @@ -177,6 +203,40 @@ public function __construct($total, $limitstart, $limit, $prefix = '', CMSApplic
if ($limit === 0) {
$this->viewall = true;
}

$this->setUrlParamsFromRequest();
}

/**
* Set URL parameters from request.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function setUrlParamsFromRequest()
{
// Get the requested parameters from the router
$client = $this->app->getName();
$router = Factory::getContainer()->get(ucfirst($client) . 'Router');
$filter = new InputFilter();

// It is applicable only for CMS router. API router works differently.
if (!$router instanceof \Joomla\CMS\Router\Router) {
return;
}

// Filter them and add to the params list
foreach ($router->getVars() as $key => $value) {
// Check if the parameter is allowed
if (empty($this->paramsFromRequest[$key])) {
continue;
}

$filterMethod = $this->paramsFromRequest[$key];

$this->setAdditionalUrlParam($key, $filter->clean($value, $filterMethod));
}
}

/**
Expand Down Expand Up @@ -663,36 +723,8 @@ protected function _buildDataObject()
{
$data = new \stdClass();

// Platform defaults
$defaultUrlParams = [
'format' => 'CMD',
'option' => 'CMD',
'controller' => 'CMD',
'view' => 'CMD',
'layout' => 'STRING',
'task' => 'CMD',
'template' => 'CMD',
'templateStyle' => 'INT',
'tmpl' => 'CMD',
'tpl' => 'CMD',
'id' => 'STRING',
'Itemid' => 'INT',
];

// Prepare the routes
$params = [];
$input = $this->app->getInput();

// Use platform defaults if parameter doesn't already exist.
foreach ($defaultUrlParams as $param => $filter) {
$value = $input->get($param, null, $filter);

if ($value === null) {
continue;
}

$params[$param] = $value;
}

if (!empty($this->additionalUrlParams)) {
foreach ($this->additionalUrlParams as $key => $value) {
Expand Down

0 comments on commit d4a1f41

Please sign in to comment.