Nuked the stale chat quotes code, rewrite when the chat actually exists.

This commit is contained in:
flash 2019-01-10 23:59:24 +01:00
parent 4a1941a447
commit e9a1f59b4f
8 changed files with 16 additions and 291 deletions

View file

@ -1,38 +0,0 @@
.chat-quote {
display: block;
background-color: var(--accent-colour);
border: 1px solid var(--accent-colour);
color: #fff;
margin: 2px;
&__line {
display: flex;
flex-wrap: wrap;
padding: 1px 4px;
background-color: fade(#000, 80%);
&:nth-child(odd) {
background-color: fade(#000, 90%);
}
}
&__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

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

View file

@ -0,0 +1,16 @@
<?php
namespace Misuzu\DatabaseMigrations\NukeChatQuotes;
use PDO;
require_once '2018_10_09_181724_chat_quotes_table.php';
function migrate_up(PDO $conn): void
{
\Misuzu\DatabaseMigrations\ChatQuotesTable\migrate_down($conn);
}
function migrate_down(PDO $conn): void
{
\Misuzu\DatabaseMigrations\ChatQuotesTable\migrate_up($conn);
}

View file

@ -31,7 +31,6 @@ 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

@ -97,7 +97,6 @@ echo tpl_render('home.' . (user_session_active() ? 'home' : 'landing'), [
'statistics' => $stats,
'latest_user' => $latestUser,
'online_users' => $onlineUsers,
'chat_quote' => chat_quotes_random(),
'featured_changelog' => $changelog,
'featured_news' => $news,
]);

View file

@ -32,58 +32,6 @@ switch ($_GET['v'] ?? null) {
]);
break;
case 'quotes':
if (!perms_check($generalPerms, MSZ_PERM_GENERAL_VIEW_LOGS)) {
echo render_error(403);
break;
}
$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));
}
$quotesPagination = pagination_create(chat_quotes_count(true), 15);
$quotesOffset = pagination_offset($quotesPagination, pagination_param());
if (!pagination_is_valid_offset($quotesOffset)) {
echo render_error(404);
break;
}
$quotes = chat_quotes_parents($quotesPagination['offset']);
echo tpl_render('manage.general.quotes', [
'quote_parents' => $quotes,
'quotes_pagination' => $quotesPagination,
]);
break;
case 'emoticons':
if (!perms_check($generalPerms, MSZ_PERM_GENERAL_MANAGE_EMOTICONS)) {
echo render_error(403);

View file

@ -1,124 +0,0 @@
<?php
define('MSZ_CHAT_QUOTES_TAKE', 15);
function chat_quotes_add(
string $text,
string $username,
int $colour,
?int $userId = null,
?int $parent = null,
?int $time = null,
?int $quoteId = null
): int {
if ($quoteId > 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 < 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 < 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);
return db_fetch($getSingle);
}
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);
}
return db_fetch_all($getParents);
}
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 = db_fetch($getParent);
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);
return db_fetch($getChildren);
}
function chat_quotes_random(): array
{
$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);
return $parent ? array_merge([$parent], chat_quotes_children($parent['quote_id'])) : [];
}

View file

@ -1,74 +0,0 @@
{% extends 'manage/general/master.twig' %}
{% from 'home/macros.twig' import chat_quote_display %}
{% from 'macros.twig' import pagination, container_title %}
{% from '_layout/input.twig' import input_hidden, input_csrf, input_text %}
{% block manage_content %}
<div class="container">
{{ container_title('Quotes') }}
{% if current_quote is defined %}
<form method="post" action="">
{{ input_csrf('add_quote') }}
{{ input_hidden('quote[id]', current_quote.quote_id|default(0)) }}
* = optional
<table style="color:inherit">
<tr>
<td>Parent ID*</td>
<td>{{ input_text('quote[parent]', '', current_quote.quote_parent|default(quote_parent), 'number') }}</td>
</tr>
<tr>
<td>User ID*</td>
<td>{{ input_text('quote[user][id]', '', current_quote.quote_user_id|default(0), 'number') }}</td>
</tr>
<tr>
<td>Date/time*</td>
<td>{{ input_text('quote[time]', '', (current_quote.quote_timestamp|default('')|date('Y-m-d H:i:s'))) }}</td>
</tr>
<tr>
<td>Username</td>
<td>{{ input_text('quote[user][name]', '', current_quote.quote_username|default(), 'text', '', true) }}</td>
</tr>
<tr>
<td>User Colour</td>
<td>{{ input_text('quote[user][colour]', '', current_quote.quote_user_colour|default(constant('MSZ_COLOUR_INHERIT')), 'number', '', true) }}</td>
</tr>
<tr>
<td colspan="2">
<textarea name="quote[text]" required class="input__textarea">{{ current_quote.quote_text|default() }}</textarea>
</td>
</tr>
</table>
<button class="input__button">Save</button>
</form>
{% elseif quote_set is defined %}
{{ chat_quote_display(quote_set) }}
<a href="?v=quotes&amp;q=-1&amp;s={{ quote_set[0].quote_id }}" class="input__button">Add</a>
Edit:
{% for key, quote in quote_set %}
<a href="?v=quotes&amp;q={{ quote.quote_id }}&amp;s={{ quote_set[0].quote_id }}" class="input__button">{{ key + 1 }} ({{ quote.quote_username }})</a>
{% endfor %}
{% else %}
<a href="?v=quotes&amp;q=-1" class="input__button">Create</a>
{% for parent in quote_parents %}
<div class="container">
{{ container_title(parent.quote_timestamp|date('Y-m-d'), '?v=quotes&s=' ~ parent.quote_id) }}
{{ chat_quote_display([parent]) }}
</div>
{% endfor %}
{{ pagination(quotes_pagination, '/manage/index.php', null, {'v': 'quotes'}) }}
{% endif %}
</div>
{% endblock %}