203 lines
7.7 KiB
PHP
203 lines
7.7 KiB
PHP
<?php
|
||
// edit_provider.php
|
||
// Edit an existing fitness provider.
|
||
// Handles rate options similar to add_provider.php.
|
||
|
||
require_once __DIR__ . '/config.php';
|
||
|
||
$provider_id = (int)($_GET['id'] ?? 0);
|
||
if ($provider_id <= 0) {
|
||
echo '<h1>Error</h1><p>Provider ID is missing or invalid.</p>';
|
||
exit;
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// Helper: Call the API (GET/PUT) – returns decoded JSON or null
|
||
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;
|
||
}
|
||
return json_decode($response, true);
|
||
}
|
||
// ------------------------------------------------------------------
|
||
|
||
// 1️⃣ Get provider details
|
||
$provider = callApi('GET', '/providers?id=' . $provider_id);
|
||
if (!$provider) {
|
||
echo '<h1>Error</h1><p>Could not load provider details.</p>';
|
||
exit;
|
||
}
|
||
if (is_array($provider) && count($provider) > 0 && isset($provider[0])) {
|
||
$provider = $provider[0];
|
||
}
|
||
|
||
// 2️⃣ Get rate‑option list (comma‑separated string → array)
|
||
$rateSetting = callApi('GET', '/settings/rate_options');
|
||
$rateOptions = [];
|
||
if ($rateSetting && isset($rateSetting['value'])) {
|
||
$rateOptions = array_map('trim', explode(',', $rateSetting['value']));
|
||
}
|
||
|
||
// 3️⃣ Decode the API’s rate string (JSON → array of arrays)
|
||
$ratesData = $provider['rates'] ?? '[]';
|
||
$decoded = [];
|
||
if (is_string($ratesData)) {
|
||
$decoded = json_decode($ratesData, true);
|
||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||
$decoded = []; // bad JSON – treat as empty
|
||
}
|
||
} elseif (is_array($ratesData)) {
|
||
$decoded = $ratesData; // already an array – legacy path
|
||
}
|
||
|
||
// 4️⃣ Build two helper arrays
|
||
$selectedRateKeys = []; // lowercase keys of selected rates
|
||
$ratesAssoc = []; // key → value
|
||
|
||
foreach ($decoded as $entry) {
|
||
if (!is_array($entry)) continue;
|
||
foreach ($entry as $k => $v) {
|
||
$kTrim = strtolower(trim($k));
|
||
$selectedRateKeys[] = $kTrim;
|
||
$ratesAssoc[$kTrim] = $v;
|
||
}
|
||
}
|
||
|
||
// 5️⃣ Find orphan rates (present in $decoded but not in $rateOptions)
|
||
$orphanRates = [];
|
||
$orphanKeys = [];
|
||
foreach ($decoded as $entry) {
|
||
if (!is_array($entry)) continue;
|
||
foreach ($entry as $k => $v) {
|
||
$kTrim = strtolower(trim($k));
|
||
if (!in_array($kTrim, array_map('strtolower', $rateOptions))) {
|
||
$orphanRates[$kTrim] = $v;
|
||
$orphanKeys[] = $kTrim;
|
||
}
|
||
}
|
||
}
|
||
|
||
// ------------------------------------------------------------------
|
||
// 6️⃣ Handle form POST
|
||
// --- Handle form POST -------------------------------------------------
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
// Base payload
|
||
$payload = [
|
||
'name' => $_POST['name'] ?? '',
|
||
'age' => (int)($_POST['age'] ?? 0),
|
||
'photo' => $_POST['photo'] ?? '',
|
||
'phone' => $_POST['phone'] ?? '',
|
||
// Convert checkbox to boolean
|
||
'private' => isset($_POST['private']),
|
||
];
|
||
|
||
// ------------------------------------------------------------------
|
||
// Rebuild the rates array from the form.
|
||
$finalRates = [];
|
||
|
||
// 1️⃣ Add the selected valid rate options (value may be changed by the user)
|
||
$selectedRateKeysRaw = $_POST['rate_selected'] ?? [];
|
||
foreach ($selectedRateKeysRaw as $opt) { // $opt is the original rate name
|
||
$newVal = $_POST['rate_value'][$opt] ?? '0'; // the updated value
|
||
$finalRates[] = [$opt => $newVal];
|
||
}
|
||
|
||
// 2️⃣ Add orphan rates that the user chose **not** to delete
|
||
$orphanDeleteSet = array_flip(array_map('strtolower', $_POST['orphan_delete'] ?? []));
|
||
foreach ($orphanRates as $key => $value) { // $key is already lower‑cased
|
||
if (!isset($orphanDeleteSet[strtolower($key)])) {
|
||
$finalRates[] = [$key => $value];
|
||
}
|
||
}
|
||
|
||
// 3️⃣ Encode the new array as JSON
|
||
//$payload['rates'] = !empty($finalRates) ? json_encode($finalRates) : null;
|
||
$payload['rates'] = !empty($finalRates) ? $finalRates : null;
|
||
|
||
|
||
// ------------------------------------------------------------------
|
||
// Submit the updated provider
|
||
$result = callApi('PUT', '/providers/' . $provider_id, $payload);
|
||
if ($result) {
|
||
header('Location: list_providers.php');
|
||
exit;
|
||
}
|
||
echo '<h1>Error</h1><p>Failed to update provider.</p>';
|
||
}
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Edit Provider</title>
|
||
<link rel="stylesheet" href="https://cosmostat.matt-cloud.com/src/styles.css">
|
||
<style>
|
||
body {font-family: Arial, sans-serif; margin: 20px;}
|
||
form {max-width: 600px;}
|
||
label {display: block; margin-top: 10px;}
|
||
input[type=text], input[type=number], textarea {width: 100%; padding: 8px; box-sizing: border-box;}
|
||
input[type=checkbox] {margin-top: 5px;}
|
||
button {margin-top: 15px; padding: 10px 15px;}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="card">
|
||
<h1>Edit Provider</h1>
|
||
<form method="post" id="editForm">
|
||
<!-- Basic fields -->
|
||
<label>Name: <input type="text" name="name" required value="<?php echo htmlspecialchars($provider['name'] ?? ''); ?>"></label>
|
||
<label>Age: <input type="number" name="age" required value="<?php echo htmlspecialchars($provider['age'] ?? ''); ?>"></label>
|
||
<label>Photo URL: <input type="text" name="photo" value="<?php echo htmlspecialchars($provider['photo'] ?? ''); ?>"></label>
|
||
<label>Phone: <input type="text" name="phone" required value="<?php echo htmlspecialchars($provider['phone'] ?? ''); ?>"></label>
|
||
|
||
<!-- Rate options -->
|
||
<?php if (!empty($rateOptions)) : ?>
|
||
<label>Rate Options:<br>
|
||
<?php foreach ($rateOptions as $opt) :
|
||
$key = strtolower(trim($opt));
|
||
$value = $ratesAssoc[$key] ?? 0;
|
||
?>
|
||
<input type="checkbox" name="rate_selected[]" value="<?php echo htmlspecialchars($opt); ?>"
|
||
<?php if (in_array($key, $selectedRateKeys)) echo 'checked'; ?> >
|
||
<?php echo htmlspecialchars($opt); ?>:
|
||
<input type="number" name="rate_value[<?php echo htmlspecialchars($opt); ?>]" value="<?php echo htmlspecialchars($value); ?>" style="width:60px;">
|
||
<br>
|
||
<?php endforeach; ?>
|
||
</label>
|
||
<?php endif; ?>
|
||
|
||
<!-- Orphan rates – show value and a Delete checkbox -->
|
||
<?php if (!empty($orphanRates)) : ?>
|
||
<label>Orphan Rates:<br>
|
||
<?php foreach ($orphanRates as $key => $value) : ?>
|
||
<?php echo htmlspecialchars($key); ?>:
|
||
<input type="number" value="<?php echo htmlspecialchars($value); ?>" style="width:60px;" disabled>
|
||
<input type="checkbox" name="orphan_delete[]" value="<?php echo htmlspecialchars($key); ?>" style="margin-left:10px;"> Delete
|
||
<br>
|
||
<?php endforeach; ?>
|
||
</label>
|
||
<?php endif; ?>
|
||
|
||
<!-- Private flag -->
|
||
<label><input type="checkbox" name="private" <?php echo (!empty($provider['private'])) ? 'checked' : ''; ?>> Private</label><br>
|
||
|
||
<!-- Buttons -->
|
||
<button type="submit">Save</button>
|
||
<button type="button" onclick="window.location.href='list_providers.php'">Cancel</button>
|
||
</form>
|
||
</div>
|
||
</body>
|
||
</html>
|