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

[4.4] Check pagination parameters from request, another try #44023

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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