Error
Provider ID is missing or invalid.
'; 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 'Could not load provider details.
'; 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 'Failed to update provider.
'; } ?>