377 lines
18 KiB
PHP
377 lines
18 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
$app = app();
|
|
$pdo = $app->pdo();
|
|
$userId = $_SESSION['user_id'] ?? null;
|
|
$error = '';
|
|
$info = '';
|
|
$search = trim((string)($_GET['q'] ?? ''));
|
|
$boardSlug = trim((string)($_GET['board'] ?? ''));
|
|
|
|
$communityCfg = require __DIR__ . '/../../../config/community.php';
|
|
$community = $pdo ? new \App\Community($pdo, $communityCfg) : null;
|
|
$access = $pdo ? new \App\CommunityAccess($pdo, $communityCfg) : null;
|
|
|
|
$stats = $community ? $community->getStats() : ['threads' => 0, 'posts' => 0, 'members' => 0];
|
|
$forumCategories = $community ? $community->listForumCategories() : [];
|
|
$selectedBoard = ($community && $boardSlug !== '') ? $community->getBoardBySlug($boardSlug) : null;
|
|
$userRoles = $access && $userId ? $access->getUserRoles((int)$userId) : [];
|
|
$isForumAdmin = $access && $userId ? $access->canModerateForum((int)$userId) : false;
|
|
$canManageApplications = $access && $userId ? ($access->canManageApplications((int)$userId) && $access->supportsApplications()) : false;
|
|
$canManageRoles = $access && $userId ? $access->canManageRoles((int)$userId) : false;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $community && $access) {
|
|
$action = (string)($_POST['action'] ?? '');
|
|
try {
|
|
if ($action === 'thread_create') {
|
|
if (!$userId) {
|
|
throw new \RuntimeException('Bitte einloggen, um Themen zu erstellen.');
|
|
}
|
|
if (!$access->canCreateThread((int)$userId)) {
|
|
$state = $access->getRestrictionState((int)$userId);
|
|
throw new \RuntimeException($state['reason'] ?: 'Du darfst aktuell keine Themen erstellen.');
|
|
}
|
|
$community->createThreadInBoard((int)$userId, (string)($_POST['board_slug'] ?? ''), (string)($_POST['title'] ?? ''), (string)($_POST['body'] ?? ''));
|
|
redirect('/community' . ($boardSlug !== '' ? '?board=' . rawurlencode($boardSlug) : ''));
|
|
} elseif ($action === 'application_decide') {
|
|
$access->decideApplication((int)$userId, (int)($_POST['application_id'] ?? 0), (string)($_POST['decision'] ?? ''), (string)($_POST['decision_reason'] ?? ''));
|
|
$info = 'Bewerbung wurde bearbeitet.';
|
|
} elseif ($action === 'report_resolve') {
|
|
$access->resolveReport((int)$userId, (int)($_POST['report_id'] ?? 0), (string)($_POST['moderator_note'] ?? ''));
|
|
$info = 'Meldung wurde abgeschlossen.';
|
|
} elseif ($action === 'role_revoke') {
|
|
$access->revokeRole((int)$userId, (int)($_POST['target_user_id'] ?? 0), (string)($_POST['role'] ?? ''));
|
|
$info = 'Rolle wurde entzogen.';
|
|
} elseif ($action === 'restriction_set') {
|
|
$access->setRestriction((int)$userId, (int)($_POST['target_user_id'] ?? 0), (string)($_POST['restriction_type'] ?? ''), (string)($_POST['reason'] ?? ''));
|
|
$info = 'Community-Sperre wurde gesetzt.';
|
|
} elseif ($action === 'restriction_clear') {
|
|
$access->clearRestriction((int)$userId, (int)($_POST['target_user_id'] ?? 0), (string)($_POST['restriction_type'] ?? ''));
|
|
$info = 'Community-Sperre wurde aufgehoben.';
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$error = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
$threads = $community
|
|
? ($search !== '' ? $community->searchThreads($search, 50, $boardSlug !== '' ? $boardSlug : null) : $community->listThreads(50, $boardSlug !== '' ? $boardSlug : null))
|
|
: [];
|
|
$applications = $canManageApplications && $access ? $access->listApplications('open') : [];
|
|
$reports = $isForumAdmin && $access ? $access->listOpenReports() : [];
|
|
$roleAssignments = $canManageRoles && $access ? $access->listRoleAssignments() : [];
|
|
?>
|
|
<main class="section">
|
|
<div class="container">
|
|
<div class="forum-hero">
|
|
<div>
|
|
<div class="forum-breadcrumbs">
|
|
<a href="/">Home</a>
|
|
<span>/</span>
|
|
<span>Community</span>
|
|
<?php if ($selectedBoard): ?>
|
|
<span>/</span>
|
|
<span><?= htmlspecialchars((string)$selectedBoard['title'], ENT_QUOTES) ?></span>
|
|
<?php endif; ?>
|
|
</div>
|
|
<h1>Community</h1>
|
|
<p class="forum-hero__copy">Fragen stellen, Erfahrungen teilen, Antworten finden und gemeinsam weiterdenken.</p>
|
|
</div>
|
|
<div class="forum-hero__actions forum-hero__actions--stack">
|
|
<form method="get" class="forum-search forum-search--hero">
|
|
<?php if ($boardSlug !== ''): ?>
|
|
<input type="hidden" name="board" value="<?= htmlspecialchars($boardSlug, ENT_QUOTES) ?>">
|
|
<?php endif; ?>
|
|
<label class="label" for="searchQHero">Suche</label>
|
|
<div class="forum-search__row">
|
|
<input id="searchQHero" name="q" class="input" value="<?= htmlspecialchars($search, ENT_QUOTES) ?>" placeholder="Thema oder Stichwort suchen">
|
|
<button class="btn" type="submit">Suchen</button>
|
|
</div>
|
|
</form>
|
|
<div class="forum-hero__cta">
|
|
<?php if ($userId): ?>
|
|
<button class="btn" type="button" data-modal-open="modalThread">Neues Thema</button>
|
|
<?php else: ?>
|
|
<a class="btn" href="/login">Einloggen und schreiben</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="toast-bar" style="margin-top:14px; border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($info): ?>
|
|
<div class="toast-bar" style="margin-top:14px;"><?= htmlspecialchars($info, ENT_QUOTES) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="forum-stats forum-stats--compact">
|
|
<div class="forum-stat">
|
|
<span class="forum-stat__label">Themen</span>
|
|
<strong><?= number_format($stats['threads']) ?></strong>
|
|
</div>
|
|
<div class="forum-stat">
|
|
<span class="forum-stat__label">Beiträge</span>
|
|
<strong><?= number_format($stats['posts']) ?></strong>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="forum-category-list">
|
|
<?php foreach ($forumCategories as $category): ?>
|
|
<section class="forum-category-card">
|
|
<div class="forum-category-card__title"><?= htmlspecialchars((string)$category['title'], ENT_QUOTES) ?></div>
|
|
<div class="forum-board-grid">
|
|
<?php foreach ($category['boards'] as $board): ?>
|
|
<?php $latest = $board['latest_thread'] ?? null; ?>
|
|
<article class="forum-board-row">
|
|
<div class="forum-board-row__main">
|
|
<div class="forum-row__icon" aria-hidden="true"><?= htmlspecialchars((string)($board['icon'] ?? '💬'), ENT_QUOTES) ?></div>
|
|
<div>
|
|
<h3>
|
|
<a href="/community?board=<?= rawurlencode((string)$board['slug']) ?>">
|
|
<?= htmlspecialchars((string)$board['title'], ENT_QUOTES) ?>
|
|
</a>
|
|
</h3>
|
|
<p><?= htmlspecialchars((string)$board['description'], ENT_QUOTES) ?></p>
|
|
<div class="forum-row__meta">
|
|
<span>Themen: <?= (int)($board['thread_count'] ?? 0) ?></span>
|
|
<span>Beiträge: <?= (int)($board['post_count'] ?? 0) ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="forum-board-row__latest">
|
|
<?php if ($latest): ?>
|
|
<strong><a href="/community_thread?id=<?= (int)$latest['id'] ?>"><?= htmlspecialchars((string)$latest['title'], ENT_QUOTES) ?></a></strong>
|
|
<span><?= htmlspecialchars((string)($latest['last_activity_by'] ?: 'Mitglied'), ENT_QUOTES) ?></span>
|
|
<span><?= htmlspecialchars((string)$latest['last_activity_at'], ENT_QUOTES) ?></span>
|
|
<?php else: ?>
|
|
<span class="muted">Noch keine Themen</span>
|
|
<?php endif; ?>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</section>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<div class="forum-board" style="margin-top:24px;">
|
|
<div class="forum-board__head">
|
|
<div>
|
|
<h2><?= htmlspecialchars($selectedBoard['title'] ?? 'Neueste Themen', ENT_QUOTES) ?></h2>
|
|
<p class="muted">
|
|
<?= htmlspecialchars($selectedBoard['description'] ?? 'Hier siehst du die zuletzt erstellten oder zuletzt aktiven Themen aus der Community.', ENT_QUOTES) ?>
|
|
</p>
|
|
</div>
|
|
<?php if ($selectedBoard): ?>
|
|
<a class="btn ghost" href="/community">Alle Bereiche anzeigen</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="forum-list">
|
|
<div class="forum-list__header">
|
|
<span>Thema</span>
|
|
<span>Antworten</span>
|
|
<span>Letzte Aktivität</span>
|
|
</div>
|
|
|
|
<?php foreach ($threads as $t): ?>
|
|
<?php
|
|
$pts = ($community && $pdo) ? $community->computePoints((int)$t['uid']) : 0.0;
|
|
$lvl = $community ? $community->membershipLevel($pts) : ['label'=>'','icon'=>''];
|
|
$preview = mb_substr((string)$t['body'], 0, 220);
|
|
?>
|
|
<article class="forum-row">
|
|
<div class="forum-row__topic">
|
|
<div class="forum-row__icon" aria-hidden="true">💬</div>
|
|
<div>
|
|
<h3><a href="/community_thread?id=<?= (int)$t['id'] ?>"><?= htmlspecialchars((string)$t['title'], ENT_QUOTES) ?></a></h3>
|
|
<div class="forum-row__meta">
|
|
<span><?= htmlspecialchars((string)($t['display_name'] ?: 'Mitglied'), ENT_QUOTES) ?></span>
|
|
<span><?= htmlspecialchars(trim((string)($lvl['icon'] ?? '') . ' ' . (string)($lvl['label'] ?? '')), ENT_QUOTES) ?></span>
|
|
<?php if (!empty($t['board_title'])): ?>
|
|
<span><?= htmlspecialchars((string)$t['board_title'], ENT_QUOTES) ?></span>
|
|
<?php endif; ?>
|
|
</div>
|
|
<p><?= nl2br(htmlspecialchars($preview, ENT_QUOTES)) ?><?= mb_strlen((string)$t['body']) > 220 ? '…' : '' ?></p>
|
|
</div>
|
|
</div>
|
|
<div class="forum-row__count">
|
|
<strong><?= (int)$t['answers'] ?></strong>
|
|
<span>Antworten</span>
|
|
</div>
|
|
<div class="forum-row__activity">
|
|
<strong><?= htmlspecialchars((string)($t['last_activity_by'] ?: 'Mitglied'), ENT_QUOTES) ?></strong>
|
|
<span><?= htmlspecialchars((string)$t['last_activity_at'], ENT_QUOTES) ?></span>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if (!$threads): ?>
|
|
<div class="forum-empty">Keine Themen gefunden.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($canManageApplications || $isForumAdmin || $canManageRoles): ?>
|
|
<div class="forum-admin-grid">
|
|
<?php if ($canManageApplications): ?>
|
|
<section class="forum-board">
|
|
<div class="forum-board__head">
|
|
<div>
|
|
<h2>Offene Bewerbungen</h2>
|
|
<p class="muted">Bewerbungen ab Rang Community-Vater prüfen.</p>
|
|
</div>
|
|
</div>
|
|
<div class="forum-admin-list">
|
|
<?php foreach ($applications as $application): ?>
|
|
<article class="forum-admin-item">
|
|
<div>
|
|
<strong><?= htmlspecialchars((string)($application['display_name'] ?: $application['email']), ENT_QUOTES) ?></strong>
|
|
<p><?= nl2br(htmlspecialchars((string)$application['motivation'], ENT_QUOTES)) ?></p>
|
|
</div>
|
|
<form method="post" class="forum-admin-item__actions">
|
|
<input type="hidden" name="action" value="application_decide">
|
|
<input type="hidden" name="application_id" value="<?= (int)$application['id'] ?>">
|
|
<textarea name="decision_reason" class="textarea" rows="3" placeholder="Begründung (optional bei Annahme, sinnvoll bei Ablehnung)"></textarea>
|
|
<div class="flex gap-12">
|
|
<button class="btn" type="submit" name="decision" value="approved">Annehmen</button>
|
|
<button class="btn ghost" type="submit" name="decision" value="rejected">Ablehnen</button>
|
|
</div>
|
|
</form>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
<?php if (!$applications): ?>
|
|
<div class="forum-empty">Keine offenen Bewerbungen.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($isForumAdmin && $access && $access->supportsReports()): ?>
|
|
<section class="forum-board">
|
|
<div class="forum-board__head">
|
|
<div>
|
|
<h2>Offene Meldungen</h2>
|
|
<p class="muted">Gemeldete Inhalte prüfen und moderieren.</p>
|
|
</div>
|
|
</div>
|
|
<div class="forum-admin-list">
|
|
<?php foreach ($reports as $report): ?>
|
|
<article class="forum-admin-item">
|
|
<div>
|
|
<strong><?= htmlspecialchars((string)$report['target_type'], ENT_QUOTES) ?> #<?= (int)$report['target_id'] ?></strong>
|
|
<p><?= nl2br(htmlspecialchars((string)$report['reason'], ENT_QUOTES)) ?></p>
|
|
<p class="muted small">Gemeldet von <?= htmlspecialchars((string)($report['reporter_name'] ?: 'Mitglied'), ENT_QUOTES) ?> am <?= htmlspecialchars((string)$report['created_at'], ENT_QUOTES) ?></p>
|
|
</div>
|
|
<form method="post" class="forum-admin-item__actions">
|
|
<input type="hidden" name="action" value="report_resolve">
|
|
<input type="hidden" name="report_id" value="<?= (int)$report['id'] ?>">
|
|
<textarea name="moderator_note" class="textarea" rows="3" placeholder="Moderationsnotiz (optional)"></textarea>
|
|
<button class="btn" type="submit">Als erledigt markieren</button>
|
|
</form>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
<?php if (!$reports): ?>
|
|
<div class="forum-empty">Keine offenen Meldungen.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($canManageRoles && $access && $access->hasModerationTables()): ?>
|
|
<section class="forum-board">
|
|
<div class="forum-board__head">
|
|
<div>
|
|
<h2>Rollenübersicht</h2>
|
|
<p class="muted">Vergebene Community-Rollen im Blick behalten.</p>
|
|
</div>
|
|
</div>
|
|
<div class="forum-admin-list">
|
|
<?php foreach ($roleAssignments as $assignment): ?>
|
|
<article class="forum-admin-item">
|
|
<div>
|
|
<strong><?= htmlspecialchars((string)($assignment['display_name'] ?: $assignment['email']), ENT_QUOTES) ?></strong>
|
|
<p class="muted small">Rolle: <?= htmlspecialchars((string)$assignment['role'], ENT_QUOTES) ?> · seit <?= htmlspecialchars((string)$assignment['assigned_at'], ENT_QUOTES) ?></p>
|
|
</div>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="role_revoke">
|
|
<input type="hidden" name="target_user_id" value="<?= (int)$assignment['user_id'] ?>">
|
|
<input type="hidden" name="role" value="<?= htmlspecialchars((string)$assignment['role'], ENT_QUOTES) ?>">
|
|
<button class="btn ghost" type="submit">Rolle entziehen</button>
|
|
</form>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
<?php if (!$roleAssignments): ?>
|
|
<div class="forum-empty">Keine Rollen vergeben.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
|
|
<?php if ($userId): ?>
|
|
<div class="modal" id="modalThread">
|
|
<div class="panel">
|
|
<div class="head flex between center-y">
|
|
<h3 style="margin:0;">Neues Thema</h3>
|
|
<button class="btn ghost" type="button" data-modal-close>✕</button>
|
|
</div>
|
|
<form method="post" class="stack gap-10" style="margin-top:12px;">
|
|
<input type="hidden" name="action" value="thread_create">
|
|
<div class="stack gap-6">
|
|
<label class="label" for="fBoardModal">Unterforum</label>
|
|
<select id="fBoardModal" name="board_slug" class="select">
|
|
<?php foreach ($forumCategories as $category): ?>
|
|
<optgroup label="<?= htmlspecialchars((string)$category['title'], ENT_QUOTES) ?>">
|
|
<?php foreach ($category['boards'] as $board): ?>
|
|
<option value="<?= htmlspecialchars((string)$board['slug'], ENT_QUOTES) ?>" <?= $boardSlug === $board['slug'] ? 'selected' : '' ?>>
|
|
<?= htmlspecialchars((string)$board['title'], ENT_QUOTES) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</optgroup>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="stack gap-6">
|
|
<label class="label" for="fTitleModal">Frage/Titel</label>
|
|
<input id="fTitleModal" name="title" class="input" required>
|
|
</div>
|
|
<div class="stack gap-6">
|
|
<label class="label" for="fBodyModal">Text</label>
|
|
<textarea id="fBodyModal" name="body" class="textarea" rows="5" required></textarea>
|
|
</div>
|
|
<div class="flex gap-12" style="flex-wrap:wrap;">
|
|
<button class="btn ghost" type="button" data-modal-close>Abbrechen</button>
|
|
<button class="btn" type="submit">Thema erstellen</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<script>
|
|
document.querySelectorAll('[data-modal-open]').forEach(btn => {
|
|
btn.addEventListener('click', () => {
|
|
const id = btn.getAttribute('data-modal-open');
|
|
const modal = document.getElementById(id);
|
|
if (modal) modal.classList.add('open');
|
|
});
|
|
});
|
|
document.querySelectorAll('[data-modal-close]').forEach(btn => {
|
|
btn.addEventListener('click', () => {
|
|
const modal = btn.closest('.modal');
|
|
if (modal) modal.classList.remove('open');
|
|
});
|
|
});
|
|
document.querySelectorAll('.modal').forEach(modal => {
|
|
modal.addEventListener('click', (e) => {
|
|
if (e.target === modal) modal.classList.remove('open');
|
|
});
|
|
});
|
|
</script>
|