From 39ff56a6b749fca2d7e2406eae0cbbfbfde7e545 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 10 Oct 2018 00:36:54 +0200 Subject: [PATCH] Added horribad quote administration thing. --- public/manage/index.php | 41 ++++++++ src/chat_quotes.php | 138 ++++++++++++++++++++------- src/manage.php | 1 + templates/manage/general/quotes.twig | 73 ++++++++++++++ 4 files changed, 216 insertions(+), 37 deletions(-) create mode 100644 templates/manage/general/quotes.twig diff --git a/public/manage/index.php b/public/manage/index.php index 694b0cd7..f2a5c456 100644 --- a/public/manage/index.php +++ b/public/manage/index.php @@ -19,6 +19,47 @@ switch ($_GET['v'] ?? null) { echo tpl_render('manage.general.logs'); break; + case 'quotes': + $setId = (int)($_GET['s'] ?? ''); + $quoteId = (int)($_GET['q'] ?? ''); + + if (!empty($_POST['quote']) && csrf_verify('add_quote', $_POST['csrf'] ?? '')) { + $quoteTime = strtotime($_POST['quote']['time'] ?? ''); + $parentId = empty($_POST['quote']['parent']) ? null : (int)($_POST['quote']['parent']); + + $quoteId = chat_quotes_add( + $_POST['quote']['text'] ?? null, + $_POST['quote']['user']['name'] ?? null, + empty($_POST['quote']['user']['colour']) ? MSZ_COLOUR_INHERIT : (int)($_POST['quote']['user']['colour']), + empty($_POST['quote']['user']['id']) ? null : (int)($_POST['quote']['user']['id']), + empty($_POST['quote']['parent']) || $_POST['quote']['id'] == $parentId ? null : (int)($_POST['quote']['parent']), + $quoteTime ? $quoteTime : null, + empty($_POST['quote']['id']) ? null : (int)($_POST['quote']['id']) + ); + + header('Location: ?v=quotes' . ($setId ? '&s=' . $setId : '&q=' . $quoteId)); + break; + } + + if ($quoteId) { + tpl_vars([ + 'current_quote' => chat_quotes_single($quoteId), + 'quote_parent' => $setId, + ]); + } elseif ($setId > 0) { + tpl_var('quote_set', chat_quotes_set($setId)); + } + + $quoteCount = chat_quotes_count(true); + $quotes = chat_quotes_parents($_GET['o'] ?? 0); + + echo tpl_render('manage.general.quotes', [ + 'quote_count' => $quoteCount, + 'quote_offset' => (int)($_GET['o'] ?? 0), + 'quote_parents' => $quotes, + ]); + break; + case 'emoticons': if (!perms_check($generalPerms, MSZ_PERM_GENERAL_MANAGE_EMOTICONS)) { echo render_error(403); diff --git a/src/chat_quotes.php b/src/chat_quotes.php index 6aa718d3..cd958dc2 100644 --- a/src/chat_quotes.php +++ b/src/chat_quotes.php @@ -1,35 +1,121 @@ 0) { + $insert = db_prepare(' + UPDATE `msz_chat_quotes` + SET `quote_parent` = :parent, + `quote_user_id` = :user_id, + `quote_username` = :username, + `quote_user_colour` = :user_colour, + `quote_timestamp` = :time, + `quote_text` = :text + WHERE `quote_id` = :id + '); + $insert->bindValue('id', $quoteId); + } else { + $insert = db_prepare(' + INSERT INTO `msz_chat_quotes` ( + `quote_parent`, `quote_user_id`, `quote_username`, + `quote_user_colour`, `quote_timestamp`, `quote_text` + ) VALUES ( + :parent, :user_id, :username, :user_colour, :time, :text + ) + '); + } - $insert->bindValue('parent', $parent); - $insert->bindValue('user_id', $userId); + $insert->bindValue('parent', $parent < 1 ? null : $parent); + $insert->bindValue('user_id', $userId < 1 ? null : $userId); $insert->bindValue('username', $username); $insert->bindValue('user_colour', $colour); - $insert->bindValue('time', date('Y-m-d H:i:s', $time ?? time())); + $insert->bindValue('time', date('Y-m-d H:i:s', $time < 1 ? time() : $time)); $insert->bindValue('text', $text); return $insert->execute() ? db_last_insert_id() : 0; } +function chat_quotes_count(bool $parentsOnly = false): int +{ + return db_query(sprintf(' + SELECT COUNT(`quote_id`) + FROM `msz_chat_quotes` + %s + ', $parentsOnly ? 'WHERE `quote_parent` IS NULL' : ''))->fetchColumn(); +} + +function chat_quotes_single(int $quoteId): array +{ + if ($quoteId < 1) { + return []; + } + + $getSingle = db_prepare(' + SELECT `quote_id`, `quote_parent`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` + FROM `msz_chat_quotes` + WHERE `quote_id` = :quote + '); + $getSingle->bindValue('quote', $quoteId); + $single = $getSingle->execute() ? $getSingle->fetch(PDO::FETCH_ASSOC) : []; + return $single ? $single : []; +} + +function chat_quotes_parents(int $offset = 0, int $take = MSZ_CHAT_QUOTES_TAKE): array +{ + $getAll = $take < 1 || $offset < 0; + + $getParents = db_prepare(sprintf(' + SELECT `quote_id`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` + FROM `msz_chat_quotes` + WHERE `quote_parent` IS NULL + ORDER BY `quote_id` DESC + %s + ', $getAll ? '' : 'LIMIT :offset, :take')); + + if (!$getAll) { + $getParents->bindValue('take', $take); + $getParents->bindValue('offset', $offset); + } + + $parents = $getParents->execute() ? $getParents->fetchAll() : []; + return $parents ? $parents : []; +} + +function chat_quotes_set(int $parentId): array +{ + $getParent = db_prepare(' + SELECT `quote_id`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` + FROM `msz_chat_quotes` + WHERE `quote_parent` IS NULL + AND `quote_id` = :parent + '); + $getParent->bindValue('parent', $parentId); + $parent = $getParent->execute() ? $getParent->fetch(PDO::FETCH_ASSOC) : []; + return $parent ? array_merge([$parent], chat_quotes_children($parent['quote_id'])) : []; +} + +function chat_quotes_children(int $parentId): array +{ + $getChildren = db_prepare(' + SELECT `quote_id`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` + FROM `msz_chat_quotes` + WHERE `quote_parent` = :parent + '); + $getChildren->bindValue('parent', $parentId); + $children = $getChildren->execute() ? $getChildren->fetchAll(PDO::FETCH_ASSOC) : []; + return $children ? $children : []; +} + function chat_quotes_random(): array { - $quotes = []; - $parent = db_query(' SELECT `quote_id`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` FROM `msz_chat_quotes` @@ -37,27 +123,5 @@ function chat_quotes_random(): array ORDER BY RAND() ')->fetch(PDO::FETCH_ASSOC); - if (!$parent) { - return []; - } - - $quotes[] = $parent; - - $getChildren = db_prepare(' - SELECT `quote_id`, `quote_user_id`, `quote_username`, `quote_user_colour`, `quote_timestamp`, `quote_text` - FROM `msz_chat_quotes` - WHERE `quote_parent` = :parent - '); - $getChildren->bindValue('parent', $parent['quote_id']); - $children = $getChildren->execute() ? $getChildren->fetchAll(PDO::FETCH_ASSOC) : []; - - if ($children) { - $quotes = array_merge($quotes, $children); - } - - usort($quotes, function ($rowA, $rowB) { - return strcmp($rowA['quote_timestamp'], $rowB['quote_timestamp']); - }); - - return $quotes; + return $parent ? array_merge([$parent], chat_quotes_children($parent['quote_id'])) : []; } diff --git a/src/manage.php b/src/manage.php index 2cf4f7e9..80636ae9 100644 --- a/src/manage.php +++ b/src/manage.php @@ -13,6 +13,7 @@ function manage_get_menu(int $userId): array $menu = []; $menu['General']['Overview'] = '/manage/index.php?v=overview'; + $menu['General']['Quotes'] = '/manage/index.php?v=quotes'; if (perms_check($perms['general'], MSZ_PERM_GENERAL_VIEW_LOGS)) { $menu['General']['Logs'] = '/manage/index.php?v=logs'; diff --git a/templates/manage/general/quotes.twig b/templates/manage/general/quotes.twig new file mode 100644 index 00000000..664c59c7 --- /dev/null +++ b/templates/manage/general/quotes.twig @@ -0,0 +1,73 @@ +{% extends 'manage/general/master.twig' %} +{% from 'home/macros.twig' import chat_quote_display %} +{% from 'macros.twig' import pagination %} + +{% block manage_content %} +
+
Quotes
+ + {% if current_quote is defined %} +
+ {{ 'add_quote'|csrf|raw }} + + + * = optional + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Parent ID*
User ID*
Date/time*
Username
User Colour
+ +
+ + +
+ {% elseif quote_set is defined %} + {{ chat_quote_display(quote_set) }} + + Add + + Edit: + {% for key, quote in quote_set %} + {{ key + 1 }} ({{ quote.quote_username }}) + {% endfor %} + {% else %} + Create + + {% for parent in quote_parents %} +
+ {{ parent.quote_timestamp|date('Y-m-d') }} + {{ chat_quote_display([parent]) }} +
+ {% endfor %} + + {{ pagination(quote_count, 15, quote_offset, '?v=quotes') }} + {% endif %} +
+{% endblock %}