fix server variables naming

This commit is contained in:
Boy132 2025-08-14 09:22:43 +02:00
parent 7d9f567254
commit 1c0b7f7d5f
2 changed files with 26 additions and 17 deletions

View File

@ -334,7 +334,8 @@ class CreateServer extends CreateRecord
$variables = [];
$set($path = 'server_variables', $serverVariables->sortBy(['sort'])->all());
for ($i = 0; $i < $serverVariables->count(); $i++) {
$set("$path.$i.variable_value", $serverVariables[$i]['default_value']);
$set("$path.$i.variable_value_input", $serverVariables[$i]['default_value']);
$set("$path.$i.variable_value_select", $serverVariables[$i]['default_value']);
$set("$path.$i.variable_id", $serverVariables[$i]['id']);
$variables[$serverVariables[$i]['env_variable']] = $serverVariables[$i]['default_value'];
}
@ -426,12 +427,10 @@ class CreateServer extends CreateRecord
->schema([
TextEntry::make(trans('admin/server.select_egg'))
->hidden(fn (Get $get) => $get('egg_id')),
TextEntry::make(trans('admin/server.no_variables'))
->hidden(fn (Get $get) => !$get('egg_id') ||
Egg::query()->find($get('egg_id'))?->variables()?->count()
),
Repeater::make('server_variables')
->label('')
->relationship('serverVariables', fn (Builder $query) => $query->orderByPowerJoins('variable.sort'))
@ -443,9 +442,19 @@ class CreateServer extends CreateRecord
->deletable(false)
->default([])
->hidden(fn ($state) => empty($state))
->schema(function () {
->mutateRelationshipDataBeforeCreateUsing(function ($data) {
$data['variable_value'] = ($data['is_select'] ? $data['variable_value_select'] : $data['variable_value_input']) ?? '';
$text = TextInput::make('variable_value')
return $data;
})
->schema(function () {
$isSelect = Hidden::make('is_select')
->dehydrated(false)
->formatStateUsing(fn (Get $get) => (bool) collect($get('rules'))->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
));
$text = TextInput::make('variable_value_input')
->hidden($this->shouldHideComponent(...))
->dehydratedWhenHidden()
->required(fn (Get $get) => in_array('required', $get('rules')))
@ -463,7 +472,7 @@ class CreateServer extends CreateRecord
},
);
$select = Select::make('variable_value')
$select = Select::make('variable_value_select')
->hidden($this->shouldHideComponent(...))
->dehydratedWhenHidden()
->options($this->getSelectOptionsFromRules(...))
@ -486,7 +495,7 @@ class CreateServer extends CreateRecord
});
}
return $components;
return [$isSelect, ...$components];
})
->columnSpan(2),
]),

View File

@ -646,19 +646,18 @@ class EditServer extends EditRecord
return $query->orderByPowerJoins('variable.sort');
})
->grid()
->mutateRelationshipDataBeforeSaveUsing(function (array &$data): array {
foreach ($data as $key => $value) {
if (!isset($data['variable_value'])) {
$data['variable_value'] = '';
}
}
->mutateRelationshipDataBeforeSaveUsing(function (array $data) {
$data['variable_value'] = ($data['is_select'] ? $data['variable_value_select'] : $data['variable_value_input']) ?? '';
return $data;
})
->reorderable(false)->addable(false)->deletable(false)
->schema(function () {
$isSelect = Hidden::make('is_select')
->dehydrated(false)
->formatStateUsing(fn (ServerVariable $serverVariable) => (bool) array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false));
$text = TextInput::make('variable_value')
$text = TextInput::make('variable_value_input')
->hidden($this->shouldHideComponent(...))
->dehydratedWhenHidden()
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
@ -676,7 +675,7 @@ class EditServer extends EditRecord
},
]);
$select = Select::make('variable_value')
$select = Select::make('variable_value_select')
->hidden($this->shouldHideComponent(...))
->dehydratedWhenHidden()
->options($this->getSelectOptionsFromRules(...))
@ -691,10 +690,11 @@ class EditServer extends EditRecord
->label(fn (ServerVariable $serverVariable) => $serverVariable->variable->name)
->hintIconTooltip(fn (ServerVariable $serverVariable) => implode('|', $serverVariable->variable->rules))
->prefix(fn (ServerVariable $serverVariable) => '{{' . $serverVariable->variable->env_variable . '}}')
->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '—' : $serverVariable->variable->description);
->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '—' : $serverVariable->variable->description)
->formatStateUsing(fn (ServerVariable $serverVariable) => $serverVariable->variable_value);
}
return $components;
return [$isSelect, ...$components];
})
->columnSpan(6),
]),