Added horribad quote administration thing.

This commit is contained in:
flash 2018-10-10 00:36:54 +02:00
parent 836f7272c5
commit 39ff56a6b7
4 changed files with 216 additions and 37 deletions

View file

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

View file

@ -1,12 +1,28 @@
<?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 $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`,
@ -15,21 +31,91 @@ function chat_quotes_add(
: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'])) : [];
}

View file

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

View file

@ -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 %}
<div class="container container--new">
<div class="container__title">Quotes</div>
{% if current_quote is defined %}
<form method="post" action="">
{{ 'add_quote'|csrf|raw }}
<input type="hidden" name="quote[id]" value="{{ current_quote.quote_id|default(0) }}">
* = optional
<table style="color:inherit">
<tr>
<td>Parent ID*</td>
<td><input type="number" name="quote[parent]" value="{{ current_quote.quote_parent|default(quote_parent) }}" class="input__text input__text--new"></td>
</tr>
<tr>
<td>User ID*</td>
<td><input type="number" name="quote[user][id]" class="input__text input__text--new" value="{{ current_quote.quote_user_id|default(0) }}"></td>
</tr>
<tr>
<td>Date/time*</td>
<td><input type="text" name="quote[time]" class="input__text input__text--new" value="{{ current_quote.quote_timestamp|default(0)|date('Y-m-d H:i:s') }}"></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" required name="quote[user][name]" class="input__text input__text--new" value="{{ current_quote.quote_username|default() }}"></td>
</tr>
<tr>
<td>User Colour</td>
<td><input type="number" required name="quote[user][colour]" class="input__text input__text--new" value="{{ current_quote.quote_user_colour|default(constant('MSZ_COLOUR_INHERIT')) }}"></td>
</tr>
<tr>
<td colspan="2">
<textarea name="quote[text]" required class="input__textarea input__textarea--new">{{ current_quote.quote_text|default() }}</textarea>
</td>
</tr>
</table>
<button class="input__button input__button--new">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 input__button--new">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 input__button--new">{{ key + 1 }} ({{ quote.quote_username }})</a>
{% endfor %}
{% else %}
<a href="?v=quotes&amp;q=-1" class="input__button input__button--new">Create</a>
{% for parent in quote_parents %}
<div class="container container--new">
<a href="?v=quotes&amp;s={{ parent.quote_id }}" class="container__title container__title--link">{{ parent.quote_timestamp|date('Y-m-d') }}</a>
{{ chat_quote_display([parent]) }}
</div>
{% endfor %}
{{ pagination(quote_count, 15, quote_offset, '?v=quotes') }}
{% endif %}
</div>
{% endblock %}