From 7c24b8d6bbafd7e438afd4f7f02cd8f5fddd5fa8 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 8 Nov 2024 17:29:55 +0000 Subject: [PATCH] New: List of options is returned after create and edit commands, to allow 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. --- Editor.php | 128 ++++++++++++++++++--------------------------- Editor/Field.php | 5 +- Editor/Options.php | 25 ++++++++- 3 files changed, 79 insertions(+), 79 deletions(-) diff --git a/Editor.php b/Editor.php index c89fb35..288c2df 100644 --- a/Editor.php +++ b/Editor.php @@ -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); @@ -1070,6 +1071,8 @@ private function _process($data) $this->_fileClean(); } + + $this->_options(true); } if ($this->_transaction) { @@ -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()) @@ -1163,38 +1163,6 @@ 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); @@ -1202,47 +1170,10 @@ private function _get($id = null, $http = null) $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), + ]; } /** @@ -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. * diff --git a/Editor/Field.php b/Editor/Field.php index e4a5463..07882ba 100644 --- a/Editor/Field.php +++ b/Editor/Field.php @@ -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; diff --git a/Editor/Options.php b/Editor/Options.php index 4400b67..96e0812 100644 --- a/Editor/Options.php +++ b/Editor/Options.php @@ -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; @@ -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. * @@ -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; + } + $label = $this->_label; $value = $this->_value; $formatter = $this->_renderer;