2024-04-29 20:06:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
namespace App\Filament\Admin\Resources\Nodes\RelationManagers;
|
2024-04-29 20:06:34 -04:00
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
use App\Filament\Admin\Resources\Servers\Pages\CreateServer;
|
2024-04-29 20:06:34 -04:00
|
|
|
use App\Models\Allocation;
|
2024-06-11 21:00:51 +02:00
|
|
|
use App\Models\Node;
|
2024-04-29 20:06:34 -04:00
|
|
|
use App\Services\Allocations\AssignmentService;
|
2025-09-08 13:12:33 -04:00
|
|
|
use Exception;
|
|
|
|
|
use Filament\Actions\Action;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Filament\Actions\DeleteBulkAction;
|
2024-12-08 16:19:04 -05:00
|
|
|
use Filament\Forms\Components\Select;
|
2024-06-29 17:38:18 -04:00
|
|
|
use Filament\Forms\Components\TagsInput;
|
|
|
|
|
use Filament\Forms\Components\TextInput;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Filament\Resources\RelationManagers\RelationManager;
|
2025-09-08 13:12:33 -04:00
|
|
|
use Filament\Schemas\Components\Utilities\Get;
|
|
|
|
|
use Filament\Schemas\Components\Utilities\Set;
|
2025-01-06 03:37:39 +01:00
|
|
|
use Filament\Tables\Columns\SelectColumn;
|
2024-06-29 17:38:18 -04:00
|
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
|
use Filament\Tables\Columns\TextInputColumn;
|
2024-04-29 20:06:34 -04:00
|
|
|
use Filament\Tables\Table;
|
|
|
|
|
|
2024-06-11 21:00:51 +02:00
|
|
|
/**
|
|
|
|
|
* @method Node getOwnerRecord()
|
|
|
|
|
*/
|
2024-04-29 20:06:34 -04:00
|
|
|
class AllocationsRelationManager extends RelationManager
|
|
|
|
|
{
|
|
|
|
|
protected static string $relationship = 'allocations';
|
|
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
protected static string|\BackedEnum|null $icon = 'tabler-plug-connected';
|
2024-04-29 20:06:34 -04:00
|
|
|
|
2025-02-08 23:16:54 -05:00
|
|
|
public function setTitle(): string
|
2024-04-29 20:06:34 -04:00
|
|
|
{
|
2025-02-08 23:16:54 -05:00
|
|
|
return trans('admin/server.allocations');
|
2024-04-29 20:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 13:12:33 -04:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2024-04-29 20:06:34 -04:00
|
|
|
public function table(Table $table): Table
|
|
|
|
|
{
|
|
|
|
|
return $table
|
2025-05-09 08:44:18 +02:00
|
|
|
->recordTitleAttribute('address')
|
2024-11-13 16:22:12 -05:00
|
|
|
->checkIfRecordIsSelectableUsing(fn (Allocation $allocation) => $allocation->server_id === null)
|
2025-09-15 23:28:57 +02:00
|
|
|
->paginationPageOptions([10, 20, 50, 100, 200, 500])
|
2024-04-29 21:35:16 -04:00
|
|
|
->searchable()
|
2025-02-08 23:16:54 -05:00
|
|
|
->heading('')
|
2024-11-13 16:21:27 -05:00
|
|
|
->selectCurrentPageOnly() //Prevent people from trying to nuke 30,000 ports at once.... -,-
|
2024-04-29 20:06:34 -04:00
|
|
|
->columns([
|
2024-12-03 05:30:04 -05:00
|
|
|
TextColumn::make('id')
|
|
|
|
|
->toggleable()
|
|
|
|
|
->toggledHiddenByDefault(),
|
2024-06-29 17:38:18 -04:00
|
|
|
TextColumn::make('port')
|
2024-05-19 20:23:59 -04:00
|
|
|
->searchable()
|
2025-02-11 22:16:48 +01:00
|
|
|
->label(trans('admin/node.ports')),
|
2024-06-29 17:38:18 -04:00
|
|
|
TextColumn::make('server.name')
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/node.table.servers'))
|
2024-04-29 21:35:16 -04:00
|
|
|
->icon('tabler-brand-docker')
|
2024-12-03 05:30:04 -05:00
|
|
|
->visibleFrom('md')
|
2024-05-12 12:35:08 -04:00
|
|
|
->searchable()
|
2024-11-13 16:22:12 -05:00
|
|
|
->url(fn (Allocation $allocation): string => $allocation->server ? route('filament.admin.resources.servers.edit', ['record' => $allocation->server]) : ''),
|
2024-06-29 17:38:18 -04:00
|
|
|
TextInputColumn::make('ip_alias')
|
2024-05-12 12:35:08 -04:00
|
|
|
->searchable()
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/node.table.alias')),
|
2025-06-26 01:49:43 +02:00
|
|
|
TextInputColumn::make('notes')
|
|
|
|
|
->label(trans('admin/node.table.allocation_notes'))
|
|
|
|
|
->placeholder(trans('admin/node.table.no_notes')),
|
2025-01-06 03:37:39 +01:00
|
|
|
SelectColumn::make('ip')
|
2025-07-10 08:59:46 +02:00
|
|
|
->options(function (Allocation $allocation) {
|
|
|
|
|
$ips = Allocation::where('port', $allocation->port)->pluck('ip');
|
|
|
|
|
|
|
|
|
|
return collect($this->getOwnerRecord()->ipAddresses())
|
|
|
|
|
->diff($ips)
|
|
|
|
|
->unshift($allocation->ip)
|
|
|
|
|
->unique()
|
|
|
|
|
->mapWithKeys(fn (string $ip) => [$ip => $ip])
|
|
|
|
|
->all();
|
|
|
|
|
})
|
2025-01-06 03:37:39 +01:00
|
|
|
->selectablePlaceholder(false)
|
2024-05-12 12:35:08 -04:00
|
|
|
->searchable()
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/node.table.ip')),
|
2024-04-29 20:06:34 -04:00
|
|
|
])
|
|
|
|
|
->headerActions([
|
2025-05-09 08:44:18 +02:00
|
|
|
Action::make('create new allocation')
|
2025-02-10 16:28:14 +01:00
|
|
|
->label(trans('admin/node.create_allocation'))
|
2025-09-08 13:12:33 -04:00
|
|
|
->schema(fn () => [
|
2024-12-08 16:19:04 -05:00
|
|
|
Select::make('allocation_ip')
|
2025-09-23 14:56:49 +02:00
|
|
|
->options(fn () => collect($this->getOwnerRecord()->ipAddresses())->mapWithKeys(fn (string $ip) => [$ip => $ip]))
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/node.ip_address'))
|
2024-04-29 20:06:34 -04:00
|
|
|
->inlineLabel()
|
2025-05-09 08:44:18 +02:00
|
|
|
->ip()
|
2025-02-08 23:16:54 -05:00
|
|
|
->helperText(trans('admin/node.ip_help'))
|
2025-01-04 22:30:37 -05:00
|
|
|
->afterStateUpdated(fn (Set $set) => $set('allocation_ports', []))
|
|
|
|
|
->live()
|
2025-09-23 14:56:49 +02:00
|
|
|
->hintAction(
|
|
|
|
|
Action::make('refresh')
|
|
|
|
|
->iconButton()
|
|
|
|
|
->icon('tabler-refresh')
|
|
|
|
|
->tooltip(trans('admin/node.refresh'))
|
|
|
|
|
->action(function () {
|
|
|
|
|
cache()->forget("nodes.{$this->getOwnerRecord()->id}.ips");
|
|
|
|
|
})
|
|
|
|
|
)
|
2024-04-29 20:06:34 -04:00
|
|
|
->required(),
|
2024-06-29 17:38:18 -04:00
|
|
|
TextInput::make('allocation_alias')
|
2025-02-08 23:16:54 -05:00
|
|
|
->label(trans('admin/node.table.alias'))
|
2024-04-29 20:06:34 -04:00
|
|
|
->inlineLabel()
|
|
|
|
|
->default(null)
|
2025-06-26 01:49:43 +02:00
|
|
|
->helperText(trans('admin/node.alias_help')),
|
2024-06-29 17:38:18 -04:00
|
|
|
TagsInput::make('allocation_ports')
|
2025-02-08 23:16:54 -05:00
|
|
|
->placeholder('27015, 27017-27019')
|
|
|
|
|
->label(trans('admin/node.ports'))
|
2024-04-29 20:06:34 -04:00
|
|
|
->inlineLabel()
|
|
|
|
|
->live()
|
2025-01-04 22:30:37 -05:00
|
|
|
->disabled(fn (Get $get) => empty($get('allocation_ip')))
|
2025-05-09 08:44:18 +02:00
|
|
|
->afterStateUpdated(fn ($state, Set $set, Get $get) => $set('allocation_ports', CreateServer::retrieveValidPorts($this->getOwnerRecord(), $state, $get('allocation_ip'))))
|
2024-04-29 20:06:34 -04:00
|
|
|
->splitKeys(['Tab', ' ', ','])
|
|
|
|
|
->required(),
|
|
|
|
|
])
|
2024-11-13 16:22:12 -05:00
|
|
|
->action(fn (array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord(), $data)),
|
2024-04-29 20:06:34 -04:00
|
|
|
])
|
2025-05-09 08:44:18 +02:00
|
|
|
->groupedBulkActions([
|
|
|
|
|
DeleteBulkAction::make()
|
2025-05-27 19:30:30 +02:00
|
|
|
->authorize(fn () => auth()->user()->can('update', $this->getOwnerRecord())),
|
2024-04-29 20:06:34 -04:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|