From aca5202d46a4fb4fa78dad58d270c74c9da4353c Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 13 May 2024 05:12:57 +0200 Subject: [PATCH 01/16] Update MySqlConnection.php (#627) --- src/Database/Connections/MySqlConnection.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Database/Connections/MySqlConnection.php b/src/Database/Connections/MySqlConnection.php index 317ef232b..24b87b664 100644 --- a/src/Database/Connections/MySqlConnection.php +++ b/src/Database/Connections/MySqlConnection.php @@ -31,7 +31,12 @@ public function isMaria() */ protected function getDefaultQueryGrammar() { - return $this->withTablePrefix(new QueryGrammar); + $grammar = new QueryGrammar; + if (method_exists($grammar, 'setConnection')) { + $grammar->setConnection($this); + } + + return $this->withTablePrefix($grammar); } /** @@ -55,7 +60,12 @@ public function getSchemaBuilder() */ protected function getDefaultSchemaGrammar() { - return $this->withTablePrefix(new SchemaGrammar); + $grammar = new SchemaGrammar; + if (method_exists($grammar, 'setConnection')) { + $grammar->setConnection($this); + } + + return $this->withTablePrefix($grammar); } /** From 9d0d6000bf02e8e095ec0de1cbda8f1d462a188b Mon Sep 17 00:00:00 2001 From: October CMS Date: Wed, 7 Aug 2024 13:37:24 +1000 Subject: [PATCH 02/16] Refactor connections to use trait Prior design would require duplicating code to extend the Connection class --- src/Database/Connections/Connection.php | 2 +- .../Connections/ExtendsConnection.php | 54 ++++++ src/Database/Connections/MySqlConnection.php | 170 +----------------- .../Connections/PostgresConnection.php | 106 +---------- src/Database/Connections/SQLiteConnection.php | 114 +----------- .../Connections/SqlServerConnection.php | 122 +------------ 6 files changed, 77 insertions(+), 491 deletions(-) create mode 100644 src/Database/Connections/ExtendsConnection.php diff --git a/src/Database/Connections/Connection.php b/src/Database/Connections/Connection.php index ac55061f5..917946d62 100644 --- a/src/Database/Connections/Connection.php +++ b/src/Database/Connections/Connection.php @@ -4,7 +4,7 @@ use Illuminate\Database\Connection as ConnectionBase; /** - * Connection base class + * @deprecated see \October\Rain\Database\Connections\ExtendsConnection */ class Connection extends ConnectionBase { diff --git a/src/Database/Connections/ExtendsConnection.php b/src/Database/Connections/ExtendsConnection.php new file mode 100644 index 000000000..383cc227e --- /dev/null +++ b/src/Database/Connections/ExtendsConnection.php @@ -0,0 +1,54 @@ +getQueryGrammar(), + $this->getPostProcessor() + ); + } + + /** + * logQuery in the connection's query log + * @param string $query + * @param array $bindings + * @param float|null $time + * @return void + */ + public function logQuery($query, $bindings, $time = null) + { + if (isset($this->events)) { + $this->events->dispatch('illuminate.query', [$query, $bindings, $time, $this->getName()]); + } + + parent::logQuery($query, $bindings, $time); + } + + /** + * fireConnectionEvent for this connection + * @param string $event + * @return void + */ + protected function fireConnectionEvent($event) + { + if (isset($this->events)) { + $this->events->dispatch('connection.'.$this->getName().'.'.$event, $this); + } + + parent::fireConnectionEvent($event); + } +} \ No newline at end of file diff --git a/src/Database/Connections/MySqlConnection.php b/src/Database/Connections/MySqlConnection.php index dfa741419..25913714d 100644 --- a/src/Database/Connections/MySqlConnection.php +++ b/src/Database/Connections/MySqlConnection.php @@ -1,173 +1,11 @@ run($query, $bindings, function ($query, $bindings) use ($sequence) { - if ($this->pretending()) { - return true; - } - - $statement = $this->getPdo()->prepare($query); - - $this->bindValues($statement, $this->prepareBindings($bindings)); - - $this->recordsHaveBeenModified(); - - $result = $statement->execute(); - - $this->lastInsertId = $this->getPdo()->lastInsertId($sequence); - - return $result; - }); - } - - /** - * Get the last insert ID. - * - * @return int - */ - public function getLastInsertId() - { - return $this->lastInsertId; - } - - /** - * Escape a binary value for safe SQL embedding. - * - * @param string $value - * @return string - */ - protected function escapeBinary($value) - { - $hex = bin2hex($value); - - return "x'{$hex}'"; - } - - /** - * Determine if the given database exception was caused by a unique constraint violation. - * - * @param \Exception $exception - * @return bool - */ - protected function isUniqueConstraintError(Exception $exception) - { - return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage())); - } - - /** - * Determine if the connected database is a MariaDB database. - * - * @return bool - */ - public function isMaria() - { - return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB'); - } - - /** - * Get the default query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\MySqlGrammar - */ - protected function getDefaultQueryGrammar() - { - $grammar = new QueryGrammar; - if (method_exists($grammar, 'setConnection')) { - $grammar->setConnection($this); - } - - return $this->withTablePrefix($grammar); - } - - /** - * Get a schema builder instance for the connection. - * - * @return \Illuminate\Database\Schema\MySqlBuilder - */ - public function getSchemaBuilder() - { - if (is_null($this->schemaGrammar)) { - $this->useDefaultSchemaGrammar(); - } - - return new MySqlBuilder($this); - } - - /** - * Get the default schema grammar instance. - * - * @return \Illuminate\Database\Schema\Grammars\MySqlGrammar - */ - protected function getDefaultSchemaGrammar() - { - $grammar = new SchemaGrammar; - if (method_exists($grammar, 'setConnection')) { - $grammar->setConnection($this); - } - - return $this->withTablePrefix($grammar); - } - - /** - * Get the schema state for the connection. - * - * @param \Illuminate\Filesystem\Filesystem|null $files - * @param callable|null $processFactory - * @return \Illuminate\Database\Schema\MySqlSchemaState - */ - public function getSchemaState(Filesystem $files = null, callable $processFactory = null) - { - return new MySqlSchemaState($this, $files, $processFactory); - } - - /** - * Get the default post processor instance. - * - * @return \Illuminate\Database\Query\Processors\MySqlProcessor - */ - protected function getDefaultPostProcessor() - { - return new MySqlProcessor; - } - - /** - * Get the Doctrine DBAL driver. - * - * @return \Illuminate\Database\PDO\MySqlDriver - */ - protected function getDoctrineDriver() - { - return new MySqlDriver; - } + use \October\Rain\Database\Connections\ExtendsConnection; } diff --git a/src/Database/Connections/PostgresConnection.php b/src/Database/Connections/PostgresConnection.php index 9bcca4dde..87adb4864 100644 --- a/src/Database/Connections/PostgresConnection.php +++ b/src/Database/Connections/PostgresConnection.php @@ -1,105 +1,11 @@ $value) { - if (is_int($value)) { - $pdoParam = PDO::PARAM_INT; - } elseif (is_resource($value)) { - $pdoParam = PDO::PARAM_LOB; - } else { - $pdoParam = PDO::PARAM_STR; - } - - $statement->bindValue( - is_string($key) ? $key : $key + 1, - $value, - $pdoParam - ); - } - } - - /** - * Get the default query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\PostgresGrammar - */ - protected function getDefaultQueryGrammar() - { - return $this->withTablePrefix(new QueryGrammar); - } - - /** - * Get a schema builder instance for the connection. - * - * @return \Illuminate\Database\Schema\PostgresBuilder - */ - public function getSchemaBuilder() - { - if (is_null($this->schemaGrammar)) { - $this->useDefaultSchemaGrammar(); - } - - return new PostgresBuilder($this); - } - - /** - * Get the default schema grammar instance. - * - * @return \Illuminate\Database\Schema\Grammars\PostgresGrammar - */ - protected function getDefaultSchemaGrammar() - { - return $this->withTablePrefix(new SchemaGrammar); - } - - /** - * Get the schema state for the connection. - * - * @param \Illuminate\Filesystem\Filesystem|null $files - * @param callable|null $processFactory - * @return \Illuminate\Database\Schema\PostgresSchemaState - */ - public function getSchemaState(Filesystem $files = null, callable $processFactory = null) - { - return new PostgresSchemaState($this, $files, $processFactory); - } - - /** - * Get the default post processor instance. - * - * @return \Illuminate\Database\Query\Processors\PostgresProcessor - */ - protected function getDefaultPostProcessor() - { - return new PostgresProcessor; - } - - /** - * Get the Doctrine DBAL driver. - * - * @return \Illuminate\Database\PDO\PostgresDriver - */ - protected function getDoctrineDriver() - { - return new PostgresDriver; - } + use \October\Rain\Database\Connections\ExtendsConnection; } diff --git a/src/Database/Connections/SQLiteConnection.php b/src/Database/Connections/SQLiteConnection.php index b444beca5..0c5a32492 100644 --- a/src/Database/Connections/SQLiteConnection.php +++ b/src/Database/Connections/SQLiteConnection.php @@ -1,113 +1,11 @@ getForeignKeyConstraintsConfigurationValue(); - - if ($enableForeignKeyConstraints === null) { - return; - } - - $enableForeignKeyConstraints - ? $this->getSchemaBuilder()->enableForeignKeyConstraints() - : $this->getSchemaBuilder()->disableForeignKeyConstraints(); - } - - /** - * Get the default query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\SQLiteGrammar - */ - protected function getDefaultQueryGrammar() - { - return $this->withTablePrefix(new QueryGrammar); - } - - /** - * Get a schema builder instance for the connection. - * - * @return \Illuminate\Database\Schema\SQLiteBuilder - */ - public function getSchemaBuilder() - { - if (is_null($this->schemaGrammar)) { - $this->useDefaultSchemaGrammar(); - } - - return new SQLiteBuilder($this); - } - - /** - * Get the default schema grammar instance. - * - * @return \Illuminate\Database\Schema\Grammars\SQLiteGrammar - */ - protected function getDefaultSchemaGrammar() - { - return $this->withTablePrefix(new SchemaGrammar); - } - - /** - * Get the schema state for the connection. - * - * @param \Illuminate\Filesystem\Filesystem|null $files - * @param callable|null $processFactory - * - * @throws \RuntimeException - */ - public function getSchemaState(Filesystem $files = null, callable $processFactory = null) - { - return new SqliteSchemaState($this, $files, $processFactory); - } - - /** - * Get the default post processor instance. - * - * @return \Illuminate\Database\Query\Processors\SQLiteProcessor - */ - protected function getDefaultPostProcessor() - { - return new SQLiteProcessor; - } - - /** - * Get the Doctrine DBAL driver. - * - * @return \Illuminate\Database\PDO\SQLiteDriver - */ - protected function getDoctrineDriver() - { - return new SQLiteDriver; - } - - /** - * Get the database connection foreign key constraints configuration option. - * - * @return bool|null - */ - protected function getForeignKeyConstraintsConfigurationValue() - { - return $this->getConfig('foreign_key_constraints'); - } + use \October\Rain\Database\Connections\ExtendsConnection; } diff --git a/src/Database/Connections/SqlServerConnection.php b/src/Database/Connections/SqlServerConnection.php index cc2dc2cb1..d1b7acd3d 100644 --- a/src/Database/Connections/SqlServerConnection.php +++ b/src/Database/Connections/SqlServerConnection.php @@ -1,121 +1,11 @@ getDriverName() === 'sqlsrv') { - return parent::transaction($callback); - } - - $this->getPdo()->exec('BEGIN TRAN'); - - // We'll simply execute the given callback within a try / catch block - // and if we catch any exception we can rollback the transaction - // so that none of the changes are persisted to the database. - try { - $result = $callback($this); - - $this->getPdo()->exec('COMMIT TRAN'); - } - - // If we catch an exception, we will rollback so nothing gets messed - // up in the database. Then we'll re-throw the exception so it can - // be handled how the developer sees fit for their applications. - catch (Throwable $e) { - $this->getPdo()->exec('ROLLBACK TRAN'); - - throw $e; - } - - return $result; - } - } - - /** - * Get the default query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\SqlServerGrammar - */ - protected function getDefaultQueryGrammar() - { - return $this->withTablePrefix(new QueryGrammar); - } - - /** - * Get a schema builder instance for the connection. - * - * @return \Illuminate\Database\Schema\SqlServerBuilder - */ - public function getSchemaBuilder() - { - if (is_null($this->schemaGrammar)) { - $this->useDefaultSchemaGrammar(); - } - - return new SqlServerBuilder($this); - } - - /** - * Get the default schema grammar instance. - * - * @return \Illuminate\Database\Schema\Grammars\SqlServerGrammar - */ - protected function getDefaultSchemaGrammar() - { - return $this->withTablePrefix(new SchemaGrammar); - } - - /** - * Get the schema state for the connection. - * - * @param \Illuminate\Filesystem\Filesystem|null $files - * @param callable|null $processFactory - * - * @throws \RuntimeException - */ - public function getSchemaState(Filesystem $files = null, callable $processFactory = null) - { - throw new RuntimeException('Schema dumping is not supported when using SQL Server.'); - } - - /** - * Get the default post processor instance. - * - * @return \Illuminate\Database\Query\Processors\SqlServerProcessor - */ - protected function getDefaultPostProcessor() - { - return new SqlServerProcessor; - } - - /** - * Get the Doctrine DBAL driver. - * - * @return \Illuminate\Database\PDO\SqlServerDriver - */ - protected function getDoctrineDriver() - { - return new SqlServerDriver; - } + use \October\Rain\Database\Connections\ExtendsConnection; } From 932627b7e2bf1bcb70a1b3c056c38ac6d68f585c Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 27 Aug 2024 16:38:59 +1000 Subject: [PATCH 03/16] Adds field option spec --- src/Element/Form/FieldOptionDefinition.php | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/Element/Form/FieldOptionDefinition.php diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php new file mode 100644 index 000000000..fe32a4815 --- /dev/null +++ b/src/Element/Form/FieldOptionDefinition.php @@ -0,0 +1,38 @@ +hidden(false) + ->readOnly(false) + ->disabled(false) + ->comment(''); + } +} From 154e9e9116b161d142b05d58ab4783ee7dfc675d Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 27 Aug 2024 16:40:22 +1000 Subject: [PATCH 04/16] Rem unused imports --- src/Element/Form/FieldOptionDefinition.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index fe32a4815..e5523f0a6 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -1,9 +1,5 @@ Date: Mon, 2 Sep 2024 13:57:01 +1000 Subject: [PATCH 05/16] Field options may have children --- src/Element/Form/FieldOptionDefinition.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index e5523f0a6..8531e2690 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -14,6 +14,7 @@ * @method FieldOptionDefinition cssColor(bool $cssColor) cssColor defines a status indicator color for the option (dropdown) * @method FieldOptionDefinition icon(bool $icon) icon specifies an icon name for this option * @method FieldOptionDefinition image(bool $image) image specifies an image URL for this option + * @method FieldOptionDefinition children(array $image) children specifies child options * * @package october\element * @author Alexey Bobkov, Samuel Georges From 96f1583f5deb3faa746a099f18699c7f8811dc2c Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 2 Sep 2024 15:11:38 +1000 Subject: [PATCH 06/16] Adds field->optionsDefinition() as "options as definition" Method is a placeholder can't really think of a better name at this point --- src/Element/Form/FieldDefinition.php | 18 ++++++++++++++++++ src/Element/Form/FieldOptionDefinition.php | 12 +++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index 94e12b868..1c441406a 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -144,6 +144,24 @@ public function options($value = null) return $this; } + /** + * optionsDefinition + */ + public function optionsDefinition() + { + $options = $this->options(); + + $result = []; + + foreach ($options as $key => $option) { + $definition = new FieldOptionDefinition; + $definition->useOptionConfig($option); + $result[$key] = $definition; + } + + return $result; + } + /** * matchesContext returns true if the field matches the supplied context */ diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index 8531e2690..065b0c67e 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -1,4 +1,4 @@ -disabled(false) ->comment(''); } + + /** + * useOptionConfig + */ + public function useOptionConfig(array $config): FieldOptionDefinition + { + // process option as documented + + return $this; + } } From 8c4e4171decb088ecfd1faefc5b205aa7073ded3 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 4 Sep 2024 14:59:21 +1000 Subject: [PATCH 07/16] Process options as documented --- src/Element/Form/FieldDefinition.php | 5 +-- src/Element/Form/FieldOptionDefinition.php | 38 +++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index 1c441406a..96c6846bb 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -153,10 +153,11 @@ public function optionsDefinition() $result = []; - foreach ($options as $key => $option) { + foreach ($options as $value => $option) { $definition = new FieldOptionDefinition; + $definition->value($value); $definition->useOptionConfig($option); - $result[$key] = $definition; + $result[$value] = $definition; } return $result; diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index 065b0c67e..994a982de 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -1,5 +1,9 @@ label($option); + return $this; + } + + if (Arr::isAssoc($option)) { + $this->useConfig($option); + return $this; + } + + $firstPart = (string) ($option[0] ?? ''); + $secondPart = (string) ($option[1] ?? ''); + + $this->label($firstPart); + $this->comment($secondPart); + + if (Html::isValidColor($secondPart)) { + $this->cssColor($secondPart); + } + elseif (strpos($secondPart, '.')) { + $this->image($secondPart); + } + else { + $this->icon($secondPart); + } return $this; } From 40a0c23a8efaa9dded90c47134246b6998bbf066 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 9 Sep 2024 11:41:22 +1000 Subject: [PATCH 08/16] Adds child option support --- src/Element/Form/FieldDefinition.php | 7 ++++--- src/Element/Form/FieldOptionDefinition.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index 96c6846bb..a43db0b90 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -154,9 +154,10 @@ public function optionsDefinition() $result = []; foreach ($options as $value => $option) { - $definition = new FieldOptionDefinition; - $definition->value($value); - $definition->useOptionConfig($option); + $definition = (new FieldOptionDefinition) + ->value($value) + ->useOptionConfig($option); + $result[$value] = $definition; } diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index 994a982de..51b316e33 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -48,6 +48,10 @@ public function useOptionConfig(array $option): FieldOptionDefinition } if (Arr::isAssoc($option)) { + if (isset($options['children']) && is_array($options['children'])) { + $options['children'] = $this->evalChildOptions($options['children']); + } + $this->useConfig($option); return $this; } @@ -70,4 +74,20 @@ public function useOptionConfig(array $option): FieldOptionDefinition return $this; } + + /** + * evalChildOptions + */ + protected function evalChildOptions(array $children): array + { + $result = []; + + foreach ($children as $value => $option) { + $result[$value] = (new FieldOptionDefinition) + ->value($value) + ->useOptionConfig($option); + } + + return $result; + } } From 0d87cd6f09d39d51b7f1515370422231f13fdec9 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 9 Sep 2024 16:08:18 +1000 Subject: [PATCH 09/16] =?UTF-8?q?optionsDefinition=20=E2=86=92=20asOptions?= =?UTF-8?q?Definition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also adds the ability to pass the options in via the method args --- src/Element/Form/FieldDefinition.php | 6 ++++-- src/Element/Form/FieldOptionDefinition.php | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index a43db0b90..be5c309f8 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -147,9 +147,11 @@ public function options($value = null) /** * optionsDefinition */ - public function optionsDefinition() + public function asOptionsDefinition($options = null) { - $options = $this->options(); + if ($options === null) { + $options = $this->options(); + } $result = []; diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index 51b316e33..ff1561c72 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -18,7 +18,8 @@ * @method FieldOptionDefinition cssColor(string $cssColor) cssColor defines a status indicator color for the option (dropdown) * @method FieldOptionDefinition icon(string $icon) icon specifies an icon name for this option * @method FieldOptionDefinition image(string $image) image specifies an image URL for this option - * @method FieldOptionDefinition children(array $image) children specifies child options + * @method FieldOptionDefinition indentLevel(int $indentLevel) indentLevel sets the level that the option sits + * @method FieldOptionDefinition children(array $image) children specifies child options as an alternative to indenting * * @package october\element * @author Alexey Bobkov, Samuel Georges From fc0a21ee804ec0e3349eda2bebcbc2619c379ba5 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 10 Sep 2024 15:07:12 +1000 Subject: [PATCH 10/16] Typo --- src/Element/Form/FieldOptionDefinition.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index ff1561c72..222032909 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -49,8 +49,8 @@ public function useOptionConfig(array $option): FieldOptionDefinition } if (Arr::isAssoc($option)) { - if (isset($options['children']) && is_array($options['children'])) { - $options['children'] = $this->evalChildOptions($options['children']); + if (isset($option['children']) && is_array($option['children'])) { + $option['children'] = $this->evalChildOptions($option['children']); } $this->useConfig($option); From 4143237735d0880c4eb7623c03ac64e8f6794e57 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 11 Sep 2024 15:19:43 +1000 Subject: [PATCH 11/16] Fixes option as string used in dropdown --- src/Element/Form/FieldOptionDefinition.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/Form/FieldOptionDefinition.php index 222032909..e84d85989 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/Form/FieldOptionDefinition.php @@ -41,7 +41,7 @@ protected function initDefaultValues() /** * useOptionConfig */ - public function useOptionConfig(array $option): FieldOptionDefinition + public function useOptionConfig($option): FieldOptionDefinition { if (!is_array($option)) { $this->label($option); From fc13a02086529240ccc2dceea6392191201838a2 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Thu, 12 Sep 2024 14:34:08 +1000 Subject: [PATCH 12/16] Move option definition up a level for scope usage --- src/Element/Filter/ScopeDefinition.php | 23 +++++++++++++ src/Element/Form/FieldDefinition.php | 3 +- ...ionDefinition.php => OptionDefinition.php} | 32 +++++++++---------- 3 files changed, 41 insertions(+), 17 deletions(-) rename src/Element/{Form/FieldOptionDefinition.php => OptionDefinition.php} (53%) diff --git a/src/Element/Filter/ScopeDefinition.php b/src/Element/Filter/ScopeDefinition.php index 832e8c5ba..7b1fc41b5 100644 --- a/src/Element/Filter/ScopeDefinition.php +++ b/src/Element/Filter/ScopeDefinition.php @@ -1,6 +1,7 @@ options(); + } + + $result = []; + + foreach ($options as $value => $option) { + $definition = (new OptionDefinition) + ->value($value) + ->useOptionConfig($option); + + $result[$value] = $definition; + } + + return $result; + } + /** * setScopeValue and merge the values as config */ diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index be5c309f8..c35519f3f 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -1,6 +1,7 @@ $option) { - $definition = (new FieldOptionDefinition) + $definition = (new OptionDefinition) ->value($value) ->useOptionConfig($option); diff --git a/src/Element/Form/FieldOptionDefinition.php b/src/Element/OptionDefinition.php similarity index 53% rename from src/Element/Form/FieldOptionDefinition.php rename to src/Element/OptionDefinition.php index e84d85989..33822a75f 100644 --- a/src/Element/Form/FieldOptionDefinition.php +++ b/src/Element/OptionDefinition.php @@ -1,30 +1,30 @@ -label($option); @@ -84,7 +84,7 @@ protected function evalChildOptions(array $children): array $result = []; foreach ($children as $value => $option) { - $result[$value] = (new FieldOptionDefinition) + $result[$value] = (new OptionDefinition) ->value($value) ->useOptionConfig($option); } From 7f9349457d501a9711b94bcdd9d9c65db9a7b6e1 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 13 Sep 2024 15:36:30 +1000 Subject: [PATCH 13/16] Rem indentLevel since it isn't used (yet) --- src/Element/OptionDefinition.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Element/OptionDefinition.php b/src/Element/OptionDefinition.php index 33822a75f..26d09ac9b 100644 --- a/src/Element/OptionDefinition.php +++ b/src/Element/OptionDefinition.php @@ -18,8 +18,7 @@ * @method OptionDefinition cssColor(string $cssColor) cssColor defines a status indicator color for the option (dropdown) * @method OptionDefinition icon(string $icon) icon specifies an icon name for this option * @method OptionDefinition image(string $image) image specifies an image URL for this option - * @method OptionDefinition indentLevel(int $indentLevel) indentLevel sets the level that the option sits - * @method OptionDefinition children(array $image) children specifies child options as an alternative to indenting + * @method OptionDefinition children(array $image) children specifies child options for a nested structure * * @package october\element * @author Alexey Bobkov, Samuel Georges From 7045215ab1e32727a441a6086b8da23727e5fb4d Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 16 Sep 2024 09:43:25 +1000 Subject: [PATCH 14/16] =?UTF-8?q?cssColor=20=E2=86=92=20color?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Element/OptionDefinition.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Element/OptionDefinition.php b/src/Element/OptionDefinition.php index 26d09ac9b..5d151a90f 100644 --- a/src/Element/OptionDefinition.php +++ b/src/Element/OptionDefinition.php @@ -15,7 +15,7 @@ * @method OptionDefinition readOnly(bool $readOnly) readOnly specifies if the option is read-only or not. * @method OptionDefinition disabled(bool $disabled) disabled specifies if the option is disabled or not. * @method OptionDefinition hidden(bool $hidden) hidden defines the option without ever displaying it - * @method OptionDefinition cssColor(string $cssColor) cssColor defines a status indicator color for the option (dropdown) + * @method OptionDefinition color(string $color) color defines a status indicator color for the option as a hex color (dropdown) * @method OptionDefinition icon(string $icon) icon specifies an icon name for this option * @method OptionDefinition image(string $image) image specifies an image URL for this option * @method OptionDefinition children(array $image) children specifies child options for a nested structure @@ -63,7 +63,7 @@ public function useOptionConfig($option): OptionDefinition $this->comment($secondPart); if (Html::isValidColor($secondPart)) { - $this->cssColor($secondPart); + $this->color($secondPart); } elseif (strpos($secondPart, '.')) { $this->image($secondPart); From 38750a96e726b0c4524fe0b14a5314a9c7cc8fd2 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 8 Oct 2024 17:08:29 +1100 Subject: [PATCH 15/16] Take value as label by default --- src/Element/Filter/ScopeDefinition.php | 6 +----- src/Element/Form/FieldDefinition.php | 6 +----- src/Element/OptionDefinition.php | 8 ++++---- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Element/Filter/ScopeDefinition.php b/src/Element/Filter/ScopeDefinition.php index 7b1fc41b5..0cf74df31 100644 --- a/src/Element/Filter/ScopeDefinition.php +++ b/src/Element/Filter/ScopeDefinition.php @@ -117,11 +117,7 @@ public function asOptionsDefinition($options = null) $result = []; foreach ($options as $value => $option) { - $definition = (new OptionDefinition) - ->value($value) - ->useOptionConfig($option); - - $result[$value] = $definition; + $result[$value] = (new OptionDefinition)->useOptionConfig($value, $option); } return $result; diff --git a/src/Element/Form/FieldDefinition.php b/src/Element/Form/FieldDefinition.php index c35519f3f..0e5651ddb 100644 --- a/src/Element/Form/FieldDefinition.php +++ b/src/Element/Form/FieldDefinition.php @@ -157,11 +157,7 @@ public function asOptionsDefinition($options = null) $result = []; foreach ($options as $value => $option) { - $definition = (new OptionDefinition) - ->value($value) - ->useOptionConfig($option); - - $result[$value] = $definition; + $result[$value] = (new OptionDefinition)->useOptionConfig($value, $option); } return $result; diff --git a/src/Element/OptionDefinition.php b/src/Element/OptionDefinition.php index 5d151a90f..c5604a0ce 100644 --- a/src/Element/OptionDefinition.php +++ b/src/Element/OptionDefinition.php @@ -40,8 +40,10 @@ protected function initDefaultValues() /** * useOptionConfig */ - public function useOptionConfig($option): OptionDefinition + public function useOptionConfig($value, $option = null): OptionDefinition { + $this->value($value)->label($value); + if (!is_array($option)) { $this->label($option); return $this; @@ -83,9 +85,7 @@ protected function evalChildOptions(array $children): array $result = []; foreach ($children as $value => $option) { - $result[$value] = (new OptionDefinition) - ->value($value) - ->useOptionConfig($option); + $result[$value] = (new OptionDefinition)->useOptionConfig($value, $option); } return $result; From 61e32e135d69cf7ed8a6b390e532dd64390b3150 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Tue, 8 Oct 2024 17:13:35 +1100 Subject: [PATCH 16/16] Comments --- src/Element/OptionDefinition.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Element/OptionDefinition.php b/src/Element/OptionDefinition.php index c5604a0ce..adecd6e9e 100644 --- a/src/Element/OptionDefinition.php +++ b/src/Element/OptionDefinition.php @@ -40,15 +40,17 @@ protected function initDefaultValues() /** * useOptionConfig */ - public function useOptionConfig($value, $option = null): OptionDefinition + public function useOptionConfig($value, $option): OptionDefinition { $this->value($value)->label($value); + // Option as string if (!is_array($option)) { $this->label($option); return $this; } + // Option as definition if (Arr::isAssoc($option)) { if (isset($option['children']) && is_array($option['children'])) { $option['children'] = $this->evalChildOptions($option['children']); @@ -58,6 +60,7 @@ public function useOptionConfig($value, $option = null): OptionDefinition return $this; } + // Option as [label, comment] $firstPart = (string) ($option[0] ?? ''); $secondPart = (string) ($option[1] ?? '');