Skip to content

Commit

Permalink
SearchControls: Validate filter columns more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Jun 8, 2022
1 parent 4f04bda commit b4a4b90
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Compat/SearchControls.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Web\Compat;

use GuzzleHttp\Psr7\ServerRequest;
use ipl\Orm\Exception\InvalidRelationException;
use ipl\Orm\Query;
use ipl\Stdlib\Seq;
use ipl\Web\Control\SearchBar;
Expand All @@ -14,13 +15,13 @@
trait SearchControls
{
/**
* Fetch meta-data for the given query
* Fetch available filter columns for the given query
*
* @param Query $query
*
* @return array
* @return array<string, string> Keys are column paths, values are labels
*/
abstract public function fetchMetaData(Query $query);
abstract public function fetchFilterColumns(Query $query);

/**
* Get whether {@see SearchControls::createSearchBar()} and {@see SearchControls::createSearchEditor()}
Expand Down Expand Up @@ -82,23 +83,27 @@ public function createSearchBar(Query $query, ...$params): SearchBar
)->setParams($redirectUrl->getParams()));
}

$metaData = $this->fetchMetaData($query);
$columnValidator = function (SearchBar\ValidatedColumn $column) use ($query, $metaData) {
$filterColumns = $this->fetchFilterColumns($query);
$columnValidator = function (SearchBar\ValidatedColumn $column) use ($query, $filterColumns) {
$columnPath = $column->getSearchValue();
if (strpos($columnPath, '.') === false) {
$columnPath = $query->getResolver()->qualifyPath($columnPath, $query->getModel()->getTableName());
}

if (! isset($metaData[$columnPath])) {
list($columnPath, $columnLabel) = Seq::find($metaData, $column->getSearchValue(), false);
try {
$definition = $query->getResolver()->getColumnDefinition($columnPath);
} catch (InvalidRelationException $_) {
list($columnPath, $columnLabel) = Seq::find($filterColumns, $column->getSearchValue(), false);
if ($columnPath === null) {
$column->setMessage(t('Is not a valid column'));
} else {
$column->setSearchValue($columnPath);
$column->setLabel($columnLabel);
}
} else {
$column->setLabel($metaData[$columnPath]);
}

if (isset($definition)) {
$column->setLabel($definition->getLabel());
}
};

Expand Down Expand Up @@ -175,11 +180,11 @@ public function createSearchEditor(Query $query, ...$params): SearchEditor
}
});

$metaData = $this->fetchMetaData($query);
$editor->on(SearchEditor::ON_VALIDATE_COLUMN, function (Filter\Condition $condition) use ($query, $metaData) {
$filterColumns = $this->fetchFilterColumns($query);
$editor->on(SearchEditor::ON_VALIDATE_COLUMN, function (Filter\Condition $condition) use ($query, $filterColumns) {
$column = $condition->getColumn();
if (! isset($metaData[$column])) {
$path = Seq::findKey($metaData, $condition->metaData()->get('columnLabel', $column), false);
if ($query->getResolver()->getColumnDefinition($column)->getLabel() === null) {
$path = Seq::findKey($filterColumns, $condition->metaData()->get('columnLabel', $column), false);
if ($path === null) {
throw new SearchBar\SearchException(t('Is not a valid column'));
} else {
Expand Down Expand Up @@ -228,9 +233,9 @@ protected function enrichFilterCondition(Filter\Condition $condition, Query $que
$condition->setColumn($path);
}

$metaData = $this->fetchMetaData($query);
if (isset($metaData[$path])) {
$condition->metaData()->set('columnLabel', $metaData[$path]);
$definition = $query->getResolver()->getColumnDefinition($path);
if ($definition->getLabel() !== null) {
$condition->metaData()->set('columnLabel', $definition->getLabel());
}
}
}

0 comments on commit b4a4b90

Please sign in to comment.