148 lines
4.9 KiB
PHP
148 lines
4.9 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.
|
|
|
|
// file settings
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
// Helper to call the API -------------------------------------------
|
|
function callApi(string $method, string $path, $body = null)
|
|
{
|
|
$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; // or throw an exception
|
|
}
|
|
|
|
return json_decode($response, true);
|
|
}
|
|
|
|
// Fetch rate options setting
|
|
$rateOptionsSetting = callApi('GET', '/settings/rate_options');
|
|
$rateOptions = [];
|
|
if ($rateOptionsSetting && isset($rateOptionsSetting['value'])) {
|
|
$rateOptions = explode(',', $rateOptionsSetting['value']);
|
|
/*
|
|
$parsed = json_decode($rateOptionsSetting['value'], true);
|
|
if (is_array($parsed)) {
|
|
$rateOptions = $parsed;
|
|
}
|
|
echo "Rate options:".$rateOptionsSetting['value'];
|
|
*/
|
|
}
|
|
|
|
// Update form handling to process selected rates
|
|
$selectedRates = $_POST['rates'] ?? [];
|
|
$ratesArray = [];
|
|
foreach ($selectedRates as $item) {
|
|
$parts = explode(':', $item, 2);
|
|
if (count($parts) == 2) {
|
|
$ratesArray[] = [$parts[0] => $parts[1]];
|
|
}
|
|
}
|
|
|
|
$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'] ?? '');
|
|
$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.'; }
|
|
// Rates now processed from checkboxes
|
|
if (empty($errors)) {
|
|
$body = [
|
|
'name' => $name,
|
|
'phone' => $phone,
|
|
'age' => $age === '' ? null : (int)$age,
|
|
'photo' => $photo === '' ? null : $photo,
|
|
'rates' => empty($ratesArray) ? null : $ratesArray,
|
|
'private'=> $private
|
|
];
|
|
$result = callApi('POST', '/providers', $body);
|
|
if ($result) {
|
|
// On successful creation, redirect to the list providers page
|
|
header('Location: list_providers.php');
|
|
exit;
|
|
} 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; ?>
|
|
<form method="post" action="">
|
|
<?php
|
|
// echo $rateOptions[0];
|
|
|
|
?>
|
|
<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>
|
|
<?php if (!empty($rateOptions)) : ?>
|
|
<label>Rate Options:<br>
|
|
<?php foreach ($rateOptions as $option) :
|
|
// echo $option;
|
|
$key = $option;
|
|
$value = 0;
|
|
?>
|
|
<input type="checkbox" name="rates[]" value="<?=htmlspecialchars($key . ':' . $value)?>"
|
|
<?php if (in_array($key . ':' . $value, $selectedRates)) echo 'checked'; ?>>
|
|
<?=htmlspecialchars($key)?>: <?=htmlspecialchars($value)?> <br>
|
|
<?php endforeach; ?>
|
|
</label>
|
|
<?php endif; ?>
|
|
<label><input type="checkbox" name="private" <?=isset($_POST['private'])?'checked':''?>> Private</label><br>
|
|
<button type="submit" class="btn">Create Provider</button>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
|
|
|