Files
Virtnet.bond/app/Admin/Resources/ServiceResource/Pages/EditService.php
2025-11-30 11:57:22 +01:00

86 lines
3.5 KiB
PHP

<?php
namespace App\Admin\Resources\ServiceResource\Pages;
use App\Admin\Resources\ServiceResource;
use App\Helpers\ExtensionHelper;
use App\Helpers\NotificationHelper;
use App\Models\Service;
use Filament\Actions;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Log;
class EditService extends EditRecord
{
protected static string $resource = ServiceResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\Action::make('changeStatus')
->label('Trigger Extension Action')
->form([
Select::make('action')
->label('Action')
->options([
'create' => 'Create server',
'suspend' => 'Suspend server',
'unsuspend' => 'Unsuspend server',
'terminate' => 'Terminate server',
'upgrade' => 'Upgrade server',
])->required(),
Checkbox::make('sendNotification')
->label('Send Notification')
->default(false),
])
->action(function (array $data, Service $record, Actions\Action $action): void {
try {
switch ($data['action']) {
case 'create':
$sdata = ExtensionHelper::createServer($record);
if ($data['sendNotification']) {
NotificationHelper::serverCreatedNotification($record->order->user, $record, $sdata);
}
break;
case 'suspend':
$sdata = ExtensionHelper::suspendServer($record);
break;
case 'unsuspend':
$sdata = ExtensionHelper::unsuspendServer($record);
break;
case 'terminate':
$sdata = ExtensionHelper::terminateServer($record);
break;
case 'upgrade':
$sdata = ExtensionHelper::upgradeServer($record);
break;
}
} catch (\Exception $e) {
if (config('app.debug')) {
throw $e;
}
Log::error($e);
Notification::make('Error')
->title('Error occured while triggering the action:')
->body($e->getMessage())
->danger()
->send();
$action->halt();
}
Notification::make('Success')
->title('Action triggered successfully')
->body('The action has been triggered successfully')
->success()
->send();
})
->color('primary')
->modalSubmitActionLabel('Trigger'),
];
}
}