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); } // ------------------------------------------------------------------ // ------------------------------------------------------------------ // Helper: upload photos (multipart) // Returns the JSON response from the API (or null on error) function uploadPhotosApi(int $provider_id, array $files): ?array { $url = BASE_API_URL . "/api/providers/{$provider_id}/photos"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Build multipart array $multipart = []; foreach ($files['tmp_name'] as $i => $tmp) { if ($files['name'][$i] === '') continue; $multipart[] = [ 'name' => 'photos', 'contents' => fopen($tmp, 'r'), 'filename' => $files['name'][$i] ]; } curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart); // Let PHP set the correct Content‑Type boundary curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ($httpCode < 400) ? json_decode($response, true) : null; } // Get provider details $provider = callApi('GET', '/providers?id=' . $provider_id); if (!$provider) { echo '

Error

Could not load provider details.

'; exit; } if (is_array($provider) && count($provider) > 0 && isset($provider[0])) { $provider = $provider[0]; } // 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'])); } // 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 } // 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; } } // 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; } } } // ------------------------------------------------------------------ // 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 = []; // 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]; } // 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]; } } // 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 '

Error

Failed to update provider.

'; }*/ if ($_SERVER['REQUEST_METHOD'] === 'POST') { /* -------- 1. Build the base payload -------- */ $payload = [ 'name' => $_POST['name'] ?? '', 'age' => (int)($_POST['age'] ?? 0), 'photo' => $_POST['photo'] ?? '', 'phone' => $_POST['phone'] ?? '', 'private' => isset($_POST['private']), ]; /* -------- 2. Re‑build the rates array -------- */ $finalRates = []; $selectedRateKeysRaw = $_POST['rate_selected'] ?? []; foreach ($selectedRateKeysRaw as $opt) { $newVal = $_POST['rate_value'][$opt] ?? '0'; $finalRates[] = [$opt => $newVal]; } // orphan rates that the user chose *not* to delete $orphanDeleteSet = array_flip(array_map('strtolower', $_POST['orphan_delete'] ?? [])); foreach ($orphanRates as $key => $value) { if (!isset($orphanDeleteSet[$key])) { $finalRates[] = [$key => $value]; } } $payload['rates'] = !empty($finalRates) ? $finalRates : null; /* -------- 3. Call the API to update provider -------- */ $result = callApi('PUT', "/providers/{$provider_id}", $payload); if (!$result) { echo '

Error

Failed to update provider.

'; exit; } /* -------- 4. Upload photos if any were chosen -------- */ if (!empty($_FILES['photos']['name'][0])) { $uploadRes = uploadPhotosApi($provider_id, $_FILES['photos']); if ($uploadRes && isset($uploadRes['photos'])) { // Update main photo if the user selected one $selectedMain = $_POST['main_photo'] ?? null; if ($selectedMain) { callApi('PUT', "/api/providers/{$provider_id}/main_photo", ['url' => $selectedMain]); } } else { // Photo upload failed – you may want to show a message echo '

Photo upload failed.

'; } } /* -------- 5. Set the chosen main photo from the new uploads -------- */ // If a main photo was chosen from the new uploads, send it if (!empty($_POST['main_photo'])) { callApi('PUT', "/api/providers/{$provider_id}/main_photo", ['url' => $_POST['main_photo']]); } /* -------- 6. Redirect back to the list -------- */ header('Location: list_providers.php'); exit; } ?> Edit Provider

Edit Provider

Existing Photos
$url): ?>

>
Upload New Photos

You can select several images at once.