Skip to content

Commit

Permalink
New: Support for Editor's new options search command
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed Nov 15, 2024
1 parent 2e997c5 commit c80526e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
28 changes: 24 additions & 4 deletions Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
45 changes: 34 additions & 11 deletions Editor/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit c80526e

Please sign in to comment.