Merged the changelog index and date templates.

This commit is contained in:
flash 2018-07-07 21:21:42 +02:00
parent 5a3e210052
commit f4d8005ab3
6 changed files with 134 additions and 85 deletions

View file

@ -12,6 +12,7 @@ $changelogRange = 30;
$changelogChange = (int)($_GET['c'] ?? 0); $changelogChange = (int)($_GET['c'] ?? 0);
$changelogDate = $_GET['d'] ?? ''; $changelogDate = $_GET['d'] ?? '';
$changelogUser = (int)($_GET['u'] ?? 0); $changelogUser = (int)($_GET['u'] ?? 0);
$changelogTags = $_GET['t'] ?? '';
$tpl->vars([ $tpl->vars([
'changelog_offset' => $changelogOffset, 'changelog_offset' => $changelogOffset,
@ -66,69 +67,18 @@ if (!empty($changelogDate)) {
echo render_error(404); echo render_error(404);
return; return;
} }
}
$getChanges = $db->prepare(' $changesCount = !empty($changelogDate) ? -1 : changelog_count_changes($changelogDate, $changelogUser);
SELECT $changes = changelog_get_changes($changelogDate, $changelogUser, $changelogOffset, $changelogRange);
c.`change_id`, c.`change_log`,
a.`action_name`, a.`action_colour`, a.`action_class`,
u.`user_id`, u.`username`,
DATE(`change_created`) as `change_date`,
!ISNULL(c.`change_text`) as `change_has_text`,
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
FROM `msz_changelog_changes` as c
LEFT JOIN `msz_users` as u
ON u.`user_id` = c.`user_id`
LEFT JOIN `msz_roles` as r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_changelog_actions` as a
ON a.`action_id` = c.`action_id`
WHERE DATE(c.`change_created`) = :date
GROUP BY `change_created`, `change_id`
ORDER BY `change_created` DESC, `change_id` DESC
');
$getChanges->bindValue('date', $changelogDate);
$changes = $getChanges->execute() ? $getChanges->fetchAll() : [];
// settings the response code to 404, but rendering our own page anyway. if (!$changes) {
if (count($changes) < 1) {
http_response_code(404); http_response_code(404);
} }
echo $tpl->render('changelog.date', [
'changes' => $changes,
]);
return;
}
$changesCount = (int)$db->query('
SELECT COUNT(`change_id`)
FROM `msz_changelog_changes`
')->fetchColumn();
$getChanges = $db->prepare('
SELECT
c.`change_id`, c.`change_log`,
a.`action_name`, a.`action_colour`, a.`action_class`,
u.`user_id`, u.`username`,
DATE(`change_created`) as `change_date`,
!ISNULL(c.`change_text`) as `change_has_text`,
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
FROM `msz_changelog_changes` as c
LEFT JOIN `msz_users` as u
ON u.`user_id` = c.`user_id`
LEFT JOIN `msz_roles` as r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_changelog_actions` as a
ON a.`action_id` = c.`action_id`
GROUP BY `change_created`, `change_id`
ORDER BY `change_created` DESC, `change_id` DESC
LIMIT :offset, :take
');
$getChanges->bindValue('offset', $changelogOffset);
$getChanges->bindValue('take', $changelogRange);
$changes = $getChanges->execute() ? $getChanges->fetchAll() : [];
echo $tpl->render('changelog.index', [ echo $tpl->render('changelog.index', [
'changes' => $changes, 'changes' => $changes,
'changelog_count' => $changesCount, 'changelog_count' => $changesCount,
'changelog_date' => $changelogDate,
'changelog_user' => $changelogUser,
]); ]);

View file

@ -240,6 +240,7 @@ class Application extends ApplicationBase
$this->templatingInstance->addFilter('byte_symbol'); $this->templatingInstance->addFilter('byte_symbol');
$this->templatingInstance->addFilter('html_link'); $this->templatingInstance->addFilter('html_link');
$this->templatingInstance->addFilter('html_colour'); $this->templatingInstance->addFilter('html_colour');
$this->templatingInstance->addFilter('url_construct');
$this->templatingInstance->addFilter('country_name', 'get_country_name'); $this->templatingInstance->addFilter('country_name', 'get_country_name');
$this->templatingInstance->addFilter('flip', 'array_flip'); $this->templatingInstance->addFilter('flip', 'array_flip');
$this->templatingInstance->addFilter('first_paragraph'); $this->templatingInstance->addFilter('first_paragraph');

View file

@ -41,3 +41,86 @@ function changelog_entry_create(int $userId, int $actionId, string $log, string
return $createChange->execute() ? (int)$dbc->lastInsertId() : 0; return $createChange->execute() ? (int)$dbc->lastInsertId() : 0;
} }
define('CHANGELOG_GET_QUERY', '
SELECT
c.`change_id`, c.`change_log`,
a.`action_name`, a.`action_colour`, a.`action_class`,
u.`user_id`, u.`username`,
DATE(`change_created`) as `change_date`,
!ISNULL(c.`change_text`) as `change_has_text`,
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
FROM `msz_changelog_changes` as c
LEFT JOIN `msz_users` as u
ON u.`user_id` = c.`user_id`
LEFT JOIN `msz_roles` as r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_changelog_actions` as a
ON a.`action_id` = c.`action_id`
WHERE %s
AND %s
GROUP BY `change_created`, `change_id`
ORDER BY `change_created` DESC, `change_id` DESC
%s
');
function changelog_get_changes(string $date, int $user, int $offset, int $take): array
{
$hasDate = strlen($date) > 0;
$hasUser = $user > 0;
$query = sprintf(
CHANGELOG_GET_QUERY,
$hasDate ? 'DATE(c.`change_created`) = :date' : '1',
$hasUser ? 'c.`user_id` = :user' : '1',
!$hasDate ? 'LIMIT :offset, :take' : ''
);
$dbc = Database::connection();
$prep = $dbc->prepare($query);
if (!$hasDate) {
$prep->bindValue('offset', $offset);
$prep->bindValue('take', $take);
} else {
$prep->bindValue('date', $date);
}
if ($hasUser) {
$prep->bindValue('user', $user);
}
return $prep->execute() ? $prep->fetchAll(PDO::FETCH_ASSOC) : [];
}
define('CHANGELOG_COUNT_QUERY', '
SELECT COUNT(`change_id`)
FROM `msz_changelog_changes`
WHERE %s
AND %s
');
function changelog_count_changes(string $date, int $user): int
{
$hasDate = strlen($date) > 0;
$hasUser = $user > 0;
$query = sprintf(
CHANGELOG_COUNT_QUERY,
$hasDate ? 'DATE(`change_created`) = :date' : '1',
$hasUser ? '`user_id` = :user' : '1'
);
$dbc = Database::connection();
$prep = $dbc->prepare($query);
if ($hasDate) {
$prep->bindValue('date', $date);
}
if ($hasUser) {
$prep->bindValue('user', $user);
}
return $prep->execute() ? (int)$prep->fetchColumn() : 0;
}

View file

@ -282,3 +282,20 @@ function html_colour(int $colour, array $attribs = []): string
return $css; return $css;
} }
function url_construct(string $path, array $query = [], string $host = ''): string
{
$url = $host . $path;
if (count($query)) {
$url .= '?';
foreach ($query as $key => $value) {
if ($value) {
$url .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
}
return substr($url, 0, -1);
}

View file

@ -1,23 +0,0 @@
{% extends '@mio/changelog/master.twig' %}
{% from '@mio/macros.twig' import navigation, pagination %}
{% from '@mio/changelog/macros.twig' import changelog_listing %}
{% set is_valid = changes|length > 0 %}
{% set title = 'Changelog » ' ~ (is_valid ? changes[0].change_date : 'Unknown date') %}
{% if is_valid %}
{% set canonical_url = '/changelog.php?d=' ~ changes[0].change_date %}
{% endif %}
{% block content %}
<div class="container">
<div class="container__title">
{{ title }}
</div>
<div class="container__content changelog__content">
{{ changelog_listing(changes, true) }}
</div>
</div>
{{ navigation(mio_navigation) }}
{% endblock %}

View file

@ -2,17 +2,38 @@
{% from '@mio/macros.twig' import navigation, pagination %} {% from '@mio/macros.twig' import navigation, pagination %}
{% from '@mio/changelog/macros.twig' import changelog_listing %} {% from '@mio/changelog/macros.twig' import changelog_listing %}
{% set is_valid = changes|length > 0 %}
{% set is_date = changelog_date|length > 0 %}
{% set title = 'Changelog' %} {% set title = 'Changelog' %}
{% set canonical_url = '/changelog.php' %}
{% if is_valid %}
{%
set canonical_url = '/changelog.php'|url_construct({
'd': changelog_date,
'u': changelog_user ? changelog_user : '',
'o': changelog_offset
})
%}
{% if is_date or changelog_user %}
{% set title = title ~ ' »' ~ (changelog_date ? ' ' ~ changes[0].change_date : '') ~ (changelog_user ? ' by ' ~ changes[0].username : '') %}
{% endif %}
{% endif %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
<div class="container__title"> <div class="container__title">
Changelog {{ title }}
</div> </div>
<div class="container__content changelog__content"> <div class="container__content changelog__content">
{{ changelog_listing(changes) }} {{ changelog_listing(changes, is_date) }}
{{ pagination(changelog_count, changelog_take, changelog_offset, '/changelog.php') }}
{% if not is_date %}
{{ pagination(changelog_count, changelog_take, changelog_offset, '/changelog.php'|url_construct({
'd': changelog_date,
'u': changelog_user ? changelog_user : ''
})) }}
{% endif %}
</div> </div>
</div> </div>