Files
MediaCollectorLibary/app/Models/AdultVideo.php
Lars Behrends 73d8441787 i dont know
2025-10-20 23:40:55 +02:00

185 lines
5.1 KiB
PHP

<?php
namespace App\Models;
class AdultVideo extends Model
{
protected string $table = 'adult_videos';
protected array $fillable = [
'title',
'overview',
'poster_url',
'backdrop_url',
'rating',
'runtime_minutes',
'release_date',
'director',
'writer',
'cast',
'genre',
'metadata',
'watched',
'watch_count',
'is_favorite',
'source_id',
'external_id'
];
public static function getAllWithPagination(\PDO $pdo, int $page, int $perPage, string $search = ''): array
{
$offset = ($page - 1) * $perPage;
$whereClause = '';
$params = [];
if (!empty($search)) {
$whereClause = "WHERE (title LIKE :search OR overview LIKE :search)";
$params['search'] = "%{$search}%";
}
$sql = "
SELECT av.*, s.display_name as source_name
FROM adult_videos av
JOIN sources s ON av.source_id = s.id
{$whereClause}
ORDER BY av.created_at DESC
LIMIT :limit OFFSET :offset
";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':limit', $perPage, \PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);
if (!empty($search)) {
$stmt->bindValue(':search', "%{$search}%", \PDO::PARAM_STR);
}
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public static function getTotalCount(\PDO $pdo, string $search = ''): int
{
$whereClause = '';
$params = [];
if (!empty($search)) {
$whereClause = "WHERE (title LIKE :search OR overview LIKE :search)";
$params['search'] = "%{$search}%";
}
$sql = "SELECT COUNT(*) as count FROM adult_videos {$whereClause}";
$stmt = $pdo->prepare($sql);
if (!empty($search)) {
$stmt->bindValue(':search', "%{$search}%", \PDO::PARAM_STR);
}
$stmt->execute();
return (int) $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
}
public function markAsWatched(): bool
{
$stmt = $this->pdo->prepare("UPDATE adult_videos SET watched = 1, watch_count = watch_count + 1, updated_at = NOW() WHERE id = :id");
return $stmt->execute(['id' => $this->id]);
}
public function markAsUnwatched(): bool
{
$stmt = $this->pdo->prepare("UPDATE adult_videos SET watched = 0, updated_at = NOW() WHERE id = :id");
return $stmt->execute(['id' => $this->id]);
}
public function toggleFavorite(): bool
{
$stmt = $this->pdo->prepare("UPDATE adult_videos SET is_favorite = !is_favorite, updated_at = NOW() WHERE id = :id");
return $stmt->execute(['id' => $this->id]);
}
public function source(): ?Source
{
$stmt = $this->pdo->prepare("SELECT * FROM sources WHERE id = :source_id");
$stmt->execute(['source_id' => $this->source_id]);
$sourceData = $stmt->fetch(\PDO::FETCH_ASSOC);
return $sourceData ? new Source($this->pdo, $sourceData) : null;
}
/**
* Get all actors associated with this adult video
*/
public function actors()
{
$stmt = $this->pdo->prepare("
SELECT a.*
FROM actors a
JOIN actor_adult_video aav ON a.id = aav.actor_id
WHERE aav.adult_video_id = :adult_video_id
ORDER BY a.name ASC
");
$stmt->execute(['adult_video_id' => $this->id]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* Add an actor to this adult video
*/
public function addActor(int $actorId): bool
{
$stmt = $this->pdo->prepare("
INSERT IGNORE INTO actor_adult_video (adult_video_id, actor_id)
VALUES (:adult_video_id, :actor_id)
");
return $stmt->execute([
'adult_video_id' => $this->id,
'actor_id' => $actorId
]);
}
/**
* Remove an actor from this adult video
*/
public function removeActor(int $actorId): bool
{
$stmt = $this->pdo->prepare("
DELETE FROM actor_adult_video
WHERE adult_video_id = :adult_video_id AND actor_id = :actor_id
");
return $stmt->execute([
'adult_video_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
]);
}
/**
* Get TV show statistics
*/
public static function getStats(\PDO $pdo): array
{
$stmt = $pdo->query("
SELECT
COUNT(*) as total_adult_videos,
COUNT(CASE WHEN is_favorite = 1 THEN 1 END) as favorite_adult_videos,
AVG(rating) as avg_rating
FROM adult_videos
");
return $stmt->fetch(\PDO::FETCH_ASSOC);
}
}