Files
xamxam/app/public/admin/fragments/peertube-browser.php

113 lines
4.6 KiB
PHP

<?php
/**
* PeerTube video browser fragment — returns a list of orphan videos.
*
* GET /admin/fragments/peertube-browser.php
*
* Lists videos on the configured PeerTube channel that are NOT linked to any
* TFE in the database. Each entry is clickable to relink via the JS handler.
*/
require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../../src/AdminAuth.php';
AdminAuth::requireLogin();
require_once APP_ROOT . '/src/Database.php';
require_once APP_ROOT . '/src/PeerTubeService.php';
$db = new Database();
if (!PeerTubeService::isConfigured($db)) {
echo '<p class="file-browser-empty">PeerTube non configuré.</p>';
exit;
}
$thesisId = isset($_GET['thesis_id']) ? (int)$_GET['thesis_id'] : 0;
// ── Collect already-linked UUIDs for the current thesis (to exclude them) ─
$pdo = $db->getConnection();
$stmt = $pdo->prepare(
"SELECT file_path FROM thesis_files
WHERE thesis_id = ? AND file_path LIKE 'peertube_ids:%'"
);
$stmt->execute([$thesisId]);
$linkedToThis = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$linkedToThis[] = substr($row['file_path'], strlen('peertube_ids:'));
}
// ── Collect all DB-linked UUIDs (any thesis that isn't soft-deleted) ─────
$stmt = $pdo->query(
"SELECT tf.file_path, t.identifier
FROM thesis_files tf
JOIN theses t ON t.id = tf.thesis_id
WHERE tf.file_path LIKE 'peertube_ids:%'
AND t.deleted_at IS NULL"
);
$dbLinked = []; // uuid → identifier
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$uuid = substr($row['file_path'], strlen('peertube_ids:'));
$dbLinked[$uuid] = $row['identifier'];
}
// ── List channel videos ─────────────────────────────────────────────────
try {
$channelVideos = PeerTubeService::listChannelVideos($db);
} catch (\Throwable $e) {
echo '<p class="file-browser-error">Erreur : ' . htmlspecialchars($e->getMessage()) . '</p>';
exit;
}
// ── Build orphan list ────────────────────────────────────────────────────
$orphans = [];
foreach ($channelVideos as $v) {
$uuid = $v['shortUUID'] ?: $v['uuid'];
if ($uuid === '') {
continue;
}
// Skip if already linked to THIS thesis
if (in_array($uuid, $linkedToThis, true)) {
continue;
}
// Mark as linked-to-other if already in DB (different thesis)
$linkedTo = $dbLinked[$uuid] ?? null;
$orphans[] = [
'uuid' => $uuid,
'name' => $v['name'],
'createdAt' => $v['createdAt'],
'linkedTo' => $linkedTo,
];
}
if (empty($orphans)) {
echo '<p class="file-browser-empty">Aucune vidéo orpheline trouvée. Toutes les vidéos de la chaîne sont déjà liées.</p>';
exit;
}
?>
<div id="peertube-browser" class="file-browser">
<p class="file-browser-hint">
<?= count($orphans) ?> vidéo(s) orpheline(s) sur la chaîne.
Cliquez pour relier à ce TFE.
</p>
<ul class="file-browser-list">
<?php foreach ($orphans as $v): ?>
<li class="file-browser-entry file-browser-file"
data-pt-uuid="<?= htmlspecialchars($v['uuid']) ?>"
data-pt-name="<?= htmlspecialchars($v['name']) ?>">
<button type="button" class="file-browser-select-btn"
onclick="XamxamRelinkPeerTube(this)"
<?= $v['linkedTo'] !== null ? 'disabled title="Déjà liée au TFE ' . htmlspecialchars($v['linkedTo']) . '"' : '' ?>
>
<span class="file-browser-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 256 256"><path d="M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm0,192a88,88,0,1,1,88-88A88.1,88.1,0,0,1,128,216Zm40-88a8,8,0,0,1-8,8H107.31l18.35,18.34a8,8,0,0,1-11.32,11.32l-32-32a8,8,0,0,1,0-11.32l32-32a8,8,0,0,1,11.32,11.32L107.31,120H160A8,8,0,0,1,168,128Z"></path></svg>
</span>
<span class="file-browser-name"><?= htmlspecialchars($v['name']) ?></span>
<span class="file-browser-size"><?= !empty($v['createdAt']) ? substr($v['createdAt'], 0, 10) : '' ?></span>
<?php if ($v['linkedTo'] !== null): ?>
<span class="file-browser-badge" style="color:var(--text-tertiary);font-size:0.85em">(<?= htmlspecialchars($v['linkedTo']) ?>)</span>
<?php endif; ?>
</button>
</li>
<?php endforeach; ?>
</ul>
</div>