|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Boy132\Subdomains\Filament\Admin\Resources\Users\RelationManagers; |
| 4 | + |
| 5 | +use App\Models\Server; |
| 6 | +use Boy132\Subdomains\Models\CloudflareDomain; |
| 7 | +use Boy132\Subdomains\Models\Subdomain; |
| 8 | +use Filament\Actions\Action; |
| 9 | +use Filament\Actions\CreateAction; |
| 10 | +use Filament\Actions\DeleteAction; |
| 11 | +use Filament\Actions\EditAction; |
| 12 | +use Filament\Forms\Components\Hidden; |
| 13 | +use Filament\Forms\Components\Select; |
| 14 | +use Filament\Forms\Components\TextInput; |
| 15 | +use Filament\Notifications\Notification; |
| 16 | +use Filament\Resources\RelationManagers\RelationManager; |
| 17 | +use Filament\Schemas\Schema; |
| 18 | +use Filament\Tables\Columns\TextColumn; |
| 19 | +use Filament\Tables\Table; |
| 20 | + |
| 21 | +/** |
| 22 | + * @method Server getOwnerRecord() |
| 23 | + */ |
| 24 | +class SubdomainRelationManager extends RelationManager |
| 25 | +{ |
| 26 | + protected static string $relationship = 'subdomains'; |
| 27 | + |
| 28 | + public function table(Table $table): Table |
| 29 | + { |
| 30 | + return $table |
| 31 | + ->heading(fn () => trans_choice('subdomains::strings.subdomain', 2) . ' (' . trans('subdomains::strings.limit') .': ' . ($this->getOwnerRecord()->subdomain_limit ?? 0) . ')') |
| 32 | + ->columns([ |
| 33 | + TextColumn::make('label') |
| 34 | + ->label(trans('subdomains::strings.name')) |
| 35 | + ->state(fn (Subdomain $subdomain) => $subdomain->getLabel()), |
| 36 | + ]) |
| 37 | + ->recordActions([ |
| 38 | + EditAction::make(), |
| 39 | + DeleteAction::make(), |
| 40 | + ]) |
| 41 | + ->headerActions([ |
| 42 | + Action::make('change_limit') |
| 43 | + ->label(trans('subdomains::strings.change_limit')) |
| 44 | + ->schema([ |
| 45 | + TextInput::make('limit') |
| 46 | + ->label(trans('subdomains::strings.limit')) |
| 47 | + ->numeric() |
| 48 | + ->required() |
| 49 | + ->default($this->getOwnerRecord()->subdomain_limit ?? 0) |
| 50 | + ->minValue(0), |
| 51 | + ]) |
| 52 | + ->action(function ($data) { |
| 53 | + $oldLimit = $this->getOwnerRecord()->subdomain_limit ?? 0; |
| 54 | + $newLimit = $data['limit']; |
| 55 | + |
| 56 | + $this->getOwnerRecord()->update(['subdomain_limit' => $newLimit]); |
| 57 | + |
| 58 | + Notification::make() |
| 59 | + ->title(trans('subdomains::strings.limit_changed')) |
| 60 | + ->body($oldLimit . ' -> ' . $newLimit) |
| 61 | + ->success() |
| 62 | + ->send(); |
| 63 | + }), |
| 64 | + CreateAction::make() |
| 65 | + ->visible(fn () => CloudflareDomain::count() > 0) |
| 66 | + ->disabled(fn () => !$this->getOwnerRecord()->allocation || $this->getOwnerRecord()->allocation->ip === '0.0.0.0' || $this->getOwnerRecord()->allocation->ip === '::') |
| 67 | + ->createAnother(false), |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + public function form(Schema $schema): Schema |
| 72 | + { |
| 73 | + return $schema |
| 74 | + ->components([ |
| 75 | + TextInput::make('name') |
| 76 | + ->label(trans('subdomains::strings.name')) |
| 77 | + ->required() |
| 78 | + ->unique(), |
| 79 | + Select::make('domain_id') |
| 80 | + ->label(trans_choice('subdomains::strings.domain', 1)) |
| 81 | + ->disabledOn('edit') |
| 82 | + ->required() |
| 83 | + ->relationship('domain', 'name') |
| 84 | + ->preload() |
| 85 | + ->searchable(), |
| 86 | + Hidden::make('record_type') |
| 87 | + ->default(fn () => is_ipv6($this->getOwnerRecord()->allocation->ip) ? 'AAAA' : 'A'), |
| 88 | + ]); |
| 89 | + } |
| 90 | +} |
0 commit comments