diff --git a/Editor.php b/Editor.php index ca84704..65eca39 100644 --- a/Editor.php +++ b/Editor.php @@ -1035,7 +1035,7 @@ private function _process($data) $this->_options(false); } elseif ($action === Editor::ACTION_SEARCH) { /* Options search */ - $this->_optionsSearch(); + $this->_optionsSearch($data); } elseif ($action === Editor::ACTION_UPLOAD && $this->_write === true) { /* File upload */ $this->_upload($data); @@ -2290,10 +2290,30 @@ private function _options($refresh) } } - private function _optionsSearch() + /** + * Perform a search action on a specific field for label/value pairs. + * + * @param array $http Submitted HTTP request for search + */ + private function _optionsSearch($http) { - // TODO! - // Get the field in question, the search term and perform the search + $field = $this->_find_field($http['field'], 'name'); + + if (!$field) { + return; + } + + $options = $field->options(); + + if (!$options) { + return; + } + + $values = $options->search($this->db(), $http['search']); + + if ($values) { + $this->_out['data'] = $values; + } } /** diff --git a/Editor/Options.php b/Editor/Options.php index 37768d1..3f304cf 100644 --- a/Editor/Options.php +++ b/Editor/Options.php @@ -83,7 +83,7 @@ class Options extends Ext /** Information for left join */ private $_leftJoin = []; - /** @var int Row limit */ + /** @var int|null Row limit */ private $_limit; /** @var callable Callback function to do rendering of labels */ @@ -312,7 +312,7 @@ public function where($_ = null) public function exec($db, $refresh, $search = null) { // If search only, and not a search action, then just return false - if ($this->searchOnly() && !$search) { + if ($this->searchOnly() && $search === null) { return false; } @@ -323,7 +323,7 @@ public function exec($db, $refresh, $search = null) $fn = $this->_customFn; if (is_callable($fn)) { - return $fn($db); + return $fn($db, $search); } $label = $this->_label; @@ -377,22 +377,32 @@ public function exec($db, $refresh, $search = null) $q->order($this->_order); } - if ($this->_limit !== null) { - $q->limit($this->_limit); - } - $rows = $q ->exec() ->fetchAll(); // Create the output array $out = []; + $max = $this->_limit; for ($i = 0, $ien = count($rows); $i < $ien; ++$i) { - $out[] = [ - 'label' => $formatter($rows[$i]), - 'value' => $rows[$i][$value], - ]; + $rowLabel = $formatter($rows[$i]); + $rowValue = $rows[$i][$value]; + + // Apply the search to the rendered label. Need to do it here rather than in SQL since + // the label is rendered in PHP. + if ($search === null || $search === '' || stripos($rowLabel, $search) === 0) { + $out[] = [ + 'label' => $rowLabel, + 'value' => $rowValue, + ]; + } + + // Limit needs to be done in PHP to allow for the PHP based filtering above, and also + // for when using a custom function. + if ($max !== null && count($out) >= $max) { + break; + } } // Stick on any extra manually added options @@ -422,4 +432,17 @@ public function exec($db, $refresh, $search = null) return $out; } + + /** + * Do a search for data on the source. + * + * @param Database $db Database connection + * @param string $term Search term + * + * @return array|bool + */ + public function search($db, $term) + { + return $this->exec($db, false, $term); + } }