LuminaWebsite/includes/reactions.php
Maaxxs 6d8747c9e4 Moderation: all comments with flags, swear word filter; reactions on pictures and comments; last active in member list; map starts with categories hidden
- Moderation page with tabs: approval, all comments (critical/suspicious flags, search, filters), swear word filter (mark / hold / block, maintained by mods, detects leetspeak)
- Discord-style emoji reactions on gallery pictures and comments (without reloading)
- Member list and profile: "last active" / online dot, sortable
- Map: all marker categories hidden at start

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 22:12:10 +02:00

128 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* Reaktionen wie bei Discord: Emojis unter Galeriebildern und Kommentaren. Jedes Mitglied kann
* pro Inhalt mehrere verschiedene Emojis setzen, jedes aber nur einmal (nochmal klicken = weg).
* Nur Emojis aus REACTION_EMOJIS - so kann niemand beliebigen Text als "Reaktion" posten.
*/
const REACTION_EMOJIS = ['👍', '❤️', '😂', '😮', '😢', '🔥', '🎉', '👏', '💪', '😍', '🤯', '👀', '🙏', '💯', '⚔️', '🏆'];
// Inhaltsarten: Schluessel => Tabelle
const REACTION_TYPES = ['picture' => 'member_gallery', 'comment' => 'gallery_comments'];
/**
* Reaktionen fuer mehrere Inhalte einer Art, in der Reihenfolge der ersten Reaktion.
*
* @param array<int, int> $ids
* @return array<int, array<string, array{count: int, mine: bool, names: array<int, string>}>>
*/
function reactions_for(string $type, array $ids): array
{
if (empty($ids)) {
return [];
}
$me = current_user();
$placeholders = implode(',', array_fill(0, count($ids), '?'));
try {
$stmt = db()->prepare(
"SELECT r.content_id, r.emoji, r.user_id, COALESCE(NULLIF(u.display_name, ''), u.username) AS name
FROM reactions r JOIN users u ON u.id = r.user_id
WHERE r.content_type = ? AND r.content_id IN ($placeholders)
ORDER BY r.created_at, r.id"
);
$stmt->execute(array_merge([$type], array_map('intval', $ids)));
$rows = $stmt->fetchAll();
} catch (PDOException $e) {
return []; // Tabelle noch nicht angelegt
}
$result = [];
foreach ($rows as $row) {
$entry = &$result[(int) $row['content_id']][$row['emoji']];
$entry ??= ['count' => 0, 'mine' => false, 'names' => []];
$entry['count']++;
$entry['names'][] = (string) $row['name'];
if ($me && (int) $row['user_id'] === (int) $me['id']) {
$entry['mine'] = true;
}
unset($entry);
}
return $result;
}
/** Reaktion setzen oder (wenn schon gesetzt) wieder entfernen. */
function reaction_toggle(string $type, int $contentId, string $emoji): void
{
$userId = (int) current_user()['id'];
$delete = db()->prepare('DELETE FROM reactions WHERE content_type = ? AND content_id = ? AND user_id = ? AND emoji = ?');
$delete->execute([$type, $contentId, $userId, $emoji]);
if ($delete->rowCount() === 0) {
db()->prepare('INSERT INTO reactions (content_type, content_id, user_id, emoji) VALUES (?, ?, ?, ?)')
->execute([$type, $contentId, $userId, $emoji]);
}
}
/** Reaktionen eines geloeschten Inhalts entfernen. */
function reactions_delete(string $type, array $ids): void
{
if (empty($ids)) {
return;
}
$placeholders = implode(',', array_fill(0, count($ids), '?'));
try {
db()->prepare("DELETE FROM reactions WHERE content_type = ? AND content_id IN ($placeholders)")
->execute(array_merge([$type], array_map('intval', $ids)));
} catch (PDOException $e) {
// Tabelle fehlt (noch)
}
}
/** Kurztext fuer den Tooltip: "Maaxxs, Damaskar und 3 weitere". */
function reaction_names_label(array $names): string
{
$shown = array_slice($names, 0, 3);
$rest = count($names) - count($shown);
return implode(', ', $shown) . ($rest > 0 ? ' und ' . $rest . ($rest === 1 ? ' weitere*r' : ' weitere') : '');
}
/**
* Reaktionsleiste: vorhandene Reaktionen als Knoepfe (eigene hervorgehoben) und ein "+" mit
* Emoji-Auswahl. Funktioniert auch ohne JavaScript (Formulare), assets/js/reactions.js macht es
* ohne Neuladen.
*
* @param array<string, array{count: int, mine: bool, names: array<int, string>}> $reactions
*/
function reactions_bar_html(string $type, int $contentId, array $reactions, bool $canReact, bool $compact = false): string
{
if (!$canReact && !$reactions) {
return '';
}
$form = static function (string $emoji, string $inner, string $class, string $title) use ($type, $contentId, $canReact): string {
if (!$canReact) {
return '<span class="' . $class . '" title="' . e($title) . '">' . $inner . '</span>';
}
return '<form method="post" action="/reaction_action.php" class="reaction-form">' . csrf_field()
. '<input type="hidden" name="type" value="' . e($type) . '"><input type="hidden" name="id" value="' . $contentId . '">'
. '<input type="hidden" name="emoji" value="' . e($emoji) . '">'
. '<button type="submit" class="' . $class . '" title="' . e($title) . '">' . $inner . '</button></form>';
};
$html = '<div class="reaction-bar' . ($compact ? ' reaction-bar-compact' : '') . '" data-type="' . e($type) . '" data-id="' . $contentId . '">';
foreach ($reactions as $emoji => $info) {
$html .= $form(
(string) $emoji,
'<span class="reaction-emoji">' . e((string) $emoji) . '</span><span class="reaction-count">' . $info['count'] . '</span>',
'reaction' . ($info['mine'] ? ' is-mine' : ''),
reaction_names_label($info['names']) . ($canReact ? ($info['mine'] ? ' - klicken zum Entfernen' : ' - klicken zum Mitreagieren') : '')
);
}
if ($canReact) {
$html .= '<details class="reaction-add"><summary class="reaction reaction-add-button" title="Reaktion hinzufügen" aria-label="Reaktion hinzufügen">'
. '<span class="reaction-emoji">😀</span><span class="reaction-plus">+</span></summary><div class="reaction-picker">';
foreach (REACTION_EMOJIS as $emoji) {
$html .= $form($emoji, e($emoji), 'reaction-pick', $emoji);
}
$html .= '</div></details>';
}
return $html . '</div>';
}