diff --git a/database/migrations/2024_03_06_094857_create_tables_table.php b/database/migrations/2024_03_06_094857_create_tables_table.php index 2a8f0cf..b53eee9 100644 --- a/database/migrations/2024_03_06_094857_create_tables_table.php +++ b/database/migrations/2024_03_06_094857_create_tables_table.php @@ -33,6 +33,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('tables'); + Schema::dropIfExists(config('filament-plugins.database_prefix') ? config('filament-plugins.database_prefix') . '_tables' : 'tables'); } }; diff --git a/database/migrations/2024_03_06_095207_create_table_cols_table.php b/database/migrations/2024_03_06_095207_create_table_cols_table.php index 664123d..2e35ffe 100644 --- a/database/migrations/2024_03_06_095207_create_table_cols_table.php +++ b/database/migrations/2024_03_06_095207_create_table_cols_table.php @@ -44,6 +44,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('table_cols'); + Schema::dropIfExists(config('filament-plugins.database_prefix') ? config('filament-plugins.database_prefix') . '_table_cols' : 'table_cols'); } }; diff --git a/database/migrations/2024_06_06_094857_update_table_cols_table.php b/database/migrations/2024_06_06_094857_update_table_cols_table.php new file mode 100644 index 0000000..c91c29c --- /dev/null +++ b/database/migrations/2024_06_06_094857_update_table_cols_table.php @@ -0,0 +1,28 @@ +integer('order')->default(0)->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table(config('filament-plugins.database_prefix') ? config('filament-plugins.database_prefix') . '_table_cols' : 'table_cols', function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +}; diff --git a/resources/lang/ar/messages.php b/resources/lang/ar/messages.php index c082fef..80c8d55 100644 --- a/resources/lang/ar/messages.php +++ b/resources/lang/ar/messages.php @@ -43,7 +43,11 @@ 'import' => [ 'title' => 'عملية ناجحة', 'body' => 'تم استيراد الاضافة بنجاح' - ] + ], + 'created' => [ + 'title' => 'عملية ناجحة', + 'body' => 'تم إنشاء الاضافة بنجاح' + ], ] ], 'tables' => [ @@ -54,6 +58,8 @@ 'form' => [ 'name' => 'الاسم', 'type' => 'النوع', + 'soft_deletes' => 'السماح بالحذف الناعم', + 'timestamps' => 'السماح بالوقت', 'nullable' => 'يمكن أن يكون فارغاً', 'foreign' => 'خارجي', 'foreign_table' => 'جدول خارجي', diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index ae6bfc7..4f27952 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -43,7 +43,11 @@ 'import' => [ 'title' => 'Success', 'body' => 'The plugin has been imported successfully.' - ] + ], + 'created' => [ + 'title' => 'Success', + 'body' => 'The plugin has been created successfully.' + ], ] ], 'tables' => [ @@ -53,6 +57,8 @@ 'columns' => 'Table Columns', 'form' => [ 'name' => 'Name', + 'soft_deletes' => 'Allow Soft Delete', + 'timestamps' => 'Allow Timestamps', 'type' => 'Type', 'nullable' => 'Nullable', 'foreign' => 'Foreign', @@ -73,7 +79,7 @@ ], 'actions' => [ 'create' => 'Create Table', - 'migrate' => 'Migrate', + 'migrate' => 'Create Migration', 'generate' => 'Generate', 'columns' => 'Add Column', 'add-id' => 'Add ID Column', diff --git a/resources/lang/nl/messages.php b/resources/lang/nl/messages.php index 856d988..12d2404 100644 --- a/resources/lang/nl/messages.php +++ b/resources/lang/nl/messages.php @@ -43,7 +43,11 @@ 'import' => [ 'title' => 'Success', 'body' => 'The plugin has been imported successfully.' - ] + ], + 'created' => [ + 'title' => 'Success', + 'body' => 'The plugin has been created successfully.' + ], ] ], 'tables' => [ diff --git a/src/Models/TableCol.php b/src/Models/TableCol.php index 03cfe30..0b63f20 100644 --- a/src/Models/TableCol.php +++ b/src/Models/TableCol.php @@ -32,7 +32,7 @@ class TableCol extends Model /** * @var array */ - protected $fillable = ['table_id', 'name', 'type', 'length', 'default', 'comment', 'foreign_table', 'foreign_col', 'foreign_model', 'nullable', 'index', 'auto_increment', 'primary', 'unique', 'unsigned', 'foreign', 'foreign_on_delete_cascade', 'created_at', 'updated_at']; + protected $fillable = ['order','table_id', 'name', 'type', 'length', 'default', 'comment', 'foreign_table', 'foreign_col', 'foreign_model', 'nullable', 'index', 'auto_increment', 'primary', 'unique', 'unsigned', 'foreign', 'foreign_on_delete_cascade', 'created_at', 'updated_at']; protected $casts = [ 'nullable' => 'boolean', diff --git a/src/Pages/Plugins.php b/src/Pages/Plugins.php index 977b596..a777dc2 100644 --- a/src/Pages/Plugins.php +++ b/src/Pages/Plugins.php @@ -225,8 +225,8 @@ public function createPlugin(array $data) $generator->generate(); Notification::make() - ->title(trans('filament-plugins::messages.plugins.notification.imported.title')) - ->body(trans('filament-plugins::messages.plugins.notification.imported.body')) + ->title(trans('filament-plugins::messages.plugins.notification.created.title')) + ->body(trans('filament-plugins::messages.plugins.notification.created.body')) ->success() ->send(); } diff --git a/src/Resources/TableResource.php b/src/Resources/TableResource.php index 91c6754..15f72a1 100644 --- a/src/Resources/TableResource.php +++ b/src/Resources/TableResource.php @@ -37,6 +37,12 @@ public static function form(Form $form): Form ->columnSpan(2) ->required() ->maxLength(255), + Forms\Components\Toggle::make('timestamps') + ->label(trans('filament-plugins::messages.tables.form.timestamps')) + ->default(true), + Forms\Components\Toggle::make('soft_deletes') + ->default(0) + ->label(trans('filament-plugins::messages.tables.form.soft_deletes')) ])->viewData([ 'module' => request()->get('module'), ]); diff --git a/src/Resources/TableResource/Pages/CreateTable.php b/src/Resources/TableResource/Pages/CreateTable.php index 998a9ef..9c6c9b6 100644 --- a/src/Resources/TableResource/Pages/CreateTable.php +++ b/src/Resources/TableResource/Pages/CreateTable.php @@ -30,4 +30,16 @@ protected function mutateFormDataBeforeCreate(array $data): array $data['module'] = $this->module; return $data; } + + protected function afterCreate(): void + { + $this->getRecord()->tableCols()->create([ + 'name' => 'id', + 'type' => 'bigint', + 'unsigned' => true, + 'auto_increment' => true, + 'primary' => true + ]); + } + } diff --git a/src/Resources/TableResource/Pages/ListTables.php b/src/Resources/TableResource/Pages/ListTables.php index f343412..7f9d512 100644 --- a/src/Resources/TableResource/Pages/ListTables.php +++ b/src/Resources/TableResource/Pages/ListTables.php @@ -15,6 +15,13 @@ public function getTitle(): string return trans('filament-plugins::messages.tables.title'); } + public function mount(): void + { + if(!request()->has('module')){ + $this->redirect(route('filament.'.filament()->getCurrentPanel()->getId().'.pages.plugins')); + } + } + protected function getHeaderActions(): array { return [ diff --git a/src/Resources/TableResource/RelationManagers/TableColsRelationManager.php b/src/Resources/TableResource/RelationManagers/TableColsRelationManager.php index 4b2352d..596febd 100644 --- a/src/Resources/TableResource/RelationManagers/TableColsRelationManager.php +++ b/src/Resources/TableResource/RelationManagers/TableColsRelationManager.php @@ -105,7 +105,7 @@ public function form(Form $form): Form public function table(Table $table): Table { return $table - ->reorderable('id') + ->reorderable('order') ->recordTitleAttribute('name') ->columns([ Tables\Columns\TextColumn::make('name') @@ -156,41 +156,6 @@ public function table(Table $table): Table 'primary' => true, ]); }), - Tables\Actions\Action::make('timestamps') - ->color('info') - ->requiresConfirmation() - ->icon('heroicon-s-plus') - ->label(trans('filament-plugins::messages.tables.actions.add-timestamps')) - ->action(function () { - $this->ownerRecord->tableCols()->createMany( - [ - [ - 'name' => 'created_at', - 'type' => 'datetime', - 'nullable' => true, - ], - [ - 'name' => 'updated_at', - 'type' => 'datetime', - 'nullable' => true, - ], - ] - ); - }), - Tables\Actions\Action::make('soft_deletes') - ->color('info') - ->requiresConfirmation() - ->icon('heroicon-s-plus') - ->label(trans('filament-plugins::messages.tables.actions.add-softdeletes')) - ->action(function () { - $this->ownerRecord->tableCols()->create( - [ - 'name' => 'deleted_at', - 'type' => 'datetime', - 'nullable' => true, - ] - ); - }) ]), @@ -199,6 +164,7 @@ public function table(Table $table): Table Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make(), ]) + ->defaultSort('order') ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), diff --git a/src/Services/Concerns/GenerateMigrations.php b/src/Services/Concerns/GenerateMigrations.php index 5a53c37..7402251 100644 --- a/src/Services/Concerns/GenerateMigrations.php +++ b/src/Services/Concerns/GenerateMigrations.php @@ -33,7 +33,7 @@ private function generateMigrations(): void module_path($this->moduleName) ."/database/migrations/". date('Y_m_d_h_mm_ss') . '_create_' . $this->tableName . '_table.php', [ "table" => $this->tableName, - "fields" => $this->getFields($this->table->tableCols) + "fields" => $this->getFields($this->table->tableCols()->orderBy('order')->get()) ], ); } @@ -78,6 +78,10 @@ private function getFields($fields): string } } + if(isset($field->unsigned) && $field->unsigned && $field->name !== 'id' && !in_array($field->type, ['string', 'text', 'longText', 'json'])){ + $finalFields .= "->unsigned()"; + } + if(isset($field->nullable) && $field->nullable){ $finalFields .= "->nullable()"; } @@ -87,10 +91,6 @@ private function getFields($fields): string $finalFields .= "->unique()"; } - if(isset($field->unsigned) && $field->unsigned && $field->name !== 'id'){ - $finalFields .= "->unsigned()"; - } - if(isset($field->comment)){ $finalFields .= "->comment('".$field->comment."')"; }