Files
MediaCollectorLibary/app/Controllers/Api/DashboardController.php
Lars Behrends eb1ec1153d Remove obsolete test scripts and add new API controllers for dashboard and game management
- Deleted test scripts: test_jellyfin_execution.php, test_stash.php, test_xbvr.php, test_xbvr_sync.php, vite.config.js
- Added DashboardController for fetching dashboard statistics and recent activity
- Added GameController for managing games, including fetching all games, game details, and games by category
- Introduced various check scripts to validate database structures and data integrity for adult videos, games, gender data, posters, and TV show actors
2026-01-18 01:42:03 +01:00

207 lines
7.2 KiB
PHP

<?php
namespace App\Controllers\Api;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Controllers\Api\ApiController;
class DashboardController extends ApiController
{
private \PDO $pdo;
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
/**
* Get dashboard statistics
*/
public function getStats(Request $request, Response $response): Response
{
try {
$stats = [];
// Get movies count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM movies");
$stmt->execute();
$moviesCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'Total Movies',
'value' => number_format($moviesCount),
'icon' => 'FilmIcon',
'color' => 'bg-blue-500',
'href' => '/movies'
];
// Get TV shows count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM tv_shows");
$stmt->execute();
$tvShowsCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'TV Shows',
'value' => number_format($tvShowsCount),
'icon' => 'TvIcon',
'color' => 'bg-purple-500',
'href' => '/tvshows'
];
// Get games count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM games");
$stmt->execute();
$gamesCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'Games',
'value' => number_format($gamesCount),
'icon' => 'GamepadIcon',
'color' => 'bg-green-500',
'href' => '/games'
];
// Get music albums count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM music_albums");
$stmt->execute();
$albumsCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'Music Albums',
'value' => number_format($albumsCount),
'icon' => 'MusicalNoteIcon',
'color' => 'bg-pink-500',
'href' => '/music'
];
// Get adult videos count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM adult_videos");
$stmt->execute();
$adultCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'Adult Videos',
'value' => number_format($adultCount),
'icon' => 'LockClosedIcon',
'color' => 'bg-red-500',
'href' => '/adult'
];
// Get actors count
$stmt = $this->pdo->prepare("SELECT COUNT(*) as count FROM actors");
$stmt->execute();
$actorsCount = $stmt->fetch(\PDO::FETCH_ASSOC)['count'];
$stats[] = [
'name' => 'Actors',
'value' => number_format($actorsCount),
'icon' => 'UserIcon',
'color' => 'bg-indigo-500',
'href' => '/actors'
];
return $this->success($response, $stats);
} catch (\Exception $e) {
return $this->error($response, 'Failed to fetch dashboard stats', 500);
}
}
/**
* Get recent activity
*/
public function getRecentActivity(Request $request, Response $response): Response
{
try {
$activities = [];
// Get recent movies (last 5)
$stmt = $this->pdo->prepare("
SELECT 'Added movie' as action, title as item,
DATE_FORMAT(created_at, '%b %d, %Y') as time,
'movie' as type
FROM movies
ORDER BY created_at DESC
LIMIT 5
");
$stmt->execute();
$recentMovies = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$activities = array_merge($activities, $recentMovies);
// Get recent TV shows (last 5)
$stmt = $this->pdo->prepare("
SELECT 'Added TV show' as action, title as item,
DATE_FORMAT(created_at, '%b %d, %Y') as time,
'tvshow' as type
FROM tv_shows
ORDER BY created_at DESC
LIMIT 5
");
$stmt->execute();
$recentTvShows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$activities = array_merge($activities, $recentTvShows);
// Get recent games (last 5)
$stmt = $this->pdo->prepare("
SELECT 'Added game' as action, title as item,
DATE_FORMAT(created_at, '%b %d, %Y') as time,
'game' as type
FROM games
ORDER BY created_at DESC
LIMIT 5
");
$stmt->execute();
$recentGames = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$activities = array_merge($activities, $recentGames);
// Get recent music albums (last 5)
$stmt = $this->pdo->prepare("
SELECT 'Added album' as action, title as item,
DATE_FORMAT(created_at, '%b %d, %Y') as time,
'music' as type
FROM music_albums
ORDER BY created_at DESC
LIMIT 5
");
$stmt->execute();
$recentAlbums = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$activities = array_merge($activities, $recentAlbums);
// Sort all activities by time (most recent first)
usort($activities, function($a, $b) {
return strtotime($b['time']) - strtotime($a['time']);
});
// Take only the 10 most recent activities
$activities = array_slice($activities, 0, 10);
// Format time to be more relative
foreach ($activities as &$activity) {
$activity['id'] = uniqid();
$activity['time'] = $this->formatRelativeTime($activity['time']);
}
return $this->success($response, $activities);
} catch (\Exception $e) {
return $this->error($response, 'Failed to fetch recent activity', 500);
}
}
/**
* Format time to relative format (simplified version)
*/
private function formatRelativeTime($dateString): string
{
$date = strtotime($dateString);
$now = time();
$diff = $now - $date;
if ($diff < 3600) {
$minutes = floor($diff / 60);
return $minutes <= 1 ? 'Just now' : "$minutes minutes ago";
} elseif ($diff < 86400) {
$hours = floor($diff / 3600);
return $hours <= 1 ? '1 hour ago' : "$hours hours ago";
} elseif ($diff < 604800) {
$days = floor($diff / 86400);
return $days <= 1 ? '1 day ago' : "$days days ago";
} else {
return date('M j, Y', $date);
}
}
}