misuzu/public/forum/topic.php

89 lines
2.6 KiB
PHP
Raw Normal View History

2018-05-18 03:20:27 +02:00
<?php
use Misuzu\Database;
require_once __DIR__ . '/../../misuzu.php';
2018-05-20 22:12:45 +02:00
$db = Database::connection();
$templating = $app->getTemplating();
$postId = (int)($_GET['p'] ?? 0);
$topicId = (int)($_GET['t'] ?? 0);
$postsOffset = max((int)($_GET['o'] ?? 0), 0);
$postsRange = max(min((int)($_GET['r'] ?? 10), 25), 5);
// find topic id
if ($topicId < 1 && $postId > 0) {
2018-05-23 03:41:57 +02:00
$postInfo = forum_post_find($postId);
2018-05-22 01:05:25 +02:00
if ($postInfo) {
$topicId = (int)$postInfo['target_topic_id'];
$postsOffset = floor($postInfo['preceeding_post_count'] / $postsRange) * $postsRange;
}
2018-05-20 22:12:45 +02:00
}
$getTopic = $db->prepare('
SELECT
2018-05-23 03:41:57 +02:00
t.`topic_id`, t.`forum_id`, t.`topic_title`, t.`topic_type`, t.`topic_locked`,
f.`forum_archived` as `topic_archived`,
2018-05-20 22:12:45 +02:00
(
2018-05-22 01:05:25 +02:00
SELECT MIN(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
) as `topic_first_post_id`,
(
SELECT COUNT(`post_id`)
FROM `msz_forum_posts`
WHERE `topic_id` = t.`topic_id`
) as `topic_post_count`
2018-05-20 22:12:45 +02:00
FROM `msz_forum_topics` as t
2018-05-23 03:41:57 +02:00
LEFT JOIN `msz_forum_categories` as f
ON f.`forum_id` = t.`forum_id`
2018-05-20 22:12:45 +02:00
WHERE t.`topic_id` = :topic_id
AND t.`topic_deleted` IS NULL
');
$getTopic->bindValue('topic_id', $topicId);
$topic = $getTopic->execute() ? $getTopic->fetch() : false;
if (!$topic) {
http_response_code(404);
echo $templating->render('errors.404');
return;
}
$getPosts = $db->prepare('
SELECT
2018-05-22 01:05:25 +02:00
p.`post_id`, p.`post_text`, p.`post_created`,
p.`topic_id`,
u.`user_id` as `poster_id`,
u.`username` as `poster_name`,
u.`created_at` as `poster_joined`,
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `poster_colour`
FROM `msz_forum_posts` as p
LEFT JOIN `msz_users` as u
ON u.`user_id` = p.`user_id`
LEFT JOIN `msz_roles` as r
ON r.`role_id` = u.`display_role`
2018-05-20 22:12:45 +02:00
WHERE `topic_id` = :topic_id
AND `post_deleted` IS NULL
2018-05-22 01:05:25 +02:00
ORDER BY `post_id`
LIMIT :offset, :take
2018-05-20 22:12:45 +02:00
');
$getPosts->bindValue('topic_id', $topic['topic_id']);
2018-05-22 01:05:25 +02:00
$getPosts->bindValue('offset', $postsOffset);
$getPosts->bindValue('take', $postsRange);
2018-05-20 22:12:45 +02:00
$posts = $getPosts->execute() ? $getPosts->fetchAll() : [];
2018-05-22 01:05:25 +02:00
if (!$posts) {
http_response_code(404);
echo $templating->render('errors.404');
return;
}
2018-05-20 22:12:45 +02:00
echo $templating->render('forum.topic', [
2018-05-23 03:41:57 +02:00
'topic_breadcrumbs' => forum_get_breadcrumbs($topic['forum_id']),
2018-05-20 22:12:45 +02:00
'topic_info' => $topic,
'topic_posts' => $posts,
2018-05-22 01:05:25 +02:00
'topic_offset' => $postsOffset,
'topic_range' => $postsRange,
2018-05-20 22:12:45 +02:00
]);