Skip to content

Commit

Permalink
SortControl: Still load a request's sort rule in getSort()
Browse files Browse the repository at this point in the history
Icinga Cube does not even use `CompatController::createSortControl()`
or a custom override like Icinga Web Graphite Integration. Therefore,
unless it calls handleRequest(), we still need to load the sort rule
from the request in getSort() directly.

Should be easily reverted.
  • Loading branch information
nilmerg committed Aug 3, 2023
1 parent 4239a3d commit 981f105
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Control/SortControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ipl\Orm\Query;
use ipl\Stdlib\Str;
use ipl\Web\Common\FormUid;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
use Psr\Http\Message\ServerRequestInterface;

Expand All @@ -29,6 +30,13 @@ class SortControl extends Form
/** @var string Name of the URL parameter which stores the sort column */
protected $sortParam = self::DEFAULT_SORT_PARAM;

/**
* @var Url Request URL
* @deprecated Access {@see self::getRequest()} instead.
* @todo Remove once cube calls {@see self::handleRequest()}.
*/
protected $url;

/** @var array Possible sort columns as sort string-value pairs */
private $columns;

Expand All @@ -41,12 +49,14 @@ class SortControl extends Form
* Create a new sort control
*
* @param array $columns Possible sort columns
* @param Url $url Request URL
*
* @internal Use {@see self::create()} instead.
*/
private function __construct(array $columns)
private function __construct(array $columns, Url $url)
{
$this->setColumns($columns);
$this->url = $url;
}

/**
Expand All @@ -63,7 +73,7 @@ public static function create(array $options)
$normalized[SortUtil::normalizeSortSpec($spec)] = $label;
}

$self = new static($normalized);
$self = new static($normalized, Url::fromRequest());

$self->on(self::ON_REQUEST, function (ServerRequestInterface $request) use ($self) {
if ($self->getMethod() === 'POST' && $request->getMethod() === 'GET') {
Expand Down Expand Up @@ -158,7 +168,11 @@ public function setSortParam(string $sortParam): self
*/
public function getSort(): ?string
{
$sort = $this->getPopulatedValue($this->getSortParam(), $this->getDefault());
if ($this->getRequest() === null) {
$sort = $this->url->getParam($this->getSortParam(), $this->getDefault());
} else {
$sort = $this->getPopulatedValue($this->getSortParam(), $this->getDefault());
}

if (! empty($sort)) {
$columns = $this->getColumns();
Expand Down

0 comments on commit 981f105

Please sign in to comment.