@@ -221,13 +221,14 @@ protected function fetchColumnSuggestions($searchTerm)
221
221
$ model = $ this ->getModel ();
222
222
$ query = $ model ::on ($ this ->getDb ());
223
223
224
- // Collect custom variables
225
224
$ parsedArrayVars = [];
226
- $ varSuggestions = [];
227
225
$ exactSearchTerm = trim ($ searchTerm , ' * ' );
226
+ $ exactVarMatches = [];
228
227
$ titleAdded = false ;
228
+
229
+ // Suggest exact custom variable matches first
229
230
if ($ exactSearchTerm !== '' ) {
230
- foreach ($ this ->getDb ()->select ($ this ->queryCustomvarConfig ($ searchTerm )) as $ customVar ) {
231
+ foreach ($ this ->getDb ()->select ($ this ->queryCustomvarConfig ($ exactSearchTerm , true )) as $ customVar ) {
231
232
$ search = $ name = $ customVar ->flatname ;
232
233
if (preg_match ('/\w+(?:\[(\d*)])+$/ ' , $ search , $ matches )) {
233
234
$ name = substr ($ search , 0 , -(strlen ($ matches [1 ]) + 2 ));
@@ -242,23 +243,19 @@ protected function fetchColumnSuggestions($searchTerm)
242
243
foreach ($ this ->customVarSources as $ relation => $ label ) {
243
244
if (isset ($ customVar ->$ relation )) {
244
245
$ 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 ;
261
254
}
255
+
256
+ $ exactVarMatches [] = $ varRelation ;
257
+
258
+ yield $ varRelation => sprintf ($ label , $ name );
262
259
}
263
260
}
264
261
}
@@ -285,19 +282,40 @@ protected function fetchColumnSuggestions($searchTerm)
285
282
286
283
// Finally, the other custom variable suggestions
287
284
$ 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 ;
298
291
}
299
292
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
+ }
301
319
}
302
320
}
303
321
}
@@ -317,10 +335,11 @@ protected function matchSuggestion($path, $label, $searchTerm)
317
335
* Create a query to fetch all available custom variables matching the given term
318
336
*
319
337
* @param string $searchTerm
338
+ * @param bool $exactMatch
320
339
*
321
340
* @return Select
322
341
*/
323
- protected function queryCustomvarConfig (string $ searchTerm ): Select
342
+ protected function queryCustomvarConfig (string $ searchTerm, bool $ exactMatch = false ): Select
324
343
{
325
344
$ customVars = CustomvarFlat::on ($ this ->getDb ());
326
345
$ tableName = $ customVars ->getModel ()->getTableName ();
@@ -346,7 +365,12 @@ protected function queryCustomvarConfig(string $searchTerm): Select
346
365
347
366
$ customVars ->columns ('flatname ' );
348
367
$ 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
+
350
374
$ idColumn = $ resolver ->qualifyColumn ('id ' , $ resolver ->getAlias ($ customVars ->getModel ()));
351
375
$ customVars = $ customVars ->assembleSelect ();
352
376
0 commit comments