From ed62b22c0cbff9489cbf083f3af439316da3c545 Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Mon, 24 Oct 2022 15:49:40 +0200 Subject: [PATCH 1/2] wip --- app/Filament/Resources/UserResource.php | 9 +++- .../ProjectsRelationManager.php | 52 +++++++++++++++++++ lang/en/projects.php | 3 +- lang/nl/projects.php | 3 +- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php diff --git a/app/Filament/Resources/UserResource.php b/app/Filament/Resources/UserResource.php index 86e1dcee..5c16af2a 100644 --- a/app/Filament/Resources/UserResource.php +++ b/app/Filament/Resources/UserResource.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources; +use App\Models\Project; use Filament\Forms; use App\Models\User; use Filament\Tables; @@ -53,11 +54,17 @@ public static function table(Table $table): Table public static function getRelations(): array { - return [ + $relationManagers = [ RelationManagers\ItemsRelationManager::class, RelationManagers\CommentsRelationManager::class, RelationManagers\VotesRelationManager::class, ]; + + if (Project::query()->where('private', '=', true)->count() > 0) { + $relationManagers[] = RelationManagers\ProjectsRelationManager::class; + } + + return $relationManagers; } public static function getPages(): array diff --git a/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php b/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php new file mode 100644 index 00000000..1b428892 --- /dev/null +++ b/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php @@ -0,0 +1,52 @@ +schema([ + Forms\Components\TextInput::make('name') + ->required() + ->maxLength(255), + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + Tables\Columns\TextColumn::make('title'), + ]) + ->filters([ + // + ]) + ->headerActions([ + Tables\Actions\AttachAction::make() + ->recordSelectOptionsQuery(fn (Builder $query): Builder => $query->where('private', '=', true)) + ->recordSelect(fn (Forms\Components\Select $select) => $select->helperText(__('projects.select-hidden-projects'))) + ->inverseRelationshipName('members') + ->preloadRecordSelect(), + ]) + ->actions([ + Tables\Actions\DetachAction::make(), + ]) + ->bulkActions([ + Tables\Actions\DetachBulkAction::make(), + ]); + } +} diff --git a/lang/en/projects.php b/lang/en/projects.php index 638679c6..9a52743f 100644 --- a/lang/en/projects.php +++ b/lang/en/projects.php @@ -2,5 +2,6 @@ return [ 'projects' => 'Projects', - 'no-projects' => 'There are no projects.' + 'no-projects' => 'There are no projects.', + 'select-hidden-projects' => 'You can only select projects where there private option is enabled.', ]; diff --git a/lang/nl/projects.php b/lang/nl/projects.php index e4b8c976..ddf91801 100644 --- a/lang/nl/projects.php +++ b/lang/nl/projects.php @@ -2,5 +2,6 @@ return [ 'projects' => 'Projecten', - 'no-projects' => 'Er zijn geen projecten.' + 'no-projects' => 'Er zijn geen projecten.', + 'select-hidden-projects' => 'Je kunt alleen projecten selecteren waar de private optie is ingeschakeld.', ]; From 17f760debcacc60b2f07c5542a4ff0be567f78bc Mon Sep 17 00:00:00 2001 From: Sebastiaan Kloos Date: Mon, 24 Oct 2022 16:11:27 +0200 Subject: [PATCH 2/2] wip --- app/Filament/Resources/UserResource.php | 9 ++------- .../RelationManagers/ProjectsRelationManager.php | 8 +++++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/UserResource.php b/app/Filament/Resources/UserResource.php index 5c16af2a..bcb33bf4 100644 --- a/app/Filament/Resources/UserResource.php +++ b/app/Filament/Resources/UserResource.php @@ -54,17 +54,12 @@ public static function table(Table $table): Table public static function getRelations(): array { - $relationManagers = [ + return [ RelationManagers\ItemsRelationManager::class, RelationManagers\CommentsRelationManager::class, RelationManagers\VotesRelationManager::class, + RelationManagers\ProjectsRelationManager::class, ]; - - if (Project::query()->where('private', '=', true)->count() > 0) { - $relationManagers[] = RelationManagers\ProjectsRelationManager::class; - } - - return $relationManagers; } public static function getPages(): array diff --git a/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php b/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php index 1b428892..1d4818e6 100644 --- a/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php +++ b/app/Filament/Resources/UserResource/RelationManagers/ProjectsRelationManager.php @@ -2,13 +2,14 @@ namespace App\Filament\Resources\UserResource\RelationManagers; +use App\Models\Project; use Filament\Forms; use Filament\Resources\Form; use Filament\Resources\RelationManagers\RelationManager; use Filament\Resources\Table; use Filament\Tables; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\SoftDeletingScope; +use Illuminate\Database\Eloquent\Model; class ProjectsRelationManager extends RelationManager { @@ -16,6 +17,11 @@ class ProjectsRelationManager extends RelationManager protected static ?string $recordTitleAttribute = 'title'; + public static function canViewForRecord(Model $ownerRecord): bool + { + return Project::query()->where('private', '=', true)->count() > 0; + } + public static function form(Form $form): Form { return $form