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..f7ddfbf --- /dev/null +++ b/migrations/2024_11_12_073145_add_json_type_to_custom_field_plain_types_table.php @@ -0,0 +1,56 @@ +where('name', 'json')->exists(); + if ($exists) { + // already exists + return; + } + + $types = ['json']; + + $plainTypes = []; + foreach ($types as $type) { + if (config('asseco-custom-fields.migrations.uuid')) { + $plainTypes[] = [ + 'id' => 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..62f4f6f 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'; + } +}