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

SearchControls: Validate filter columns more efficiently #83

Merged
merged 2 commits into from
Jun 9, 2022
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
72 changes: 50 additions & 22 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,31 @@ public function createSearchBar(Query $query, ...$params): SearchBar
)->setParams($redirectUrl->getParams()));
}

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

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

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

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

$metaData = $this->fetchMetaData($query);
$editor->on(SearchEditor::ON_VALIDATE_COLUMN, function (Filter\Condition $condition) use ($query, $metaData) {
$column = $condition->getColumn();
if (! isset($metaData[$column])) {
$path = Seq::findKey($metaData, $condition->metaData()->get('columnLabel', $column), false);
if ($path === null) {
$filterColumns = $this->fetchFilterColumns($query);
$editor->on(SearchEditor::ON_VALIDATE_COLUMN, function (Filter\Condition $condition) use ($query, $filterColumns) {
$searchPath = $condition->getColumn();
if (strpos($searchPath, '.') === false) {
$condition->setColumn($query->getResolver()->qualifyPath(
$searchPath,
$query->getModel()->getTableName()
));
}

try {
$query->getResolver()->getColumnDefinition($searchPath);
} catch (InvalidRelationException $_) {
$columnPath = Seq::findKey(
$filterColumns,
$condition->metaData()->get('columnLabel', $searchPath),
false
);
if ($columnPath === null) {
$condition->setColumn($searchPath);
throw new SearchBar\SearchException(t('Is not a valid column'));
} else {
$condition->setColumn($path);
$condition->setColumn($columnPath);
}
}
})->on(SearchEditor::ON_SUCCESS, function (SearchEditor $form) {
Expand Down Expand Up @@ -228,9 +251,14 @@ 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]);
try {
$label = $query->getResolver()->getColumnDefinition($path)->getLabel();
} catch (InvalidRelationException $_) {
$label = null;
}

if (isset($label)) {
$condition->metaData()->set('columnLabel', $label);
}
}
}