false, 'error' => 'Unauthorized', 'message' => 'You must be logged in' ], JSON_UNESCAPED_UNICODE); exit; } // Sprawdzenie czy użytkownik ma rolę admina if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') { http_response_code(403); echo json_encode([ 'success' => false, 'error' => 'Forbidden', 'message' => 'Only administrators can access this endpoint' ], JSON_UNESCAPED_UNICODE); exit; } // ===== BAZA DANYCH ===== // Ścieżki względem katalogu: administration/disciplines/ping-pong/settings require_once __DIR__ . '/../../../includes/config.php'; require_once __DIR__ . '/../../../../api/DisciplineSettingsModel.php'; require_once __DIR__ . '/../../../../api/DisciplineSettingsService.php'; // ===== ROUTING ===== // Wydziel dyscyplinę z URL: /administration/disciplines/{discipline}/settings // lub /administration/api/disciplines/{discipline}/settings (alternatywnie) $requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $pathParts = array_filter(explode('/', $requestUri)); // Spróbuj znaleźć dyscyplinę w ścieżce $discipline = null; foreach (['ping-pong', 'rock-paper-scissors', 'table-football'] as $disc) { if (in_array($disc, $pathParts)) { $discipline = $disc; break; } } // Fallback: jeśli brak dyscypliny, domyślnie ping-pong if (!$discipline) { $discipline = 'ping-pong'; } // ===== INICJALIZACJA SERWISÓW ===== try { $model = new DisciplineSettingsModel($pdo); $service = new DisciplineSettingsService($model); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => 'Database initialization error', 'details' => $e->getMessage() ], JSON_UNESCAPED_UNICODE); exit; } // ===== ROUTING METOD ===== try { if ($_SERVER['REQUEST_METHOD'] === 'GET') { handleGetSettings($service, $discipline); } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { handlePostSettings($service, $discipline); } else { http_response_code(405); echo json_encode([ 'success' => false, 'error' => 'Method Not Allowed', 'message' => 'Only GET and POST methods are supported' ], JSON_UNESCAPED_UNICODE); } } catch (InvalidArgumentException $e) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => 'Validation Error', 'message' => $e->getMessage() ], JSON_UNESCAPED_UNICODE); } catch (RuntimeException $e) { http_response_code(400); echo json_encode([ 'success' => false, 'error' => 'Business Logic Error', 'message' => $e->getMessage() ], JSON_UNESCAPED_UNICODE); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => 'Server Error', 'message' => $e->getMessage() ], JSON_UNESCAPED_UNICODE); } // ===== OBSŁUGIWACZE METOD ===== /** * Obsługuje GET - pobranie ustawień * * Query parameters: * - version: (opcjonalne) konkretna wersja ustawień * - snapshot: (opcjonalne) pobierz snapshot do startu meczu */ function handleGetSettings($service, $discipline) { // Czy chcemy snapshot? $snapshot = isset($_GET['snapshot']) && $_GET['snapshot'] === 'true'; $version = isset($_GET['version']) ? (int)$_GET['version'] : null; if ($snapshot) { $result = $service->getMatchSnapshot($discipline, $version); echo json_encode($result, JSON_UNESCAPED_UNICODE); } else { // Zwróć normalne ustawienia $settings = $service->getSettingsForAPI($discipline); echo json_encode([ 'success' => true, 'data' => $settings ], JSON_UNESCAPED_UNICODE); } } /** * Obsługuje POST - aktualizacja ustawień * * Body (JSON): * { * "rules": { * "pointsToWin": 11, * "setsToWin": 3, * "serveRotation": 2, * "specialRules": "Deuce at 10-10..." * }, * "customization": { * "tableColor": "#2d5016", * "ballColor": "#ff6600", * ... * } * } */ function handlePostSettings($service, $discipline) { // Pobierz raw body $body = file_get_contents('php://input'); // Dekoduj JSON $input = json_decode($body, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new InvalidArgumentException('Invalid JSON: ' . json_last_error_msg()); } if (!is_array($input)) { throw new InvalidArgumentException('Request body must be a JSON object'); } // Sprawdź czy jest opcja reset if (isset($input['reset']) && $input['reset'] === true) { $userId = (int)($_SESSION['id'] ?? $_SESSION['user_id'] ?? 0); $result = $service->resetToDefaults($discipline, $userId); http_response_code(200); echo json_encode([ 'success' => true, 'message' => "Ustawienia dla $discipline zostały przywrócone do domyślnych.", 'data' => $result ], JSON_UNESCAPED_UNICODE); return; } // Normalnie: aktualizuj ustawienia $userId = (int)($_SESSION['id'] ?? $_SESSION['user_id'] ?? 0); $result = $service->validateAndUpdate($discipline, $input, $userId); http_response_code(200); echo json_encode([ 'success' => true, 'message' => "Ustawienia dla $discipline zapisane.", 'data' => $result ], JSON_UNESCAPED_UNICODE); } ?>