From 836f7272c50572c696c97df9845ca0e4b01669af Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 9 Oct 2018 23:09:54 +0200 Subject: [PATCH] Add chat quotes to front page. --- assets/less/classes/chat-quote.less | 37 +++++++++++ assets/less/main.less | 1 + .../2018_10_09_181724_chat_quotes_table.php | 32 ++++++++++ misuzu.php | 1 + public/index.php | 1 + src/chat_quotes.php | 63 +++++++++++++++++++ templates/home/index.twig | 8 +++ templates/home/macros.twig | 18 ++++++ 8 files changed, 161 insertions(+) create mode 100644 assets/less/classes/chat-quote.less create mode 100644 database/2018_10_09_181724_chat_quotes_table.php create mode 100644 src/chat_quotes.php create mode 100644 templates/home/macros.twig diff --git a/assets/less/classes/chat-quote.less b/assets/less/classes/chat-quote.less new file mode 100644 index 00000000..fb7d874d --- /dev/null +++ b/assets/less/classes/chat-quote.less @@ -0,0 +1,37 @@ +.chat-quote { + display: block; + background-color: #212121; + border: 1px solid #808080; + color: #fff; + margin: 1px; + + &__line { + display: flex; + flex-wrap: wrap; + padding: 1px 4px; + + &:nth-child(odd) { + background-color: #0009; + } + } + + &__time { + font-size: .8em; + margin: 0 4px 0 2px; + } + + &__username { + color: var(--user-colour); + font-weight: 700; + text-decoration: none; + + &[href]:hover { + text-decoration: underline; + } + } + + &__text { + width: 100%; + flex: 1 0 auto; + } +} diff --git a/assets/less/main.less b/assets/less/main.less index ffa80280..df08a451 100644 --- a/assets/less/main.less +++ b/assets/less/main.less @@ -110,6 +110,7 @@ body { @import "classes/index"; @import "classes/permissions"; @import "classes/auth"; +@import "classes/chat-quote"; // Manage @import "classes/manage/manage"; diff --git a/database/2018_10_09_181724_chat_quotes_table.php b/database/2018_10_09_181724_chat_quotes_table.php new file mode 100644 index 00000000..e8c39b67 --- /dev/null +++ b/database/2018_10_09_181724_chat_quotes_table.php @@ -0,0 +1,32 @@ +exec(" + CREATE TABLE `msz_chat_quotes` ( + `quote_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + `quote_parent` INT(10) UNSIGNED NULL DEFAULT NULL, + `quote_user_id` INT(10) UNSIGNED NULL DEFAULT NULL, + `quote_username` VARCHAR(30) NOT NULL, + `quote_user_colour` INT(10) UNSIGNED NOT NULL DEFAULT '1073741824', + `quote_timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `quote_text` TEXT NOT NULL, + PRIMARY KEY (`quote_id`), + INDEX `msz_chat_quotes_parent` (`quote_parent`), + INDEX `msz_chat_quotes_user_id_foreign` (`quote_user_id`), + CONSTRAINT `msz_chat_quotes_user_id_foreign` + FOREIGN KEY (`quote_user_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE SET NULL + ) + "); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec('DROP TABLE `msz_chat_quotes`'); +} diff --git a/misuzu.php b/misuzu.php index 90a8eda1..f6470405 100644 --- a/misuzu.php +++ b/misuzu.php @@ -30,6 +30,7 @@ require_once 'src/array.php'; require_once 'src/audit_log.php'; require_once 'src/cache.php'; require_once 'src/changelog.php'; +require_once 'src/chat_quotes.php'; require_once 'src/colour.php'; require_once 'src/comments.php'; require_once 'src/config.php'; diff --git a/public/index.php b/public/index.php index 06444fc6..8c883430 100644 --- a/public/index.php +++ b/public/index.php @@ -84,6 +84,7 @@ echo tpl_render('home.index', [ 'users_count' => $statistics['users'], 'last_user' => $statistics['lastUser'], 'online_users' => $onlineUsers, + 'chat_quote' => chat_quotes_random(), 'featured_changelog' => $changelog, 'featured_news' => $news, ]); diff --git a/src/chat_quotes.php b/src/chat_quotes.php new file mode 100644 index 00000000..6aa718d3 --- /dev/null +++ b/src/chat_quotes.php @@ -0,0 +1,63 @@ +bindValue('parent', $parent); + $insert->bindValue('user_id', $userId); + $insert->bindValue('username', $username); + $insert->bindValue('user_colour', $colour); + $insert->bindValue('time', date('Y-m-d H:i:s', $time ?? time())); + $insert->bindValue('text', $text); + + return $insert->execute() ? db_last_insert_id() : 0; +} + +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` + WHERE `quote_parent` IS NULL + 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; +} diff --git a/templates/home/index.twig b/templates/home/index.twig index ec131a17..cb841ac0 100644 --- a/templates/home/index.twig +++ b/templates/home/index.twig @@ -1,12 +1,20 @@ {% extends 'home/master.twig' %} {% from 'news/macros.twig' import news_preview %} {% from 'changelog/macros.twig' import changelog_listing %} +{% from 'home/macros.twig' import chat_quote_display %} {% set canonical_url = '/' %} {% block content %}
+ {% if chat_quote is defined and chat_quote is iterable and chat_quote|length > 0 %} +
+
Chat quote from {{ chat_quote[0].quote_timestamp|date('Y-m-d') }}
+ {{ chat_quote_display(chat_quote) }} +
+ {% endif %} +
Statistics
diff --git a/templates/home/macros.twig b/templates/home/macros.twig new file mode 100644 index 00000000..4d240161 --- /dev/null +++ b/templates/home/macros.twig @@ -0,0 +1,18 @@ +{# the HTML looks a bit odd, but it's to make copying the text work as expected #} +{% macro chat_quote_display(lines) %} +
{% spaceless %} + {% for line in lines %} +
+ ({{ line.quote_timestamp|date('H:i:s') }}) + {% spaceless %} + {{ line.quote_username }} + {% endspaceless %} + : + {% spaceless %} + {{ line.quote_text }} + {% endspaceless %} +
+ {% endfor %} + {% endspaceless %}
+{% endmacro %}