Apply strict_types and extensive type declarations throughout the API and models, improving type safety and error handling. Key changes: add declare(strict_types=1) to many files; convert properties, method parameters and return values to typed signatures (PDO, arrays, ints, strings, bools, nullables); switch exception handling to Throwable in index and Router; improve Router, controllers and model method signatures and nullability handling; refine file/image serving security checks and headers in ImageController; strengthen Database typing and initialization methods; return explicit types from BaseModel CRUD helpers and counting; update Media/Cast/Adult/Game/Console/Settings controllers and models to use typed methods, better validation, and clearer update/create return types. Also add AGENTS.md (agent skills index), update README with Swagger/OpenAPI usage instructions, and add /.windsurf to .gitignore. These changes aim to harden runtime correctness, make intended contracts explicit, and prepare the codebase for easier maintenance and static analysis.
89 lines
3.0 KiB
PHP
89 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/BaseModel.php';
|
|
|
|
class Settings extends BaseModel {
|
|
protected string $table = 'settings';
|
|
|
|
public function __construct(PDO $pdo) {
|
|
parent::__construct($pdo);
|
|
}
|
|
|
|
public function getSettings(): ?array {
|
|
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = 1");
|
|
$stmt->execute();
|
|
$settings = $stmt->fetch();
|
|
|
|
if ($settings) {
|
|
// Decode enabled_categories from JSON
|
|
$settings['enabled_categories'] = $settings['enabled_categories'] ? json_decode($settings['enabled_categories'], true) : [];
|
|
// Convert boolean fields from tinyint to boolean
|
|
$settings['show_adult_content'] = (bool)$settings['show_adult_content'];
|
|
$settings['auto_play_trailers'] = (bool)$settings['auto_play_trailers'];
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
public function updateSettings(array $data): ?array {
|
|
$updateData = [];
|
|
|
|
if (isset($data['enabled_categories']) && is_array($data['enabled_categories'])) {
|
|
$updateData['enabled_categories'] = json_encode($data['enabled_categories']);
|
|
}
|
|
|
|
if (isset($data['items_per_page'])) {
|
|
$updateData['items_per_page'] = (int)$data['items_per_page'];
|
|
}
|
|
|
|
if (isset($data['default_view'])) {
|
|
$updateData['default_view'] = $data['default_view'];
|
|
}
|
|
|
|
if (isset($data['show_adult_content'])) {
|
|
$updateData['show_adult_content'] = $data['show_adult_content'] ? 1 : 0;
|
|
}
|
|
|
|
if (isset($data['auto_play_trailers'])) {
|
|
$updateData['auto_play_trailers'] = $data['auto_play_trailers'] ? 1 : 0;
|
|
}
|
|
|
|
if (isset($data['language'])) {
|
|
$updateData['language'] = $data['language'];
|
|
}
|
|
|
|
if (isset($data['theme'])) {
|
|
$updateData['theme'] = $data['theme'];
|
|
}
|
|
|
|
if (isset($data['jellyfin_library_mappings'])) {
|
|
$updateData['jellyfin_library_mappings'] = $data['jellyfin_library_mappings'];
|
|
}
|
|
|
|
// Check if settings row exists
|
|
$existing = $this->findById(1);
|
|
|
|
if ($existing) {
|
|
$this->update(1, $updateData);
|
|
return $this->getSettings();
|
|
} else {
|
|
// Create default settings if not exists
|
|
$defaultData = [
|
|
'enabled_categories' => json_encode(['Anime', 'Movies', 'TV Series', 'Music', 'Books', 'Consoles', 'Games', 'Adult']),
|
|
'items_per_page' => 20,
|
|
'default_view' => 'grid',
|
|
'show_adult_content' => 0,
|
|
'auto_play_trailers' => 0,
|
|
'language' => 'en',
|
|
'theme' => 'system',
|
|
'jellyfin_library_mappings' => ''
|
|
];
|
|
$mergedData = array_merge($defaultData, $updateData);
|
|
$this->create($mergedData);
|
|
return $this->getSettings();
|
|
}
|
|
}
|
|
}
|