= 400) { return null; // Could also throw an exception if preferred } return json_decode($response, true); } $messages = []; // Handle form submission – update existing settings and/or create a new one if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Update existing settings if (isset($_POST['orig_name'], $_POST['value'])) { foreach ($_POST['orig_name'] as $idx => $orig) { $newVal = $_POST['value'][$idx] ?? ''; if ($newVal === '') continue; // skip empty values $res = callApi('PUT', '/settings/' . urlencode($orig), ['value' => $newVal]); if ($res === null) { $messages[] = "Failed to update setting '" . htmlspecialchars($orig) . "'."; } else { $messages[] = "Updated setting '" . htmlspecialchars($orig) . "'."; } } } // Create a single new setting $newName = $_POST['new_name'] ?? ''; $newValue = $_POST['new_value'] ?? ''; if ($newName !== '' && $newValue !== '') { $res = callApi('POST', '/settings', ['name' => $newName, 'value' => $newValue]); if ($res === null) { $messages[] = "Failed to create new setting '" . htmlspecialchars($newName) . "'."; } else { $messages[] = "Created new setting '" . htmlspecialchars($newName) . "'."; } } elseif ($newName !== '' || $newValue !== '') { $messages[] = "Both name and value are required to create a new setting."; } } // Retrieve current settings to display $settings = callApi('GET', '/settings'); $settings = $settings ?? []; ?>