Files
papa-kind-treff.info/partials/landing/community/index.php
Lars Gebhardt-Kusche 57e197755e
All checks were successful
Deploy / deploy (push) Successful in 47s
adfs
2026-07-18 01:04:18 +02:00

177 lines
6.7 KiB
PHP
Executable File

<?php
declare(strict_types=1);
$app = app();
$pdo = $app->pdo();
$userId = $_SESSION['user_id'] ?? null;
$error = '';
$search = trim((string)($_GET['q'] ?? ''));
$communityCfg = require __DIR__ . '/../../../config/community.php';
$community = $pdo ? new \App\Community($pdo, $communityCfg) : null;
$stats = $community ? $community->getStats() : ['threads' => 0, 'posts' => 0, 'members' => 0];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'thread_create') {
if (!$userId) {
$error = 'Bitte einloggen, um Fragen zu stellen.';
} elseif ($community) {
$title = trim((string)($_POST['title'] ?? ''));
$body = trim((string)($_POST['body'] ?? ''));
if ($title === '' || $body === '') {
$error = 'Titel und Text sind erforderlich.';
} else {
$community->createThread((int)$userId, $title, $body);
redirect('/community');
}
}
}
$threads = $community ? ($search !== '' ? $community->searchThreads($search, 50) : $community->listThreads(50)) : [];
?>
<main class="section">
<div class="container">
<div class="forum-hero">
<div>
<div class="forum-breadcrumbs">
<a href="/">Home</a>
<span>/</span>
<span>Community</span>
</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">
<?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>
<?php if ($error): ?>
<div class="toast-bar" style="border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
<?php endif; ?>
<div class="forum-stats">
<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 class="forum-stat">
<span class="forum-stat__label">Mitglieder</span>
<strong><?= number_format($stats['members']) ?></strong>
</div>
</div>
<div class="forum-board">
<div class="forum-board__head">
<div>
<h2>Forum</h2>
<p class="muted">Hier findest du die neuesten Themen, Antworten und laufenden Gespräche.</p>
</div>
<form method="get" class="forum-search">
<label class="label" for="searchQ">Suche</label>
<div class="forum-search__row">
<input id="searchQ" name="q" class="input" value="<?= htmlspecialchars($search, ENT_QUOTES) ?>" placeholder="Schlagwort eingeben">
<button class="btn" type="submit">Suchen</button>
</div>
</form>
</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($t['title'], ENT_QUOTES) ?></a></h3>
<div class="forum-row__meta">
<span><?= htmlspecialchars($t['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($lvl['icon'] ?? '', ENT_QUOTES) ?> <?= htmlspecialchars($lvl['label'] ?? '', ENT_QUOTES) ?></span>
<span><?= number_format($pts, 1) ?> Punkte</span>
</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($t['last_activity_by'] ?: 'Mitglied', ENT_QUOTES) ?></strong>
<span><?= htmlspecialchars($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>
</div>
</main>
<?php if ($userId): ?>
<div class="modal" id="modalThread">
<div class="panel">
<div class="head flex between center-y">
<h3 style="margin:0;">Neue Frage</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="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="4" 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">Frage 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>