@@ -83,7 +83,7 @@ class Options extends Ext
83
83
/** Information for left join */
84
84
private $ _leftJoin = [];
85
85
86
- /** @var int Row limit */
86
+ /** @var int|null Row limit */
87
87
private $ _limit ;
88
88
89
89
/** @var callable Callback function to do rendering of labels */
@@ -312,7 +312,7 @@ public function where($_ = null)
312
312
public function exec ($ db , $ refresh , $ search = null )
313
313
{
314
314
// If search only, and not a search action, then just return false
315
- if ($ this ->searchOnly () && ! $ search ) {
315
+ if ($ this ->searchOnly () && $ search === null ) {
316
316
return false ;
317
317
}
318
318
@@ -323,7 +323,7 @@ public function exec($db, $refresh, $search = null)
323
323
324
324
$ fn = $ this ->_customFn ;
325
325
if (is_callable ($ fn )) {
326
- return $ fn ($ db );
326
+ return $ fn ($ db, $ search );
327
327
}
328
328
329
329
$ label = $ this ->_label ;
@@ -377,22 +377,32 @@ public function exec($db, $refresh, $search = null)
377
377
$ q ->order ($ this ->_order );
378
378
}
379
379
380
- if ($ this ->_limit !== null ) {
381
- $ q ->limit ($ this ->_limit );
382
- }
383
-
384
380
$ rows = $ q
385
381
->exec ()
386
382
->fetchAll ();
387
383
388
384
// Create the output array
389
385
$ out = [];
386
+ $ max = $ this ->_limit ;
390
387
391
388
for ($ i = 0 , $ ien = count ($ rows ); $ i < $ ien ; ++$ i ) {
392
- $ out [] = [
393
- 'label ' => $ formatter ($ rows [$ i ]),
394
- 'value ' => $ rows [$ i ][$ value ],
395
- ];
389
+ $ rowLabel = $ formatter ($ rows [$ i ]);
390
+ $ rowValue = $ rows [$ i ][$ value ];
391
+
392
+ // Apply the search to the rendered label. Need to do it here rather than in SQL since
393
+ // the label is rendered in PHP.
394
+ if ($ search === null || $ search === '' || stripos ($ rowLabel , $ search ) === 0 ) {
395
+ $ out [] = [
396
+ 'label ' => $ rowLabel ,
397
+ 'value ' => $ rowValue ,
398
+ ];
399
+ }
400
+
401
+ // Limit needs to be done in PHP to allow for the PHP based filtering above, and also
402
+ // for when using a custom function.
403
+ if ($ max !== null && count ($ out ) >= $ max ) {
404
+ break ;
405
+ }
396
406
}
397
407
398
408
// Stick on any extra manually added options
@@ -422,4 +432,17 @@ public function exec($db, $refresh, $search = null)
422
432
423
433
return $ out ;
424
434
}
435
+
436
+ /**
437
+ * Do a search for data on the source.
438
+ *
439
+ * @param Database $db Database connection
440
+ * @param string $term Search term
441
+ *
442
+ * @return array|bool
443
+ */
444
+ public function search ($ db , $ term )
445
+ {
446
+ return $ this ->exec ($ db , false , $ term );
447
+ }
425
448
}
0 commit comments