From 29a8102ef2dc98a8d0af8d7cc638b15843490a97 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Mon, 11 Nov 2024 16:31:49 +0000 Subject: [PATCH] Reworking how the Options class works to be able to have a function function as part of the class rather than separate in the field. --- Editor.php | 32 +++++++++++++++++++++++++------- Editor/Field.php | 35 +++-------------------------------- Editor/Options.php | 44 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 40 deletions(-) diff --git a/Editor.php b/Editor.php index 288c2df..c62d048 100644 --- a/Editor.php +++ b/Editor.php @@ -77,6 +77,9 @@ class Editor extends Ext /** Request type - delete */ const ACTION_DELETE = 'remove'; + /** Request type - options search */ + const ACTION_SEARCH = 'search'; + /** Request type - upload */ const ACTION_UPLOAD = 'upload'; @@ -104,6 +107,8 @@ public static function action($http, $name = 'action') return self::ACTION_EDIT; case 'remove': return self::ACTION_DELETE; + case 'search': + return self::ACTION_SEARCH; case 'upload': return self::ACTION_UPLOAD; default: @@ -1025,6 +1030,9 @@ private function _process($data) /* Get data */ $this->_out = array_merge($this->_out, $this->_get(null, $data)); $this->_options(false); + } elseif ($action === Editor::ACTION_SEARCH) { + /* Options search */ + $this->_optionsSearch(); } elseif ($action === Editor::ACTION_UPLOAD && $this->_write === true) { /* File upload */ $this->_upload($data); @@ -2232,14 +2240,18 @@ 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'] = []; + $options = $field->options(); + + if ($options) { + $opts = $options($this->_db, $refresh); + + if ($opts !== false) { + if (!isset($this->_out['options'])) { + $this->_out['options'] = []; + } + + $this->_out['options'][$field->name()] = $opts; } - - $this->_out['options'][$field->name()] = $opts; } if (! $refresh) { @@ -2268,6 +2280,12 @@ private function _options($refresh) } } + private function _optionsSearch() + { + // TODO! + // Get the field in question, the search term and perform the search + } + /** * Delete one or more rows from the database for an individual table. * diff --git a/Editor/Field.php b/Editor/Field.php index 07882ba..b991b7f 100644 --- a/Editor/Field.php +++ b/Editor/Field.php @@ -129,9 +129,6 @@ public function __construct($dbField = null, $name = null) /** @var SearchBuilderOptions|null */ private $_sbopts; - /** @var callable|null */ - private $_optsFn; - /** @var callable|null */ private $_spoptsFn; @@ -311,15 +308,13 @@ public function options($table = null, $value = null, $label = null, $condition // Overloads for backwards compatibility if (is_a($table, 'DataTables\Editor\Options')) { // Options class - $this->_optsFn = null; $this->_opts = $table; } elseif ($table instanceof \Closure) { // Function - $this->_opts = null; - $this->_optsFn = $table; + $this->_opts = Options::inst() + ->fn($table); } else { - $this->_optsFn = null; - $this->_opts = (new Options()) + $this->_opts = Options::inst() ->table($table) ->value($value) ->label($label); @@ -601,30 +596,6 @@ public function apply($action, $data = null) return true; } - /** - * Execute the ipOpts to get the list of options to return to the client- - * 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, $refresh) - { - if ($this->_optsFn) { - $fn = $this->_optsFn; - - return $fn($db); - } elseif ($this->_opts) { - return $this->_opts->exec($db, $refresh); - } - - return false; - } - /** * Execute the spopts to get the list of options for SearchPanes to return * to the client-side. diff --git a/Editor/Options.php b/Editor/Options.php index 96e0812..986441a 100644 --- a/Editor/Options.php +++ b/Editor/Options.php @@ -89,6 +89,9 @@ class Options extends Ext /** @var callable Callback function to do rendering of labels */ private $_renderer; + /** @var boolean Indicate if options should get got for create/edit or on search only */ + private $_searchOnly = false; + /** @var callable Callback function to add where conditions */ private $_where; @@ -97,6 +100,9 @@ class Options extends Ext private $_manualAdd = []; + /** @var callable|null */ + private $_customFn; + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Public methods */ @@ -137,6 +143,19 @@ public function alwaysRefresh($_ = null) return $this->_getSet($this->_alwaysRefresh, $_); } + /** + * Custom function to get the options, rather than using the built in DB + * + * @param callable|null $_ Function that will be run to get the list of + * options. + * + * @return ($_ is null ? callable : $this) + */ + public function fn($_ = null) + { + return $this->_getSet($this->_customFn, $_); + } + /** * Get / set the column(s) to use as the label value of the options. * @@ -222,6 +241,20 @@ public function render($_ = null) return $this->_getSet($this->_renderer, $_); } + /** + * 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 searchOnly($_ = null) + { + return $this->_getSet($this->_searchOnly, $_); + } + /** * Get / set the database table from which to gather the options for the * list. @@ -275,13 +308,22 @@ public function where($_ = null) * * @internal */ - public function exec($db, $refresh) + public function exec($db, $refresh, $search) { + // If search only, and not a search action, then just return false + if ($this->searchOnly() && ! $search) { + return false; + } + // Only get the options if doing a full load, or always is set if ($refresh === true && !$this->alwaysRefresh()) { return false; } + if ($this->fn()) { + return $this->_customFn($db); + } + $label = $this->_label; $value = $this->_value; $formatter = $this->_renderer;