From b4a4b90b84f4c9885fa870444bd710b8f0982bb6 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 8 Jun 2022 15:19:17 +0200 Subject: [PATCH] SearchControls: Validate filter columns more efficiently requires https://github.com/Icinga/ipl-orm/pull/64 --- src/Compat/SearchControls.php | 37 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Compat/SearchControls.php b/src/Compat/SearchControls.php index 3517c1b9..630f6740 100644 --- a/src/Compat/SearchControls.php +++ b/src/Compat/SearchControls.php @@ -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; @@ -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 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()} @@ -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()); } }; @@ -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 { @@ -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()); } } }