-
Notifications
You must be signed in to change notification settings - Fork 282
Add CompatForm based version of ConfigForm
#5480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheSyscall
wants to merge
14
commits into
main
Choose a base branch
from
config-form-5479
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6ae5543
Add `CompatForm` based version of `ConfigForm`
TheSyscall 3b9a9b6
Expand `ConfigForm` to also allow usage as create and delete form
TheSyscall ee4e674
Change license and add SPDX-header
TheSyscall cff2699
Use fluent setters
TheSyscall d444566
Remove superfluous hasSection call
TheSyscall 9efc29c
Split section handling of ConfigForm into new ConfigSectionForm
TheSyscall 6e382b6
Allow renming sections
TheSyscall 4711b41
fixup! phpcs
TheSyscall 5f76490
Only populate fields that actually have a value
TheSyscall 3dc7543
Check against '' instad of empty()
TheSyscall e045445
Cleanup
TheSyscall 44c4fa8
Force rename to also be a valid form submission
TheSyscall db42eff
Rename should only work on form submission
TheSyscall 94ee656
Docstring formating
TheSyscall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| <?php | ||
|
|
||
| // SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com> | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| namespace Icinga\Web\Form; | ||
|
|
||
| use Exception; | ||
| use Icinga\Application\Config; | ||
| use Icinga\Exception\ProgrammingError; | ||
| use Icinga\Web\Widget\ShowConfiguration; | ||
| use ipl\Web\Compat\CompatForm; | ||
|
|
||
| /** | ||
| * Form base-class providing standard functionality for configuration forms | ||
| */ | ||
| class ConfigForm extends CompatForm | ||
| { | ||
| /** @var string Name of the submit button element */ | ||
| protected const SUBMIT_BUTTON_NAME = 'store'; | ||
|
|
||
| /** | ||
| * A list of elements that should not be saved to the configuration | ||
| * | ||
| * @var string[] | ||
| */ | ||
| protected array $ignoredElements = [self::SUBMIT_BUTTON_NAME]; | ||
|
|
||
| /** | ||
| * The configuration to work with | ||
| * | ||
| * @var Config|null | ||
| */ | ||
| protected ?Config $config = null; | ||
|
|
||
| /** | ||
| * Set the configuration to use when populating and saving | ||
| * | ||
| * @param Config $config The configuration to use | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setConfig(Config $config): static | ||
| { | ||
| $this->config = $config; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function ensureAssembled(): static | ||
| { | ||
| if (! $this->hasBeenAssembled) { | ||
| parent::ensureAssembled(); | ||
| $this->populateFromConfig(); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Populate the form elements from the configuration | ||
| * | ||
| * @return void | ||
| * | ||
| * @throws ProgrammingError | ||
| */ | ||
| protected function populateFromConfig(): void | ||
| { | ||
| if (! $this->config) { | ||
| throw new ProgrammingError("A config object must be set before populating the form."); | ||
| } | ||
|
|
||
| $populate = []; | ||
| foreach ($this->getElements() as $element) { | ||
| [$section, $key] = $this->getIniKeyFromName($element->getName()); | ||
| if ($section === null || $key === null) { | ||
| continue; | ||
| } | ||
| $value = $this->getPopulatedValue($element->getName()) ?? $this->config->get($section, $key); | ||
| if ($value === null) { | ||
| continue; | ||
| } | ||
| $populate[$element->getName()] = $value; | ||
| } | ||
| $this->populate($populate); | ||
| } | ||
|
|
||
| /** | ||
| * Get the section and key from the element name | ||
| * | ||
| * @param string $name The element name | ||
| * | ||
| * @return string[]|null | ||
| */ | ||
| protected function getIniKeyFromName(string $name): ?array | ||
| { | ||
| $parts = explode('__', $name, 2); | ||
|
|
||
| if (count($parts) !== 2) { | ||
| return [null, null]; | ||
| } | ||
|
|
||
| return $parts; | ||
| } | ||
|
|
||
| /** | ||
| * Get the value of a configuration key from an element name | ||
| * | ||
| * @param string $name The element name | ||
| * @param mixed $default The default value to return if the config entry does not exist | ||
| * | ||
| * @return mixed The value of the configuration key or the default value | ||
| */ | ||
| public function getConfigValue(string $name, mixed $default = null): mixed | ||
| { | ||
| [$section, $key] = $this->getIniKeyFromName($name); | ||
| if ($section === null || $key === null) { | ||
| return $default; | ||
| } | ||
|
|
||
| return $this->config->get($section, $key, $default); | ||
| } | ||
|
|
||
| /** | ||
| * Persist the current configuration to disk | ||
| * | ||
| * If an error occurs, the form will be re-rendered with the error message | ||
| * and the raw INI configuration. | ||
| * | ||
| * @throws ProgrammingError | ||
| */ | ||
| protected function save(): void | ||
| { | ||
| if (! $this->config) { | ||
| throw new ProgrammingError("A config object must be set before saving the configuration."); | ||
| } | ||
|
|
||
| foreach ($this->getElements() as $element) { | ||
| if (in_array($element->getName(), $this->ignoredElements)) { | ||
| continue; | ||
| } | ||
| [$section, $key] = $this->getIniKeyFromName($element->getName()); | ||
| if ($section === null || $key === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $value = $this->getPopulatedValue($element->getName()); | ||
| $configSection = $this->config->getSection($section); | ||
| if ((string) $value === '') { | ||
| unset($configSection[$key]); | ||
| } else { | ||
| $configSection->$key = $value; | ||
| } | ||
|
|
||
| if ($configSection->isEmpty()) { | ||
| $this->config->removeSection($section); | ||
| } else { | ||
| $this->config->setSection($section, $configSection); | ||
| } | ||
| } | ||
|
|
||
| $this->config->saveIni(); | ||
| } | ||
|
|
||
| protected function onSuccess(): void | ||
| { | ||
| try { | ||
| $this->save(); | ||
| } catch (Exception $e) { | ||
| $content = $this->getContent(); | ||
| array_unshift( | ||
| $content, | ||
| new ShowConfiguration( | ||
| $e, | ||
| $this->config, | ||
| ) | ||
| ); | ||
| $this->setContent($content); | ||
| throw $e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Add the store button to the form | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function addButtonElements(): void | ||
| { | ||
| $this->addElement('submit', static::SUBMIT_BUTTON_NAME, [ | ||
| 'label' => $this->translate('Store'), | ||
| ]); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.