This commit is contained in:
2025-12-30 00:22:18 +01:00
parent 3ec7c9182b
commit 6e76029223

View File

@@ -35,6 +35,7 @@ $threads = [];
$configCommunity = require __DIR__ . '/../../config/community.php'; $configCommunity = require __DIR__ . '/../../config/community.php';
$pointsCfg = $configCommunity['points'] ?? []; $pointsCfg = $configCommunity['points'] ?? [];
$levelsCfg = $configCommunity['levels'] ?? []; $levelsCfg = $configCommunity['levels'] ?? [];
$search = trim((string)($_GET['q'] ?? ''));
if ($pdo) { if ($pdo) {
$pdo->prepare('CREATE TABLE IF NOT EXISTS forum_posts ( $pdo->prepare('CREATE TABLE IF NOT EXISTS forum_posts (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
@@ -46,7 +47,22 @@ if ($pdo) {
INDEX idx_thread (thread_id) INDEX idx_thread (thread_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci')->execute(); ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci')->execute();
$sql = 'SELECT ft.id, ft.title, ft.body, ft.created_at, $conditions = [];
$params = [];
if ($search !== '') {
$tokens = preg_split('/\\s+/', $search);
$i = 0;
foreach ($tokens as $tok) {
$tok = trim($tok);
if ($tok === '') continue;
$ph = ':t' . $i++;
$conditions[] = "(ft.title LIKE $ph OR ft.body LIKE $ph)";
$params[$ph] = '%' . $tok . '%';
}
}
$whereSearch = $conditions ? ('AND ' . implode(' AND ', $conditions)) : '';
$sql = "SELECT ft.id, ft.title, ft.body, ft.created_at,
u.id as uid, u.created_at as user_created, u.id as uid, u.created_at as user_created,
p.display_name, p.display_name,
(SELECT COUNT(*) FROM forum_posts fp WHERE fp.thread_id = ft.id) AS answers, (SELECT COUNT(*) FROM forum_posts fp WHERE fp.thread_id = ft.id) AS answers,
@@ -55,9 +71,12 @@ if ($pdo) {
FROM forum_threads ft FROM forum_threads ft
JOIN users u ON u.id = ft.user_id JOIN users u ON u.id = ft.user_id
LEFT JOIN user_profiles p ON p.user_id = u.id LEFT JOIN user_profiles p ON p.user_id = u.id
WHERE 1=1 $whereSearch
ORDER BY ft.created_at DESC ORDER BY ft.created_at DESC
LIMIT 50'; LIMIT 50";
$threads = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) ?: []; $stmt = $pdo->prepare($sql);
$stmt->execute($params);
$threads = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
} }
function compute_points(array $row, \PDO $pdo, array $pointsCfg): float { function compute_points(array $row, \PDO $pdo, array $pointsCfg): float {
@@ -96,40 +115,38 @@ function membership_level(float $points, array $levels): array {
<div class="toast-bar" style="border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div> <div class="toast-bar" style="border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
<?php endif; ?> <?php endif; ?>
<?php if ($userId): ?> <div class="flex between center-y" style="margin:14px 0; gap:12px; flex-wrap:wrap;">
<form method="post" class="stack gap-12 card" style="margin-top:14px; padding:16px;"> <form method="get" class="flex gap-8" style="flex-wrap:wrap; align-items:flex-end;">
<input type="hidden" name="action" value="thread_create"> <div class="stack gap-4">
<div class="stack gap-6"> <label class="label" for="searchQ">Suche nach Stichwort</label>
<label class="label" for="fTitle">Frage/Titel</label> <input id="searchQ" name="q" class="input" value="<?= htmlspecialchars($search, ENT_QUOTES) ?>" placeholder="Schlagwort eingeben">
<input id="fTitle" name="title" class="input" required>
</div> </div>
<div class="stack gap-6"> <button class="btn" type="submit">Suchen</button>
<label class="label" for="fBody">Text</label>
<textarea id="fBody" name="body" class="textarea" rows="4" required></textarea>
</div>
<button class="btn" type="submit">Frage erstellen</button>
</form> </form>
<?php if ($userId): ?>
<button class="btn" type="button" data-modal-open="modalThread">Neue Frage</button>
<?php else: ?> <?php else: ?>
<p class="muted">Bitte <a href="/login">einloggen</a>, um Fragen zu stellen oder zu antworten.</p> <a class="btn ghost" href="/login">Login für neue Frage</a>
<?php endif; ?> <?php endif; ?>
</div>
<div class="stack gap-12" style="margin-top:20px;"> <div class="stack gap-8" style="margin-top:10px;">
<?php foreach ($threads as $t): ?> <?php foreach ($threads as $t): ?>
<?php <?php
$pts = $pdo ? compute_points($t, $pdo, $pointsCfg) : 0.0; $pts = $pdo ? compute_points($t, $pdo, $pointsCfg) : 0.0;
$lvl = membership_level($pts, $levelsCfg); $lvl = membership_level($pts, $levelsCfg);
?> ?>
<article class="card"> <article class="card" style="padding:14px;">
<div class="event__body"> <div class="event__body">
<div class="event__meta"> <div class="event__meta" style="flex-wrap:wrap; gap:8px;">
<span><?= htmlspecialchars($t['created_at'], ENT_QUOTES) ?></span> <span><?= htmlspecialchars($t['created_at'], ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($t['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span> <span><?= htmlspecialchars($t['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($lvl['icon'] ?? '', ENT_QUOTES) ?> <?= htmlspecialchars($lvl['label'] ?? '', ENT_QUOTES) ?> (<?= number_format($pts,1) ?> Punkte)</span> <span><?= htmlspecialchars($lvl['icon'] ?? '', ENT_QUOTES) ?> <?= htmlspecialchars($lvl['label'] ?? '', ENT_QUOTES) ?> (<?= number_format($pts,1) ?> Punkte)</span>
<span>Beiträge: <?= (int)$t['user_posts'] + (int)$t['answers'] ?></span> <span>Beiträge: <?= (int)$t['user_posts'] + (int)$t['answers'] ?></span>
<span>Antworten: <?= (int)$t['answers'] ?></span>
</div> </div>
<h3><a href="/community_thread?id=<?= (int)$t['id'] ?>"><?= htmlspecialchars($t['title'], ENT_QUOTES) ?></a></h3> <h3 style="margin:6px 0;"><a href="/community_thread?id=<?= (int)$t['id'] ?>"><?= htmlspecialchars($t['title'], ENT_QUOTES) ?></a></h3>
<p class="muted"><?= nl2br(htmlspecialchars(substr($t['body'], 0, 240), ENT_QUOTES)) ?><?= strlen($t['body']) > 240 ? '…' : '' ?></p> <p class="muted small"><?= nl2br(htmlspecialchars(substr($t['body'], 0, 200), ENT_QUOTES)) ?><?= strlen($t['body']) > 200 ? '…' : '' ?></p>
<p class="muted small">Antworten: <?= (int)$t['answers'] ?></p>
</div> </div>
</article> </article>
<?php endforeach; ?> <?php endforeach; ?>
@@ -139,3 +156,50 @@ function membership_level(float $points, array $levels): array {
</div> </div>
</div> </div>
</main> </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>