Skip to content

Commit c80526e

Browse files
committed
New: Support for Editor's new options search command
1 parent 2e997c5 commit c80526e

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

Editor.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ private function _process($data)
10351035
$this->_options(false);
10361036
} elseif ($action === Editor::ACTION_SEARCH) {
10371037
/* Options search */
1038-
$this->_optionsSearch();
1038+
$this->_optionsSearch($data);
10391039
} elseif ($action === Editor::ACTION_UPLOAD && $this->_write === true) {
10401040
/* File upload */
10411041
$this->_upload($data);
@@ -2290,10 +2290,30 @@ private function _options($refresh)
22902290
}
22912291
}
22922292

2293-
private function _optionsSearch()
2293+
/**
2294+
* Perform a search action on a specific field for label/value pairs.
2295+
*
2296+
* @param array $http Submitted HTTP request for search
2297+
*/
2298+
private function _optionsSearch($http)
22942299
{
2295-
// TODO!
2296-
// Get the field in question, the search term and perform the search
2300+
$field = $this->_find_field($http['field'], 'name');
2301+
2302+
if (!$field) {
2303+
return;
2304+
}
2305+
2306+
$options = $field->options();
2307+
2308+
if (!$options) {
2309+
return;
2310+
}
2311+
2312+
$values = $options->search($this->db(), $http['search']);
2313+
2314+
if ($values) {
2315+
$this->_out['data'] = $values;
2316+
}
22972317
}
22982318

22992319
/**

Editor/Options.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Options extends Ext
8383
/** Information for left join */
8484
private $_leftJoin = [];
8585

86-
/** @var int Row limit */
86+
/** @var int|null Row limit */
8787
private $_limit;
8888

8989
/** @var callable Callback function to do rendering of labels */
@@ -312,7 +312,7 @@ public function where($_ = null)
312312
public function exec($db, $refresh, $search = null)
313313
{
314314
// If search only, and not a search action, then just return false
315-
if ($this->searchOnly() && !$search) {
315+
if ($this->searchOnly() && $search === null) {
316316
return false;
317317
}
318318

@@ -323,7 +323,7 @@ public function exec($db, $refresh, $search = null)
323323

324324
$fn = $this->_customFn;
325325
if (is_callable($fn)) {
326-
return $fn($db);
326+
return $fn($db, $search);
327327
}
328328

329329
$label = $this->_label;
@@ -377,22 +377,32 @@ public function exec($db, $refresh, $search = null)
377377
$q->order($this->_order);
378378
}
379379

380-
if ($this->_limit !== null) {
381-
$q->limit($this->_limit);
382-
}
383-
384380
$rows = $q
385381
->exec()
386382
->fetchAll();
387383

388384
// Create the output array
389385
$out = [];
386+
$max = $this->_limit;
390387

391388
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+
}
396406
}
397407

398408
// Stick on any extra manually added options
@@ -422,4 +432,17 @@ public function exec($db, $refresh, $search = null)
422432

423433
return $out;
424434
}
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+
}
425448
}

0 commit comments

Comments
 (0)