Add chat quotes to front page.

This commit is contained in:
flash 2018-10-09 23:09:54 +02:00
parent a103701d6a
commit 836f7272c5
8 changed files with 161 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -110,6 +110,7 @@ body {
@import "classes/index";
@import "classes/permissions";
@import "classes/auth";
@import "classes/chat-quote";
// Manage
@import "classes/manage/manage";

View file

@ -0,0 +1,32 @@
<?php
namespace Misuzu\DatabaseMigrations\ChatQuotesTable;
use PDO;
function migrate_up(PDO $conn): void
{
$conn->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`');
}

View file

@ -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';

View file

@ -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,
]);

63
src/chat_quotes.php Normal file
View file

@ -0,0 +1,63 @@
<?php
function chat_quotes_add(
string $text,
string $username,
int $colour,
?int $userId = null,
?int $parent = null,
?int $time = null
): int {
$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('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;
}

View file

@ -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 %}
<div class="index">
<div class="index__sidebar">
{% if chat_quote is defined and chat_quote is iterable and chat_quote|length > 0 %}
<div class="container">
<div class="container__title">Chat quote from {{ chat_quote[0].quote_timestamp|date('Y-m-d') }}</div>
{{ chat_quote_display(chat_quote) }}
</div>
{% endif %}
<div class="container">
<div class="container__title">Statistics</div>
<div class="container__content">

View file

@ -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) %}
<div class="chat-quote">{% spaceless %}
{% for line in lines %}
<div class="chat-quote__line">
<span class="chat-quote__time">({{ line.quote_timestamp|date('H:i:s') }}) </span>
<span><a class="chat-quote__username" style="{{ line.quote_user_colour|html_colour }}"
{% if line.quote_user_id is not null %}href="/profile.php?u={{ line.quote_user_id }}"{% endif %}>{% spaceless %}
{{ line.quote_username }}
{% endspaceless %}</a></span>
<span class="chat-quote__separator">: </span>
<span class="chat-quote__text">{% spaceless %}
{{ line.quote_text }}
{% endspaceless %}</span>
</div>
{% endfor %}
{% endspaceless %}</div>
{% endmacro %}