moving helper functions to scripts folder

This commit is contained in:
matt committed 2026-10-21 04:14:10 -07:00
1 parent e7fad500f9
commit dc39b6988c
9 files changed
+525 -60

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
<?php
// settings.php
// This page displays all settings stored in the Flask API and allows editing them.
// It supports updating existing settings via PUT and creating a single new setting via POST.
// The design follows the pattern used in list_providers.php.
// file settings
require_once __DIR__ . '/config.php';
// --- Helper to call the Flask API -------------------------------------------
function callApi(string $method, string $path, $body = null)
{
// API base URL – update if your API is hosted elsewhere
$url = BASE_API_URL . $path;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 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 ?? [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Settings</title>
<link rel="stylesheet" href="https://cosmostat.matt-cloud.com/src/styles.css">
<style>
body {font-family: Arial, sans-serif; margin: 2rem;}
.messages {margin-bottom: 1rem;}
.messages .error {color: red;}
.messages .success {color: green;}
table {width: 100%; border-collapse: collapse;}
input[type=text] {width: 95%;}
.btn {padding: 6px 12px; margin-top: 10px;}
</style>
</head>
<body>
<div class="card">
<h1>Settings</h1>
<?php if (!empty($messages)) : ?>
<div class="messages">
<?php foreach ($messages as $msg) : ?>
<div class="success"><?= htmlspecialchars($msg) ?></div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="post">
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<?php if (!empty($settings)) : ?>
<?php foreach ($settings as $s) : ?>
<tr>
<td><?= htmlspecialchars($s['name']) ?></td>
<td>
<input type="text" name="value[]" value="<?= htmlspecialchars($s['value']) ?>" />
<input type="hidden" name="orig_name[]" value="<?= htmlspecialchars($s['name']) ?>" />
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<tr>
<td><input type="text" name="new_name" placeholder="New setting name" /></td>
<td><input type="text" name="new_value" placeholder="New setting value" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="Save Settings" class="btn" />
</form>
</div>
</body>
</html>