Skip to content

Commit

Permalink
Soft deletes not set #16
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Jun 24, 2024
1 parent ccd4007 commit b7b660b
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
};
28 changes: 28 additions & 0 deletions database/migrations/2024_06_06_094857_update_table_cols_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table(config('filament-plugins.database_prefix') ? config('filament-plugins.database_prefix') . '_table_cols' : 'table_cols', function (Blueprint $table) {
$table->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');
});
}
};
8 changes: 7 additions & 1 deletion resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
'import' => [
'title' => 'عملية ناجحة',
'body' => 'تم استيراد الاضافة بنجاح'
]
],
'created' => [
'title' => 'عملية ناجحة',
'body' => 'تم إنشاء الاضافة بنجاح'
],
]
],
'tables' => [
Expand All @@ -54,6 +58,8 @@
'form' => [
'name' => 'الاسم',
'type' => 'النوع',
'soft_deletes' => 'السماح بالحذف الناعم',
'timestamps' => 'السماح بالوقت',
'nullable' => 'يمكن أن يكون فارغاً',
'foreign' => 'خارجي',
'foreign_table' => 'جدول خارجي',
Expand Down
10 changes: 8 additions & 2 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -53,6 +57,8 @@
'columns' => 'Table Columns',
'form' => [
'name' => 'Name',
'soft_deletes' => 'Allow Soft Delete',
'timestamps' => 'Allow Timestamps',
'type' => 'Type',
'nullable' => 'Nullable',
'foreign' => 'Foreign',
Expand All @@ -73,7 +79,7 @@
],
'actions' => [
'create' => 'Create Table',
'migrate' => 'Migrate',
'migrate' => 'Create Migration',
'generate' => 'Generate',
'columns' => 'Add Column',
'add-id' => 'Add ID Column',
Expand Down
6 changes: 5 additions & 1 deletion resources/lang/nl/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Models/TableCol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/Pages/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/TableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]);
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/TableResource/Pages/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}

}
7 changes: 7 additions & 0 deletions src/Resources/TableResource/Pages/ListTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
]
);
})
]),


Expand All @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions src/Services/Concerns/GenerateMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
],
);
}
Expand Down Expand Up @@ -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()";
}
Expand All @@ -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."')";
}
Expand Down

0 comments on commit b7b660b

Please sign in to comment.