pdo = $pdo; } public function index(Request $request, Response $response, $args) { $settings = $this->getSettings(); $sources = $this->getSources(); return $this->view->render($response, 'admin/settings.twig', [ 'title' => 'Admin Settings', 'settings' => $settings, 'sources' => $sources ]); } public function save(Request $request, Response $response, $args) { $data = $request->getParsedBody(); // Save general settings $this->saveGeneralSettings($data); // Save source-specific settings if (isset($data['sources']) && is_array($data['sources'])) { $this->saveSourceSettings($data['sources']); } return $this->view->render($response, 'admin/settings.twig', [ 'title' => 'Admin Settings', 'settings' => $this->getSettings(), 'sources' => $this->getSources(), 'success' => 'Settings saved successfully!' ]); } private function getSettings(): array { // Get general application settings $settings = []; // Get media type visibility settings $stmt = $this->pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'media_visibility_%'"); $stmt->execute(); $mediaVisibilitySettings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); $settings['media_visibility'] = [ 'games' => $mediaVisibilitySettings['media_visibility_games'] ?? 'authenticated', // Default: authenticated users only 'movies' => $mediaVisibilitySettings['media_visibility_movies'] ?? 'authenticated', 'tvshows' => $mediaVisibilitySettings['media_visibility_tvshows'] ?? 'authenticated', 'music' => $mediaVisibilitySettings['media_visibility_music'] ?? 'authenticated', 'adult' => $mediaVisibilitySettings['media_visibility_adult'] ?? 'authenticated', // Adult content requires auth by default 'actors' => $mediaVisibilitySettings['media_visibility_actors'] ?? 'authenticated' ]; // You can extend this to include more settings like: // - Sync intervals // - Default sync types // - Notification preferences // - Theme settings // - etc. return $settings; } private function getSources(): array { $stmt = $this->pdo->prepare("SELECT * FROM sources ORDER BY display_name"); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } private function saveGeneralSettings(array $data): void { // Save general settings to a settings table or config file // For now, we'll store them in a simple settings table foreach ($data as $key => $value) { if (strpos($key, 'setting_') === 0) { $settingKey = substr($key, 8); // Remove 'setting_' prefix // Check if setting exists $stmt = $this->pdo->prepare("SELECT id FROM settings WHERE setting_key = :key LIMIT 1"); $stmt->execute(['key' => $settingKey]); $existing = $stmt->fetch(PDO::FETCH_ASSOC); if ($existing) { // Update existing setting $stmt = $this->pdo->prepare("UPDATE settings SET setting_value = :value WHERE setting_key = :key"); $stmt->execute(['key' => $settingKey, 'value' => $value]); } else { // Insert new setting $stmt = $this->pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (:key, :value)"); $stmt->execute(['key' => $settingKey, 'value' => $value]); } } } // Save media visibility settings if (isset($data['media_visibility']) && is_array($data['media_visibility'])) { foreach ($data['media_visibility'] as $mediaType => $visibility) { $settingKey = "media_visibility_{$mediaType}"; // Check if setting exists $stmt = $this->pdo->prepare("SELECT id FROM settings WHERE setting_key = :key LIMIT 1"); $stmt->execute(['key' => $settingKey]); $existing = $stmt->fetch(PDO::FETCH_ASSOC); if ($existing) { // Update existing setting $stmt = $this->pdo->prepare("UPDATE settings SET setting_value = :value WHERE setting_key = :key"); $stmt->execute(['key' => $settingKey, 'value' => $visibility]); } else { // Insert new setting $stmt = $this->pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (:key, :value)"); $stmt->execute(['key' => $settingKey, 'value' => $visibility]); } } } } private function saveSourceSettings(array $sources): void { foreach ($sources as $sourceId => $sourceData) { // Update source configuration $config = isset($sourceData['config']) ? json_encode($sourceData['config']) : '{}'; $stmt = $this->pdo->prepare(" UPDATE sources SET api_url = :api_url, api_key = :api_key, config = :config, is_active = :is_active WHERE id = :id "); $stmt->execute([ 'id' => $sourceId, 'api_url' => $sourceData['api_url'] ?? '', 'api_key' => $sourceData['api_key'] ?? '', 'config' => $config, 'is_active' => isset($sourceData['is_active']) ? 1 : 0 ]); } } }