Add strict types and type hints across API
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.
This commit is contained in:
@@ -1,34 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
require_once __DIR__ . '/../services/ImageHandler.php';
|
||||
|
||||
class Adult extends MediaType {
|
||||
private $imageHandler;
|
||||
private $isUpdate = false;
|
||||
private $mediaId = null;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private ImageHandler $imageHandler;
|
||||
private bool $isUpdate = false;
|
||||
private ?int $mediaId = null;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
$this->imageHandler = new ImageHandler();
|
||||
}
|
||||
|
||||
protected function getType() {
|
||||
|
||||
protected function getType(): string {
|
||||
return 'Adult';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Adult spezifische Validierung
|
||||
// Eventuell Altersverifikation etc.
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function processPosterField($data) {
|
||||
|
||||
protected function processPosterField(array $data): array {
|
||||
error_log("Adult::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
|
||||
|
||||
|
||||
if ($this->isUpdate && $this->mediaId && isset($data['poster']) && !empty($data['poster'])) {
|
||||
$currentMedia = $this->findById($this->mediaId);
|
||||
if ($currentMedia && !empty($currentMedia['poster'])) {
|
||||
@@ -39,15 +42,15 @@ class Adult extends MediaType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isset($data['poster']) && !empty($data['poster'])) {
|
||||
error_log("Adult::processPosterField - Poster found, length: " . strlen($data['poster']));
|
||||
|
||||
|
||||
if (strpos($data['poster'], '/images/') === 0 || filter_var($data['poster'], FILTER_VALIDATE_URL)) {
|
||||
error_log("Adult::processPosterField - Poster is already a path or URL, skipping processing");
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
$posterPath = $this->imageHandler->saveBase64Image($data['poster'], 'adult/poster');
|
||||
error_log("Adult::processPosterField - ImageHandler returned: " . ($posterPath ?: 'null'));
|
||||
if ($posterPath) {
|
||||
@@ -61,23 +64,23 @@ class Adult extends MediaType {
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
$data['type'] = 'Adult';
|
||||
$this->validateTypeSpecificFields($data);
|
||||
$data = $this->processPosterField($data);
|
||||
return parent::createWithRelations($data);
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
$this->isUpdate = true;
|
||||
$this->mediaId = $id;
|
||||
$this->validateTypeSpecificFields($data);
|
||||
$data = $this->processPosterField($data);
|
||||
parent::updateWithRelations($id, $data);
|
||||
return parent::updateWithRelations($id, $data);
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Adult Content suchen
|
||||
$filters['type'] = 'Adult';
|
||||
return parent::search($filters, $page, $limit);
|
||||
|
||||
@@ -1,41 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/Cast.php';
|
||||
require_once __DIR__ . '/../services/ApiLogger.php';
|
||||
|
||||
class AdultCast extends Cast {
|
||||
|
||||
public function getWithAdultSpecifics($id) {
|
||||
|
||||
public function getWithAdultSpecifics(?int $id): ?array {
|
||||
$cast = $this->getWithFilmography($id);
|
||||
if (!$cast) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$cast['adult_specifics'] = $this->getAdultSpecifics($id);
|
||||
|
||||
|
||||
return $cast;
|
||||
}
|
||||
|
||||
public function getAdultSpecifics($castId) {
|
||||
|
||||
public function getAdultSpecifics(?int $castId): array|false {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM adult_cast_specifics WHERE cast_id = ?");
|
||||
$stmt->execute([$castId]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
public function createWithAdultSpecifics($data) {
|
||||
|
||||
public function createWithAdultSpecifics(array $data): int {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics called with data: " . json_encode($data));
|
||||
|
||||
|
||||
$name = $data['name'] ?? null;
|
||||
if (!$name) {
|
||||
throw new Exception('Name is required');
|
||||
}
|
||||
|
||||
|
||||
// cleanname generieren
|
||||
$cleanname = generateCleanName($name);
|
||||
|
||||
|
||||
// Process photo field (base64 to file path)
|
||||
$data = $this->processPhotoField($data);
|
||||
|
||||
|
||||
// Zuerst Basis-Cast erstellen
|
||||
$castData = [
|
||||
'name' => $name,
|
||||
@@ -45,16 +47,16 @@ class AdultCast extends Cast {
|
||||
'birthDate' => $data['birthDate'] ?? null,
|
||||
'birthPlace' => $data['birthPlace'] ?? null
|
||||
];
|
||||
|
||||
$castId = $this->create($castData);
|
||||
|
||||
$castId = (int)$this->create($castData);
|
||||
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics: Base cast created with id: $castId");
|
||||
|
||||
|
||||
// Occupations speichern
|
||||
if (isset($data['occupations']) && is_array($data['occupations'])) {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics: Saving occupations: " . json_encode($data['occupations']));
|
||||
$this->saveRelatedItems('occupations', $castId, $data['occupations'], 'cast_id');
|
||||
}
|
||||
|
||||
|
||||
// Adult-spezifische Daten speichern
|
||||
if (isset($data['adult_specifics']) && is_array($data['adult_specifics'])) {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics: Saving adult_specifics");
|
||||
@@ -62,20 +64,20 @@ class AdultCast extends Cast {
|
||||
} else {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast createWithAdultSpecifics: No adult_specifics found in data");
|
||||
}
|
||||
|
||||
|
||||
return $castId;
|
||||
}
|
||||
|
||||
public function updateWithAdultSpecifics($id, $data) {
|
||||
|
||||
public function updateWithAdultSpecifics(int $id, array $data): bool {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics called with id: $id, data: " . json_encode($data));
|
||||
|
||||
|
||||
// Set update flag for image replacement
|
||||
$this->isUpdate = true;
|
||||
$this->castId = $id;
|
||||
|
||||
|
||||
// Process photo field (base64 to file path)
|
||||
$data = $this->processPhotoField($data);
|
||||
|
||||
|
||||
// Basis-Cast aktualisieren
|
||||
$castData = [];
|
||||
foreach (['name', 'photo', 'bio', 'birthDate', 'birthPlace'] as $field) {
|
||||
@@ -83,19 +85,19 @@ class AdultCast extends Cast {
|
||||
$castData[$field] = $data[$field];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!empty($castData)) {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics: Updating base cast data: " . json_encode($castData));
|
||||
$this->update($id, $castData);
|
||||
}
|
||||
|
||||
|
||||
// Occupations aktualisieren
|
||||
if (isset($data['occupations']) && is_array($data['occupations'])) {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics: Updating occupations: " . json_encode($data['occupations']));
|
||||
$this->pdo->prepare("DELETE FROM occupations WHERE cast_id = ?")->execute([$id]);
|
||||
$this->saveRelatedItems('occupations', $id, $data['occupations'], 'cast_id');
|
||||
}
|
||||
|
||||
|
||||
// Adult-spezifische Daten aktualisieren
|
||||
if (isset($data['adult_specifics']) && is_array($data['adult_specifics'])) {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics: Saving adult_specifics");
|
||||
@@ -104,11 +106,11 @@ class AdultCast extends Cast {
|
||||
} else {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast updateWithAdultSpecifics: No adult_specifics found in data");
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function saveAdultSpecifics($castId, $specifics) {
|
||||
|
||||
protected function saveAdultSpecifics(int $castId, array $specifics): void {
|
||||
ApiLogger::getInstance()->logDebug("AdultCast saveAdultSpecifics called for cast_id: $castId");
|
||||
ApiLogger::getInstance()->logDebug("Specifics data: " . json_encode($specifics));
|
||||
|
||||
@@ -200,13 +202,13 @@ class AdultCast extends Cast {
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteAdultSpecifics($castId) {
|
||||
public function deleteAdultSpecifics(?int $castId): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM adult_cast_specifics WHERE cast_id = ?");
|
||||
$stmt->execute([$castId]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function searchAdultActors($filters = [], $page = 1, $limit = 2000000000) {
|
||||
public function searchAdultActors(array $filters = [], int $page = 1, int $limit = 2000000000): array {
|
||||
// Adult Actors mit Specifics suchen
|
||||
$query = "
|
||||
SELECT cs.*,
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
abstract class BaseModel {
|
||||
protected $pdo;
|
||||
protected $table;
|
||||
|
||||
public function __construct($pdo) {
|
||||
protected PDO $pdo;
|
||||
protected string $table;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
protected function findById($id) {
|
||||
|
||||
protected function findById(int $id): array|false {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
protected function findAll($conditions = [], $orderBy = 'createdAt DESC', $limit = null, $offset = null) {
|
||||
|
||||
protected function findAll(array $conditions = [], string $orderBy = 'createdAt DESC', ?int $limit = null, ?int $offset = null): array {
|
||||
$query = "SELECT * FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
// LIKE Operator
|
||||
@@ -28,26 +30,26 @@ abstract class BaseModel {
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$query .= " ORDER BY $orderBy";
|
||||
|
||||
if ($limit) {
|
||||
|
||||
if ($limit !== null) {
|
||||
$query .= " LIMIT " . (int)$limit;
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
|
||||
if ($offset !== null) {
|
||||
$query .= " OFFSET " . (int)$offset;
|
||||
}
|
||||
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
protected function count($conditions = []) {
|
||||
|
||||
protected function count(array $conditions = []): int|false {
|
||||
$query = "SELECT COUNT(*) FROM {$this->table} WHERE 1=1";
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($conditions as $field => $value) {
|
||||
if (is_array($value)) {
|
||||
$query .= " AND $field LIKE ?";
|
||||
@@ -57,42 +59,42 @@ abstract class BaseModel {
|
||||
$params[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
protected function create($data) {
|
||||
|
||||
protected function create(array $data): int|false {
|
||||
$fields = array_keys($data);
|
||||
$placeholders = array_fill(0, count($fields), '?');
|
||||
|
||||
|
||||
$query = "INSERT INTO {$this->table} (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $placeholders) . ")";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute(array_values($data));
|
||||
|
||||
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
protected function update($id, $data) {
|
||||
|
||||
protected function update(int $id, array $data): bool {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
|
||||
|
||||
$params[] = $id;
|
||||
|
||||
|
||||
$query = "UPDATE {$this->table} SET " . implode(', ', $fields) . " WHERE id = ?";
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
|
||||
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
protected function delete($id) {
|
||||
|
||||
protected function delete(int $id): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->rowCount() > 0;
|
||||
|
||||
@@ -1,32 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/BaseModel.php';
|
||||
require_once __DIR__ . '/../services/ImageHandler.php';
|
||||
|
||||
class Cast extends BaseModel {
|
||||
protected $table = 'cast_staff';
|
||||
private $imageHandler;
|
||||
protected $isUpdate = false;
|
||||
protected $castId = null;
|
||||
|
||||
public function __construct($pdo) {
|
||||
protected string $table = 'cast_staff';
|
||||
private ImageHandler $imageHandler;
|
||||
protected bool $isUpdate = false;
|
||||
protected ?int $castId = null;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
$this->imageHandler = new ImageHandler();
|
||||
}
|
||||
|
||||
public function getWithFilmography($id) {
|
||||
|
||||
public function getWithFilmography(?int $id): ?array {
|
||||
$cast = $this->findById($id);
|
||||
if (!$cast) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$cast['occupations'] = $this->getRelatedItems('occupations', $id, 'cast_id');
|
||||
$cast['filmography'] = $this->getMediaForCast($id);
|
||||
|
||||
|
||||
return $cast;
|
||||
}
|
||||
|
||||
public function getMediaForCast($castId) {
|
||||
|
||||
public function getMediaForCast(?int $castId): array {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT m.id, m.title, m.year, m.poster, m.category, m.type, mc.role, mc.characterName
|
||||
FROM media m
|
||||
@@ -37,8 +39,8 @@ class Cast extends BaseModel {
|
||||
$stmt->execute([$castId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
$conditions = [];
|
||||
|
||||
if (isset($filters['search'])) {
|
||||
@@ -88,7 +90,7 @@ class Cast extends BaseModel {
|
||||
];
|
||||
}
|
||||
|
||||
protected function processPhotoField($data) {
|
||||
protected function processPhotoField(array $data): array {
|
||||
error_log("Cast::processPhotoField - Checking for photo field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
|
||||
|
||||
if ($this->isUpdate && $this->castId && isset($data['photo']) && !empty($data['photo'])) {
|
||||
@@ -124,7 +126,7 @@ class Cast extends BaseModel {
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function createWithOccupations($data) {
|
||||
public function createWithOccupations(array $data): int {
|
||||
$name = $data['name'] ?? null;
|
||||
if (!$name) {
|
||||
throw new Exception('Name is required');
|
||||
@@ -153,7 +155,7 @@ class Cast extends BaseModel {
|
||||
return $castId;
|
||||
}
|
||||
|
||||
public function updateWithOccupations($id, $data) {
|
||||
public function updateWithOccupations(int $id, array $data): bool {
|
||||
$this->isUpdate = true;
|
||||
$this->castId = $id;
|
||||
|
||||
@@ -184,13 +186,13 @@ class Cast extends BaseModel {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function findByCleanName($cleanname) {
|
||||
public function findByCleanName(string $cleanname): array|false {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE cleanname = ?");
|
||||
$stmt->execute([$cleanname]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
protected function getRelatedItems($table, $id, $fkColumn = 'media_id') {
|
||||
protected function getRelatedItems(string $table, int $id, string $fkColumn = 'media_id'): array {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM $table WHERE $fkColumn = ?");
|
||||
$stmt->execute([$id]);
|
||||
$items = $stmt->fetchAll();
|
||||
@@ -201,7 +203,7 @@ class Cast extends BaseModel {
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function saveRelatedItems($table, $id, $items, $fkColumn = 'media_id') {
|
||||
protected function saveRelatedItems(string $table, int $id, array $items, string $fkColumn = 'media_id'): void {
|
||||
$valueColumn = $fkColumn === 'cast_id' ? 'occupation' : 'genre';
|
||||
$stmt = $this->pdo->prepare("INSERT INTO $table ($fkColumn, $valueColumn) VALUES (?, ?)");
|
||||
foreach ($items as $item) {
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
|
||||
class Console extends MediaType {
|
||||
protected function getType() {
|
||||
protected function getType(): string {
|
||||
return 'Console';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Console spezifische Validierung
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Consoles suchen
|
||||
$filters['type'] = 'Console';
|
||||
return parent::search($filters, $page, $limit);
|
||||
|
||||
@@ -1,27 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
require_once __DIR__ . '/../services/ImageHandler.php';
|
||||
|
||||
class Game extends MediaType {
|
||||
private $imageHandler;
|
||||
private $isUpdate = false;
|
||||
private $mediaId = null;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private ImageHandler $imageHandler;
|
||||
private bool $isUpdate = false;
|
||||
private ?int $mediaId = null;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
$this->imageHandler = new ImageHandler();
|
||||
}
|
||||
|
||||
protected function getType() {
|
||||
|
||||
protected function getType(): string {
|
||||
return 'Game';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Game spezifische Validierung
|
||||
if (isset($data['hasIcon'])) {
|
||||
$data['hasIcon'] = is_numeric($data['hasIcon']) ? (int)$data['hasIcon'] : 0;
|
||||
@@ -47,11 +49,11 @@ class Game extends MediaType {
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process poster field - convert base64 to file path
|
||||
*/
|
||||
protected function processImageField($data, $type) {
|
||||
protected function processImageField(array $data, string $type): array {
|
||||
if ($this->isUpdate && $this->mediaId && isset($data[$type]) && !empty($data[$type])) {
|
||||
$currentMedia = $this->findById($this->mediaId);
|
||||
if ($currentMedia && !empty($currentMedia[$type])) {
|
||||
@@ -73,7 +75,7 @@ class Game extends MediaType {
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
public function createWithRelations(array $data): int {
|
||||
// Typ setzen
|
||||
$data['type'] = 'Game';
|
||||
|
||||
@@ -152,7 +154,7 @@ class Game extends MediaType {
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
// Set update flag and mediaId for image replacement
|
||||
$this->isUpdate = true;
|
||||
$this->mediaId = $id;
|
||||
@@ -236,7 +238,7 @@ class Game extends MediaType {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getWithGameInfo($id) {
|
||||
public function getWithGameInfo(?int $id): ?array {
|
||||
$media = parent::getWithRelations($id);
|
||||
if (!$media) {
|
||||
return null;
|
||||
@@ -264,7 +266,7 @@ class Game extends MediaType {
|
||||
return $media;
|
||||
}
|
||||
|
||||
public function getGameInfoForList($mediaId) {
|
||||
public function getGameInfoForList(?int $mediaId): ?array {
|
||||
// Media-Games Daten abrufen (ohne vollständige Relationen für Performance)
|
||||
$mediaGameId = $this->getMediaGameId($mediaId);
|
||||
if (!$mediaGameId) {
|
||||
@@ -281,7 +283,7 @@ class Game extends MediaType {
|
||||
return $gameInfo;
|
||||
}
|
||||
|
||||
public static function interpolateQuery($query, $params) {
|
||||
public static function interpolateQuery(string $query, array $params): string {
|
||||
$keys = array();
|
||||
|
||||
# build a regular expression for each parameter
|
||||
@@ -300,7 +302,7 @@ class Game extends MediaType {
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function createMediaGame($data) {
|
||||
protected function createMediaGame(array $data): string {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO media_games (media_id, sortingName, notes, completionStatus, source, gameId, pluginId,
|
||||
isInstalled, installDirectory, installSize, hidden, favorite, playCount,
|
||||
@@ -337,7 +339,7 @@ class Game extends MediaType {
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
protected function updateMediaGame($mediaId, $data) {
|
||||
protected function updateMediaGame(int $mediaId, array $data): void {
|
||||
$setClause = [];
|
||||
$params = [];
|
||||
|
||||
@@ -354,14 +356,14 @@ class Game extends MediaType {
|
||||
$stmt->execute($params);
|
||||
}
|
||||
|
||||
protected function getMediaGameId($mediaId) {
|
||||
protected function getMediaGameId(?int $mediaId): ?int {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM media_games WHERE media_id = ?");
|
||||
$stmt->execute([$mediaId]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
protected function getMediaGameData($mediaGameId) {
|
||||
protected function getMediaGameData(?int $mediaGameId): array {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM media_games WHERE id = ?");
|
||||
$stmt->execute([$mediaGameId]);
|
||||
$data = $stmt->fetch();
|
||||
@@ -373,7 +375,7 @@ class Game extends MediaType {
|
||||
return $data ?: [];
|
||||
}
|
||||
|
||||
protected function saveAchievements($mediaGameId, $achievements) {
|
||||
protected function saveAchievements(string $mediaGameId, array $achievements): void {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO achievements (media_game_id, name, description, icon, unlocked, unlocked_date)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
@@ -396,26 +398,26 @@ class Game extends MediaType {
|
||||
}
|
||||
}
|
||||
|
||||
protected function getAchievements($mediaGameId) {
|
||||
protected function getAchievements(string $mediaGameId): array {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM achievements WHERE media_game_id = ?");
|
||||
$stmt->execute([$mediaGameId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
protected function saveGameRelation($table, $mediaGameId, $items, $field) {
|
||||
protected function saveGameRelation(string $table, string $mediaGameId, array $items, string $field): void {
|
||||
$stmt = $this->pdo->prepare("INSERT INTO $table (media_game_id, $field) VALUES (?, ?)");
|
||||
foreach ($items as $item) {
|
||||
$stmt->execute([$mediaGameId, $item]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function consoleExists($name) {
|
||||
protected function consoleExists(string $name): bool {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM media WHERE type = 'Console' AND title = ?");
|
||||
$stmt->execute([$name]);
|
||||
return $stmt->fetch() !== false;
|
||||
}
|
||||
|
||||
protected function createConsole($name) {
|
||||
protected function createConsole(string $name): string {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO media (title, cleanname, type, createdAt, updatedAt)
|
||||
VALUES (?, ?, 'Console', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
@@ -424,7 +426,7 @@ class Game extends MediaType {
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
protected function saveGameRelationWithConsole($table, $mediaGameId, $items, $field) {
|
||||
protected function saveGameRelationWithConsole(string $table, string $mediaGameId, array $items, string $field): void {
|
||||
$stmt = $this->pdo->prepare("INSERT INTO $table (media_game_id, $field) VALUES (?, ?)");
|
||||
foreach ($items as $item) {
|
||||
// Check if console exists, create if not
|
||||
@@ -435,7 +437,7 @@ class Game extends MediaType {
|
||||
}
|
||||
}
|
||||
|
||||
protected function getGameRelation($table, $mediaGameId, $field) {
|
||||
protected function getGameRelation(string $table, string $mediaGameId, string $field): array {
|
||||
$stmt = $this->pdo->prepare("SELECT $field FROM $table WHERE media_game_id = ?");
|
||||
$stmt->execute([$mediaGameId]);
|
||||
$items = $stmt->fetchAll();
|
||||
@@ -444,7 +446,7 @@ class Game extends MediaType {
|
||||
}, $items);
|
||||
}
|
||||
|
||||
protected function saveLinks($mediaGameId, $links) {
|
||||
protected function saveLinks(string $mediaGameId, array $links): void {
|
||||
$stmt = $this->pdo->prepare("INSERT INTO game_links (media_game_id, name, url) VALUES (?, ?, ?)");
|
||||
foreach ($links as $link) {
|
||||
$stmt->execute([
|
||||
@@ -455,13 +457,13 @@ class Game extends MediaType {
|
||||
}
|
||||
}
|
||||
|
||||
protected function getLinks($mediaGameId) {
|
||||
protected function getLinks(string $mediaGameId): array {
|
||||
$stmt = $this->pdo->prepare("SELECT name, url FROM game_links WHERE media_game_id = ?");
|
||||
$stmt->execute([$mediaGameId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Games suchen
|
||||
$filters['type'] = 'Game';
|
||||
return parent::search($filters, $page, $limit);
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/BaseModel.php';
|
||||
|
||||
class Media extends BaseModel {
|
||||
protected $table = 'media';
|
||||
|
||||
public function getBase($id) {
|
||||
protected string $table = 'media';
|
||||
|
||||
public function getBase(?int $id): array|false {
|
||||
return $this->findById($id);
|
||||
}
|
||||
|
||||
public function getWithRelations($id) {
|
||||
public function getWithRelations(?int $id): ?array {
|
||||
$media = $this->findById($id);
|
||||
if (!$media) {
|
||||
return null;
|
||||
@@ -22,8 +24,8 @@ class Media extends BaseModel {
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
$conditions = [];
|
||||
|
||||
if (isset($filters['category'])) {
|
||||
@@ -99,7 +101,7 @@ class Media extends BaseModel {
|
||||
];
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
public function createWithRelations(array $data): int {
|
||||
$title = $data['title'] ?? null;
|
||||
if (!$title) {
|
||||
throw new Exception('Title is required');
|
||||
@@ -147,7 +149,7 @@ class Media extends BaseModel {
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
$mediaData = [];
|
||||
|
||||
foreach (['title', 'year', 'poster', 'banner', 'description', 'rating', 'category', 'type', 'status', 'aspectRatio', 'runtime', 'director', 'writer', 'releaseDate', 'source'] as $field) {
|
||||
@@ -186,13 +188,13 @@ class Media extends BaseModel {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function findByCleanName($cleanname) {
|
||||
public function findByCleanName(string $cleanname): array|false {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE cleanname = ?");
|
||||
$stmt->execute([$cleanname]);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
protected function saveRelatedItems($table, $id, $items, $fkColumn = 'media_id') {
|
||||
protected function saveRelatedItems(string $table, int $id, array $items, string $fkColumn = 'media_id'): void {
|
||||
$valueColumn = $table === 'genres' ? 'genre' : ($table === 'tags' ? 'tag' : ($table === 'occupations' ? 'occupation' : 'studio'));
|
||||
$stmt = $this->pdo->prepare("INSERT INTO $table ($fkColumn, $valueColumn) VALUES (?, ?)");
|
||||
foreach ($items as $item) {
|
||||
@@ -200,7 +202,7 @@ class Media extends BaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
protected function getCastForMedia($mediaId) {
|
||||
protected function getCastForMedia(?int $mediaId): array {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT cs.*, mc.role, mc.characterName, mc.characterImage
|
||||
FROM cast_staff cs
|
||||
@@ -215,7 +217,7 @@ class Media extends BaseModel {
|
||||
return $cast;
|
||||
}
|
||||
|
||||
protected function getRelatedItems($table, $id, $fkColumn = 'media_id') {
|
||||
protected function getRelatedItems(string $table, int $id, string $fkColumn = 'media_id'): array {
|
||||
$stmt = $this->pdo->prepare("SELECT * FROM $table WHERE $fkColumn = ?");
|
||||
$stmt->execute([$id]);
|
||||
$items = $stmt->fetchAll();
|
||||
@@ -230,7 +232,7 @@ class Media extends BaseModel {
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function saveCastAssignments($mediaId, $castData) {
|
||||
protected function saveCastAssignments(int $mediaId, array $castData): void {
|
||||
foreach ($castData as $member) {
|
||||
$castId = null;
|
||||
if (isset($member['id']) && $member['id']) {
|
||||
|
||||
@@ -1,39 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/Media.php';
|
||||
|
||||
abstract class MediaType extends Media {
|
||||
protected $type;
|
||||
|
||||
public function __construct($pdo) {
|
||||
protected string $type;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
$this->type = $this->getType();
|
||||
}
|
||||
|
||||
abstract protected function getType();
|
||||
|
||||
abstract protected function validateTypeSpecificFields($data);
|
||||
|
||||
abstract protected function getTypeSpecificFields();
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
abstract protected function getType(): string;
|
||||
|
||||
abstract protected function validateTypeSpecificFields(array $data): array;
|
||||
|
||||
abstract protected function getTypeSpecificFields(): array;
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
// Typ setzen
|
||||
$data['type'] = $this->type;
|
||||
|
||||
|
||||
// Typ-spezifische Validierung
|
||||
$this->validateTypeSpecificFields($data);
|
||||
|
||||
|
||||
return parent::createWithRelations($data);
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
// Typ-spezifische Validierung
|
||||
$this->validateTypeSpecificFields($data);
|
||||
|
||||
|
||||
return parent::updateWithRelations($id, $data);
|
||||
}
|
||||
|
||||
protected function getRequiredFields() {
|
||||
|
||||
protected function getRequiredFields(): array {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
require_once __DIR__ . '/../services/ImageHandler.php';
|
||||
|
||||
class Movie extends MediaType {
|
||||
private $imageHandler;
|
||||
private $isUpdate = false;
|
||||
private $mediaId = null;
|
||||
|
||||
public function __construct($pdo) {
|
||||
private ImageHandler $imageHandler;
|
||||
private bool $isUpdate = false;
|
||||
private ?int $mediaId = null;
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
$this->imageHandler = new ImageHandler();
|
||||
}
|
||||
|
||||
protected function getType() {
|
||||
|
||||
protected function getType(): string {
|
||||
return 'Movie';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return ['runtime', 'director', 'writer'];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Movies sollten bestimmte Felder haben
|
||||
if (isset($data['runtime']) && !is_numeric($data['runtime'])) {
|
||||
throw new Exception('Runtime must be a number');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function processPosterField($data) {
|
||||
|
||||
protected function processPosterField(array $data): array {
|
||||
error_log("Movie::processPosterField - Checking for poster field, isUpdate: " . ($this->isUpdate ? 'yes' : 'no'));
|
||||
|
||||
|
||||
// If this is an update and poster is being changed, delete old image
|
||||
if ($this->isUpdate && $this->mediaId && isset($data['poster']) && !empty($data['poster'])) {
|
||||
$currentMedia = $this->findById($this->mediaId);
|
||||
@@ -42,15 +45,15 @@ class Movie extends MediaType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isset($data['poster']) && !empty($data['poster'])) {
|
||||
error_log("Movie::processPosterField - Poster found, length: " . strlen($data['poster']));
|
||||
|
||||
|
||||
if (strpos($data['poster'], '/images/') === 0 || filter_var($data['poster'], FILTER_VALIDATE_URL)) {
|
||||
error_log("Movie::processPosterField - Poster is already a path or URL, skipping processing");
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
$posterPath = $this->imageHandler->saveBase64Image($data['poster'], 'movies/poster');
|
||||
error_log("Movie::processPosterField - ImageHandler returned: " . ($posterPath ?: 'null'));
|
||||
if ($posterPath) {
|
||||
@@ -64,23 +67,23 @@ class Movie extends MediaType {
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
$data['type'] = 'Movie';
|
||||
$data = $this->validateTypeSpecificFields($data);
|
||||
$data = $this->processPosterField($data);
|
||||
return parent::createWithRelations($data);
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
$this->isUpdate = true;
|
||||
$this->mediaId = $id;
|
||||
$this->validateTypeSpecificFields($data);
|
||||
$data = $this->processPosterField($data);
|
||||
parent::updateWithRelations($id, $data);
|
||||
return parent::updateWithRelations($id, $data);
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Movies suchen
|
||||
$filters['type'] = 'Movie';
|
||||
return parent::search($filters, $page, $limit);
|
||||
|
||||
@@ -1,46 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
|
||||
class Music extends MediaType {
|
||||
protected function getType() {
|
||||
protected function getType(): string {
|
||||
return 'Album';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return ['artist', 'album_type', 'release_date'];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Music spezifische Validierung
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Music suchen
|
||||
$filters['type'] = 'Album';
|
||||
return parent::search($filters, $page, $limit);
|
||||
}
|
||||
|
||||
public function getWithTracks($id) {
|
||||
|
||||
public function getWithTracks(?int $id): ?array {
|
||||
$media = $this->getWithRelations($id);
|
||||
if (!$media) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$media['tracks'] = $this->getTracks($id);
|
||||
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
public function getTracks($mediaId) {
|
||||
|
||||
public function getTracks(?int $mediaId): array {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT * FROM tracks WHERE media_id = ? ORDER BY track_number
|
||||
");
|
||||
$stmt->execute([$mediaId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function addTrack($mediaId, $trackData) {
|
||||
|
||||
public function addTrack(?int $mediaId, array $trackData): int {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO tracks (media_id, track_number, title, artist)
|
||||
VALUES (?, ?, ?, ?)
|
||||
@@ -52,20 +55,20 @@ class Music extends MediaType {
|
||||
//$trackData['duration'] ?? null,
|
||||
$trackData['artist'] ?? null
|
||||
]);
|
||||
return $this->pdo->lastInsertId();
|
||||
return (int)$this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public function updateTrack($trackId, $trackData) {
|
||||
|
||||
public function updateTrack(?int $trackId, array $trackData): bool {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach (['track_number', 'title', 'artist'] as $field) {
|
||||
if (array_key_exists($field, $trackData)) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $trackData[$field];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!empty($fields)) {
|
||||
$params[] = $trackId;
|
||||
$stmt = $this->pdo->prepare("UPDATE tracks SET " . implode(', ', $fields) . " WHERE id = ?");
|
||||
@@ -74,29 +77,29 @@ class Music extends MediaType {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function deleteTrack($trackId) {
|
||||
|
||||
public function deleteTrack(?int $trackId): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM tracks WHERE id = ?");
|
||||
$stmt->execute([$trackId]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
$mediaId = parent::createWithRelations($data);
|
||||
|
||||
|
||||
// Tracks speichern
|
||||
if (isset($data['tracks']) && is_array($data['tracks'])) {
|
||||
foreach ($data['tracks'] as $track) {
|
||||
$this->addTrack($mediaId, $track);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
parent::updateWithRelations($id, $data);
|
||||
|
||||
|
||||
// Tracks aktualisieren
|
||||
if (isset($data['tracks']) && is_array($data['tracks'])) {
|
||||
// Alle existierenden Tracks löschen
|
||||
@@ -106,7 +109,6 @@ class Music extends MediaType {
|
||||
$this->addTrack($id, $track);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/MediaType.php';
|
||||
|
||||
class Series extends MediaType {
|
||||
protected function getType() {
|
||||
protected function getType(): string {
|
||||
return 'TV';
|
||||
}
|
||||
|
||||
protected function getTypeSpecificFields() {
|
||||
|
||||
protected function getTypeSpecificFields(): array {
|
||||
return ['runtime', 'director', 'writer'];
|
||||
}
|
||||
|
||||
protected function validateTypeSpecificFields($data) {
|
||||
|
||||
protected function validateTypeSpecificFields(array $data): array {
|
||||
// Series Validierung
|
||||
if (isset($data['runtime']) && !is_numeric($data['runtime'])) {
|
||||
throw new Exception('Runtime must be a number');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function search($filters = [], $page = 1, $limit = 20) {
|
||||
|
||||
public function search(array $filters = [], int $page = 1, int $limit = 20): array {
|
||||
// Nur Series suchen
|
||||
$filters['type'] = 'TV';
|
||||
return parent::search($filters, $page, $limit);
|
||||
}
|
||||
|
||||
public function getWithEpisodes($id) {
|
||||
|
||||
public function getWithEpisodes(?int $id): ?array {
|
||||
$media = $this->getWithRelations($id);
|
||||
if (!$media) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
$media['episodes'] = $this->getEpisodes($id);
|
||||
$media['seasons'] = $this->getSeasons($id);
|
||||
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
public function getEpisodes($mediaId, $season = null) {
|
||||
|
||||
public function getEpisodes(?int $mediaId, ?int $season = null): array {
|
||||
$query = "SELECT * FROM episodes WHERE media_id = ?";
|
||||
$params = [$mediaId];
|
||||
|
||||
|
||||
if ($season !== null) {
|
||||
$query .= " AND season = ?";
|
||||
$params[] = $season;
|
||||
}
|
||||
|
||||
|
||||
$query .= " ORDER BY season, episode_number";
|
||||
|
||||
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function getSeasons($mediaId) {
|
||||
|
||||
public function getSeasons(?int $mediaId): array {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT DISTINCT season, COUNT(*) as episode_count,
|
||||
MIN(air_date) as first_air_date, MAX(air_date) as last_air_date
|
||||
@@ -64,8 +67,8 @@ class Series extends MediaType {
|
||||
$stmt->execute([$mediaId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
public function addEpisode($mediaId, $episodeData) {
|
||||
|
||||
public function addEpisode(?int $mediaId, array $episodeData): int {
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO episodes (media_id, season, episode_number, title, description, air_date, duration, thumbnail)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
@@ -80,20 +83,20 @@ class Series extends MediaType {
|
||||
$episodeData['duration'] ?? null,
|
||||
$episodeData['thumbnail'] ?? null
|
||||
]);
|
||||
return $this->pdo->lastInsertId();
|
||||
return (int)$this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
public function updateEpisode($episodeId, $episodeData) {
|
||||
|
||||
public function updateEpisode(?int $episodeId, array $episodeData): bool {
|
||||
$fields = [];
|
||||
$params = [];
|
||||
|
||||
|
||||
foreach (['season', 'episode_number', 'title', 'description', 'air_date', 'duration', 'thumbnail'] as $field) {
|
||||
if (array_key_exists($field, $episodeData)) {
|
||||
$fields[] = "$field = ?";
|
||||
$params[] = $episodeData[$field];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!empty($fields)) {
|
||||
$params[] = $episodeId;
|
||||
$stmt = $this->pdo->prepare("UPDATE episodes SET " . implode(', ', $fields) . " WHERE id = ?");
|
||||
@@ -102,29 +105,29 @@ class Series extends MediaType {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function deleteEpisode($episodeId) {
|
||||
|
||||
public function deleteEpisode(?int $episodeId): bool {
|
||||
$stmt = $this->pdo->prepare("DELETE FROM episodes WHERE id = ?");
|
||||
$stmt->execute([$episodeId]);
|
||||
return $stmt->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function createWithRelations($data) {
|
||||
|
||||
public function createWithRelations(array $data): int {
|
||||
$mediaId = parent::createWithRelations($data);
|
||||
|
||||
|
||||
// Episoden speichern
|
||||
if (isset($data['episodes']) && is_array($data['episodes'])) {
|
||||
foreach ($data['episodes'] as $episode) {
|
||||
$this->addEpisode($mediaId, $episode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
public function updateWithRelations($id, $data) {
|
||||
|
||||
public function updateWithRelations(int $id, array $data): bool {
|
||||
parent::updateWithRelations($id, $data);
|
||||
|
||||
|
||||
// Episoden aktualisieren
|
||||
if (isset($data['episodes']) && is_array($data['episodes'])) {
|
||||
// Alle existierenden Episoden löschen
|
||||
@@ -134,7 +137,6 @@ class Series extends MediaType {
|
||||
$this->addEpisode($id, $episode);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/BaseModel.php';
|
||||
|
||||
class Settings extends BaseModel {
|
||||
protected $table = 'settings';
|
||||
|
||||
public function __construct($pdo) {
|
||||
protected string $table = 'settings';
|
||||
|
||||
public function __construct(PDO $pdo) {
|
||||
parent::__construct($pdo);
|
||||
}
|
||||
|
||||
public function getSettings() {
|
||||
|
||||
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) : [];
|
||||
@@ -21,11 +23,11 @@ class Settings extends BaseModel {
|
||||
$settings['show_adult_content'] = (bool)$settings['show_adult_content'];
|
||||
$settings['auto_play_trailers'] = (bool)$settings['auto_play_trailers'];
|
||||
}
|
||||
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public function updateSettings($data) {
|
||||
|
||||
public function updateSettings(array $data): ?array {
|
||||
$updateData = [];
|
||||
|
||||
if (isset($data['enabled_categories']) && is_array($data['enabled_categories'])) {
|
||||
|
||||
Reference in New Issue
Block a user