From 5e7cc82aa91429ba26f6fc94f849c98fb8e3a8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Tue, 12 Nov 2024 09:14:28 +0100 Subject: [PATCH 1/4] Add JSON type CF Add JSON type CF --- config/asseco-custom-fields.php | 2 + ...type_to_custom_field_plain_types_table.php | 50 +++++++++++++++++++ ..._add_json_to_custom_field_values_table.php | 32 ++++++++++++ src/App/Contracts/PlainTypes/JsonType.php | 12 +++++ src/App/Http/Requests/ValueRequest.php | 1 + src/App/Models/Value.php | 7 ++- src/App/PlainTypes/JsonType.php | 24 +++++++++ 7 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php create mode 100644 migrations/2024_11_12_073245_add_json_to_custom_field_values_table.php create mode 100644 src/App/Contracts/PlainTypes/JsonType.php create mode 100644 src/App/PlainTypes/JsonType.php diff --git a/config/asseco-custom-fields.php b/config/asseco-custom-fields.php index 36da062..1f77fd5 100644 --- a/config/asseco-custom-fields.php +++ b/config/asseco-custom-fields.php @@ -16,6 +16,7 @@ use Asseco\CustomFields\App\PlainTypes\DateType; use Asseco\CustomFields\App\PlainTypes\FloatType; use Asseco\CustomFields\App\PlainTypes\IntegerType; +use Asseco\CustomFields\App\PlainTypes\JsonType; use Asseco\CustomFields\App\PlainTypes\StringType; use Asseco\CustomFields\App\PlainTypes\TextType; use Asseco\CustomFields\App\PlainTypes\TimeType; @@ -48,6 +49,7 @@ 'string' => StringType::class, 'text' => TextType::class, 'time' => TimeType::class, + 'json' => JsonType::class, ], 'migrations' => [ diff --git a/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php new file mode 100644 index 0000000..770c3e2 --- /dev/null +++ b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php @@ -0,0 +1,50 @@ + Str::uuid(), + 'name' => $type, + 'created_at' => now(), + 'updated_at' => now(), + ]; + } else { + $plainTypes[] = [ + 'name' => $type, + 'created_at' => now(), + 'updated_at' => now(), + ]; + } + } + + DB::table('custom_field_plain_types')->insert($plainTypes); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::table('custom_field_plain_types') + ->where('name', 'json') + ->delete(); + } +}; diff --git a/migrations/2024_11_12_073245_add_json_to_custom_field_values_table.php b/migrations/2024_11_12_073245_add_json_to_custom_field_values_table.php new file mode 100644 index 0000000..a325d1a --- /dev/null +++ b/migrations/2024_11_12_073245_add_json_to_custom_field_values_table.php @@ -0,0 +1,32 @@ +json('json')->nullable()->default(null)->after('time'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('custom_field_values', function (Blueprint $table) { + $table->dropColumn('json'); + }); + } +}; diff --git a/src/App/Contracts/PlainTypes/JsonType.php b/src/App/Contracts/PlainTypes/JsonType.php new file mode 100644 index 0000000..6ec4cea --- /dev/null +++ b/src/App/Contracts/PlainTypes/JsonType.php @@ -0,0 +1,12 @@ + 'nullable|string', 'date' => 'nullable|string', 'time' => 'nullable|string', + 'json' => 'nullable|array', ]; } diff --git a/src/App/Models/Value.php b/src/App/Models/Value.php index 92c23ae..72988b7 100644 --- a/src/App/Models/Value.php +++ b/src/App/Models/Value.php @@ -23,7 +23,7 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value * Columns which are classified as value columns. */ public const VALUE_COLUMNS = [ - 'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time', + 'string', 'integer', 'float', 'text', 'boolean', 'datetime', 'date', 'time', 'json', ]; /** @@ -36,6 +36,11 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value protected $guarded = ['id', 'created_at', 'updated_at']; protected $appends = ['value']; + protected $casts = [ + 'float' => 'float', + 'boolean' => 'boolean', + 'json' => 'array', + ]; protected static function booted() { diff --git a/src/App/PlainTypes/JsonType.php b/src/App/PlainTypes/JsonType.php new file mode 100644 index 0000000..cd7a244 --- /dev/null +++ b/src/App/PlainTypes/JsonType.php @@ -0,0 +1,24 @@ +where('name', 'json'); + }); + } + + public static function mapToValueColumn(): string + { + return 'json'; + } +} From a785a2660e989a8a27ffefff407b4d8d60a5da1a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 12 Nov 2024 08:14:41 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/App/Models/Value.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App/Models/Value.php b/src/App/Models/Value.php index 72988b7..62f4f6f 100644 --- a/src/App/Models/Value.php +++ b/src/App/Models/Value.php @@ -37,9 +37,9 @@ class Value extends Model implements \Asseco\CustomFields\App\Contracts\Value protected $appends = ['value']; protected $casts = [ - 'float' => 'float', - 'boolean' => 'boolean', - 'json' => 'array', + 'float' => 'float', + 'boolean' => 'boolean', + 'json' => 'array', ]; protected static function booted() From de8c2a8b1b96977d6d1cbaa67eddf2ac105bd097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Ga=C5=A1pari=C4=87?= Date: Tue, 12 Nov 2024 09:19:45 +0100 Subject: [PATCH 3/4] Update 2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php a --- ...145_add_json_type_to_custom_field_plain_types_table.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php index 770c3e2..58bd2c8 100644 --- a/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php +++ b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php @@ -13,6 +13,13 @@ */ public function up() { + + $exists = DB::table('custom_field_plain_types')->where('name', 'json')->exists(); + if ($exists) { + // already exists + return; + } + $types = ['json']; $plainTypes = []; From 123c822c480d36c4b27ed456aec8e2d4d4444c8e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 12 Nov 2024 08:20:10 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- ...12_073145_add_json_type_to_custom_field_plain_types_table.php | 1 - 1 file changed, 1 deletion(-) diff --git a/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php index 58bd2c8..f7ddfbf 100644 --- a/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php +++ b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php @@ -13,7 +13,6 @@ */ public function up() { - $exists = DB::table('custom_field_plain_types')->where('name', 'json')->exists(); if ($exists) { // already exists