Skip to content

Commit

Permalink
Reworking how the Options class works to be able to have a function f…
Browse files Browse the repository at this point in the history
…unction as part of the class rather than separate in the field.
  • Loading branch information
AllanJard committed Nov 11, 2024
1 parent 7c24b8d commit 29a8102
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
32 changes: 25 additions & 7 deletions Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {

Check failure on line 2245 in Editor.php

View workflow job for this annotation

GitHub Actions / Unit (latest, StaticAnalysis)

If condition is always true.
$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) {
Expand Down Expand Up @@ -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.
*
Expand Down
35 changes: 3 additions & 32 deletions Editor/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 43 additions & 1 deletion Editor/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -97,6 +100,9 @@ class Options extends Ext

private $_manualAdd = [];

Check failure on line 101 in Editor/Options.php

View workflow job for this annotation

GitHub Actions / Unit (latest, StaticAnalysis)

Property DataTables\Editor\Options::$_manualAdd is never read, only written.

/** @var callable|null */
private $_customFn;

Check failure on line 104 in Editor/Options.php

View workflow job for this annotation

GitHub Actions / Unit (latest, StaticAnalysis)

Property DataTables\Editor\Options::$_customFn is never written, only read.

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Check failure on line 315 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.
}

// 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;
Expand Down

0 comments on commit 29a8102

Please sign in to comment.