Files
MediaCollectorLibary/app/Controllers/AdminController.php
Lars Behrends 929ee43001 first commit
2025-10-17 13:29:28 +02:00

130 lines
4.2 KiB
PHP

<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Models\Source;
use App\Models\SyncLog;
use App\Services\SteamSyncService;
use App\Services\JellyfinSyncService;
use App\Services\StashSyncService;
use App\Services\XbvrSyncService;
use App\Services\AdultSyncService;
use App\Services\ExophaseSyncService;
use PDO;
use Slim\Views\Twig;
class AdminController extends Controller
{
private PDO $pdo;
public function __construct(PDO $pdo, Twig $view)
{
parent::__construct($view);
$this->pdo = $pdo;
}
public function index(Request $request, Response $response, $args)
{
$sourceModel = new Source($this->pdo);
$sources = $sourceModel->findAll();
$syncLogModel = new SyncLog($this->pdo);
$recentSyncs = SyncLog::getRecent($this->pdo, 10);
return $this->view->render($response, 'admin/index.twig', [
'title' => 'Admin Dashboard',
'sources' => $sources,
'recent_syncs' => $recentSyncs
]);
}
public function syncSource(Request $request, Response $response, $args)
{
$sourceId = $args['id'];
$syncType = $request->getQueryParams()['type'] ?? 'full';
$sourceModel = new Source($this->pdo);
$source = $sourceModel->find($sourceId);
if (!$source) {
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
}
// Start sync in background (simplified - in production you'd use queues)
$syncLogId = $this->startSync($source, $syncType);
return $this->json($response, [
'success' => true,
'sync_log_id' => $syncLogId,
'message' => 'Sync started successfully'
]);
}
public function syncStatus(Request $request, Response $response, $args)
{
$syncLogId = $args['id'];
$syncLogModel = new SyncLog($this->pdo);
$syncLog = $syncLogModel->find($syncLogId);
if (!$syncLog) {
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
}
return $this->json($response, [
'id' => $syncLog['id'],
'status' => $syncLog['status'],
'sync_type' => $syncLog['sync_type'],
'total_items' => $syncLog['total_items'],
'processed_items' => $syncLog['processed_items'],
'new_items' => $syncLog['new_items'],
'updated_items' => $syncLog['updated_items'],
'deleted_items' => $syncLog['deleted_items'],
'started_at' => $syncLog['started_at'],
'completed_at' => $syncLog['completed_at'],
'message' => $syncLog['message'],
'errors' => $syncLog['errors'] ? json_decode($syncLog['errors'], true) : []
]);
}
public function sources(Request $request, Response $response, $args)
{
$sourceModel = new Source($this->pdo);
$sources = $sourceModel->findAll();
return $this->view->render($response, 'admin/sources.twig', [
'title' => 'Source Management',
'sources' => $sources
]);
}
private function startSync(array $source, string $syncType): int
{
// Create appropriate sync service based on source type
switch ($source['name']) {
case 'steam':
$syncService = new SteamSyncService($this->pdo, $source);
break;
case 'jellyfin':
$syncService = new JellyfinSyncService($this->pdo, $source);
break;
case 'stash':
$syncService = new StashSyncService($this->pdo, $source);
break;
case 'adult':
$syncService = new AdultSyncService($this->pdo, $source);
break;
case 'exophase':
$syncService = new ExophaseSyncService($this->pdo, $source);
break;
default:
throw new \Exception('Unsupported source type: ' . $source['name']);
}
// Start sync (this would typically be queued in production)
return $syncService->startSync($syncType);
}
}