impoort stuff!

This commit is contained in:
Lars Behrends
2025-10-24 16:04:34 +02:00
parent 73d8441787
commit 218d0c28c0
17 changed files with 3043 additions and 277 deletions

View File

@@ -37,19 +37,32 @@ class TvShow extends Model
];
/**
* Get all actors associated with this TV show
* Remove an actor from this TV show
*/
public function actors()
public function removeActor(int $actorId): bool
{
$stmt = $this->pdo->prepare("
SELECT a.*
FROM actors a
JOIN actor_tv_show ats ON a.id = ats.actor_id
WHERE ats.tv_show_id = :tv_show_id
ORDER BY a.name ASC
DELETE FROM actor_tv_show
WHERE tv_show_id = :tv_show_id AND actor_id = :actor_id
");
$stmt->execute(['tv_show_id' => $this->id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $stmt->execute([
'tv_show_id' => $this->id,
'actor_id' => $actorId
]);
}
/**
* Update the cast field with actor names
*/
public function updateCastField(): bool
{
$actors = $this->actors();
$actorNames = array_column($actors, 'name');
$castString = implode(', ', $actorNames);
return $this->update($this->id, [
'cast' => $castString
]);
}
/**
@@ -151,35 +164,51 @@ class TvShow extends Model
}
public function getSeasonsWithEpisodes(): array
{
$stmt = $this->pdo->prepare("
SELECT s.*,
COUNT(e.id) as episode_count,
SUM(CASE WHEN e.watched = 1 THEN 1 ELSE 0 END) as watched_episodes
FROM seasons s
LEFT JOIN episodes e ON s.id = e.season_id
WHERE s.tv_show_id = :tv_show_id
GROUP BY s.id
ORDER BY s.season_number ASC
");
$stmt->execute(['tv_show_id' => $this->id]);
$seasons = $stmt->fetchAll(\PDO::FETCH_ASSOC);
// Get episodes for each season
foreach ($seasons as &$season) {
{
// Get all episodes for this TV show, grouped by season
$stmt = $this->pdo->prepare("
SELECT e.*,
(SELECT COUNT(*) FROM user_episodes WHERE episode_id = e.id AND watched = 1) as watch_count
FROM episodes e
WHERE e.season_id = :season_id
ORDER BY e.episode_number ASC
SELECT season_number,
COUNT(*) as episode_count,
SUM(CASE WHEN watched = 1 THEN 1 ELSE 0 END) as watched_episodes
FROM tv_episodes
WHERE tv_show_id = :tv_show_id
GROUP BY season_number
ORDER BY season_number ASC
");
$stmt->execute(['season_id' => $season['id']]);
$season['episodes'] = $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
$stmt->execute(['tv_show_id' => $this->id]);
$seasonStats = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $seasons;
}
$seasons = [];
// For each season, get the episodes and create a season object
foreach ($seasonStats as $stat) {
$seasonNumber = $stat['season_number'];
// Get episodes for this season
$stmt = $this->pdo->prepare("
SELECT e.*
FROM tv_episodes e
WHERE e.tv_show_id = :tv_show_id AND e.season_number = :season_number
ORDER BY e.episode_number ASC
");
$stmt->execute([
'tv_show_id' => $this->id,
'season_number' => $seasonNumber
]);
$episodes = $stmt->fetchAll(\PDO::FETCH_ASSOC);
// Create a season object (simulating the old seasons table structure)
$seasons[] = [
'id' => null, // No seasons table, so no ID
'season_number' => $seasonNumber,
'episode_count' => (int)$stat['episode_count'],
'watched_episodes' => (int)$stat['watched_episodes'],
'episodes' => $episodes
];
}
return $seasons;
}
/**
* Get similar TV shows based on genres