Add all files
This commit is contained in:
59
app/Admin/Resources/ServerResource/Pages/EditServer.php
Normal file
59
app/Admin/Resources/ServerResource/Pages/EditServer.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Admin\Resources\ServerResource\Pages;
|
||||
|
||||
use App\Admin\Resources\ServerResource;
|
||||
use App\Helpers\ExtensionHelper;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class EditServer extends EditRecord
|
||||
{
|
||||
protected static string $resource = ServerResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make()->before(fn ($record) => ExtensionHelper::call($record, 'disabled', [$record], mayFail: true)),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
foreach ($this->record->settings as $setting) {
|
||||
$data['settings'][$setting->key] = $setting->value;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function handleRecordUpdate(Model $record, array $data): Model
|
||||
{
|
||||
$record->update(Arr::except($data, ['settings']));
|
||||
|
||||
if (!isset($data['settings'])) {
|
||||
return $record;
|
||||
}
|
||||
|
||||
$config = ExtensionHelper::getConfig($record->type, $record->extension);
|
||||
|
||||
foreach ($config as $option) {
|
||||
$record->settings()->updateOrCreate([
|
||||
'key' => $option['name'],
|
||||
'settingable_id' => $record->id,
|
||||
'settingable_type' => $record->getMorphClass(),
|
||||
], [
|
||||
'type' => $option['database_type'] ?? 'string',
|
||||
'value' => isset($data['settings'][$option['name']]) ? (is_array($data['settings'][$option['name']]) ? json_encode($data['settings'][$option['name']]) : $data['settings'][$option['name']]) : null,
|
||||
'encrypted' => $option['encrypted'] ?? false,
|
||||
]);
|
||||
}
|
||||
|
||||
ExtensionHelper::call($record, 'updated', [$record], mayFail: true);
|
||||
|
||||
// Maybe the extension changed the record, so we need to refresh it
|
||||
return $record->refresh();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user