mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
Stuff i guess ?
This commit is contained in:
@@ -46,6 +46,150 @@ class Movie extends Model
|
||||
return $sourceData ? new Source($this->pdo, $sourceData) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total count of movies with optional filters
|
||||
*/
|
||||
public static function getTotalCount(\PDO $pdo, string $search = '', array $genres = [], array $directors = []): int
|
||||
{
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
$sql = "SELECT COUNT(*) as count FROM movies m JOIN sources s ON m.source_id = s.id";
|
||||
|
||||
if (!empty($search)) {
|
||||
$where[] = "(m.title LIKE :search OR m.overview LIKE :search)";
|
||||
$params[':search'] = "%$search%";
|
||||
}
|
||||
|
||||
if (!empty($genres)) {
|
||||
$genreConditions = [];
|
||||
foreach ($genres as $i => $genre) {
|
||||
$param = ":genre_$i";
|
||||
$genreConditions[] = "m.genre LIKE $param";
|
||||
$params[$param] = "%$genre%";
|
||||
}
|
||||
$where[] = "(" . implode(' OR ', $genreConditions) . ")";
|
||||
}
|
||||
|
||||
if (!empty($directors)) {
|
||||
$directorConditions = [];
|
||||
foreach ($directors as $i => $director) {
|
||||
$param = ":director_$i";
|
||||
$directorConditions[] = "m.director LIKE $param";
|
||||
$params[$param] = "%$director%";
|
||||
}
|
||||
$where[] = "(" . implode(' OR ', $directorConditions) . ")";
|
||||
}
|
||||
|
||||
if (!empty($where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $where);
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
return (int)$stmt->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated movies with optional filters
|
||||
*/
|
||||
public function getPaginated(
|
||||
\PDO $pdo,
|
||||
int $page = 1,
|
||||
int $perPage = 20,
|
||||
string $search = '',
|
||||
array $genres = [],
|
||||
string $sort = 'title_asc'
|
||||
): array {
|
||||
$offset = ($page - 1) * $perPage;
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
if (!empty($search)) {
|
||||
$where[] = "(title LIKE :search OR overview LIKE :search OR director LIKE :search OR writer LIKE :search)";
|
||||
$params[':search'] = "%$search%";
|
||||
}
|
||||
|
||||
if (!empty($genres)) {
|
||||
$genreConditions = [];
|
||||
foreach ($genres as $i => $genre) {
|
||||
$param = ":genre$i";
|
||||
$genreConditions[] = "genre LIKE $param";
|
||||
$params[$param] = "%$genre%";
|
||||
}
|
||||
$where[] = "(" . implode(' OR ', $genreConditions) . ")";
|
||||
}
|
||||
|
||||
// Determine sort order
|
||||
$orderBy = 'title ASC';
|
||||
switch ($sort) {
|
||||
case 'title_desc':
|
||||
$orderBy = 'title DESC';
|
||||
break;
|
||||
case 'release_asc':
|
||||
$orderBy = 'release_date ASC';
|
||||
break;
|
||||
case 'release_desc':
|
||||
$orderBy = 'release_date DESC';
|
||||
break;
|
||||
case 'rating_desc':
|
||||
$orderBy = 'rating DESC';
|
||||
break;
|
||||
case 'rating_asc':
|
||||
$orderBy = 'rating ASC';
|
||||
break;
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM {$this->table}";
|
||||
if (!empty($where)) {
|
||||
$sql .= " WHERE " . implode(' AND ', $where);
|
||||
}
|
||||
$sql .= " ORDER BY $orderBy LIMIT :limit OFFSET :offset";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
// Bind parameters
|
||||
foreach ($params as $key => $value) {
|
||||
$stmt->bindValue($key, $value);
|
||||
}
|
||||
$stmt->bindValue(':limit', $perPage, \PDO::PARAM_INT);
|
||||
$stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);
|
||||
|
||||
$stmt->execute();
|
||||
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all unique genres from movies
|
||||
*/
|
||||
public function getGenres(\PDO $pdo): array
|
||||
{
|
||||
$stmt = $pdo->query("SELECT DISTINCT genre FROM {$this->table} WHERE genre IS NOT NULL AND genre != ''");
|
||||
$results = $stmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
$genres = [];
|
||||
foreach ($results as $genreList) {
|
||||
$genreArray = array_map('trim', explode(',', $genreList));
|
||||
$genres = array_merge($genres, $genreArray);
|
||||
}
|
||||
|
||||
$genres = array_unique($genres);
|
||||
sort($genres);
|
||||
|
||||
return array_values(array_filter($genres));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all unique directors from movies
|
||||
*/
|
||||
public function getDirectors(\PDO $pdo): array
|
||||
{
|
||||
$stmt = $pdo->query("SELECT DISTINCT director FROM {$this->table} WHERE director IS NOT NULL AND director != '' ORDER BY director");
|
||||
return $stmt->fetchAll(\PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
public function markAsWatched(): bool
|
||||
{
|
||||
$this->watched = true;
|
||||
@@ -108,7 +252,7 @@ class Movie extends Model
|
||||
$stmt->execute(['limit' => $limit]);
|
||||
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/*
|
||||
public static function getTotalCount(\PDO $pdo, string $search = '', array $genres = [], array $directors = []): int
|
||||
{
|
||||
$sql = "SELECT COUNT(*) as count FROM movies m JOIN sources s ON m.source_id = s.id";
|
||||
@@ -146,8 +290,8 @@ class Movie extends Model
|
||||
$stmt->execute();
|
||||
return (int) $stmt->fetch()['count'];
|
||||
}
|
||||
|
||||
public static function getAllWithPagination(\PDO $pdo, int $page, int $perPage, string $search = '', array $genres = [], array $directors = []): array
|
||||
*/
|
||||
public static function getAllWithPagination(\PDO $pdo, int $page, int $perPage, string $search = '', array $genres = [], array $directors = [], string $sort = 'title_asc'): array
|
||||
{
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
@@ -170,20 +314,47 @@ class Movie extends Model
|
||||
$params["genre_{$index}"] = $genre;
|
||||
}
|
||||
$whereClause = !empty($search) ? " AND" : " WHERE";
|
||||
$sql .= $whereClause . " m.genre IN (" . implode(',', $placeholders) . ")";
|
||||
$sql .= $whereClause . " (";
|
||||
foreach ($placeholders as $i => $placeholder) {
|
||||
if ($i > 0) $sql .= " OR ";
|
||||
$sql .= "m.genre LIKE $placeholder";
|
||||
}
|
||||
$sql .= ")";
|
||||
}
|
||||
|
||||
if (!empty($directors)) {
|
||||
$placeholders = [];
|
||||
foreach ($directors as $index => $director) {
|
||||
$placeholders[] = ":director_{$index}";
|
||||
$params["director_{$index}"] = $director;
|
||||
$params["director_{$index}"] = "%$director%";
|
||||
}
|
||||
$whereClause = (!empty($search) || !empty($genres)) ? " AND" : " WHERE";
|
||||
$sql .= $whereClause . " m.director IN (" . implode(',', $placeholders) . ")";
|
||||
$sql .= $whereClause . " (";
|
||||
foreach ($placeholders as $i => $placeholder) {
|
||||
if ($i > 0) $sql .= " OR ";
|
||||
$sql .= "m.director LIKE $placeholder";
|
||||
}
|
||||
$sql .= ")";
|
||||
}
|
||||
|
||||
$sql .= " ORDER BY m.title ASC LIMIT :limit OFFSET :offset";
|
||||
// Add sorting
|
||||
$sortOptions = [
|
||||
'title_asc' => 'm.title ASC',
|
||||
'title_desc' => 'm.title DESC',
|
||||
'year_asc' => 'm.release_date ASC',
|
||||
'year_desc' => 'm.release_date DESC',
|
||||
'rating_asc' => 'm.rating ASC NULLS LAST',
|
||||
'rating_desc' => 'm.rating DESC NULLS LAST',
|
||||
'views_asc' => 'm.watch_count ASC',
|
||||
'views_desc' => 'm.watch_count DESC',
|
||||
'added_asc' => 'm.created_at ASC',
|
||||
'added_desc' => 'm.created_at DESC',
|
||||
'last_watched_asc' => 'm.last_watched_at ASC NULLS LAST',
|
||||
'last_watched_desc' => 'm.last_watched_at DESC NULLS LAST'
|
||||
];
|
||||
|
||||
$sortClause = $sortOptions[$sort] ?? 'm.title ASC';
|
||||
$sql .= " ORDER BY $sortClause LIMIT :limit OFFSET :offset";
|
||||
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->bindValue(':limit', $perPage, \PDO::PARAM_INT);
|
||||
|
||||
Reference in New Issue
Block a user