Skip to content

Commit

Permalink
Properly apply given default sort
Browse files Browse the repository at this point in the history
CompatController::createSortControl():
- Set Default before assembling to set the correct value for the sort dropdown.
- Throw error if 3rd param is given but is not present in $columns

SortControl::apply():
- Remove unused param and code.
  • Loading branch information
sukhwinder33445 committed Jan 20, 2025
1 parent 003c51b commit ee5ece1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Html\ValidHtml;
use ipl\Orm\Common\SortUtil;
use ipl\Orm\Query;
use ipl\Stdlib\Contract\Paginatable;
use ipl\Web\Control\LimitControl;
Expand Down Expand Up @@ -294,15 +295,25 @@ public function createSortControl(Query $query, array $columns): SortControl

$this->params->shift($sortControl->getSortParam());

$sortControl->handleRequest($this->getServerRequest());

$defaultSort = null;

if (func_num_args() === 3) {
$defaultSort = func_get_args()[2];
}

return $sortControl->apply($query, $defaultSort);
$default = $defaultSort ?? $query->getModel()->getDefaultSort();
if (! empty($default)) {
$sortControl->setDefault(SortUtil::normalizeSortSpec($default));

Check failure on line 305 in src/Compat/CompatController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $sort of static method ipl\Orm\Common\SortUtil::normalizeSortSpec() expects array|string, mixed given.

Check failure on line 305 in src/Compat/CompatController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $sort of static method ipl\Orm\Common\SortUtil::normalizeSortSpec() expects array|string, mixed given.

$columns = $sortControl->getColumns();
$default = $sortControl->getDefault();
if (! empty($defaultSort) && ! isset($columns[$default])) {
throw new InvalidArgumentException(sprintf('Invalid default sort "%s" given', $default));
}
}

$sortControl->handleRequest($this->getServerRequest());

return $sortControl->apply($query);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Control/SortControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,14 @@ public function getSort(): ?string
*
* @return $this
*/
public function apply(Query $query, $defaultSort = null): self
public function apply(Query $query): self
{
if ($this->getRequest() === null) {
// handleRequest() has not been called yet
// TODO: Remove this once everything using this requires ipl v0.12.0
$this->handleRequest(ServerRequest::fromGlobals());
}

$default = $defaultSort ?? (array) $query->getModel()->getDefaultSort();
if (! empty($default)) {
$this->setDefault(SortUtil::normalizeSortSpec($default));
}

$sort = $this->getSort();
if (! empty($sort)) {
$query->orderBy(SortUtil::createOrderBy($sort));
Expand Down

0 comments on commit ee5ece1

Please sign in to comment.