Skip to content

Commit

Permalink
New: List of options is returned after create and edit commands, to a…
Browse files Browse the repository at this point in the history
…llow new options created from the action to be included in the lists.

New: If this behaviour is not desirable (as it is an extra query), the `Field->alwaysRefresh()` method can be used to disable it.
  • Loading branch information
AllanJard committed Nov 8, 2024
1 parent 6849359 commit 7c24b8d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
128 changes: 52 additions & 76 deletions Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ private function _process($data)
if ($action === Editor::ACTION_READ) {
/* Get data */
$this->_out = array_merge($this->_out, $this->_get(null, $data));
$this->_options(false);
} elseif ($action === Editor::ACTION_UPLOAD && $this->_write === true) {
/* File upload */
$this->_upload($data);
Expand Down Expand Up @@ -1070,6 +1071,8 @@ private function _process($data)

$this->_fileClean();
}

$this->_options(true);
}

if ($this->_transaction) {
Expand Down Expand Up @@ -1116,9 +1119,6 @@ private function _get($id = null, $http = null)
return [];
}

// print_r($id);
// print_r($http);

$query = $this->_db
->query('select')
->table($this->_read_table())
Expand Down Expand Up @@ -1163,86 +1163,17 @@ private function _get($id = null, $http = null)
$out[] = $inner;
}

// Field options
$options = [];
$spOptions = [];
$sbOptions = [];
$searchPanes = [];

if ($id === null) {
foreach ($this->_fields as $field) {
$opts = $field->optionsExec($this->_db);

if ($opts !== false) {
$options[$field->name()] = $opts;
}

// SearchPanes options
$spOpts = $field->searchPaneOptionsExec($field, $this, $http, $this->_fields, $this->_leftJoin);

if ($spOpts !== false) {
$spOptions[$field->name()] = $spOpts;
}

$sbOpts = $field->searchBuilderOptionsExec($field, $this, $http, $this->_fields, $this->_leftJoin);

if ($sbOpts !== false) {
$sbOptions[$field->name()] = $sbOpts;
}
}
}

$searchPanes['options'] = $spOptions;
$searchBuilder['options'] = $sbOptions;

// Row based "joins"
for ($i = 0; $i < count($this->_join); ++$i) {
$this->_join[$i]->data($this, $out, $options);
}

$this->_trigger('postGet', $out, $id);

if (count($searchPanes['options']) > 0 && count($searchBuilder['options']) > 0) {
return array_merge(
[
'data' => $out,
'options' => $options,
'files' => $this->_fileData(null, null, $out),
'searchBuilder' => $searchBuilder,
'searchPanes' => $searchPanes,
],
$ssp
);
} elseif (count($searchBuilder['options']) > 0) {
return array_merge(
[
'data' => $out,
'options' => $options,
'files' => $this->_fileData(null, null, $out),
'searchBuilder' => $searchBuilder,
],
$ssp
);
} elseif (count($searchPanes['options']) > 0) {
return array_merge(
[
'data' => $out,
'options' => $options,
'files' => $this->_fileData(null, null, $out),
'searchPanes' => $searchPanes,
],
$ssp
);
}

return array_merge(
[
'data' => $out,
'options' => $options,
'files' => $this->_fileData(null, null, $out),
],
$ssp
);
return [
'data' => $out,
'files' => $this->_fileData(null, null, $out),
];
}

/**
Expand Down Expand Up @@ -2292,6 +2223,51 @@ private function _insert_or_update_table($table, $values, $where = null)
return $this->_db->push($table, $set, $where, $pkey);
}

/**
* Get option lists for select, radio, autocomplete, etc
*
* @param boolean $refresh false for initial load, true if after insert, update
*/
private function _options($refresh)
{
foreach ($this->_fields as $field) {
// Basic options class
$opts = $field->optionsExec($this->_db, $refresh);

if ($opts !== false) {
if (!isset($this->_out['options'])) {
$this->_out['options'] = [];
}

$this->_out['options'][$field->name()] = $opts;
}

if (! $refresh) {
// SearchPanes options
$spOpts = $field->searchPaneOptionsExec($field, $this, $this->_processData, $this->_fields, $this->_leftJoin);

if ($spOpts !== false) {
if (!isset($this->_out['searchPanes'])) {
$this->_out['searchPanes'] = ['options' => []];
}

$this->_out['searchPanes']['options'] = $spOpts;
}

// SearchBuilder options
$sbOpts = $field->searchBuilderOptionsExec($field, $this, $this->_processData, $this->_fields, $this->_leftJoin);

if ($sbOpts !== false) {
if (!isset($this->_out['searchBuilder'])) {
$this->_out['searchBuilder'] = ['options' => []];
}

$this->_out['searchBuilder']['options'] = $sbOpts;
}
}
}
}

/**
* Delete one or more rows from the database for an individual table.
*
Expand Down
5 changes: 3 additions & 2 deletions Editor/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,19 +606,20 @@ public function apply($action, $data = null)
* side.
*
* @param Database $db Database instance
* @param boolean $refresh Indicate if this is a refresh or a full load
*
* @return false|array Array of value / label options for the list
*
* @internal
*/
public function optionsExec($db)
public function optionsExec($db, $refresh)
{
if ($this->_optsFn) {
$fn = $this->_optsFn;

return $fn($db);
} elseif ($this->_opts) {
return $this->_opts->exec($db);
return $this->_opts->exec($db, $refresh);
}

return false;
Expand Down
25 changes: 24 additions & 1 deletion Editor/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class Options extends Ext
* Private parameters
*/

/** @var boolean Indicate if options should always be refreshed */
private $_alwaysRefresh = true;

/** @var string Table to get the information from */
private $_table;

Expand Down Expand Up @@ -120,6 +123,20 @@ public function add($label, $value = null)
return $this;
}

/**
* Get / set the flag to indicate if the options should always be refreshed
* (i.e. on get, create and edit) or only on the initial data load (false)
*
* @param boolean|null $_ Flag to set the always refresh set to, or null to
* get the current state.
*
* @return ($_ is null ? boolean : $this)
*/
public function alwaysRefresh($_ = null)
{
return $this->_getSet($this->_alwaysRefresh, $_);
}

/**
* Get / set the column(s) to use as the label value of the options.
*
Expand Down Expand Up @@ -252,13 +269,19 @@ public function where($_ = null)
* Execute the options (i.e. get them).
*
* @param Database $db Database connection
* @param boolean $refresh Indicate if this is a refresh or a full load
*
* @return array List of options
*
* @internal
*/
public function exec($db)
public function exec($db, $refresh)
{
// Only get the options if doing a full load, or always is set
if ($refresh === true && !$this->alwaysRefresh()) {
return false;

Check failure on line 282 in Editor/Options.php

View workflow job for this annotation

GitHub Actions / Unit (latest, StaticAnalysis)

Method DataTables\Editor\Options::exec() should return array but returns false.
}

$label = $this->_label;
$value = $this->_value;
$formatter = $this->_renderer;
Expand Down

0 comments on commit 7c24b8d

Please sign in to comment.