2022-09-13 13:14:49 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
use stdClass;
|
|
|
|
use RuntimeException;
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumCtx = $msz->getForumContext();
|
|
|
|
$forumCategories = $forumCtx->getCategories();
|
|
|
|
$forumTopics = $forumCtx->getTopics();
|
|
|
|
$forumPosts = $forumCtx->getPosts();
|
2023-09-06 13:50:19 +00:00
|
|
|
$usersCtx = $msz->getUsersContext();
|
2023-08-28 01:17:34 +00:00
|
|
|
$mode = (string)filter_input(INPUT_GET, 'm');
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-09-06 20:06:07 +00:00
|
|
|
$authInfo = $msz->getAuthInfo();
|
|
|
|
$currentUser = $authInfo->getUserInfo();
|
2023-08-02 22:12:47 +00:00
|
|
|
$currentUserId = $currentUser === null ? '0' : $currentUser->getId();
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($mode === 'mark') {
|
2023-09-06 20:06:07 +00:00
|
|
|
if(!$authInfo->isLoggedIn())
|
2023-08-31 15:59:53 +00:00
|
|
|
Template::throwError(403);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
$categoryId = filter_input(INPUT_GET, 'f', FILTER_SANITIZE_NUMBER_INT);
|
2023-08-04 20:51:02 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
|
|
|
|
$categoryInfos = $categoryId === null
|
2023-09-08 13:22:46 +00:00
|
|
|
? $forumCategories->getCategories()
|
|
|
|
: $forumCategories->getCategoryChildren(parentInfo: $categoryId, includeSelf: true);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
foreach($categoryInfos as $categoryInfo) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$perms = $authInfo->getPerms('forum', $categoryInfo);
|
2023-08-30 22:37:21 +00:00
|
|
|
if($perms->check(Perm::F_CATEGORY_LIST))
|
2023-09-08 13:22:46 +00:00
|
|
|
$forumCategories->updateUserReadCategory($userInfo, $categoryInfo);
|
2023-08-04 20:51:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
url_redirect($categoryId ? 'forum-category' : 'forum-index', ['forum' => $categoryId]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Template::render('confirm', [
|
|
|
|
'title' => 'Mark forum as read',
|
|
|
|
'message' => 'Are you sure you want to mark ' . ($categoryId < 1 ? 'the entire' : 'this') . ' forum as read?',
|
|
|
|
'return' => url($categoryId ? 'forum-category' : 'forum-index', ['forum' => $categoryId]),
|
|
|
|
'params' => [
|
|
|
|
'forum' => $categoryId,
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:59:53 +00:00
|
|
|
if($mode !== '')
|
|
|
|
Template::throwError(404);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$categories = $forumCategories->getCategories(hidden: false, asTree: true);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
foreach($categories as $categoryId => $category) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$perms = $authInfo->getPerms('forum', $category->info);
|
2023-08-30 22:37:21 +00:00
|
|
|
if(!$perms->check(Perm::F_CATEGORY_LIST)) {
|
2023-08-28 01:17:34 +00:00
|
|
|
unset($categories[$categoryId]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$unread = false;
|
|
|
|
|
|
|
|
if($category->info->mayHaveChildren())
|
|
|
|
foreach($category->children as $childId => $child) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$childPerms = $authInfo->getPerms('forum', $child->info);
|
2023-08-30 22:37:21 +00:00
|
|
|
if(!$childPerms->check(Perm::F_CATEGORY_LIST)) {
|
2023-08-28 01:17:34 +00:00
|
|
|
unset($category->children[$childId]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$childUnread = false;
|
|
|
|
|
|
|
|
if($category->info->isListing()) {
|
|
|
|
if($child->info->mayHaveChildren()) {
|
|
|
|
foreach($child->children as $grandChildId => $grandChild) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$grandChildPerms = $authInfo->getPerms('forum', $grandChild->info);
|
2023-08-30 22:37:21 +00:00
|
|
|
if(!$grandChildPerms->check(Perm::F_CATEGORY_LIST)) {
|
2023-08-28 01:17:34 +00:00
|
|
|
unset($child->children[$grandChildId]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grandChildUnread = false;
|
|
|
|
|
|
|
|
if($grandChild->info->mayHaveTopics()) {
|
|
|
|
$catIds = [$grandChild->info->getId()];
|
|
|
|
foreach($grandChild->childIds as $greatGrandChildId) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$greatGrandChildPerms = $authInfo->getPerms('forum', $greatGrandChildId);
|
2023-08-30 22:37:21 +00:00
|
|
|
if($greatGrandChildPerms->check(Perm::F_CATEGORY_LIST))
|
2023-08-28 01:17:34 +00:00
|
|
|
$catIds[] = $greatGrandChildId;
|
|
|
|
}
|
|
|
|
|
2023-09-08 13:22:46 +00:00
|
|
|
$grandChildUnread = $forumCategories->checkCategoryUnread($catIds, $currentUser);
|
2023-08-28 01:17:34 +00:00
|
|
|
if($grandChildUnread)
|
|
|
|
$childUnread = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grandChild->perms = $grandChildPerms;
|
|
|
|
$grandChild->unread = $grandChildUnread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($child->info->mayHaveChildren() || $child->info->mayHaveTopics()) {
|
|
|
|
$catIds = [$child->info->getId()];
|
|
|
|
foreach($child->childIds as $grandChildId) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$grandChildPerms = $authInfo->getPerms('forum', $grandChildId);
|
2023-08-30 22:37:21 +00:00
|
|
|
if($grandChildPerms->check(Perm::F_CATEGORY_LIST))
|
2023-08-28 01:17:34 +00:00
|
|
|
$catIds[] = $grandChildId;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-09-08 13:22:46 +00:00
|
|
|
$lastPostInfo = $forumPosts->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
|
2023-08-28 01:17:34 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
$lastPostInfo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($lastPostInfo !== null) {
|
|
|
|
$child->lastPost = new stdClass;
|
|
|
|
$child->lastPost->info = $lastPostInfo;
|
2023-09-08 13:22:46 +00:00
|
|
|
$child->lastPost->topicInfo = $forumTopics->getTopic(postInfo: $lastPostInfo);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
if($lastPostInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$child->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId());
|
|
|
|
$child->lastPost->colour = $usersCtx->getUserColour($child->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
2022-09-13 13:14:49 +00:00
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($child->info->mayHaveTopics() && !$childUnread) {
|
2023-09-08 13:22:46 +00:00
|
|
|
$childUnread = $forumCategories->checkCategoryUnread($child->info, $currentUser);
|
2023-08-28 01:17:34 +00:00
|
|
|
if($childUnread)
|
|
|
|
$unread = true;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
$child->perms = $childPerms;
|
|
|
|
$child->unread = $childUnread;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 01:17:34 +00:00
|
|
|
if($category->info->mayHaveTopics() && !$unread)
|
2023-09-08 13:22:46 +00:00
|
|
|
$unread = $forumCategories->checkCategoryUnread($category->info, $currentUser);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
if(!$category->info->isListing()) {
|
|
|
|
if(!array_key_exists('0', $categories)) {
|
|
|
|
$categories['0'] = $root = new stdClass;
|
|
|
|
$root->info = null;
|
|
|
|
$root->perms = 0;
|
|
|
|
$root->unread = false;
|
|
|
|
$root->colour = null;
|
|
|
|
$root->children = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$categories['0']->children[$categoryId] = $category;
|
|
|
|
unset($categories[$categoryId]);
|
|
|
|
|
|
|
|
if($category->info->mayHaveChildren() || $category->info->mayHaveTopics()) {
|
|
|
|
$catIds = [$category->info->getId()];
|
|
|
|
foreach($category->childIds as $childId) {
|
2023-09-06 20:06:07 +00:00
|
|
|
$childPerms = $authInfo->getPerms('forum', $childId);
|
2023-08-30 22:37:21 +00:00
|
|
|
if($childPerms->check(Perm::F_CATEGORY_LIST))
|
2023-08-28 01:17:34 +00:00
|
|
|
$catIds[] = $childId;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-09-08 13:22:46 +00:00
|
|
|
$lastPostInfo = $forumPosts->getPost(categoryInfos: $catIds, getLast: true, deleted: false);
|
2023-08-28 01:17:34 +00:00
|
|
|
} catch(RuntimeException $ex) {
|
|
|
|
$lastPostInfo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($lastPostInfo !== null) {
|
|
|
|
$category->lastPost = new stdClass;
|
|
|
|
$category->lastPost->info = $lastPostInfo;
|
2023-09-08 13:22:46 +00:00
|
|
|
$category->lastPost->topicInfo = $forumTopics->getTopic(postInfo: $lastPostInfo);
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
if($lastPostInfo->hasUserId()) {
|
2023-09-06 13:50:19 +00:00
|
|
|
$category->lastPost->user = $usersCtx->getUserInfo($lastPostInfo->getUserId());
|
|
|
|
$category->lastPost->colour = $usersCtx->getUserColour($category->lastPost->user);
|
2023-08-28 01:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$category->perms = $perms;
|
|
|
|
$category->unread = $unread;
|
2022-09-13 13:14:49 +00:00
|
|
|
}
|
2023-08-28 01:17:34 +00:00
|
|
|
|
|
|
|
Template::render('forum.index', [
|
|
|
|
'forum_categories' => $categories,
|
|
|
|
'forum_empty' => empty($categories),
|
|
|
|
'forum_show_mark_as_read' => $currentUser !== null,
|
|
|
|
]);
|