Files
Fitness/web/add_provider.php
T
2026-10-16 03:40:21 -07:00

108 lines
3.8 KiB
PHP

<?php
// add_providers.php
// Simple page that displays a form to create a new fitness provider.
// It uses the same Flask API as list_providers.php (http://172.25.1.105:5000).
// When the form is submitted, it POSTs JSON to /providers.
// Helper to call the API -------------------------------------------
function callApi(string $method, string $path, $body = null)
{
$url = 'http://172.25.1.105:5000' . $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; // or throw an exception
}
return json_decode($response, true);
}
$created = null;
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Collect form data
$name = trim($_POST['name'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$age = trim($_POST['age'] ?? '');
$photo = trim($_POST['photo'] ?? '');
$rates = trim($_POST['rates'] ?? '');
$private = isset($_POST['private']) ? true : false;
// Basic validation
if ($name === '') { $errors[] = 'Name is required.'; }
if ($age !== '' && !is_numeric($age)) { $errors[] = 'Age must be a number.'; }
if ($rates !== '' && !is_numeric($rates)) { $errors[] = 'Rates must be a number.'; }
if (empty($errors)) {
$body = [
'name' => $name,
'phone' => $phone,
'age' => $age === '' ? null : (int)$age,
'photo' => $photo === '' ? null : $photo,
'rates' => $rates === '' ? null : (float)$rates,
'private'=> $private
];
$result = callApi('POST', '/providers', $body);
if ($result) {
$created = $result; // API returns created provider object
} else {
$errors[] = 'Failed to create provider. API error.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Provider</title>
<link rel="stylesheet" href="https://cosmostat.matt-cloud.com/src/styles.css">
<style>
body { font-family: Arial, sans-serif; margin: 2rem; }
form { max-width: 600px; }
label { display: block; margin-top: 1rem; }
input[type=text], input[type=number] { width: 100%; padding: 0.5rem; }
.error { color: red; }
.success { color: green; }
</style>
</head>
<body>
<div class="card">
<h1>Add New Provider</h1>
<?php if ($errors): ?>
<div class="error">
<?php foreach ($errors as $e): ?>
<p><?=htmlspecialchars($e)?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($created): ?>
<div class="success">
<p>Provider created successfully with ID <?=htmlspecialchars($created['id'] ?? '')?>.</p>
</div>
<?php endif; ?>
<form method="post" action="">
<label>Name:<br><input type="text" name="name" value="<?=htmlspecialchars($_POST['name'] ?? '')?>" required></label>
<label>Phone:<br><input type="text" name="phone" value="<?=htmlspecialchars($_POST['phone'] ?? '')?>"></label>
<label>Age:<br><input type="number" name="age" value="<?=htmlspecialchars($_POST['age'] ?? '')?>"></label>
<label>Photo URL:<br><input type="text" name="photo" value="<?=htmlspecialchars($_POST['photo'] ?? '')?>"></label>
<label>Rates:<br><input type="text" name="rates" value="<?=htmlspecialchars($_POST['rates'] ?? '')?>"></label>
<label><input type="checkbox" name="private" <?=isset($_POST['private'])?'checked':''?>> Private</label>
<button type="submit">Create Provider</button>
</form>
</div>
</body>
</html>