mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
113 lines
4.3 KiB
PHP
113 lines
4.3 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">
|
|
<img src="/assets/icons/arrow-circle-left.svg" width="24" height="24" alt="" aria-hidden="true">
|
|
</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>
|