Skip to content

Commit 0777300

Browse files
committed
add subdomain relation manager to server (admin)
1 parent e61e257 commit 0777300

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

subdomains/lang/en/strings.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
'no_subdomains' => 'No Subdomains',
88
'subdomain' => 'Subdomain|Subdomains',
9-
'limit' => 'Subdomain Limit Reached',
9+
'limit' => 'Limit',
10+
'change_limit' => 'Change Limit',
11+
'limit_changed' => 'Limit changed',
12+
'limit_reached' => 'Subdomain Limit Reached',
1013
'create_subdomain' => 'Create Subdomain',
1114

1215
'name' => 'Name',

subdomains/src/Filament/Admin/Resources/CloudflareDomains/CloudflareDomainResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static function table(Table $table): Table
7171
public static function form(Schema $schema): Schema
7272
{
7373
return $schema
74+
->columns(1)
7475
->components([
7576
TextInput::make('name')
7677
->label(trans('subdomains::strings.name'))
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

subdomains/src/Filament/Server/Resources/Subdomains/SubdomainResource.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function table(Table $table): Table
8686
->toolbarActions([
8787
CreateAction::make()
8888
->icon('tabler-world-plus')
89-
->tooltip(fn () => static::getBadgeCount() >= static::getBadgeLimit() ? trans('subdomains::strings.limit') : trans('subdomains::strings.create_subdomain'))
89+
->tooltip(fn () => static::getBadgeCount() >= static::getBadgeLimit() ? trans('subdomains::strings.limit_reached') : trans('subdomains::strings.create_subdomain'))
9090
->disabled(fn () => static::getBadgeCount() >= static::getBadgeLimit())
9191
->color(fn () => static::getBadgeCount() >= static::getBadgeLimit() ? 'danger' : 'primary')
9292
->createAnother(false)
@@ -106,6 +106,7 @@ public static function form(Schema $schema): Schema
106106
->unique(),
107107
Select::make('domain_id')
108108
->label(trans_choice('subdomains::strings.domain', 1))
109+
->disabledOn('edit')
109110
->required()
110111
->relationship('domain', 'name')
111112
->preload()

subdomains/src/Providers/SubdomainsPluginProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Boy132\Subdomains\Providers;
44

5+
use App\Filament\Admin\Resources\Servers\ServerResource;
56
use App\Models\Role;
67
use App\Models\Server;
8+
use Boy132\Subdomains\Filament\Admin\Resources\Users\RelationManagers\SubdomainRelationManager;
79
use Boy132\Subdomains\Models\Subdomain;
810
use Illuminate\Support\Facades\Http;
911
use Illuminate\Support\ServiceProvider;
@@ -12,6 +14,8 @@ class SubdomainsPluginProvider extends ServiceProvider
1214
{
1315
public function register(): void
1416
{
17+
ServerResource::registerCustomRelations(SubdomainRelationManager::class);
18+
1519
Role::registerCustomDefaultPermissions('cloudflare_domain');
1620
Role::registerCustomModelIcon('cloudflare_domain', 'tabler-world-www');
1721
}

0 commit comments

Comments
 (0)