Skip to content

Commit 2c0aa6e

Browse files
committed
Add filter to retrieve only the exactly matched columns
1 parent cc6c809 commit 2c0aa6e

File tree

1 file changed

+56
-32
lines changed

1 file changed

+56
-32
lines changed

library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,14 @@ protected function fetchColumnSuggestions($searchTerm)
221221
$model = $this->getModel();
222222
$query = $model::on($this->getDb());
223223

224-
// Collect custom variables
225224
$parsedArrayVars = [];
226-
$varSuggestions = [];
227225
$exactSearchTerm = trim($searchTerm, ' *');
226+
$exactVarMatches = [];
228227
$titleAdded = false;
228+
229+
// Suggest exact custom variable matches first
229230
if ($exactSearchTerm !== '') {
230-
foreach ($this->getDb()->select($this->queryCustomvarConfig($searchTerm)) as $customVar) {
231+
foreach ($this->getDb()->select($this->queryCustomvarConfig($exactSearchTerm, true)) as $customVar) {
231232
$search = $name = $customVar->flatname;
232233
if (preg_match('/\w+(?:\[(\d*)])+$/', $search, $matches)) {
233234
$name = substr($search, 0, -(strlen($matches[1]) + 2));
@@ -242,23 +243,19 @@ protected function fetchColumnSuggestions($searchTerm)
242243
foreach ($this->customVarSources as $relation => $label) {
243244
if (isset($customVar->$relation)) {
244245
$varRelation = $relation . '.vars.' . $search;
245-
$varLabel = sprintf($label, $name);
246-
// Suggest exact custom variable matches first
247-
if ($name === $exactSearchTerm) {
248-
if ($titleAdded === false) {
249-
$this->addHtml(HtmlElement::create(
250-
'li',
251-
['class' => static::SUGGESTION_TITLE_CLASS],
252-
t('Best Suggestions')
253-
));
254-
255-
$titleAdded = true;
256-
}
257-
258-
yield $varRelation => $varLabel;
259-
} else {
260-
$varSuggestions[$varRelation] = $varLabel;
246+
if ($titleAdded === false) {
247+
$this->addHtml(HtmlElement::create(
248+
'li',
249+
['class' => static::SUGGESTION_TITLE_CLASS],
250+
t('Best Suggestions')
251+
));
252+
253+
$titleAdded = true;
261254
}
255+
256+
$exactVarMatches[] = $varRelation;
257+
258+
yield $varRelation => sprintf($label, $name);
262259
}
263260
}
264261
}
@@ -285,19 +282,40 @@ protected function fetchColumnSuggestions($searchTerm)
285282

286283
// Finally, the other custom variable suggestions
287284
$titleAdded = false;
288-
foreach ($varSuggestions as $relation => $label) {
289-
if ($this->matchSuggestion($relation, $label, $searchTerm)) {
290-
if ($titleAdded === false) {
291-
$this->addHtml(HtmlElement::create(
292-
'li',
293-
['class' => static::SUGGESTION_TITLE_CLASS],
294-
t('Custom Variables')
295-
));
296-
297-
$titleAdded = true;
285+
foreach ($this->getDb()->select($this->queryCustomvarConfig($searchTerm)) as $customVar) {
286+
$search = $name = $customVar->flatname;
287+
if (preg_match('/\w+(?:\[(\d*)])+$/', $search, $matches)) {
288+
$name = substr($search, 0, -(strlen($matches[1]) + 2));
289+
if (isset($parsedArrayVars[$name])) {
290+
continue;
298291
}
299292

300-
yield $relation => $label;
293+
$parsedArrayVars[$name] = true;
294+
$search = $name . '[*]';
295+
}
296+
297+
foreach ($this->customVarSources as $relation => $label) {
298+
if (isset($customVar->$relation)) {
299+
$varRelation = $relation . '.vars.' . $search;
300+
$varLabel = sprintf($label, $name);
301+
// Suggest exact custom variable matches first
302+
if (
303+
! in_array($varRelation, $exactVarMatches)
304+
&& $this->matchSuggestion($varRelation, $varLabel, $searchTerm)
305+
) {
306+
if ($titleAdded === false) {
307+
$this->addHtml(HtmlElement::create(
308+
'li',
309+
['class' => static::SUGGESTION_TITLE_CLASS],
310+
t('Custom Variables')
311+
));
312+
313+
$titleAdded = true;
314+
}
315+
316+
yield $varRelation => $varLabel;
317+
}
318+
}
301319
}
302320
}
303321
}
@@ -317,10 +335,11 @@ protected function matchSuggestion($path, $label, $searchTerm)
317335
* Create a query to fetch all available custom variables matching the given term
318336
*
319337
* @param string $searchTerm
338+
* @param bool $exactMatch
320339
*
321340
* @return Select
322341
*/
323-
protected function queryCustomvarConfig(string $searchTerm): Select
342+
protected function queryCustomvarConfig(string $searchTerm, bool $exactMatch = false): Select
324343
{
325344
$customVars = CustomvarFlat::on($this->getDb());
326345
$tableName = $customVars->getModel()->getTableName();
@@ -346,7 +365,12 @@ protected function queryCustomvarConfig(string $searchTerm): Select
346365

347366
$customVars->columns('flatname');
348367
$this->applyRestrictions($customVars);
349-
$customVars->filter(Filter::like('flatname', $searchTerm));
368+
if ($exactMatch) {
369+
$customVars->filter(Filter::equal('flatname', $searchTerm));
370+
} else {
371+
$customVars->filter(Filter::like('flatname', $searchTerm));
372+
}
373+
350374
$idColumn = $resolver->qualifyColumn('id', $resolver->getAlias($customVars->getModel()));
351375
$customVars = $customVars->assembleSelect();
352376

0 commit comments

Comments
 (0)