Tabelle const REACTION_TYPES = ['picture' => 'member_gallery', 'comment' => 'gallery_comments']; /** * Reaktionen fuer mehrere Inhalte einer Art, in der Reihenfolge der ersten Reaktion. * * @param array $ids * @return array}>> */ 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}> $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 '' . $inner . ''; } return '
' . csrf_field() . '' . '' . '
'; }; $html = '
'; foreach ($reactions as $emoji => $info) { $html .= $form( (string) $emoji, '' . e((string) $emoji) . '' . $info['count'] . '', 'reaction' . ($info['mine'] ? ' is-mine' : ''), reaction_names_label($info['names']) . ($canReact ? ($info['mine'] ? ' - klicken zum Entfernen' : ' - klicken zum Mitreagieren') : '') ); } if ($canReact) { $html .= '
' . '๐Ÿ˜€+
'; foreach (REACTION_EMOJIS as $emoji) { $html .= $form($emoji, e($emoji), 'reaction-pick', $emoji); } $html .= '
'; } return $html . '
'; }