[setup] have ConfigSaveEvent carry its own params

This commit is contained in:
Shish 2024-02-10 00:23:30 +00:00
parent 31dc7f1ee0
commit c084016b5b
2 changed files with 12 additions and 6 deletions

View file

@ -17,11 +17,17 @@ require_once "config.php";
class ConfigSaveEvent extends Event class ConfigSaveEvent extends Event
{ {
public Config $config; public Config $config;
/** @var array<string, mixed> $values */
public array $values;
public function __construct(Config $config) /**
* @param array<string, mixed> $values
*/
public function __construct(Config $config, array $values)
{ {
parent::__construct(); parent::__construct();
$this->config = $config; $this->config = $config;
$this->values = $values;
} }
} }
@ -340,7 +346,7 @@ class Setup extends Extension
send_event(new SetupBuildingEvent($panel)); send_event(new SetupBuildingEvent($panel));
$this->theme->display_page($page, $panel); $this->theme->display_page($page, $panel);
} elseif ($event->get_arg(0) == "save" && $user->check_auth_token()) { } elseif ($event->get_arg(0) == "save" && $user->check_auth_token()) {
send_event(new ConfigSaveEvent($config)); send_event(new ConfigSaveEvent($config, $event->POST));
$config->save(); $config->save();
$page->flash("Config saved"); $page->flash("Config saved");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
@ -384,11 +390,11 @@ class Setup extends Extension
public function onConfigSave(ConfigSaveEvent $event): void public function onConfigSave(ConfigSaveEvent $event): void
{ {
$config = $event->config; $config = $event->config;
foreach ($_POST as $_name => $junk) { foreach ($event->values as $_name => $junk) {
if (substr($_name, 0, 6) == "_type_") { if (substr($_name, 0, 6) == "_type_") {
$name = substr($_name, 6); $name = substr($_name, 6);
$type = $_POST["_type_$name"]; $type = $event->values["_type_$name"];
$value = isset($_POST["_config_$name"]) ? $_POST["_config_$name"] : null; $value = isset($event->values["_config_$name"]) ? $event->values["_config_$name"] : null;
switch ($type) { switch ($type) {
case "string": case "string":
$config->set_string($name, $value); $config->set_string($name, $value);

View file

@ -160,7 +160,7 @@ class UserConfig extends Extension
} }
$target_config = UserConfig::get_for_user($duser->id); $target_config = UserConfig::get_for_user($duser->id);
send_event(new ConfigSaveEvent($target_config)); send_event(new ConfigSaveEvent($target_config, $event->POST));
$target_config->save(); $target_config->save();
$page->flash("Config saved"); $page->flash("Config saved");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);