2024-04-09 18:45:01 -04:00
< ? php
2024-12-07 15:51:27 +01:00
namespace App\Filament\Admin\Resources\DatabaseHostResource\Pages ;
2024-04-09 18:45:01 -04:00
2024-12-07 15:51:27 +01:00
use App\Filament\Admin\Resources\DatabaseHostResource ;
2024-06-26 00:42:55 +02:00
use App\Services\Databases\Hosts\HostCreationService ;
2024-04-23 19:45:11 -04:00
use Filament\Forms ;
use Filament\Forms\Components\Section ;
2024-10-04 01:15:08 +02:00
use Filament\Forms\Components\Select ;
use Filament\Forms\Components\TextInput ;
2024-04-23 19:45:11 -04:00
use Filament\Forms\Form ;
2024-06-26 00:42:55 +02:00
use Filament\Notifications\Notification ;
2024-10-04 01:15:08 +02:00
use Filament\Resources\Pages\CreateRecord ;
2024-12-02 22:27:25 +01:00
use Filament\Support\Exceptions\Halt ;
2024-06-26 00:42:55 +02:00
use Illuminate\Database\Eloquent\Model ;
use PDOException ;
2024-04-09 18:45:01 -04:00
class CreateDatabaseHost extends CreateRecord
{
2024-10-19 17:22:03 -04:00
private HostCreationService $service ;
2024-04-09 18:45:01 -04:00
protected static string $resource = DatabaseHostResource :: class ;
2024-04-09 21:01:30 -04:00
protected ? string $heading = 'Database Hosts' ;
2024-04-25 18:57:21 -04:00
protected static bool $canCreateAnother = false ;
2024-04-09 21:01:30 -04:00
protected ? string $subheading = '(database servers that can have individual databases)' ;
2024-04-23 19:45:11 -04:00
2024-10-20 11:41:46 -04:00
public function boot ( HostCreationService $service ) : void
2024-10-19 17:22:03 -04:00
{
$this -> service = $service ;
}
2024-04-23 19:45:11 -04:00
public function form ( Form $form ) : Form
{
return $form
-> schema ([
2024-05-14 19:30:07 -04:00
Section :: make ()
-> columns ([
'default' => 2 ,
'sm' => 3 ,
'md' => 3 ,
'lg' => 4 ,
])
-> schema ([
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'host' )
2024-05-14 19:30:07 -04:00
-> columnSpan ( 2 )
-> helperText ( 'The IP address or Domain name that should be used when attempting to connect to this MySQL host from this Panel to create new databases.' )
-> required ()
-> live ( onBlur : true )
-> afterStateUpdated ( fn ( $state , Forms\Set $set ) => $set ( 'name' , $state ))
2024-06-16 19:56:18 +02:00
-> maxLength ( 255 ),
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'port' )
2024-05-14 19:30:07 -04:00
-> columnSpan ( 1 )
-> helperText ( 'The port that MySQL is running on for this host.' )
-> required ()
-> numeric ()
-> default ( 3306 )
-> minValue ( 0 )
-> maxValue ( 65535 ),
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'max_databases' )
2024-05-14 19:30:07 -04:00
-> label ( 'Max databases' )
-> helpertext ( 'Blank is unlimited.' )
-> numeric (),
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'name' )
2024-05-14 19:30:07 -04:00
-> label ( 'Display Name' )
-> helperText ( 'A short identifier used to distinguish this location from others. Must be between 1 and 60 characters, for example, us.nyc.lvl3.' )
-> required ()
-> maxLength ( 60 ),
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'username' )
2024-05-14 19:30:07 -04:00
-> helperText ( 'The username of an account that has enough permissions to create new users and databases on the system.' )
-> required ()
2024-06-16 19:56:18 +02:00
-> maxLength ( 255 ),
2024-06-29 17:38:18 -04:00
TextInput :: make ( 'password' )
2024-05-14 19:30:07 -04:00
-> helperText ( 'The password for the database user.' )
-> password ()
-> revealable ()
2024-06-16 19:56:18 +02:00
-> maxLength ( 255 )
2024-05-14 19:30:07 -04:00
-> required (),
2024-12-06 20:24:30 -05:00
Select :: make ( 'node_ids' )
-> multiple ()
2024-05-14 19:30:07 -04:00
-> searchable ()
-> preload ()
-> helperText ( 'This setting only defaults to this database host when adding a database to a server on the selected node.' )
2024-12-06 20:24:30 -05:00
-> label ( 'Linked Nodes' )
-> relationship ( 'nodes' , 'name' ),
2024-05-14 19:30:07 -04:00
]),
2024-04-23 19:45:11 -04:00
]);
2024-05-10 16:15:10 -04:00
}
2024-05-12 22:34:41 -04:00
protected function getHeaderActions () : array
{
return [
$this -> getCreateFormAction () -> formId ( 'form' ),
];
}
2024-06-26 00:42:55 +02:00
2024-05-12 22:34:41 -04:00
protected function getFormActions () : array
{
return [];
}
2024-06-26 00:42:55 +02:00
protected function handleRecordCreation ( array $data ) : Model
{
2024-12-02 22:27:25 +01:00
try {
return $this -> service -> handle ( $data );
} catch ( PDOException $exception ) {
2024-06-26 00:42:55 +02:00
Notification :: make ()
-> title ( 'Error connecting to database host' )
2024-12-02 22:27:25 +01:00
-> body ( $exception -> getMessage ())
2024-06-26 00:42:55 +02:00
-> color ( 'danger' )
-> icon ( 'tabler-database' )
-> danger ()
-> send ();
2024-12-02 22:27:25 +01:00
throw new Halt ();
2024-06-26 00:42:55 +02:00
}
}
2024-04-09 18:45:01 -04:00
}