This commit is contained in:
2025-12-29 02:10:04 +01:00
parent b99239ca66
commit 33482a0291
4 changed files with 228 additions and 2 deletions

129
public/page/community.php Normal file
View File

@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
$app = app();
$pdo = $app->pdo();
$userId = $_SESSION['user_id'] ?? null;
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'thread_create') {
if (!$userId) {
$error = 'Bitte einloggen, um Fragen zu stellen.';
} elseif ($pdo) {
$title = trim((string)($_POST['title'] ?? ''));
$body = trim((string)($_POST['body'] ?? ''));
if ($title === '' || $body === '') {
$error = 'Titel und Text sind erforderlich.';
} else {
$pdo->prepare('CREATE TABLE IF NOT EXISTS forum_threads (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
title VARCHAR(200) NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci')->execute();
$pdo->prepare('INSERT INTO forum_threads (user_id, title, body) VALUES (:uid, :title, :body)')
->execute(['uid' => $userId, 'title' => $title, 'body' => $body]);
header('Location: /community');
exit;
}
}
}
$threads = [];
if ($pdo) {
$pdo->prepare('CREATE TABLE IF NOT EXISTS forum_posts (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
thread_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_thread (thread_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci')->execute();
$sql = 'SELECT ft.id, ft.title, ft.body, ft.created_at,
u.id as uid, u.created_at as user_created,
p.display_name,
(SELECT COUNT(*) FROM forum_posts fp WHERE fp.thread_id = ft.id) AS answers,
(SELECT COUNT(*) FROM forum_posts fp2 WHERE fp2.user_id = u.id) +
(SELECT COUNT(*) FROM forum_threads ft2 WHERE ft2.user_id = u.id) AS user_posts
FROM forum_threads ft
JOIN users u ON u.id = ft.user_id
LEFT JOIN user_profiles p ON p.user_id = u.id
ORDER BY ft.created_at DESC
LIMIT 50';
$threads = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC) ?: [];
}
function compute_points(array $row, \PDO $pdo): float {
$uid = (int)$row['uid'];
$threads = (int)($row['user_posts'] ?? 0);
$answers = (int)($row['answers'] ?? 0);
$eventCreated = (int)$pdo->query("SELECT COUNT(*) FROM events WHERE created_by = {$uid}")->fetchColumn();
$eventParticipants = (float)$pdo->query("SELECT COUNT(*) FROM event_participants WHERE user_id = {$uid}")->fetchColumn();
$invites = 0;
return $threads * 0.5 + $answers * 1 + $eventCreated * 1 + $eventParticipants * 0.1 + $invites * 0.5;
}
function membership_level(float $points): string {
if ($points >= 1000) return 'Daddy of Daddies';
if ($points >= 300) return 'Master Daddy';
if ($points >= 150) return 'Good Daddy';
if ($points >= 50) return 'Mini Daddy';
if ($points >= 5) return 'Baby Daddy';
return 'New Daddy';
}
?>
<main class="section">
<div class="container">
<p class="eyebrow">Community</p>
<h1>Forum</h1>
<?php if ($error): ?>
<div class="toast-bar" style="border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
<?php endif; ?>
<?php if ($userId): ?>
<form method="post" class="stack gap-12 card" style="margin-top:14px; padding:16px;">
<input type="hidden" name="action" value="thread_create">
<div class="stack gap-6">
<label class="label" for="fTitle">Frage/Titel</label>
<input id="fTitle" name="title" class="input" required>
</div>
<div class="stack gap-6">
<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>
<?php else: ?>
<p class="muted">Bitte <a href="/login">einloggen</a>, um Fragen zu stellen oder zu antworten.</p>
<?php endif; ?>
<div class="stack gap-12" style="margin-top:20px;">
<?php foreach ($threads as $t): ?>
<?php
$pts = $pdo ? compute_points($t, $pdo) : 0.0;
$lvl = membership_level($pts);
?>
<article class="card">
<div class="event__body">
<div class="event__meta">
<span><?= htmlspecialchars($t['created_at'], ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($t['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($lvl, ENT_QUOTES) ?> (<?= number_format($pts,1) ?> Punkte)</span>
<span>Beiträge: <?= (int)$t['user_posts'] + (int)$t['answers'] ?></span>
</div>
<h3><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">Antworten: <?= (int)$t['answers'] ?></p>
</div>
</article>
<?php endforeach; ?>
<?php if (!$threads): ?>
<p class="muted">Noch keine Fragen gestellt.</p>
<?php endif; ?>
</div>
</div>
</main>

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
$app = app();
$pdo = $app->pdo();
$userId = $_SESSION['user_id'] ?? null;
$id = (int)($_GET['id'] ?? 0);
$error = '';
if (!$id) {
http_response_code(404);
exit('<p>Thread nicht gefunden.</p>');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reply') {
if (!$userId) {
$error = 'Bitte einloggen, um zu antworten.';
} elseif ($pdo) {
$body = trim((string)($_POST['body'] ?? ''));
if ($body === '') {
$error = 'Antwort darf nicht leer sein.';
} else {
$pdo->prepare('INSERT INTO forum_posts (thread_id, user_id, body) VALUES (:tid, :uid, :body)')
->execute(['tid' => $id, 'uid' => $userId, 'body' => $body]);
header('Location: /community_thread?id=' . $id);
exit;
}
}
}
$thread = null;
$posts = [];
if ($pdo) {
$stmt = $pdo->prepare('SELECT ft.*, p.display_name FROM forum_threads ft LEFT JOIN user_profiles p ON p.user_id = ft.user_id WHERE ft.id = :id');
$stmt->execute(['id' => $id]);
$thread = $stmt->fetch(PDO::FETCH_ASSOC);
if ($thread) {
$stmt = $pdo->prepare('SELECT fp.*, p.display_name FROM forum_posts fp LEFT JOIN user_profiles p ON p.user_id = fp.user_id WHERE fp.thread_id = :id ORDER BY fp.created_at ASC');
$stmt->execute(['id' => $id]);
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
}
}
if (!$thread) {
http_response_code(404);
exit('<p>Thread nicht gefunden.</p>');
}
?>
<main class="section">
<div class="container">
<p class="eyebrow">Community</p>
<h1><?= htmlspecialchars($thread['title'], ENT_QUOTES) ?></h1>
<p class="muted">Von <?= htmlspecialchars($thread['display_name'] ?: 'Mitglied', ENT_QUOTES) ?> · <?= htmlspecialchars($thread['created_at'], ENT_QUOTES) ?></p>
<article class="card" style="margin-top:12px;">
<div class="event__body">
<p><?= nl2br(htmlspecialchars($thread['body'], ENT_QUOTES)) ?></p>
</div>
</article>
<h3 style="margin-top:16px;">Antworten (<?= count($posts) ?>)</h3>
<div class="stack gap-12" style="margin-top:10px;">
<?php foreach ($posts as $p): ?>
<article class="card">
<div class="event__body">
<div class="event__meta">
<span><?= htmlspecialchars($p['created_at'], ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($p['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
</div>
<p><?= nl2br(htmlspecialchars($p['body'], ENT_QUOTES)) ?></p>
</div>
</article>
<?php endforeach; ?>
<?php if (!$posts): ?>
<p class="muted">Noch keine Antworten.</p>
<?php endif; ?>
</div>
<?php if ($error): ?>
<div class="toast-bar" style="margin-top:12px; border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
<?php endif; ?>
<?php if ($userId): ?>
<form method="post" class="stack gap-12 card" style="margin-top:14px; padding:16px;">
<input type="hidden" name="action" value="reply">
<div class="stack gap-6">
<label class="label" for="replyBody">Antwort</label>
<textarea id="replyBody" name="body" class="textarea" rows="4" required></textarea>
</div>
<button class="btn" type="submit">Antwort senden</button>
</form>
<?php else: ?>
<p class="muted" style="margin-top:12px;">Bitte <a href=\"/login\">einloggen</a>, um zu antworten.</p>
<?php endif; ?>
</div>
</main>

View File

@@ -54,12 +54,12 @@ if ($q !== '' && $pdo) {
<span><?= htmlspecialchars($ev['starts_at'], ENT_QUOTES) ?></span>
<span>📍 <?= htmlspecialchars($ev['region'] ?: $ev['city'], ENT_QUOTES) ?></span>
<span><?= $ev['visibility'] === 'public' ? 'Öffentlich' : 'Mitglieder' ?></span>
<span class="badge"><?= ((int)$ev['allow_kids'] === 1) ? 'Mit Kindern' : 'Ohne Kinder' ?></span>
</div>
<h3><?= htmlspecialchars($ev['title'], ENT_QUOTES) ?></h3>
<p class="muted"><?= htmlspecialchars($ev['teaser_public'], ENT_QUOTES) ?></p>
<p><strong>Kinder:</strong> <?= ((int)$ev['allow_kids'] === 1) ? 'Mit Kindern' : 'Ohne Kinder' ?></p>
<?php if (!empty($ev['location_label'])): ?>
<p><strong>Ort:</strong> <?= htmlspecialchars($ev['location_label'], ENT_QUOTES) ?></p>
<p class="muted small"><strong>Ort:</strong> <?= htmlspecialchars($ev['location_label'], ENT_QUOTES) ?></p>
<?php endif; ?>
<details>
<summary style="cursor:pointer;">Details anzeigen</summary>