asdasd
This commit is contained in:
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