asdasd
This commit is contained in:
125
partials/landing/community/index.php
Normal file
125
partials/landing/community/index.php
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
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">
|
||||||
|
<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; ?>
|
||||||
|
|
||||||
|
<div class="flex between center-y" style="margin:14px 0; gap:12px; flex-wrap:wrap;">
|
||||||
|
<form method="get" class="flex gap-8" style="flex-wrap:wrap; align-items:flex-end;">
|
||||||
|
<div class="stack gap-4">
|
||||||
|
<label class="label" for="searchQ">Suche nach Stichwort</label>
|
||||||
|
<input id="searchQ" name="q" class="input" value="<?= htmlspecialchars($search, ENT_QUOTES) ?>" placeholder="Schlagwort eingeben">
|
||||||
|
</div>
|
||||||
|
<button class="btn" type="submit">Suchen</button>
|
||||||
|
</form>
|
||||||
|
<?php if ($userId): ?>
|
||||||
|
<button class="btn" type="button" data-modal-open="modalThread">Neue Frage</button>
|
||||||
|
<?php else: ?>
|
||||||
|
<a class="btn ghost" href="/login">Login für neue Frage</a>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="stack gap-8" style="margin-top:10px;">
|
||||||
|
<?php foreach ($threads as $t): ?>
|
||||||
|
<?php
|
||||||
|
$pts = ($community && $pdo) ? $community->computePoints((int)$t['uid']) : 0.0;
|
||||||
|
$lvl = $community ? $community->membershipLevel($pts) : ['label'=>'','icon'=>''];
|
||||||
|
?>
|
||||||
|
<article class="card" style="padding:14px;">
|
||||||
|
<div class="event__body">
|
||||||
|
<div class="event__meta" style="flex-wrap:wrap; gap:8px;">
|
||||||
|
<span><?= htmlspecialchars($t['created_at'], 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>Beiträge: <?= (int)$t['user_posts'] + (int)$t['answers'] ?></span>
|
||||||
|
<span>Antworten: <?= (int)$t['answers'] ?></span>
|
||||||
|
</div>
|
||||||
|
<h3 style="margin:6px 0;"><a href="/community_thread?id=<?= (int)$t['id'] ?>"><?= htmlspecialchars($t['title'], ENT_QUOTES) ?></a></h3>
|
||||||
|
<p class="muted small"><?= nl2br(htmlspecialchars(substr($t['body'], 0, 200), ENT_QUOTES)) ?><?= strlen($t['body']) > 200 ? '…' : '' ?></p>
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php if (!$threads): ?>
|
||||||
|
<p class="muted">Keine Treffer.</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</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>
|
||||||
82
partials/landing/community/thread.php
Normal file
82
partials/landing/community/thread.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
$app = app();
|
||||||
|
$pdo = $app->pdo();
|
||||||
|
$userId = $_SESSION['user_id'] ?? null;
|
||||||
|
$error = '';
|
||||||
|
$threadId = (int)($_GET['id'] ?? 0);
|
||||||
|
|
||||||
|
$communityCfg = require __DIR__ . '/../../../config/community.php';
|
||||||
|
$community = $pdo ? new \App\Community($pdo, $communityCfg) : null;
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reply') {
|
||||||
|
if (!$userId) {
|
||||||
|
$error = 'Bitte einloggen, um zu antworten.';
|
||||||
|
} elseif ($community) {
|
||||||
|
$body = trim((string)($_POST['body'] ?? ''));
|
||||||
|
if ($body === '') {
|
||||||
|
$error = 'Antwort darf nicht leer sein.';
|
||||||
|
} else {
|
||||||
|
$community->createPost((int)$userId, $threadId, $body);
|
||||||
|
redirect('/community_thread?id=' . $threadId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$thread = $community ? $community->getThread($threadId) : null;
|
||||||
|
$posts = $community ? $community->listPosts($threadId) : [];
|
||||||
|
|
||||||
|
if (!$thread) {
|
||||||
|
http_response_code(404);
|
||||||
|
echo "<p>Thread nicht gefunden.</p>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<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>
|
||||||
@@ -1,208 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
$app = app();
|
tpl('community/index', 'landing');
|
||||||
$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 = [];
|
|
||||||
$configCommunity = require __DIR__ . '/../../config/community.php';
|
|
||||||
$pointsCfg = $configCommunity['points'] ?? [];
|
|
||||||
$levelsCfg = $configCommunity['levels'] ?? [];
|
|
||||||
$search = trim((string)($_GET['q'] ?? ''));
|
|
||||||
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();
|
|
||||||
|
|
||||||
$conditions = [];
|
|
||||||
$params = [];
|
|
||||||
if ($search !== '') {
|
|
||||||
$tokens = preg_split('/\\s+/', $search);
|
|
||||||
$i = 0;
|
|
||||||
foreach ($tokens as $tok) {
|
|
||||||
$tok = trim($tok);
|
|
||||||
if ($tok === '') continue;
|
|
||||||
$ph1 = 't' . $i . '_title';
|
|
||||||
$ph2 = 't' . $i . '_body';
|
|
||||||
$i++;
|
|
||||||
$conditions[] = "(ft.title LIKE :$ph1 OR ft.body LIKE :$ph2)";
|
|
||||||
$params[$ph1] = '%' . $tok . '%';
|
|
||||||
$params[$ph2] = '%' . $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,
|
|
||||||
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
|
|
||||||
WHERE 1=1 $whereSearch
|
|
||||||
ORDER BY ft.created_at DESC
|
|
||||||
LIMIT 50";
|
|
||||||
$stmt = $pdo->prepare($sql);
|
|
||||||
$stmt->execute($params);
|
|
||||||
$threads = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
|
|
||||||
}
|
|
||||||
|
|
||||||
function compute_points(array $row, \PDO $pdo, array $pointsCfg): 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 * ($pointsCfg['forum_question'] ?? 0.5)
|
|
||||||
+ $answers * ($pointsCfg['forum_answer'] ?? 1.0)
|
|
||||||
+ $eventCreated * ($pointsCfg['event_create'] ?? 1.0)
|
|
||||||
+ $eventParticipants * ($pointsCfg['event_participation'] ?? 0.1)
|
|
||||||
+ $invites * ($pointsCfg['invite'] ?? 0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
function membership_level(float $points, array $levels): array {
|
|
||||||
usort($levels, fn($a,$b) => ($b['min'] ?? 0) <=> ($a['min'] ?? 0));
|
|
||||||
foreach ($levels as $lvl) {
|
|
||||||
if ($points >= (float)($lvl['min'] ?? 0)) {
|
|
||||||
return [
|
|
||||||
'label' => $lvl['label'] ?? 'New Daddy',
|
|
||||||
'icon' => $lvl['icon'] ?? '',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$fallback = $levels ? $levels[count($levels)-1] : ['label' => 'New Daddy','icon' => ''];
|
|
||||||
return ['label' => $fallback['label'], 'icon' => $fallback['icon'] ?? ''];
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
<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; ?>
|
|
||||||
|
|
||||||
<div class="flex between center-y" style="margin:14px 0; gap:12px; flex-wrap:wrap;">
|
|
||||||
<form method="get" class="flex gap-8" style="flex-wrap:wrap; align-items:flex-end;">
|
|
||||||
<div class="stack gap-4">
|
|
||||||
<label class="label" for="searchQ">Suche nach Stichwort</label>
|
|
||||||
<input id="searchQ" name="q" class="input" value="<?= htmlspecialchars($search, ENT_QUOTES) ?>" placeholder="Schlagwort eingeben">
|
|
||||||
</div>
|
|
||||||
<button class="btn" type="submit">Suchen</button>
|
|
||||||
</form>
|
|
||||||
<?php if ($userId): ?>
|
|
||||||
<button class="btn" type="button" data-modal-open="modalThread">Neue Frage</button>
|
|
||||||
<?php else: ?>
|
|
||||||
<a class="btn ghost" href="/login">Login für neue Frage</a>
|
|
||||||
<?php endif; ?>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="stack gap-8" style="margin-top:10px;">
|
|
||||||
<?php foreach ($threads as $t): ?>
|
|
||||||
<?php
|
|
||||||
$pts = $pdo ? compute_points($t, $pdo, $pointsCfg) : 0.0;
|
|
||||||
$lvl = membership_level($pts, $levelsCfg);
|
|
||||||
?>
|
|
||||||
<article class="card" style="padding:14px;">
|
|
||||||
<div class="event__body">
|
|
||||||
<div class="event__meta" style="flex-wrap:wrap; gap:8px;">
|
|
||||||
<span><?= htmlspecialchars($t['created_at'], 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>Beiträge: <?= (int)$t['user_posts'] + (int)$t['answers'] ?></span>
|
|
||||||
<span>Antworten: <?= (int)$t['answers'] ?></span>
|
|
||||||
</div>
|
|
||||||
<h3 style="margin:6px 0;"><a href="/community_thread?id=<?= (int)$t['id'] ?>"><?= htmlspecialchars($t['title'], ENT_QUOTES) ?></a></h3>
|
|
||||||
<p class="muted small"><?= nl2br(htmlspecialchars(substr($t['body'], 0, 200), ENT_QUOTES)) ?><?= strlen($t['body']) > 200 ? '…' : '' ?></p>
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
<?php if (!$threads): ?>
|
|
||||||
<p class="muted">Noch keine Fragen gestellt.</p>
|
|
||||||
<?php endif; ?>
|
|
||||||
</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>
|
|
||||||
|
|||||||
@@ -1,95 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
$app = app();
|
tpl('community/thread', 'landing');
|
||||||
$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>
|
|
||||||
|
|||||||
120
src/App/Community.php
Normal file
120
src/App/Community.php
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
final class Community
|
||||||
|
{
|
||||||
|
public function __construct(private \PDO $pdo, private array $config)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createThread(int $userId, string $title, string $body): void
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare('INSERT INTO forum_threads (user_id, title, body) VALUES (:uid, :title, :body)');
|
||||||
|
$stmt->execute([
|
||||||
|
':uid' => $userId,
|
||||||
|
':title' => trim($title),
|
||||||
|
':body' => trim($body),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createPost(int $userId, int $threadId, string $body): void
|
||||||
|
{
|
||||||
|
$stmt = $this->pdo->prepare('INSERT INTO forum_posts (thread_id, user_id, body) VALUES (:tid, :uid, :body)');
|
||||||
|
$stmt->execute([
|
||||||
|
':tid' => $threadId,
|
||||||
|
':uid' => $userId,
|
||||||
|
':body' => trim($body),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function searchThreads(string $query, int $limit = 50): array
|
||||||
|
{
|
||||||
|
$conditions = [];
|
||||||
|
$params = [];
|
||||||
|
$tokens = array_filter(preg_split('/\s+/', trim($query)) ?: [], fn($t) => $t !== '');
|
||||||
|
$i = 0;
|
||||||
|
foreach ($tokens as $tok) {
|
||||||
|
$ph1 = ':t' . $i . 'a';
|
||||||
|
$ph2 = ':t' . $i . 'b';
|
||||||
|
$conditions[] = "(ft.title LIKE $ph1 OR ft.body LIKE $ph2)";
|
||||||
|
$params[$ph1] = '%' . $tok . '%';
|
||||||
|
$params[$ph2] = '%' . $tok . '%';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
$where = $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,
|
||||||
|
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
|
||||||
|
WHERE 1=1 $where
|
||||||
|
ORDER BY ft.created_at DESC
|
||||||
|
LIMIT :lim";
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
foreach ($params as $k => $v) {
|
||||||
|
$stmt->bindValue($k, $v, \PDO::PARAM_STR);
|
||||||
|
}
|
||||||
|
$stmt->bindValue(':lim', $limit, \PDO::PARAM_INT);
|
||||||
|
$stmt->execute();
|
||||||
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listThreads(int $limit = 50): array
|
||||||
|
{
|
||||||
|
return $this->searchThreads('', $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getThread(int $id): ?array
|
||||||
|
{
|
||||||
|
$stmt = $this->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]);
|
||||||
|
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
return $row ?: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listPosts(int $threadId): array
|
||||||
|
{
|
||||||
|
$stmt = $this->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' => $threadId]);
|
||||||
|
return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function computePoints(int $userId): float
|
||||||
|
{
|
||||||
|
$pointsCfg = $this->config['points'] ?? [];
|
||||||
|
$eventCreated = (int)$this->pdo->query("SELECT COUNT(*) FROM events WHERE created_by = {$userId}")->fetchColumn();
|
||||||
|
$eventParticipants = (float)$this->pdo->query("SELECT COUNT(*) FROM event_participants WHERE user_id = {$userId}")->fetchColumn();
|
||||||
|
$threadCount = (int)$this->pdo->query("SELECT COUNT(*) FROM forum_threads WHERE user_id = {$userId}")->fetchColumn();
|
||||||
|
$answerCount = (int)$this->pdo->query("SELECT COUNT(*) FROM forum_posts WHERE user_id = {$userId}")->fetchColumn();
|
||||||
|
$invites = 0;
|
||||||
|
|
||||||
|
return $threadCount * ($pointsCfg['forum_question'] ?? 0.5)
|
||||||
|
+ $answerCount * ($pointsCfg['forum_answer'] ?? 1.0)
|
||||||
|
+ $eventCreated * ($pointsCfg['event_create'] ?? 1.0)
|
||||||
|
+ $eventParticipants * ($pointsCfg['event_participation'] ?? 0.1)
|
||||||
|
+ $invites * ($pointsCfg['invite'] ?? 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function membershipLevel(float $points): array
|
||||||
|
{
|
||||||
|
$levels = $this->config['levels'] ?? [];
|
||||||
|
usort($levels, fn($a,$b) => ($b['min'] ?? 0) <=> ($a['min'] ?? 0));
|
||||||
|
foreach ($levels as $lvl) {
|
||||||
|
if ($points >= (float)($lvl['min'] ?? 0)) {
|
||||||
|
return [
|
||||||
|
'label' => $lvl['label'] ?? 'New Daddy',
|
||||||
|
'icon' => $lvl['icon'] ?? '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$fallback = $levels ? $levels[count($levels)-1] : ['label' => 'New Daddy','icon' => ''];
|
||||||
|
return ['label' => $fallback['label'], 'icon' => $fallback['icon'] ?? ''];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user